From fc911add58c97b878b5b2eff867ce35216eb2a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 9 Jul 2014 00:49:37 +0200 Subject: [PATCH] Show changelog in the update dialog. --- gui/dialogs/UpdateDialog.cpp | 145 +++++++++++++++++++++++++++++++++++ gui/dialogs/UpdateDialog.h | 15 ++++ gui/dialogs/UpdateDialog.ui | 116 ++++++++++++++++++---------- 3 files changed, 236 insertions(+), 40 deletions(-) diff --git a/gui/dialogs/UpdateDialog.cpp b/gui/dialogs/UpdateDialog.cpp index c56798b4..3194208f 100644 --- a/gui/dialogs/UpdateDialog.cpp +++ b/gui/dialogs/UpdateDialog.cpp @@ -1,17 +1,162 @@ #include "UpdateDialog.h" #include "ui_UpdateDialog.h" #include "gui/Platform.h" +#include +#include "logger/QsLog.h" UpdateDialog::UpdateDialog(QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog) { MultiMCPlatform::fixWM_CLASS(this); ui->setupUi(this); + loadChangelog(); } UpdateDialog::~UpdateDialog() { } +void UpdateDialog::loadChangelog() +{ + dljob.reset(new NetJob("Changelog")); + auto url = QString("https://raw.githubusercontent.com/MultiMC/MultiMC5/%1/changelog.md").arg(BuildConfig.VERSION_CHANNEL); + changelogDownload = ByteArrayDownload::make(QUrl(url)); + dljob->addNetAction(changelogDownload); + connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded); + connect(dljob.get(), &NetJob::failed, this, &UpdateDialog::changelogFailed); + dljob->start(); +} + +// TODO: this will be replaced. +QString reprocessMarkdown(QString markdown) +{ + QString htmlData; + QTextStream html(&htmlData); + auto lines = markdown.split(QRegExp("[\r]?[\n]"),QString::KeepEmptyParts); + enum + { + BASE, + LIST1, + LIST2 + }state = BASE; + html << ""; + int i = 0; + auto procLine = [&](QString line) -> QString + { + // [GitHub issues](https://github.com/MultiMC/MultiMC5/issues) + line.replace(QRegExp("\\[(.+)\\]\\((.+)\\)"), "\\1"); + return line; + }; + for(auto line: lines) + { + if(line.isEmpty()) + { + // html << "
\n"; + } + else switch (state) + { + case BASE: + if(line.startsWith("##")) + { + html << "

" << procLine(line.mid(2)) << "

\n"; + } + else if(line.startsWith("#")) + { + html << "

" << procLine(line.mid(1)) << "

\n"; + } + else if(line.startsWith("- ")) + { + state = LIST1; + html << "\n"; + html << "

" << procLine(line.mid(2)) << "

\n"; + } + else if(line.startsWith("#")) + { + state = BASE; + html << "\n"; + html << "

" << procLine(line.mid(1)) << "

\n"; + } + else if(line.startsWith("- ")) + { + html << "
  • " << procLine(line.mid(2)) << "
  • \n"; + } + else if(line.startsWith(" - ")) + { + state = LIST2; + html << "\n"; + html << "\n"; + html << "

    " << procLine(line.mid(2)) << "

    \n"; + } + else if(line.startsWith("#")) + { + state = BASE; + html << "\n"; + html << "\n"; + html << "

    " << procLine(line.mid(1)) << "

    \n"; + } + else if(line.startsWith("- ")) + { + state = LIST1; + html << "\n"; + html << "
  • " << procLine(line.mid(2)) << "
  • \n"; + } + else if(line.startsWith(" - ")) + { + html << "
  • " << procLine(line.mid(4)) << "
  • \n"; + } + else QLOG_ERROR() << "Invalid input on line " << i << ": " << line; + break; + } + i++; + } + if(state == LIST2) + { + html << "\n"; + state = LIST1; + } + if(state == LIST1) + { + html << "\n"; + state = BASE; + } + if (state != BASE) + { + QLOG_ERROR() << "Reprocessing markdown didn't end in a final state!"; + } + html << "\n"; + QLOG_DEBUG() << htmlData; + return htmlData; +} + +void UpdateDialog::changelogLoaded() +{ + auto rawMarkdown = QString::fromUtf8(changelogDownload->m_data); + auto html = reprocessMarkdown(rawMarkdown); + ui->changelogBrowser->setHtml(html); +} + +void UpdateDialog::changelogFailed() +{ + ui->changelogBrowser->setHtml(tr("

    Failed to fetch changelog...

    ")); +} + void UpdateDialog::on_btnUpdateLater_clicked() { reject(); diff --git a/gui/dialogs/UpdateDialog.h b/gui/dialogs/UpdateDialog.h index c13eb6bf..970ef42a 100644 --- a/gui/dialogs/UpdateDialog.h +++ b/gui/dialogs/UpdateDialog.h @@ -16,6 +16,8 @@ #pragma once #include +#include "logic/net/ByteArrayDownload.h" +#include "logic/net/NetJob.h" namespace Ui { @@ -43,4 +45,17 @@ public slots: void on_btnUpdateNow_clicked(); void on_btnUpdateOnExit_clicked(); void on_btnUpdateLater_clicked(); + + /// Starts loading the changelog + void loadChangelog(); + + /// Slot for when the chengelog loads successfully. + void changelogLoaded(); + + /// Slot for when the chengelog fails to load... + void changelogFailed(); + +private: + ByteArrayDownloadPtr changelogDownload; + NetJobPtr dljob; }; diff --git a/gui/dialogs/UpdateDialog.ui b/gui/dialogs/UpdateDialog.ui index 1fe65e62..c77d70de 100644 --- a/gui/dialogs/UpdateDialog.ui +++ b/gui/dialogs/UpdateDialog.ui @@ -6,65 +6,101 @@ 0 0 - 350 - 260 + 657 + 673 MultiMC Update - + :/icons/toolbar/checkupdate:/icons/toolbar/checkupdate - - - A new MultiMC update is available! + + + + + + 14 + + + + A new MultiMC update is available! + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + changelogBrowser + + + + + + + + + <!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:'Bitstream Vera Sans'; font-size:11pt; 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-size:22pt;">Loading changelog...</span></p></body></html> - - Qt::AlignCenter + + true - - - - 0 - 0 - - - - Update now - - - - - - - Update after MultiMC closes - - - - - - - - 0 - 0 - - - - Don't update yet - - + + + + + + 0 + 0 + + + + Update now + + + + + + + Update after MultiMC closes + + + + + + + + 0 + 0 + + + + Don't update yet + + + + + + changelogBrowser + btnUpdateNow + btnUpdateOnExit + btnUpdateLater + - +