blob: 25cfa85352025a54eae2ed292baefa869782fccc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "UpdateDialog.h"
#include "ui_UpdateDialog.h"
#include "gui/Platform.h"
#include "logger/QsLog.h"
#include "MultiMC.h"
#include <logic/settings/SettingsObject.h>
UpdateDialog::UpdateDialog(QWidget *parent) : QDialog(parent), ui(new Ui::UpdateDialog)
{
MultiMCPlatform::fixWM_CLASS(this);
ui->setupUi(this);
auto channel = MMC->settings()->get("UpdateChannel").toString();
ui->label->setText(tr("A new %1 update is available!").arg(channel));
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 << "<html>";
int i = 0;
auto procLine = [&](QString line) -> QString
{
// [GitHub issues](https://github.com/MultiMC/MultiMC5/issues)
line.replace(QRegExp("\\[([^\\]]+)\\]\\(([^\\)]+)\\)"), "<a href=\"\\2\">\\1</a>");
return line;
};
for(auto line: lines)
{
if(line.isEmpty())
{
// html << "<br />\n";
}
else switch (state)
{
case BASE:
if(line.startsWith("##"))
{
html << "<h2>" << procLine(line.mid(2)) << "</h2>\n";
}
else if(line.startsWith("#"))
{
html << "<h1>" << procLine(line.mid(1)) << "</h1>\n";
}
else if(line.startsWith("- "))
{
state = LIST1;
html << "<ul>\n";
html << "<li>" << procLine(line.mid(2)) << "</li>\n";
}
else QLOG_ERROR() << "Invalid input on line " << i << ": " << line;
break;
case LIST1:
if(line.startsWith("##"))
{
state = BASE;
html << "</ul>\n";
html << "<h2>" << procLine(line.mid(2)) << "</h2>\n";
}
else if(line.startsWith("#"))
{
state = BASE;
html << "</ul>\n";
html << "<h1>" << procLine(line.mid(1)) << "</h1>\n";
}
else if(line.startsWith("- "))
{
html << "<li>" << procLine(line.mid(2)) << "</li>\n";
}
else if(line.startsWith(" - "))
{
state = LIST2;
html << "<ul>\n";
html << "<li>" << procLine(line.mid(4)) << "</li>\n";
}
else QLOG_ERROR() << "Invalid input on line " << i << ": " << line;
break;
case LIST2:
if(line.startsWith("##"))
{
state = BASE;
html << "</ul>\n";
html << "</ul>\n";
html << "<h2>" << procLine(line.mid(2)) << "</h2>\n";
}
else if(line.startsWith("#"))
{
state = BASE;
html << "</ul>\n";
html << "</ul>\n";
html << "<h1>" << procLine(line.mid(1)) << "</h1>\n";
}
else if(line.startsWith("- "))
{
state = LIST1;
html << "</ul>\n";
html << "<li>" << procLine(line.mid(2)) << "</li>\n";
}
else if(line.startsWith(" - "))
{
html << "<li>" << procLine(line.mid(4)) << "</li>\n";
}
else QLOG_ERROR() << "Invalid input on line " << i << ": " << line;
break;
}
i++;
}
if(state == LIST2)
{
html << "</ul>\n";
state = LIST1;
}
if(state == LIST1)
{
html << "</ul>\n";
state = BASE;
}
if (state != BASE)
{
QLOG_ERROR() << "Reprocessing markdown didn't end in a final state!";
}
html << "</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("<p align=\"center\" <span style=\"font-size:22pt;\">Failed to fetch changelog...</span></p>"));
}
void UpdateDialog::on_btnUpdateLater_clicked()
{
reject();
}
void UpdateDialog::on_btnUpdateNow_clicked()
{
done(UPDATE_NOW);
}
void UpdateDialog::on_btnUpdateOnExit_clicked()
{
done(UPDATE_ONEXIT);
}
|