2013-12-02 05:25:24 +05:30
|
|
|
#include "UpdateDialog.h"
|
|
|
|
#include "ui_UpdateDialog.h"
|
2015-02-02 06:44:14 +05:30
|
|
|
#include <QDebug>
|
2021-11-20 20:52:22 +05:30
|
|
|
#include "Application.h"
|
2015-02-09 06:21:14 +05:30
|
|
|
#include <settings/SettingsObject.h>
|
2015-12-28 09:15:49 +05:30
|
|
|
#include <Json.h>
|
2013-12-02 05:25:24 +05:30
|
|
|
|
2015-12-28 09:15:49 +05:30
|
|
|
#include "BuildConfig.h"
|
2017-05-06 21:57:47 +05:30
|
|
|
#include "HoeDown.h"
|
2015-12-28 09:15:49 +05:30
|
|
|
|
2014-07-14 04:27:54 +05:30
|
|
|
UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog)
|
2013-12-02 05:25:24 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->setupUi(this);
|
2021-11-20 20:52:22 +05:30
|
|
|
auto channel = APPLICATION->settings()->get("UpdateChannel").toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
if(hasUpdate)
|
|
|
|
{
|
|
|
|
ui->label->setText(tr("A new %1 update is available!").arg(channel));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->label->setText(tr("No %1 updates found. You are running the latest version.").arg(channel));
|
|
|
|
ui->btnUpdateNow->setHidden(true);
|
|
|
|
ui->btnUpdateLater->setText(tr("Close"));
|
|
|
|
}
|
2019-09-27 03:53:30 +05:30
|
|
|
ui->changelogBrowser->setHtml(tr("<center><h1>Loading changelog...</h1></center>"));
|
2018-07-15 18:21:05 +05:30
|
|
|
loadChangelog();
|
2021-11-20 20:52:22 +05:30
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get("UpdateDialogGeometry").toByteArray()));
|
2013-12-02 05:25:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
UpdateDialog::~UpdateDialog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-09 04:19:37 +05:30
|
|
|
void UpdateDialog::loadChangelog()
|
|
|
|
{
|
2021-11-20 20:52:22 +05:30
|
|
|
auto channel = APPLICATION->settings()->get("UpdateChannel").toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
dljob.reset(new NetJob("Changelog"));
|
|
|
|
QString url;
|
|
|
|
if(channel == "stable")
|
|
|
|
{
|
2021-10-22 02:55:52 +05:30
|
|
|
url = QString("https://raw.githubusercontent.com/MultiMC/Launcher/%1/changelog.md").arg(channel);
|
2018-07-15 18:21:05 +05:30
|
|
|
m_changelogType = CHANGELOG_MARKDOWN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-22 02:55:52 +05:30
|
|
|
url = QString("https://api.github.com/repos/MultiMC/Launcher/compare/%1...%2").arg(BuildConfig.GIT_COMMIT, channel);
|
2018-07-15 18:21:05 +05:30
|
|
|
m_changelogType = CHANGELOG_COMMITS;
|
|
|
|
}
|
|
|
|
dljob->addNetAction(Net::Download::makeByteArray(QUrl(url), &changelogData));
|
|
|
|
connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded);
|
|
|
|
connect(dljob.get(), &NetJob::failed, this, &UpdateDialog::changelogFailed);
|
2021-11-22 03:51:12 +05:30
|
|
|
dljob->start(APPLICATION->network());
|
2014-07-09 04:19:37 +05:30
|
|
|
}
|
|
|
|
|
2015-05-17 02:34:00 +05:30
|
|
|
QString reprocessMarkdown(QByteArray markdown)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
HoeDown hoedown;
|
|
|
|
QString output = hoedown.process(markdown);
|
2015-05-17 02:34:00 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// HACK: easier than customizing hoedown
|
2021-10-22 02:55:52 +05:30
|
|
|
output.replace(QRegExp("GH-([0-9]+)"), "<a href=\"https://github.com/MultiMC/Launcher/issues/\\1\">GH-\\1</a>");
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << output;
|
|
|
|
return output;
|
2014-07-09 04:19:37 +05:30
|
|
|
}
|
|
|
|
|
2015-12-28 09:15:49 +05:30
|
|
|
QString reprocessCommits(QByteArray json)
|
|
|
|
{
|
2021-11-20 20:52:22 +05:30
|
|
|
auto channel = APPLICATION->settings()->get("UpdateChannel").toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
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");
|
2015-12-28 09:15:49 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
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 parents_info = Json::ensureArray(commitobj, "parents");
|
|
|
|
// NOTE: this ignores merge commits, because they have more than one parent
|
|
|
|
if(parents_info.size() > 1)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
{
|
2021-10-22 02:55:52 +05:30
|
|
|
result += QString("<a href=\"https://github.com/MultiMC/Launcher/issues/%1\">GH-%2</a>").arg(issuenr, issuenr);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
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>";
|
|
|
|
};
|
2015-12-28 09:15:49 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
if(status == "identical")
|
|
|
|
{
|
|
|
|
return QObject::tr("<p>There are no code changes between your current version and latest %1.</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).arg(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 (const JSONValidationError &e)
|
|
|
|
{
|
|
|
|
qWarning() << "Got an unparseable commit log from github:" << e.what();
|
|
|
|
qDebug() << json;
|
|
|
|
}
|
|
|
|
return QString();
|
2015-12-28 09:15:49 +05:30
|
|
|
}
|
|
|
|
|
2014-07-09 04:19:37 +05:30
|
|
|
void UpdateDialog::changelogLoaded()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QString result;
|
|
|
|
switch(m_changelogType)
|
|
|
|
{
|
|
|
|
case CHANGELOG_COMMITS:
|
|
|
|
result = reprocessCommits(changelogData);
|
|
|
|
break;
|
|
|
|
case CHANGELOG_MARKDOWN:
|
|
|
|
result = reprocessMarkdown(changelogData);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
changelogData.clear();
|
|
|
|
ui->changelogBrowser->setHtml(result);
|
2014-07-09 04:19:37 +05:30
|
|
|
}
|
|
|
|
|
2015-04-26 17:17:14 +05:30
|
|
|
void UpdateDialog::changelogFailed(QString reason)
|
2014-07-09 04:19:37 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->changelogBrowser->setHtml(tr("<p align=\"center\" <span style=\"font-size:22pt;\">Failed to fetch changelog... Error: %1</span></p>").arg(reason));
|
2014-07-09 04:19:37 +05:30
|
|
|
}
|
|
|
|
|
2013-12-02 05:25:24 +05:30
|
|
|
void UpdateDialog::on_btnUpdateLater_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
reject();
|
2013-12-02 05:25:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDialog::on_btnUpdateNow_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
done(UPDATE_NOW);
|
2013-12-02 05:25:24 +05:30
|
|
|
}
|
2017-05-09 05:24:28 +05:30
|
|
|
|
|
|
|
void UpdateDialog::closeEvent(QCloseEvent* evt)
|
|
|
|
{
|
2021-11-20 20:52:22 +05:30
|
|
|
APPLICATION->settings()->set("UpdateDialogGeometry", saveGeometry().toBase64());
|
2018-07-15 18:21:05 +05:30
|
|
|
QDialog::closeEvent(evt);
|
2017-05-09 05:24:28 +05:30
|
|
|
}
|