diff options
author | Petr Mrázek <peterix@gmail.com> | 2015-12-28 04:45:49 +0100 |
---|---|---|
committer | Petr Mrázek <peterix@gmail.com> | 2015-12-28 05:36:17 +0100 |
commit | 5402acb3c6cf9b63c9df69ee463cae02259dfdef (patch) | |
tree | 76bbfd58704b3634fe6a707c6ec13a5dccb267ea /application/dialogs | |
parent | e8063d193d56acc10c5a533cffd95c9271b24e65 (diff) | |
download | MultiMC-5402acb3c6cf9b63c9df69ee463cae02259dfdef.tar MultiMC-5402acb3c6cf9b63c9df69ee463cae02259dfdef.tar.gz MultiMC-5402acb3c6cf9b63c9df69ee463cae02259dfdef.tar.lz MultiMC-5402acb3c6cf9b63c9df69ee463cae02259dfdef.tar.xz MultiMC-5402acb3c6cf9b63c9df69ee463cae02259dfdef.zip |
GH-1360 add basic changelog based on github API, fix update dialog buttons
Diffstat (limited to 'application/dialogs')
-rw-r--r-- | application/dialogs/UpdateDialog.cpp | 106 | ||||
-rw-r--r-- | application/dialogs/UpdateDialog.h | 7 | ||||
-rw-r--r-- | application/dialogs/UpdateDialog.ui | 8 |
3 files changed, 113 insertions, 8 deletions
diff --git a/application/dialogs/UpdateDialog.cpp b/application/dialogs/UpdateDialog.cpp index 4661bcb5..ff9513d1 100644 --- a/application/dialogs/UpdateDialog.cpp +++ b/application/dialogs/UpdateDialog.cpp @@ -3,10 +3,13 @@ #include <QDebug> #include "MultiMC.h" #include <settings/SettingsObject.h> +#include <Json.h> #include <hoedown/html.h> #include <hoedown/document.h> +#include "BuildConfig.h" + UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog) { ui->setupUi(this); @@ -18,7 +21,8 @@ UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), u else { ui->label->setText(tr("No %1 updates found. You are running the latest version.").arg(channel)); - ui->btnUpdateNow->setDisabled(true); + ui->btnUpdateNow->setHidden(true); + ui->btnUpdateLater->setText(tr("Close")); } loadChangelog(); } @@ -31,7 +35,17 @@ void UpdateDialog::loadChangelog() { auto channel = MMC->settings()->get("UpdateChannel").toString(); dljob.reset(new NetJob("Changelog")); - auto url = QString("https://raw.githubusercontent.com/MultiMC/MultiMC5/%1/changelog.md").arg(channel); + QString url; + if(channel == "stable") + { + url = QString("https://raw.githubusercontent.com/MultiMC/MultiMC5/%1/changelog.md").arg(channel); + m_changelogType = CHANGELOG_MARKDOWN; + } + else + { + url = QString("https://api.github.com/repos/MultiMC/MultiMC5/compare/%1...%2").arg(BuildConfig.GIT_COMMIT, channel); + m_changelogType = CHANGELOG_COMMITS; + } changelogDownload = ByteArrayDownload::make(QUrl(url)); dljob->addNetAction(changelogDownload); connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded); @@ -106,10 +120,94 @@ QString reprocessMarkdown(QByteArray markdown) return output; } +QString reprocessCommits(QByteArray json) +{ + auto channel = MMC->settings()->get("UpdateChannel").toString(); + try + { + QString result; + auto document = Json::requireDocument(json); + auto rootobject = Json::requireObject(document); + auto status = Json::requireString(rootobject, "status"); + auto diff_url = Json::requireString(rootobject, "html_url"); + + auto print_commits = [&]() + { + result += "<table cellspacing=0 cellpadding=2 style='border-width: 1px; border-style: solid'>"; + auto commitarray = Json::requireArray(rootobject, "commits"); + for(int i = commitarray.size() - 1; i >= 0; i--) + { + const auto & commitval = commitarray[i]; + auto commitobj = Json::requireObject(commitval); + auto commit_url = Json::requireString(commitobj, "html_url"); + auto commit_info = Json::requireObject(commitobj, "commit"); + auto commit_message = Json::requireString(commit_info, "message"); + auto lines = commit_message.split('\n'); + QRegularExpression regexp("(?<prefix>(GH-(?<issuenr>[0-9]+))|(NOISSUE)|(SCRATCH))? *(?<rest>.*) *"); + auto match = regexp.match(lines.takeFirst(), 0, QRegularExpression::NormalMatch); + auto issuenr = match.captured("issuenr"); + auto prefix = match.captured("prefix"); + auto rest = match.captured("rest"); + result += "<tr><td>"; + if(issuenr.length()) + { + result += QString("<a href=\"https://github.com/MultiMC/MultiMC5/issues/%1\">GH-%2</a>").arg(issuenr, issuenr); + } + else if(prefix.length()) + { + result += QString("<a href=\"%1\">%2</a>").arg(commit_url, prefix); + } + else + { + result += QString("<a href=\"%1\">NOISSUE</a>").arg(commit_url); + } + result += "</td>"; + lines.prepend(rest); + result += "<td><p>" + lines.join("<br />") + "</p></td></tr>"; + } + result += "</table>"; + }; + + if(status == "identical") + { + return QObject::tr("<p>There is are no code changes between your current version and %1 HEAD.</p>").arg(channel); + } + else if(status == "ahead") + { + result += QObject::tr("<p>Following commits were added since last update:</p>"); + print_commits(); + } + else if(status == "diverged") + { + auto commit_ahead = Json::requireInteger(rootobject, "ahead_by"); + auto commit_behind = Json::requireInteger(rootobject, "behind_by"); + result += QObject::tr("<p>The update removes %1 commits and adds the following %2:</p>").arg(commit_behind, commit_ahead); + print_commits(); + } + result += QObject::tr("<p>You can <a href=\"%1\">look at the changes on github</a>.</p>").arg(diff_url); + return result; + } + catch (JSONValidationError & e) + { + qWarning() << "Got an unparseable commit log from github:" << e.what(); + qDebug() << json; + } + return QString(); +} + void UpdateDialog::changelogLoaded() { - auto html = reprocessMarkdown(changelogDownload->m_data); - ui->changelogBrowser->setHtml(html); + QString result; + switch(m_changelogType) + { + case CHANGELOG_COMMITS: + result = reprocessCommits(changelogDownload->m_data); + break; + case CHANGELOG_MARKDOWN: + result = reprocessMarkdown(changelogDownload->m_data); + break; + } + ui->changelogBrowser->setHtml(result); } void UpdateDialog::changelogFailed(QString reason) diff --git a/application/dialogs/UpdateDialog.h b/application/dialogs/UpdateDialog.h index 5237df5c..403b78ad 100644 --- a/application/dialogs/UpdateDialog.h +++ b/application/dialogs/UpdateDialog.h @@ -30,6 +30,12 @@ enum UpdateAction UPDATE_NOW = QDialog::Accepted, }; +enum ChangelogType +{ + CHANGELOG_MARKDOWN, + CHANGELOG_COMMITS +}; + class UpdateDialog : public QDialog { Q_OBJECT @@ -56,4 +62,5 @@ public slots: private: ByteArrayDownloadPtr changelogDownload; NetJobPtr dljob; + ChangelogType m_changelogType = CHANGELOG_MARKDOWN; }; diff --git a/application/dialogs/UpdateDialog.ui b/application/dialogs/UpdateDialog.ui index cbe1ef74..8e34a521 100644 --- a/application/dialogs/UpdateDialog.ui +++ b/application/dialogs/UpdateDialog.ui @@ -46,7 +46,7 @@ <string><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Oxygen-Sans'; font-size:11pt; font-weight:400; font-style:normal;"> +</style></head><body style=" font-family:'Noto Sans'; font-size:12pt; font-weight:400; font-style:normal;"> <p align="center" style=" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Bitstream Vera Sans'; font-size:22pt;">Loading changelog...</span></p></body></html></string> </property> <property name="openExternalLinks"> @@ -55,8 +55,8 @@ p, li { white-space: pre-wrap; } </widget> </item> <item> - <layout class="QHBoxLayout" name="horizontalLayout"> - <item> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> <widget class="QPushButton" name="btnUpdateNow"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> @@ -69,7 +69,7 @@ p, li { white-space: pre-wrap; } </property> </widget> </item> - <item> + <item row="0" column="1"> <widget class="QPushButton" name="btnUpdateLater"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> |