From db877ba121ff87a4e029daf8555d85dfef45993a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 9 Feb 2015 01:51:14 +0100 Subject: NOISSUE move everything. --- application/dialogs/UpdateDialog.cpp | 186 +++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 application/dialogs/UpdateDialog.cpp (limited to 'application/dialogs/UpdateDialog.cpp') diff --git a/application/dialogs/UpdateDialog.cpp b/application/dialogs/UpdateDialog.cpp new file mode 100644 index 00000000..f7e0bcd6 --- /dev/null +++ b/application/dialogs/UpdateDialog.cpp @@ -0,0 +1,186 @@ +#include "UpdateDialog.h" +#include "ui_UpdateDialog.h" +#include "Platform.h" +#include +#include "MultiMC.h" +#include + +UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog) +{ + MultiMCPlatform::fixWM_CLASS(this); + ui->setupUi(this); + auto channel = MMC->settings()->get("UpdateChannel").toString(); + 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->setDisabled(true); + ui->btnUpdateOnExit->setDisabled(true); + } + loadChangelog(); +} + +UpdateDialog::~UpdateDialog() +{ +} + +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); + 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 qCritical() << "Invalid input on line " << i << ": " << line; + break; + case LIST1: + if(line.startsWith("##")) + { + state = BASE; + 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 << "
    • " << procLine(line.mid(4)) << "
    • \n"; + } + else qCritical() << "Invalid input on line " << i << ": " << line; + break; + case LIST2: + if(line.startsWith("##")) + { + state = BASE; + 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 qCritical() << "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) + { + qCritical() << "Reprocessing markdown didn't end in a final state!"; + } + html << "\n"; + qDebug() << 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(); +} + +void UpdateDialog::on_btnUpdateNow_clicked() +{ + done(UPDATE_NOW); +} + +void UpdateDialog::on_btnUpdateOnExit_clicked() +{ + done(UPDATE_ONEXIT); +} -- cgit v1.2.3