|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2015-2016 Oleg Shparber |
| 4 | +** Contact: https://go.zealdocs.org/l/contact |
| 5 | +** |
| 6 | +** This file is part of Zeal. |
| 7 | +** |
| 8 | +** Zeal is free software: you can redistribute it and/or modify |
| 9 | +** it under the terms of the GNU General Public License as published by |
| 10 | +** the Free Software Foundation, either version 3 of the License, or |
| 11 | +** (at your option) any later version. |
| 12 | +** |
| 13 | +** Zeal is distributed in the hope that it will be useful, |
| 14 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +** GNU General Public License for more details. |
| 17 | +** |
| 18 | +** You should have received a copy of the GNU General Public License |
| 19 | +** along with Zeal. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +** |
| 21 | +****************************************************************************/ |
| 22 | + |
| 23 | +#include "application.h" |
| 24 | + |
| 25 | +#include "extractor.h" |
| 26 | +#include "filemanager.h" |
| 27 | +#include "networkaccessmanager.h" |
| 28 | +#include "settings.h" |
| 29 | + |
| 30 | +#include <registry/docsetregistry.h> |
| 31 | +#include <registry/searchquery.h> |
| 32 | +#include <ui/mainwindow.h> |
| 33 | +#include <util/version.h> |
| 34 | + |
| 35 | +#include <QCoreApplication> |
| 36 | +#include <QJsonArray> |
| 37 | +#include <QJsonDocument> |
| 38 | +#include <QJsonObject> |
| 39 | +#include <QMetaObject> |
| 40 | +#include <QNetworkProxy> |
| 41 | +#include <QNetworkReply> |
| 42 | +#include <QScopedPointer> |
| 43 | +#include <QSysInfo> |
| 44 | +#include <QThread> |
| 45 | + |
| 46 | +using namespace Zeal; |
| 47 | +using namespace Zeal::Core; |
| 48 | + |
| 49 | +namespace { |
| 50 | +const char ReleasesApiUrl[] = "http://api.zealdocs.org/v1/releases"; |
| 51 | +} |
| 52 | + |
| 53 | +Application *Application::m_instance = nullptr; |
| 54 | + |
| 55 | +Application::Application(QObject *parent) : |
| 56 | + QObject(parent) |
| 57 | +{ |
| 58 | + // Ensure only one instance of Application |
| 59 | + Q_ASSERT(!m_instance); |
| 60 | + m_instance = this; |
| 61 | + |
| 62 | + m_settings = new Settings(this); |
| 63 | + m_networkManager = new NetworkAccessManager(this); |
| 64 | + |
| 65 | + m_fileManager = new FileManager(this); |
| 66 | + |
| 67 | + // Extractor setup |
| 68 | +// m_extractorThread = new QThread(this); |
| 69 | +// m_extractor = new Extractor(); |
| 70 | +// m_extractor->moveToThread(m_extractorThread); |
| 71 | +// m_extractorThread->start(); |
| 72 | +// connect(m_extractor, &Extractor::completed, this, &Application::extractionCompleted); |
| 73 | +// connect(m_extractor, &Extractor::error, this, &Application::extractionError); |
| 74 | +// connect(m_extractor, &Extractor::progress, this, &Application::extractionProgress); |
| 75 | + |
| 76 | + m_docsetRegistry = new Registry::DocsetRegistry(); |
| 77 | + |
| 78 | + connect(m_settings, &Settings::updated, this, &Application::applySettings); |
| 79 | + applySettings(); |
| 80 | + |
| 81 | + m_mainWindow = new WidgetUi::MainWindow(this); |
| 82 | + |
| 83 | + if (m_settings->startMinimized) { |
| 84 | + if (m_settings->showSystrayIcon && m_settings->minimizeToSystray) |
| 85 | + return; |
| 86 | + |
| 87 | + m_mainWindow->showMinimized(); |
| 88 | + } else { |
| 89 | + m_mainWindow->show(); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +Application::~Application() |
| 94 | +{ |
| 95 | +// m_extractorThread->quit(); |
| 96 | +// m_extractorThread->wait(); |
| 97 | +// delete m_extractor; |
| 98 | + delete m_mainWindow; |
| 99 | + delete m_docsetRegistry; |
| 100 | +} |
| 101 | + |
| 102 | +/*! |
| 103 | + * \internal |
| 104 | + * \brief Returns a pointer to the Core::Application instance. |
| 105 | + * \return A pointer or \c nullptr, if no instance has been created. |
| 106 | + */ |
| 107 | +Application *Application::instance() |
| 108 | +{ |
| 109 | + return m_instance; |
| 110 | +} |
| 111 | + |
| 112 | +WidgetUi::MainWindow *Application::mainWindow() const |
| 113 | +{ |
| 114 | + return m_mainWindow; |
| 115 | +} |
| 116 | + |
| 117 | +QNetworkAccessManager *Application::networkManager() const |
| 118 | +{ |
| 119 | + return m_networkManager; |
| 120 | +} |
| 121 | + |
| 122 | +Settings *Application::settings() const |
| 123 | +{ |
| 124 | + return m_settings; |
| 125 | +} |
| 126 | + |
| 127 | +Registry::DocsetRegistry *Application::docsetRegistry() |
| 128 | +{ |
| 129 | + return m_docsetRegistry; |
| 130 | +} |
| 131 | + |
| 132 | +FileManager *Application::fileManager() const |
| 133 | +{ |
| 134 | + return m_fileManager; |
| 135 | +} |
| 136 | + |
| 137 | +void Application::executeQuery(const Registry::SearchQuery &query, bool preventActivation) |
| 138 | +{ |
| 139 | + m_mainWindow->search(query); |
| 140 | + |
| 141 | + if (preventActivation) |
| 142 | + return; |
| 143 | + |
| 144 | + m_mainWindow->bringToFront(); |
| 145 | +} |
| 146 | + |
| 147 | +//void Application::extract(const QString &filePath, const QString &destination, const QString &root) |
| 148 | +//{ |
| 149 | +//// QMetaObject::invokeMethod(m_extractor, "extract", Qt::QueuedConnection, |
| 150 | +//// Q_ARG(QString, filePath), Q_ARG(QString, destination), |
| 151 | +//// Q_ARG(QString, root)); |
| 152 | +//} |
| 153 | + |
| 154 | +QNetworkReply *Application::download(const QUrl &url) |
| 155 | +{ |
| 156 | + static const QString ua = userAgent(); |
| 157 | + static const QByteArray uaJson = userAgentJson().toUtf8(); |
| 158 | + |
| 159 | + QNetworkRequest request(url); |
| 160 | + request.setHeader(QNetworkRequest::UserAgentHeader, ua); |
| 161 | + |
| 162 | + if (url.host().endsWith(QLatin1String(".zealdocs.org", Qt::CaseInsensitive))) |
| 163 | + request.setRawHeader("X-Zeal-User-Agent", uaJson); |
| 164 | + |
| 165 | + return m_networkManager->get(request); |
| 166 | +} |
| 167 | + |
| 168 | +/*! |
| 169 | + \internal |
| 170 | +
|
| 171 | + Performs a check whether a new Zeal version is available. Setting \a quiet to true supresses |
| 172 | + error and "you are using the latest version" message boxes. |
| 173 | +*/ |
| 174 | +// void Application::checkForUpdates(bool quiet) |
| 175 | +// { |
| 176 | +// QNetworkReply *reply = download(QUrl(ReleasesApiUrl)); |
| 177 | +// connect(reply, &QNetworkReply::finished, this, [this, quiet]() { |
| 178 | +// QScopedPointer<QNetworkReply, QScopedPointerDeleteLater> reply( |
| 179 | +// qobject_cast<QNetworkReply *>(sender())); |
| 180 | + |
| 181 | +// if (reply->error() != QNetworkReply::NoError) { |
| 182 | +// if (!quiet) |
| 183 | +// emit updateCheckError(reply->errorString()); |
| 184 | +// return; |
| 185 | +// } |
| 186 | + |
| 187 | +// QJsonParseError jsonError; |
| 188 | +// const QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &jsonError); |
| 189 | + |
| 190 | +// if (jsonError.error != QJsonParseError::NoError) { |
| 191 | +// if (!quiet) |
| 192 | +// emit updateCheckError(jsonError.errorString()); |
| 193 | +// return; |
| 194 | +// } |
| 195 | + |
| 196 | +// const QJsonObject latestVersionInfo = jsonDoc.array().first().toObject(); |
| 197 | +// const Util::Version latestVersion = latestVersionInfo[QStringLiteral("version")].toString(); |
| 198 | +// if (latestVersion > Util::Version(QCoreApplication::applicationVersion())) |
| 199 | +// emit updateCheckDone(latestVersion.toString()); |
| 200 | +// else if (!quiet) |
| 201 | +// emit updateCheckDone(); |
| 202 | +// }); |
| 203 | +// } |
| 204 | + |
| 205 | +void Application::applySettings() |
| 206 | +{ |
| 207 | + m_docsetRegistry->setStoragePath(m_settings->docsetPath); |
| 208 | + m_docsetRegistry->setFuzzySearchEnabled(m_settings->fuzzySearchEnabled); |
| 209 | + |
| 210 | + // HTTP Proxy Settings |
| 211 | + switch (m_settings->proxyType) { |
| 212 | + case Core::Settings::ProxyType::None: |
| 213 | + QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy); |
| 214 | + break; |
| 215 | + |
| 216 | + case Core::Settings::ProxyType::System: |
| 217 | + QNetworkProxyFactory::setUseSystemConfiguration(true); |
| 218 | + break; |
| 219 | + |
| 220 | + case Core::Settings::ProxyType::UserDefined: { |
| 221 | + QNetworkProxy proxy(QNetworkProxy::HttpProxy, m_settings->proxyHost, m_settings->proxyPort); |
| 222 | + if (m_settings->proxyAuthenticate) { |
| 223 | + proxy.setUser(m_settings->proxyUserName); |
| 224 | + proxy.setPassword(m_settings->proxyPassword); |
| 225 | + } |
| 226 | + |
| 227 | + QNetworkProxy::setApplicationProxy(proxy); |
| 228 | + |
| 229 | + // Force NM to pick up changes. |
| 230 | + m_networkManager->clearAccessCache(); |
| 231 | + break; |
| 232 | + } |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +QString Application::userAgent() |
| 237 | +{ |
| 238 | + return QStringLiteral("Zeal/%1").arg(QCoreApplication::applicationVersion()); |
| 239 | +} |
| 240 | + |
| 241 | +QString Application::userAgentJson() const |
| 242 | +{ |
| 243 | + QJsonObject app = { |
| 244 | + {QStringLiteral("version"), QCoreApplication::applicationVersion()}, |
| 245 | + {QStringLiteral("qt_version"), qVersion()}, |
| 246 | + {QStringLiteral("install_id"), m_settings->installId} |
| 247 | + }; |
| 248 | + |
| 249 | + QJsonObject os = { |
| 250 | + {QStringLiteral("arch"), QSysInfo::currentCpuArchitecture()}, |
| 251 | + {QStringLiteral("name"), QSysInfo::prettyProductName()}, |
| 252 | + {QStringLiteral("product_type"), QSysInfo::productType()}, |
| 253 | + {QStringLiteral("product_version"), QSysInfo::productVersion()}, |
| 254 | + {QStringLiteral("kernel_type"), QSysInfo::kernelType()}, |
| 255 | + {QStringLiteral("kernel_version"), QSysInfo::kernelVersion()}, |
| 256 | + {QStringLiteral("locale"), QLocale::system().name()} |
| 257 | + }; |
| 258 | + |
| 259 | + QJsonObject ua = { |
| 260 | + {QStringLiteral("app"), app}, |
| 261 | + {QStringLiteral("os"), os} |
| 262 | + }; |
| 263 | + |
| 264 | + return QString::fromUtf8(QJsonDocument(ua).toJson(QJsonDocument::Compact)); |
| 265 | +} |
0 commit comments