10 #include <QMessageBox>
11 #include <QApplication>
12 #include <QJsonDocument>
13 #include <QDesktopServices>
27 m_customAppcast =
false;
28 m_notifyOnUpdate =
true;
29 m_notifyOnFinish =
false;
30 m_updateAvailable =
false;
31 m_downloaderEnabled =
true;
32 m_moduleName = qApp->applicationName();
33 m_moduleVersion = qApp->applicationVersion();
34 m_mandatoryUpdate =
false;
37 m_manager =
new QNetworkAccessManager();
40 m_platform =
"windows";
41 #elif defined Q_OS_MAC
43 #elif defined Q_OS_LINUX
45 #elif defined Q_OS_ANDROID
46 m_platform =
"android";
47 #elif defined Q_OS_IOS
51 setUserAgentString(QString(
"%1/%2 (Qt; QSimpleUpdater)").arg(qApp->applicationName(), qApp->applicationVersion()));
54 connect(m_manager, SIGNAL(finished(QNetworkReply *)),
this, SLOT(onReply(QNetworkReply *)));
119 return m_downloadUrl;
128 return m_latestVersion;
137 return m_userAgentString;
145 return m_moduleVersion;
155 return m_customAppcast;
164 return m_notifyOnUpdate;
176 return m_notifyOnFinish;
185 return m_mandatoryUpdate;
194 return m_updateAvailable;
204 return m_downloaderEnabled;
223 QNetworkRequest request(
url());
227 m_manager->get(request);
255 m_notifyOnUpdate = notify;
264 m_notifyOnFinish = notify;
275 m_userAgentString = agent;
296 m_downloaderEnabled = enabled;
340 m_mandatoryUpdate = mandatory_update;
345 void Updater::onReply(QNetworkReply *reply)
348 QUrl redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
349 if (!redirect.isEmpty())
351 setUrl(redirect.toString());
357 if (reply->error() != QNetworkReply::NoError)
359 setUpdateAvailable(
false);
373 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
376 if (document.isNull())
378 setUpdateAvailable(
false);
384 QJsonObject updates = document.object().value(
"updates").toObject();
385 QJsonObject platform = updates.value(
platformKey()).toObject();
388 m_openUrl = platform.value(
"open-url").toString();
389 m_changelog = platform.value(
"changelog").toString();
390 m_downloadUrl = platform.value(
"download-url").toString();
391 m_latestVersion = platform.value(
"latest-version").toString();
392 if (platform.contains(
"mandatory"))
393 m_mandatoryUpdate = platform.value(
"mandatory").toBool();
404 void Updater::setUpdateAvailable(
const bool available)
406 m_updateAvailable = available;
409 box.setTextFormat(Qt::RichText);
410 box.setIcon(QMessageBox::Information);
414 QString text = tr(
"Would you like to download the update now?");
415 if (m_mandatoryUpdate)
417 text = tr(
"Would you like to download the update now? This is a mandatory update, "
418 "you should install this version after exiting application!");
425 box.setInformativeText(text);
426 box.setStandardButtons(QMessageBox::No | QMessageBox::Yes);
427 box.setDefaultButton(QMessageBox::Yes);
429 if (box.exec() == QMessageBox::Yes)
433 QDesktopServices::openUrl(QUrl(
openUrl()));
438 QString fileName =
downloadUrl().split(
"/").last();
439 m_downloader->
setFileName(fileName.split(
"?").first());
458 box.setStandardButtons(QMessageBox::Close);
459 box.setInformativeText(tr(
"No updates are available for the moment"));
461 + tr(
"Congratulations! You are running the "
462 "latest version of %1")
476 bool Updater::compare(
const QString &x,
const QString &y)
478 QStringList versionsX = x.split(
".");
479 QStringList versionsY = y.split(
".");
481 int count = qMin(versionsX.count(), versionsY.count());
483 for (
int i = 0; i <
count; ++i)
485 int a = QString(versionsX.at(i)).toInt();
486 int b = QString(versionsY.at(i)).toInt();
495 return versionsY.count() < versionsX.count();
static bool Print(const char *format,...)
Prints out a formatted message in console.
Implements an integrated file downloader with a nice UI.
void setUserAgentString(const QString &agent)
bool useCustomInstallProcedures() const
void startDownload(const QUrl &url)
void setFileName(const QString &file)
void setMandatoryUpdate(const bool mandatory_update)
void setUseCustomInstallProcedures(const bool custom)
void setUrlId(const QString &url)
QString changelog() const
void setNotifyOnFinish(const bool notify)
void setUrl(const QString &url)
void setDownloaderEnabled(const bool enabled)
bool useCustomInstallProcedures() const
bool notifyOnFinish() const
void downloadFinished(const QString &url, const QString &filepath)
void setUseCustomInstallProcedures(const bool custom)
QString moduleName() const
QString latestVersion() const
bool mandatoryUpdate() const
void setModuleName(const QString &name)
QString downloadUrl() const
void checkingFinished(const QString &url)
void setPlatformKey(const QString &platformKey)
bool customAppcast() const
void setModuleVersion(const QString &version)
void setMandatoryUpdate(const bool mandatory_update)
bool downloaderEnabled() const
bool notifyOnUpdate() const
QString platformKey() const
void setUseCustomAppcast(const bool customAppcast)
bool updateAvailable() const
void appcastDownloaded(const QString &url, const QByteArray &data)
void setNotifyOnUpdate(const bool notify)
QString userAgentString() const
QString moduleVersion() const
void setUserAgentString(const QString &agent)