pollymc/application/dialogs/UpdateDialog.cpp

131 lines
3.0 KiB
C++
Raw Normal View History

#include "UpdateDialog.h"
#include "ui_UpdateDialog.h"
2015-02-09 06:21:14 +05:30
#include "Platform.h"
#include <QDebug>
#include "MultiMC.h"
2015-02-09 06:21:14 +05:30
#include <settings/SettingsObject.h>
#include <hoedown/html.h>
#include <hoedown/document.h>
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);
}
2014-07-09 04:19:37 +05:30
loadChangelog();
}
UpdateDialog::~UpdateDialog()
{
}
2014-07-09 04:19:37 +05:30
void UpdateDialog::loadChangelog()
{
auto channel = MMC->settings()->get("UpdateChannel").toString();
2014-07-09 04:19:37 +05:30
dljob.reset(new NetJob("Changelog"));
auto url = QString("https://raw.githubusercontent.com/MultiMC/MultiMC5/%1/changelog.md").arg(channel);
2014-07-09 04:19:37 +05:30
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();
}
/**
* hoedown wrapper, because dealing with resource lifetime in C is stupid
*/
class HoeDown
2014-07-09 04:19:37 +05:30
{
public:
class buffer
2014-07-09 04:19:37 +05:30
{
public:
buffer(size_t unit = 4096)
2014-07-09 04:19:37 +05:30
{
buf = hoedown_buffer_new(unit);
2014-07-09 04:19:37 +05:30
}
~buffer()
2014-07-09 04:19:37 +05:30
{
hoedown_buffer_free(buf);
2014-07-09 04:19:37 +05:30
}
const char * cstr()
{
return hoedown_buffer_cstr(buf);
}
void put(QByteArray input)
{
hoedown_buffer_put(buf, (uint8_t *) input.data(), input.size());
}
const uint8_t * data() const
{
return buf->data;
}
size_t size() const
{
return buf->size;
}
hoedown_buffer * buf;
} ib, ob;
HoeDown()
2014-07-09 04:19:37 +05:30
{
renderer = hoedown_html_renderer_new((hoedown_html_flags) 0,0);
document = hoedown_document_new(renderer, (hoedown_extensions) 0, 8);
2014-07-09 04:19:37 +05:30
}
~HoeDown()
2014-07-09 04:19:37 +05:30
{
hoedown_document_free(document);
hoedown_html_renderer_free(renderer);
2014-07-09 04:19:37 +05:30
}
QString process(QByteArray input)
2014-07-09 04:19:37 +05:30
{
ib.put(input);
hoedown_document_render(document, ob.buf, ib.data(), ib.size());
return ob.cstr();
2014-07-09 04:19:37 +05:30
}
private:
hoedown_document * document;
hoedown_renderer * renderer;
};
QString reprocessMarkdown(QByteArray markdown)
{
HoeDown hoedown;
QString output = hoedown.process(markdown);
// HACK: easier than customizing hoedown
output.replace(QRegExp("GH-([0-9]+)"), "<a href=\"https://github.com/MultiMC/MultiMC5/issues/\\1\">GH-\\1</a>");
qDebug() << output;
return output;
2014-07-09 04:19:37 +05:30
}
void UpdateDialog::changelogLoaded()
{
auto html = reprocessMarkdown(changelogDownload->m_data);
2014-07-09 04:19:37 +05:30
ui->changelogBrowser->setHtml(html);
}
2015-04-26 17:17:14 +05:30
void UpdateDialog::changelogFailed(QString reason)
2014-07-09 04:19:37 +05:30
{
2015-04-26 17:17:14 +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
}
void UpdateDialog::on_btnUpdateLater_clicked()
{
reject();
}
void UpdateDialog::on_btnUpdateNow_clicked()
{
done(UPDATE_NOW);
}