1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include "updateregistry.h"
#include <stdexcept>
#include <QLoggingCategory>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDateTime>
#include <QBuffer>
#include <QTimer>
#include <QDebug>
#include "remotefilefetcher.h"
Q_LOGGING_CATEGORY(CATEGORY_UPDATES, "UPD")
using namespace Flipper;
UpdateRegistry::UpdateRegistry(const QString &directoryUrl, QObject *parent):
QAbstractListModel(parent),
m_directoryUrl(directoryUrl),
m_checkTimer(new QTimer(this)),
m_state(State::Unknown)
{
connect(this, &UpdateRegistry::stateChanged, this, &UpdateRegistry::latestVersionChanged);
connect(m_checkTimer, &QTimer::timeout, this, &UpdateRegistry::check);
check();
}
void UpdateRegistry::setDirectoryUrl(const QString &directoryUrl)
{
m_directoryUrl = directoryUrl;
check();
}
void UpdateRegistry::fillFromJson(const QByteArray &text)
{
beginRemoveRows(QModelIndex(), 0, m_channels.size() - 1);
m_channels.clear();
endRemoveRows();
const auto doc = QJsonDocument::fromJson(text);
if(doc.isNull()) {
qCCritical(CATEGORY_UPDATES) << "Failed to parse the document";
return;
} else if(!doc.isObject()) {
qCCritical(CATEGORY_UPDATES) << "Json document is not an object";
return;
}
const auto &obj = doc.object();
if(!obj.contains("channels")) {
qCCritical(CATEGORY_UPDATES) << "No channels data in json file";
return;
} else if(!obj["channels"].isArray()) {
qCCritical(CATEGORY_UPDATES) << "Expected to get an array of channels";
return;
}
const auto &arr = obj["channels"].toArray();
try {
for(const auto &val : arr) {
const Updates::ChannelInfo info(val);
beginInsertRows(QModelIndex(), m_channels.size(), m_channels.size());
m_channels.insert(info.name(), info);
endInsertRows();
}
} catch(std::runtime_error &e) {
qCCritical(CATEGORY_UPDATES) << "Failed to parse update information:" << e.what();
}
}
const QStringList UpdateRegistry::channelNames() const
{
auto names = m_channels.keys();
// Move Development channel to the bottom of the list
if(names.first().startsWith(QStringLiteral("dev"))) {
names.move(0, names.size() - 1);
}
return names;
}
UpdateRegistry::State UpdateRegistry::state() const
{
return m_state;
}
const Updates::VersionInfo UpdateRegistry::latestVersion() const
{
return channel(updateChannel()).latestVersion();
}
const Updates::ChannelInfo UpdateRegistry::channel(const QString &channelName) const
{
return m_channels.value(channelName);
}
int UpdateRegistry::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_channels.size();
}
QVariant UpdateRegistry::data(const QModelIndex &index, int role) const
{
const auto row = index.row();
if(row >= m_channels.size()) {
qCDebug(CATEGORY_UPDATES) << "Invalid row index:" << row;
return QVariant();
}
const auto key = channelNames()[row];
const auto &channel = m_channels[key];
switch(role) {
case NameRole:
return channel.name();
case TitleRole:
return channel.title();
case DescriptionRole:
return channel.description();
default:
return QVariant();
}
}
QHash<int, QByteArray> UpdateRegistry::roleNames() const
{
return {
{ NameRole, QByteArrayLiteral("name") },
{ TitleRole, QByteArrayLiteral("title") },
{ DescriptionRole, QByteArrayLiteral("description") }
};
}
void UpdateRegistry::check()
{
if(m_directoryUrl.isEmpty()) {
setState(State::ErrorOccured);
return;
}
setState(State::Checking);
auto *fetcher = new RemoteFileFetcher(this);
auto *buf = new QBuffer(this);
fetcher->connect(fetcher, &RemoteFileFetcher::finished, this, [=]() {
if(fetcher->isError()) {
qCCritical(CATEGORY_UPDATES).noquote() << "Failed to fetch update information:" << fetcher->errorString();
setState(State::ErrorOccured);
} else {
qCDebug(CATEGORY_UPDATES).noquote() << "Fetched update information from" << m_directoryUrl;
buf->open(QIODevice::ReadOnly);
fillFromJson(buf->readAll());
setState(m_channels.isEmpty() ? State::ErrorOccured : State::Ready);
}
fetcher->deleteLater();
buf->deleteLater();
});
if(!fetcher->fetch(m_directoryUrl, buf)) {
qCCritical(CATEGORY_UPDATES).noquote() << "Failed to fetch update information:" << fetcher->errorString();
setState(State::ErrorOccured);
buf->deleteLater();
}
m_checkTimer->start(std::chrono::minutes(10));
}
void UpdateRegistry::setState(State newState)
{
if(m_state == newState) {
return;
}
m_state = newState;
emit stateChanged();
}
|