-
Notifications
You must be signed in to change notification settings - Fork 780
/
application.cpp
320 lines (263 loc) · 9.19 KB
/
application.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/****************************************************************************
**
** Copyright (C) 2015-2016 Oleg Shparber
** Contact: https://go.zealdocs.org/l/contact
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "application.h"
#include "extractor.h"
#include "filemanager.h"
#include "httpserver.h"
#include "networkaccessmanager.h"
#include "settings.h"
#include <registry/docsetregistry.h>
#include <registry/searchquery.h>
#include <ui/mainwindow.h>
#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMetaObject>
#include <QNetworkProxy>
#include <QNetworkReply>
#include <QScopedPointer>
#include <QStandardPaths>
#include <QSysInfo>
#include <QThread>
using namespace Zeal;
using namespace Zeal::Core;
namespace {
constexpr char ReleasesApiUrl[] = "https://api.zealdocs.org/v1/releases";
} // namespace
Application *Application::m_instance = nullptr;
Application::Application(QObject *parent)
: QObject(parent)
{
// Ensure only one instance of Application
Q_ASSERT(!m_instance);
m_instance = this;
m_settings = new Settings(this);
m_networkManager = new NetworkAccessManager(this);
m_fileManager = new FileManager(this);
m_httpServer = new HttpServer(this);
connect(m_networkManager, &QNetworkAccessManager::sslErrors, this, [this](QNetworkReply *reply, const QList<QSslError> &errors)
{
if (m_settings->isIgnoreSSLErrorsEnabled) {
// Ignore all SSL errors
reply->ignoreSslErrors();
}
});
// Extractor setup
m_extractorThread = new QThread(this);
m_extractor = new Extractor();
m_extractor->moveToThread(m_extractorThread);
m_extractorThread->start();
connect(m_extractor, &Extractor::completed, this, &Application::extractionCompleted);
connect(m_extractor, &Extractor::error, this, &Application::extractionError);
connect(m_extractor, &Extractor::progress, this, &Application::extractionProgress);
m_docsetRegistry = new Registry::DocsetRegistry();
connect(m_settings, &Settings::updated, this, &Application::applySettings);
applySettings();
m_mainWindow = new WidgetUi::MainWindow(this);
if (m_settings->startMinimized) {
if (m_settings->showSystrayIcon && m_settings->minimizeToSystray)
return;
m_mainWindow->showMinimized();
} else {
m_mainWindow->show();
}
}
Application::~Application()
{
m_extractorThread->quit();
m_extractorThread->wait();
delete m_extractor;
delete m_mainWindow;
delete m_docsetRegistry;
}
/*!
* \internal
* \brief Returns a pointer to the Core::Application instance.
* \return A pointer or \c nullptr, if no instance has been created.
*/
Application *Application::instance()
{
return m_instance;
}
WidgetUi::MainWindow *Application::mainWindow() const
{
return m_mainWindow;
}
QNetworkAccessManager *Application::networkManager() const
{
return m_networkManager;
}
Settings *Application::settings() const
{
return m_settings;
}
Registry::DocsetRegistry *Application::docsetRegistry()
{
return m_docsetRegistry;
}
FileManager *Application::fileManager() const
{
return m_fileManager;
}
HttpServer *Application::httpServer() const
{
return m_httpServer;
}
QString Application::cacheLocation()
{
#ifndef PORTABLE_BUILD
return QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#else
return QCoreApplication::applicationDirPath() + QLatin1String("/cache");
#endif
}
QString Application::configLocation()
{
#ifndef PORTABLE_BUILD
// TODO: Replace 'Zeal/Zeal' with 'zeal'.
return QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
#else
return QCoreApplication::applicationDirPath() + QLatin1String("/config");
#endif
}
QVersionNumber Application::version()
{
static const auto vn = QVersionNumber::fromString(QCoreApplication::applicationVersion());
return vn;
}
QString Application::versionString()
{
static const auto v = QStringLiteral("v%1").arg(QCoreApplication::applicationVersion());
return v;
}
void Application::executeQuery(const Registry::SearchQuery &query, bool preventActivation)
{
m_mainWindow->search(query);
if (preventActivation)
return;
m_mainWindow->bringToFront();
}
void Application::extract(const QString &filePath, const QString &destination, const QString &root)
{
QMetaObject::invokeMethod(m_extractor, "extract", Qt::QueuedConnection,
Q_ARG(QString, filePath), Q_ARG(QString, destination),
Q_ARG(QString, root));
}
QNetworkReply *Application::download(const QUrl &url)
{
static const QString ua = userAgent();
static const QByteArray uaJson = userAgentJson().toUtf8();
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::UserAgentHeader, ua);
if (url.host().endsWith(QLatin1String(".zealdocs.org", Qt::CaseInsensitive)))
request.setRawHeader("X-Zeal-User-Agent", uaJson);
return m_networkManager->get(request);
}
/*!
\internal
Performs a check whether a new Zeal version is available. Setting \a quiet to true supresses
error and "you are using the latest version" message boxes.
*/
void Application::checkForUpdates(bool quiet)
{
QNetworkReply *reply = download(QUrl(ReleasesApiUrl));
connect(reply, &QNetworkReply::finished, this, [this, quiet]() {
QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply(
qobject_cast<QNetworkReply *>(sender()));
if (reply->error() != QNetworkReply::NoError) {
if (!quiet)
emit updateCheckError(reply->errorString());
return;
}
QJsonParseError jsonError;
const QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
if (!quiet)
emit updateCheckError(jsonError.errorString());
return;
}
const QJsonObject versionInfo = jsonDoc.array().first().toObject(); // Latest is the first.
const auto latestVersion
= QVersionNumber::fromString(versionInfo[QLatin1String("version")].toString());
if (latestVersion > version()) {
emit updateCheckDone(latestVersion.toString());
} else if (!quiet) {
emit updateCheckDone();
}
});
}
void Application::applySettings()
{
m_docsetRegistry->setStoragePath(m_settings->docsetPath);
m_docsetRegistry->setFuzzySearchEnabled(m_settings->isFuzzySearchEnabled);
// HTTP Proxy Settings
switch (m_settings->proxyType) {
case Settings::ProxyType::None:
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
break;
case Settings::ProxyType::System:
QNetworkProxyFactory::setUseSystemConfiguration(true);
break;
case Settings::ProxyType::Http:
case Settings::ProxyType::Socks5: {
const QNetworkProxy::ProxyType type = m_settings->proxyType == Settings::ProxyType::Socks5
? QNetworkProxy::Socks5Proxy
: QNetworkProxy::HttpProxy;
QNetworkProxy proxy(type, m_settings->proxyHost, m_settings->proxyPort);
if (m_settings->proxyAuthenticate) {
proxy.setUser(m_settings->proxyUserName);
proxy.setPassword(m_settings->proxyPassword);
}
QNetworkProxy::setApplicationProxy(proxy);
break;
}
}
// Force NM to pick up changes.
m_networkManager->clearAccessCache();
}
QString Application::userAgent()
{
return QStringLiteral("Zeal/%1").arg(QCoreApplication::applicationVersion());
}
QString Application::userAgentJson() const
{
QJsonObject app = {
{QStringLiteral("version"), QCoreApplication::applicationVersion()},
{QStringLiteral("qt_version"), qVersion()},
{QStringLiteral("install_id"), m_settings->installId}
};
QJsonObject os = {
{QStringLiteral("arch"), QSysInfo::currentCpuArchitecture()},
{QStringLiteral("name"), QSysInfo::prettyProductName()},
{QStringLiteral("product_type"), QSysInfo::productType()},
{QStringLiteral("product_version"), QSysInfo::productVersion()},
{QStringLiteral("kernel_type"), QSysInfo::kernelType()},
{QStringLiteral("kernel_version"), QSysInfo::kernelVersion()},
{QStringLiteral("locale"), QLocale::system().name()}
};
QJsonObject ua = {
{QStringLiteral("app"), app},
{QStringLiteral("os"), os}
};
return QString::fromUtf8(QJsonDocument(ua).toJson(QJsonDocument::Compact));
}