2013-12-01 23:55:24 +00:00
|
|
|
#include "UpdateDialog.h"
|
|
|
|
#include "ui_UpdateDialog.h"
|
2015-02-02 01:14:14 +00:00
|
|
|
#include <QDebug>
|
2014-07-09 18:48:46 +01:00
|
|
|
#include "MultiMC.h"
|
2015-02-09 00:51:14 +00:00
|
|
|
#include <settings/SettingsObject.h>
|
2013-12-01 23:55:24 +00:00
|
|
|
|
2015-05-16 22:04:00 +01:00
|
|
|
#include <hoedown/html.h>
|
|
|
|
#include <hoedown/document.h>
|
|
|
|
|
2014-07-13 23:57:54 +01:00
|
|
|
UpdateDialog::UpdateDialog(bool hasUpdate, QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog)
|
2013-12-01 23:55:24 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-07-09 18:48:46 +01:00
|
|
|
auto channel = MMC->settings()->get("UpdateChannel").toString();
|
2014-07-13 23:57:54 +01:00
|
|
|
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-08 23:49:37 +01:00
|
|
|
loadChangelog();
|
2013-12-01 23:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateDialog::~UpdateDialog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-08 23:49:37 +01:00
|
|
|
void UpdateDialog::loadChangelog()
|
|
|
|
{
|
2014-07-09 18:48:46 +01:00
|
|
|
auto channel = MMC->settings()->get("UpdateChannel").toString();
|
2014-07-08 23:49:37 +01:00
|
|
|
dljob.reset(new NetJob("Changelog"));
|
2014-07-09 18:48:46 +01:00
|
|
|
auto url = QString("https://raw.githubusercontent.com/MultiMC/MultiMC5/%1/changelog.md").arg(channel);
|
2014-07-08 23:49:37 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-05-16 22:04:00 +01:00
|
|
|
/**
|
|
|
|
* hoedown wrapper, because dealing with resource lifetime in C is stupid
|
|
|
|
*/
|
|
|
|
class HoeDown
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
public:
|
|
|
|
class buffer
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
public:
|
|
|
|
buffer(size_t unit = 4096)
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
buf = hoedown_buffer_new(unit);
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
2015-05-16 22:04:00 +01:00
|
|
|
~buffer()
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
hoedown_buffer_free(buf);
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
2015-05-16 22:04:00 +01:00
|
|
|
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-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
renderer = hoedown_html_renderer_new((hoedown_html_flags) 0,0);
|
|
|
|
document = hoedown_document_new(renderer, (hoedown_extensions) 0, 8);
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
2015-05-16 22:04:00 +01:00
|
|
|
~HoeDown()
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
hoedown_document_free(document);
|
|
|
|
hoedown_html_renderer_free(renderer);
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
2015-05-16 22:04:00 +01:00
|
|
|
QString process(QByteArray input)
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
ib.put(input);
|
|
|
|
hoedown_document_render(document, ob.buf, ib.data(), ib.size());
|
|
|
|
return ob.cstr();
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
2015-05-16 22:04:00 +01:00
|
|
|
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-08 23:49:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDialog::changelogLoaded()
|
|
|
|
{
|
2015-05-16 22:04:00 +01:00
|
|
|
auto html = reprocessMarkdown(changelogDownload->m_data);
|
2014-07-08 23:49:37 +01:00
|
|
|
ui->changelogBrowser->setHtml(html);
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:47:14 +01:00
|
|
|
void UpdateDialog::changelogFailed(QString reason)
|
2014-07-08 23:49:37 +01:00
|
|
|
{
|
2015-04-26 12:47:14 +01:00
|
|
|
ui->changelogBrowser->setHtml(tr("<p align=\"center\" <span style=\"font-size:22pt;\">Failed to fetch changelog... Error: %1</span></p>").arg(reason));
|
2014-07-08 23:49:37 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 23:55:24 +00:00
|
|
|
void UpdateDialog::on_btnUpdateLater_clicked()
|
|
|
|
{
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDialog::on_btnUpdateNow_clicked()
|
|
|
|
{
|
|
|
|
done(UPDATE_NOW);
|
|
|
|
}
|