| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/webfactory.h" |
| 4 | |
| 5 | #include "gui/messagebox.h" |
| 6 | #include "miscellaneous/application.h" |
| 7 | #include "miscellaneous/iconfactory.h" |
| 8 | #include "miscellaneous/settings.h" |
| 9 | #include "network-web/adblock/adblockmanager.h" |
| 10 | #include "network-web/apiserver.h" |
| 11 | #include "network-web/articleparse.h" |
| 12 | #include "network-web/cookiejar.h" |
| 13 | #include "network-web/readability.h" |
| 14 | |
| 15 | #include <QDesktopServices> |
| 16 | #include <QProcess> |
| 17 | #include <QUrl> |
| 18 | |
| 19 | #if defined(NO_LITE) |
| 20 | #include "network-web/gemini/geminischemehandler.h" |
| 21 | #include "network-web/webengine/networkurlinterceptor.h" |
| 22 | |
| 23 | #if QT_VERSION_MAJOR == 6 |
| 24 | #include <QWebEngineDownloadRequest> |
| 25 | #else |
| 26 | #include <QWebEngineDownloadItem> |
| 27 | #endif |
| 28 | |
| 29 | #include <QWebEngineProfile> |
| 30 | #include <QWebEngineScript> |
| 31 | #include <QWebEngineScriptCollection> |
| 32 | #include <QWebEngineSettings> |
| 33 | #include <QWebEngineUrlScheme> |
| 34 | #endif |
| 35 | |
| 36 | WebFactory::WebFactory(QObject* parent) : QObject(parent), m_apiServer(nullptr), m_customUserAgent(QString()) { |
| 37 | m_adBlock = new AdBlockManager(this); |
| 38 | |
| 39 | if (qApp->settings()->value(GROUP(Network), SETTING(Network::EnableApiServer)).toBool()) { |
| 40 | startApiServer(); |
| 41 | } |
| 42 | |
| 43 | #if defined(NO_LITE) |
| 44 | |
| 45 | QWebEngineUrlScheme gemini_scheme("gemini"); |
| 46 | |
| 47 | gemini_scheme.setSyntax(QWebEngineUrlScheme::Syntax::Host); |
| 48 | // gemini_scheme.setFlags(QWebEngineUrlScheme::Flag::SecureScheme); |
| 49 | |
| 50 | QWebEngineUrlScheme::registerScheme(gemini_scheme); |
| 51 | |
| 52 | m_geminiHandler = new GeminiSchemeHandler(this); |
| 53 | |
| 54 | if (qApp->settings()->value(GROUP(Browser), SETTING(Browser::DisableCache)).toBool()) { |
| 55 | qWarningNN << LOGSEC_NETWORK << "Using off-the-record WebEngine profile."; |
| 56 | |
| 57 | m_engineProfile = new QWebEngineProfile(this); |
| 58 | } |
| 59 | else { |
| 60 | m_engineProfile = new QWebEngineProfile(QSL(APP_LOW_NAME), this); |
| 61 | } |
| 62 | |
| 63 | m_engineProfile->installUrlSchemeHandler("gemini", m_geminiHandler); |
| 64 | |
| 65 | m_engineSettings = nullptr; |
| 66 | m_urlInterceptor = new NetworkUrlInterceptor(this); |
| 67 | #endif |
| 68 | |
| 69 | m_cookieJar = new CookieJar(this); |
| 70 | m_readability = new Readability(this); |
| 71 | m_articleParse = new ArticleParse(this); |
| 72 | |
| 73 | #if defined(NO_LITE) |
| 74 | #if QT_VERSION >= 0x050D00 // Qt >= 5.13.0 |
| 75 | m_engineProfile->setUrlRequestInterceptor(m_urlInterceptor); |
| 76 | #else |
| 77 | m_engineProfile->setRequestInterceptor(m_urlInterceptor); |
| 78 | #endif |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | WebFactory::~WebFactory() { |
| 83 | stopApiServer(); |
| 84 | |
| 85 | #if defined(NO_LITE) |
| 86 | if (m_engineSettings != nullptr && m_engineSettings->menu() != nullptr) { |
| 87 | m_engineSettings->menu()->deleteLater(); |
| 88 | } |
| 89 | #endif |
| 90 | |
| 91 | /*if (m_cookieJar != nullptr) { |
| 92 | m_cookieJar->deleteLater(); |
| 93 | }*/ |
| 94 | } |
| 95 | |
| 96 | bool WebFactory::sendMessageViaEmail(const Message& message) { |
| 97 | if (qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalEmailEnabled)).toBool()) { |
| 98 | const QString browser = |
| 99 | qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalEmailExecutable)).toString(); |
| 100 | const QString arguments = |
| 101 | qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalEmailArguments)).toString(); |
| 102 | const QStringList tokenized_arguments = |
| 103 | TextFactory::tokenizeProcessArguments(arguments.arg(message.m_title, stripTags(message.m_contents))); |
| 104 | |
| 105 | return IOFactory::startProcessDetached(browser, tokenized_arguments); |
| 106 | } |
| 107 | else { |
| 108 | // Send it via mailto protocol. |
| 109 | // NOTE: http://en.wikipedia.org/wiki/Mailto |
| 110 | return QDesktopServices::openUrl(QSL("mailto:?subject=%1&body=%2") |
| 111 | .arg(QString(QUrl::toPercentEncoding(message.m_title)), |
| 112 | QString(QUrl::toPercentEncoding(stripTags(message.m_contents))))); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #if defined(NO_LITE) |
| 117 | void WebFactory::loadCustomCss(const QString user_styles_path) { |
| 118 | if (QFile::exists(user_styles_path)) { |
| 119 | QByteArray css_data = IOFactory::readFile(user_styles_path); |
| 120 | QString name = "rssguard-user-styles"; |
| 121 | QWebEngineScript script; |
| 122 | QString s = QSL("(function() {" |
| 123 | " css = document.createElement('style');" |
| 124 | " css.type = 'text/css';" |
| 125 | " css.id = '%1';" |
| 126 | " document.head.appendChild(css);" |
| 127 | " css.innerText = '%2';" |
| 128 | "})()") |
| 129 | .arg(name, css_data.simplified()); |
| 130 | script.setName(name); |
| 131 | script.setSourceCode(s); |
| 132 | script.setInjectionPoint(QWebEngineScript::DocumentReady); |
| 133 | script.setRunsOnSubFrames(false); |
| 134 | script.setWorldId(QWebEngineScript::ApplicationWorld); |
| 135 | |
| 136 | m_engineProfile->scripts()->insert(script); |
| 137 | |
| 138 | qDebugNN << LOGSEC_CORE << "Loading user CSS style file" << QUOTE_W_SPACE_DOT(user_styles_path); |
| 139 | } |
| 140 | else { |
| 141 | qWarningNN << LOGSEC_CORE << "User CSS style was not provided in file" << QUOTE_W_SPACE_DOT(user_styles_path); |
| 142 | } |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | bool WebFactory::openUrlInExternalBrowser(const QUrl& url) const { |
| 147 | QString my_url = url.toString(QUrl::ComponentFormattingOption::FullyEncoded); |
| 148 | |
| 149 | qDebugNN << LOGSEC_NETWORK << "We are trying to open URL" << QUOTE_W_SPACE_DOT(my_url); |
| 150 | |
| 151 | bool result = false; |
| 152 | |
| 153 | if (qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalBrowserEnabled)).toBool()) { |
| 154 | const QString browser = |
| 155 | qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalBrowserExecutable)).toString(); |
| 156 | const QString arguments = |
| 157 | qApp->settings()->value(GROUP(Browser), SETTING(Browser::CustomExternalBrowserArguments)).toString(); |
| 158 | const auto nice_args = arguments.arg(my_url); |
| 159 | |
| 160 | qDebugNN << LOGSEC_NETWORK << "Arguments for external browser:" << QUOTE_W_SPACE_DOT(nice_args); |
| 161 | |
| 162 | result = IOFactory::startProcessDetached(browser, TextFactory::tokenizeProcessArguments(nice_args)); |
| 163 | |
| 164 | if (!result) { |
| 165 | qDebugNN << LOGSEC_NETWORK << "External web browser call failed."; |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | result = QDesktopServices::openUrl(my_url); |
| 170 | } |
| 171 | |
| 172 | if (!result) { |
| 173 | // We display GUI information that browser was not probably opened. |
| 174 | MsgBox::show(qApp->mainFormWidget(), |
| 175 | QMessageBox::Icon::Critical, |
| 176 | tr("Navigate to website manually"), |
| 177 | tr("%1 was unable to launch your web browser with the given URL, you need to open the " |
| 178 | "below website URL in your web browser manually.") |
| 179 | .arg(QSL(APP_NAME)), |
| 180 | {}, |
| 181 | my_url, |
| 182 | QMessageBox::StandardButton::Ok); |
| 183 | } |
| 184 | |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | QString WebFactory::stripTags(QString text) { |
| 189 | static QRegularExpression reg_tags(QSL("<[^>]*>")); |
| 190 | |
| 191 | return text.remove(reg_tags); |
| 192 | } |
| 193 | |
| 194 | QString WebFactory::unescapeHtml(const QString& html) { |
| 195 | if (html.isEmpty()) { |
| 196 | return html; |
| 197 | } |
| 198 | |
| 199 | static QMap<QString, char16_t> entities = generateUnescapes(); |
| 200 | |
| 201 | QString output; |
| 202 | output.reserve(html.size()); |
| 203 | |
| 204 | // Traverse input HTML string and replace named/number entities. |
| 205 | for (int pos = 0; pos < html.size();) { |
| 206 | const QChar first = html.at(pos); |
| 207 | |
| 208 | if (first == QChar('&')) { |
| 209 | // We need to find ending ';'. |
| 210 | int pos_end = -1; |
| 211 | |
| 212 | // We're finding end of entity, but also we limit searching window to 10 characters. |
| 213 | for (int pos_find = pos; pos_find <= pos + 10 && pos_find < html.size(); pos_find++) { |
| 214 | if (html.at(pos_find) == QChar(';')) { |
| 215 | // We found end of the entity. |
| 216 | pos_end = pos_find; |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (pos_end + 1 > pos) { |
| 222 | // OK, we have entity. |
| 223 | if (html.at(pos + 1) == QChar('#')) { |
| 224 | // We have numbered entity. |
| 225 | uint number; |
| 226 | QString number_str; |
| 227 | |
| 228 | if (html.at(pos + 2) == QChar('x')) { |
| 229 | // base-16 number. |
| 230 | number_str = html.mid(pos + 3, pos_end - pos - 3); |
| 231 | number = number_str.toUInt(nullptr, 16); |
| 232 | } |
| 233 | else { |
| 234 | // base-10 number. |
| 235 | number_str = html.mid(pos + 2, pos_end - pos - 2); |
| 236 | number = number_str.toUInt(); |
| 237 | } |
| 238 | |
| 239 | if (number > 0U) { |
| 240 | output.append(QString::fromUcs4((const char32_t*)&number, 1)); |
| 241 | } |
| 242 | else { |
| 243 | // Failed to convert to number, leave intact. |
| 244 | output.append(html.mid(pos, pos_end - pos + 1)); |
| 245 | } |
| 246 | |
| 247 | pos = pos_end + 1; |
| 248 | continue; |
| 249 | } |
| 250 | else { |
| 251 | // We have named entity. |
| 252 | auto entity_name = html.mid(pos + 1, pos_end - pos - 1); |
| 253 | |
| 254 | if (entities.contains(entity_name)) { |
| 255 | // Entity found, proceed. |
| 256 | output.append(entities.value(entity_name)); |
| 257 | } |
| 258 | else { |
| 259 | // Entity NOT found, leave intact. |
| 260 | output.append('&'); |
| 261 | output.append(entity_name); |
| 262 | output.append(';'); |
| 263 | } |
| 264 | |
| 265 | pos = pos_end + 1; |
| 266 | continue; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // No entity, normally append and continue. |
| 272 | output.append(first); |
| 273 | pos++; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | qDebugNN << LOGSEC_CORE |
| 278 | << "Unescaped string" << QUOTE_W_SPACE(html) |
| 279 | << "to" << QUOTE_W_SPACE_DOT(output); |
| 280 | */ |
| 281 | |
| 282 | return output; |
| 283 | } |
| 284 | |
| 285 | QString WebFactory::limitSizeOfHtmlImages(const QString& html, int desired_width, int desired_max_height) const { |
| 286 | static QRegularExpression exp_image_tag(QSL("<img ([^>]+)>")); |
| 287 | static QRegularExpression exp_image_attrs(QSL("(\\w+)=\"([^\"]+)\"")); |
| 288 | static bool is_lite = qApp->usingLite(); |
| 289 | |
| 290 | // Replace too big pictures. What it exactly does: |
| 291 | // - find all <img> tags and check for existence of height/width attributes: |
| 292 | // - both found -> keep aspect ratio and change to fit width if too big (or limit height if configured) |
| 293 | // - height found only -> limit height if configured |
| 294 | // - width found only -> change to fit width if too big |
| 295 | // - nothing found (image dimensions are taken directly from picture) -> limit height if configured, |
| 296 | QRegularExpressionMatch exp_match; |
| 297 | qsizetype match_offset = 0; |
| 298 | QString my_html = html; |
| 299 | QElapsedTimer tmr; |
| 300 | |
| 301 | #if !defined(NDEBUG) |
| 302 | // IOFactory::writeFile("a.html", html.toUtf8()); |
| 303 | #endif |
| 304 | |
| 305 | tmr.start(); |
| 306 | |
| 307 | while ((exp_match = exp_image_tag.match(my_html, match_offset)).hasMatch()) { |
| 308 | QString img_reconstructed = QSL("<img"); |
| 309 | |
| 310 | // QString full = exp_match.captured(); |
| 311 | // auto aa = exp_match.capturedLength(); |
| 312 | |
| 313 | QString img_tag_inner_text = exp_match.captured(1); |
| 314 | |
| 315 | // We found image, now we parse its attributes and process them. |
| 316 | QRegularExpressionMatchIterator attrs_match_iter = exp_image_attrs.globalMatch(img_tag_inner_text); |
| 317 | QMap<QString, QString> attrs; |
| 318 | |
| 319 | while (attrs_match_iter.hasNext()) { |
| 320 | QRegularExpressionMatch attrs_match = attrs_match_iter.next(); |
| 321 | |
| 322 | QString attr_name = attrs_match.captured(1); |
| 323 | QString attr_value = attrs_match.captured(2); |
| 324 | |
| 325 | attrs.insert(attr_name, attr_value); |
| 326 | } |
| 327 | |
| 328 | // Now, we edit height/width differently, depending whether this is |
| 329 | // simpler HTML (lite) viewer, or WebEngine full-blown viewer. |
| 330 | if (is_lite) { |
| 331 | if (attrs.contains("height") && attrs.contains("width")) { |
| 332 | double ratio = attrs.value("width").toDouble() / attrs.value("height").toDouble(); |
| 333 | |
| 334 | if (desired_max_height > 0) { |
| 335 | // We limit height. |
| 336 | attrs.insert("height", QString::number(desired_max_height)); |
| 337 | attrs.insert("width", QString::number(int(ratio * desired_max_height))); |
| 338 | } |
| 339 | |
| 340 | // We fit width. |
| 341 | if (attrs.value("width").toInt() > desired_width) { |
| 342 | attrs.insert("width", QString::number(desired_width)); |
| 343 | attrs.insert("height", QString::number(int(desired_width / ratio))); |
| 344 | } |
| 345 | } |
| 346 | else if (attrs.contains("width")) { |
| 347 | // Only width. |
| 348 | if (attrs.value("width").toInt() > desired_width) { |
| 349 | attrs.insert("width", QString::number(desired_width)); |
| 350 | } |
| 351 | } |
| 352 | else { |
| 353 | // No dimensions given. |
| 354 | // In this case we simply rely on original image dimensions |
| 355 | // if no specific limit is set. |
| 356 | // Too wide images will get downscaled. |
| 357 | if (desired_max_height > 0) { |
| 358 | attrs.insert("height", QString::number(desired_max_height)); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | else { |
| 363 | attrs.remove("width"); |
| 364 | attrs.remove("height"); |
| 365 | |
| 366 | if (desired_max_height > 0) { |
| 367 | attrs.insert("style", QSL("max-height: %1px !important;").arg(desired_max_height)); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Re-insert all attributes. |
| 372 | while (!attrs.isEmpty()) { |
| 373 | auto first_key = attrs.firstKey(); |
| 374 | auto first_value = attrs.first(); |
| 375 | |
| 376 | img_reconstructed += QSL(" %1=\"%2\"").arg(first_key, first_value); |
| 377 | |
| 378 | attrs.remove(first_key); |
| 379 | } |
| 380 | |
| 381 | img_reconstructed += QSL(">"); |
| 382 | |
| 383 | my_html = my_html.replace(exp_match.capturedStart(), exp_match.capturedLength(), img_reconstructed); |
| 384 | |
| 385 | /*if (found_width > desired_width) { |
| 386 | qWarningNN << LOGSEC_GUI << "Element" << QUOTE_W_SPACE(exp_match.captured()) |
| 387 | << "is too wide, setting smaller value to prevent horizontal scrollbars."; |
| 388 | |
| 389 | my_html = |
| 390 | my_html.replace(exp_match.capturedStart(1), exp_match.capturedLength(1), QString::number(desired_width)); |
| 391 | }*/ |
| 392 | |
| 393 | match_offset = exp_match.capturedStart() + img_reconstructed.size(); |
| 394 | } |
| 395 | |
| 396 | #if !defined(NDEBUG) |
| 397 | // IOFactory::writeFile("b.html", my_html.toUtf8()); |
| 398 | #endif |
| 399 | |
| 400 | qDebugNN << LOGSEC_GUI << "HTML image resizing took" << NONQUOTE_W_SPACE(tmr.elapsed()) << "miliseconds."; |
| 401 | return my_html; |
| 402 | } |
| 403 | |
| 404 | QString WebFactory::processFeedUriScheme(const QString& url) { |
| 405 | if (url.startsWith(QSL(URI_SCHEME_FEED))) { |
| 406 | return QSL(URI_SCHEME_HTTPS) + url.mid(QSL(URI_SCHEME_FEED).size()); |
| 407 | } |
| 408 | else if (url.startsWith(QSL(URI_SCHEME_FEED_SHORT))) { |
| 409 | return url.mid(QSL(URI_SCHEME_FEED_SHORT).size()); |
| 410 | } |
| 411 | else { |
| 412 | return url; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void WebFactory::updateProxy() { |
| 417 | const QNetworkProxy::ProxyType selected_proxy_type = |
| 418 | static_cast<QNetworkProxy::ProxyType>(qApp->settings()->value(GROUP(Proxy), SETTING(Proxy::Type)).toInt()); |
| 419 | |
| 420 | if (selected_proxy_type == QNetworkProxy::NoProxy) { |
| 421 | qDebugNN << LOGSEC_NETWORK << "Disabling application-wide proxy completely."; |
| 422 | |
| 423 | QNetworkProxyFactory::setUseSystemConfiguration(false); |
| 424 | QNetworkProxy::setApplicationProxy(QNetworkProxy::ProxyType::NoProxy); |
| 425 | } |
| 426 | else if (selected_proxy_type == QNetworkProxy::ProxyType::DefaultProxy) { |
| 427 | qDebugNN << LOGSEC_NETWORK << "Using application-wide proxy to be system's default proxy."; |
| 428 | QNetworkProxyFactory::setUseSystemConfiguration(true); |
| 429 | } |
| 430 | else { |
| 431 | const Settings* settings = qApp->settings(); |
| 432 | QNetworkProxy new_proxy; |
| 433 | |
| 434 | // Custom proxy is selected, set it up. |
| 435 | new_proxy.setType(selected_proxy_type); |
| 436 | new_proxy.setHostName(settings->value(GROUP(Proxy), SETTING(Proxy::Host)).toString()); |
| 437 | new_proxy.setPort(quint16(settings->value(GROUP(Proxy), SETTING(Proxy::Port)).toInt())); |
| 438 | new_proxy.setUser(settings->value(GROUP(Proxy), SETTING(Proxy::Username)).toString()); |
| 439 | new_proxy.setPassword(settings->password(GROUP(Proxy), SETTING(Proxy::Password)).toString()); |
| 440 | |
| 441 | qWarningNN << LOGSEC_NETWORK |
| 442 | << "Activating application-wide custom proxy, address:" << QUOTE_W_SPACE_COMMA(new_proxy.hostName()) |
| 443 | << " type:" << QUOTE_W_SPACE_DOT(new_proxy.type()); |
| 444 | |
| 445 | QNetworkProxy::setApplicationProxy(new_proxy); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | AdBlockManager* WebFactory::adBlock() const { |
| 450 | return m_adBlock; |
| 451 | } |
| 452 | |
| 453 | #if defined(NO_LITE) |
| 454 | NetworkUrlInterceptor* WebFactory::urlIinterceptor() const { |
| 455 | return m_urlInterceptor; |
| 456 | } |
| 457 | |
| 458 | QWebEngineProfile* WebFactory::engineProfile() const { |
| 459 | return m_engineProfile; |
| 460 | } |
| 461 | |
| 462 | QAction* WebFactory::engineSettingsAction() { |
| 463 | if (m_engineSettings == nullptr) { |
| 464 | m_engineSettings = new QAction(qApp->icons()->fromTheme(QSL("applications-internet"), QSL("internet-services")), |
| 465 | tr("Web engine settings"), |
| 466 | this); |
| 467 | m_engineSettings->setMenu(new QMenu()); |
| 468 | createMenu(m_engineSettings->menu()); |
| 469 | connect(m_engineSettings->menu(), &QMenu::aboutToShow, this, [this]() { |
| 470 | createMenu(); |
| 471 | }); |
| 472 | } |
| 473 | |
| 474 | return m_engineSettings; |
| 475 | } |
| 476 | |
| 477 | void WebFactory::createMenu(QMenu* menu) { |
| 478 | if (menu == nullptr) { |
| 479 | menu = qobject_cast<QMenu*>(sender()); |
| 480 | |
| 481 | if (menu == nullptr) { |
| 482 | return; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | menu->clear(); |
| 487 | QList<QAction*> actions; |
| 488 | |
| 489 | actions << createEngineSettingsAction(tr("Auto-load images"), QWebEngineSettings::WebAttribute::AutoLoadImages); |
| 490 | actions << createEngineSettingsAction(tr("JS enabled"), QWebEngineSettings::WebAttribute::JavascriptEnabled); |
| 491 | actions << createEngineSettingsAction(tr("JS can open popup windows"), |
| 492 | QWebEngineSettings::WebAttribute::JavascriptCanOpenWindows); |
| 493 | actions << createEngineSettingsAction(tr("JS can access clipboard"), |
| 494 | QWebEngineSettings::WebAttribute::JavascriptCanAccessClipboard); |
| 495 | actions << createEngineSettingsAction(tr("Hyperlinks can get focus"), |
| 496 | QWebEngineSettings::WebAttribute::LinksIncludedInFocusChain); |
| 497 | actions << createEngineSettingsAction(tr("Local storage enabled"), |
| 498 | QWebEngineSettings::WebAttribute::LocalStorageEnabled); |
| 499 | actions << createEngineSettingsAction(tr("Local content can access remote URLs"), |
| 500 | QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls); |
| 501 | actions << createEngineSettingsAction(tr("XSS auditing enabled"), |
| 502 | QWebEngineSettings::WebAttribute::XSSAuditingEnabled); |
| 503 | actions << createEngineSettingsAction(tr("Spatial navigation enabled"), |
| 504 | QWebEngineSettings::WebAttribute::SpatialNavigationEnabled); |
| 505 | actions << createEngineSettingsAction(tr("Local content can access local files"), |
| 506 | QWebEngineSettings::WebAttribute::LocalContentCanAccessFileUrls); |
| 507 | actions << createEngineSettingsAction(tr("Hyperlink auditing enabled"), |
| 508 | QWebEngineSettings::WebAttribute::HyperlinkAuditingEnabled); |
| 509 | actions << createEngineSettingsAction(tr("Animate scrolling"), |
| 510 | QWebEngineSettings::WebAttribute::ScrollAnimatorEnabled); |
| 511 | actions << createEngineSettingsAction(tr("Error pages enabled"), QWebEngineSettings::WebAttribute::ErrorPageEnabled); |
| 512 | actions << createEngineSettingsAction(tr("Plugins enabled"), QWebEngineSettings::WebAttribute::PluginsEnabled); |
| 513 | actions << createEngineSettingsAction(tr("Fullscreen enabled"), |
| 514 | QWebEngineSettings::WebAttribute::FullScreenSupportEnabled); |
| 515 | |
| 516 | #if !defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) |
| 517 | actions << createEngineSettingsAction(tr("Screen capture enabled"), |
| 518 | QWebEngineSettings::WebAttribute::ScreenCaptureEnabled); |
| 519 | actions << createEngineSettingsAction(tr("WebGL enabled"), QWebEngineSettings::WebAttribute::WebGLEnabled); |
| 520 | actions << createEngineSettingsAction(tr("Accelerate 2D canvas"), |
| 521 | QWebEngineSettings::WebAttribute::Accelerated2dCanvasEnabled); |
| 522 | actions << createEngineSettingsAction(tr("Print element backgrounds"), |
| 523 | QWebEngineSettings::WebAttribute::PrintElementBackgrounds); |
| 524 | actions << createEngineSettingsAction(tr("Allow running insecure content"), |
| 525 | QWebEngineSettings::WebAttribute::AllowRunningInsecureContent); |
| 526 | actions << createEngineSettingsAction(tr("Allow geolocation on insecure origins"), |
| 527 | QWebEngineSettings::WebAttribute::AllowGeolocationOnInsecureOrigins); |
| 528 | #endif |
| 529 | |
| 530 | actions << createEngineSettingsAction(tr("JS can activate windows"), |
| 531 | QWebEngineSettings::WebAttribute::AllowWindowActivationFromJavaScript); |
| 532 | actions << createEngineSettingsAction(tr("Show scrollbars"), QWebEngineSettings::WebAttribute::ShowScrollBars); |
| 533 | actions << createEngineSettingsAction(tr("Media playback with gestures"), |
| 534 | QWebEngineSettings::WebAttribute::PlaybackRequiresUserGesture); |
| 535 | actions << createEngineSettingsAction(tr("WebRTC uses only public interfaces"), |
| 536 | QWebEngineSettings::WebAttribute::WebRTCPublicInterfacesOnly); |
| 537 | actions << createEngineSettingsAction(tr("JS can paste from clipboard"), |
| 538 | QWebEngineSettings::WebAttribute::JavascriptCanPaste); |
| 539 | actions << createEngineSettingsAction(tr("DNS prefetch enabled"), |
| 540 | QWebEngineSettings::WebAttribute::DnsPrefetchEnabled); |
| 541 | |
| 542 | #if QT_VERSION >= 0x050D00 // Qt >= 5.13.0 |
| 543 | actions << createEngineSettingsAction(tr("PDF viewer enabled"), QWebEngineSettings::WebAttribute::PdfViewerEnabled); |
| 544 | #endif |
| 545 | |
| 546 | menu->addActions(actions); |
| 547 | } |
| 548 | |
| 549 | void WebFactory::webEngineSettingChanged(bool enabled) { |
| 550 | const QAction* const act = qobject_cast<QAction*>(sender()); |
| 551 | |
| 552 | QWebEngineSettings::WebAttribute attribute = static_cast<QWebEngineSettings::WebAttribute>(act->data().toInt()); |
| 553 | |
| 554 | qApp->settings()->setValue(WebEngineAttributes::ID, QString::number(static_cast<int>(attribute)), enabled); |
| 555 | m_engineProfile->settings()->setAttribute(attribute, act->isChecked()); |
| 556 | } |
| 557 | |
| 558 | QAction* WebFactory::createEngineSettingsAction(const QString& title, int web_attribute) { |
| 559 | // TODO: ověřit že cast je funkční |
| 560 | QWebEngineSettings::WebAttribute attribute = QWebEngineSettings::WebAttribute(web_attribute); |
| 561 | |
| 562 | auto* act = new QAction(title, m_engineSettings->menu()); |
| 563 | |
| 564 | act->setData(attribute); |
| 565 | act->setCheckable(true); |
| 566 | act->setChecked(qApp->settings() |
| 567 | ->value(WebEngineAttributes::ID, QString::number(static_cast<int>(attribute)), true) |
| 568 | .toBool()); |
| 569 | |
| 570 | auto enabl = act->isChecked(); |
| 571 | |
| 572 | m_engineProfile->settings()->setAttribute(attribute, enabl); |
| 573 | connect(act, &QAction::toggled, this, &WebFactory::webEngineSettingChanged); |
| 574 | return act; |
| 575 | } |
| 576 | |
| 577 | #endif |
| 578 | |
| 579 | CookieJar* WebFactory::cookieJar() const { |
| 580 | return m_cookieJar; |
| 581 | } |
| 582 | |
| 583 | Readability* WebFactory::readability() const { |
| 584 | return m_readability; |
| 585 | } |
| 586 | |
| 587 | ArticleParse* WebFactory::articleParse() const { |
| 588 | return m_articleParse; |
| 589 | } |
| 590 | |
| 591 | void WebFactory::startApiServer() { |
| 592 | m_apiServer = new ApiServer(this); |
| 593 | m_apiServer->setListenAddressPort(QSL("http://localhost:54123"), true); |
| 594 | |
| 595 | qDebugNN << LOGSEC_NETWORK << "Started API server:" << QUOTE_W_SPACE_DOT(m_apiServer->listenAddressPort()); |
| 596 | } |
| 597 | |
| 598 | void WebFactory::stopApiServer() { |
| 599 | if (m_apiServer != nullptr) { |
| 600 | qDebugNN << LOGSEC_NETWORK << "Stopped API server:" << QUOTE_W_SPACE_DOT(m_apiServer->listenAddressPort()); |
| 601 | |
| 602 | delete m_apiServer; |
| 603 | m_apiServer = nullptr; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | QMap<QString, char16_t> WebFactory::generateUnescapes() { |
| 608 | QMap<QString, char16_t> res; |
| 609 | res[QSL("AElig")] = 0x00c6; |
| 610 | res[QSL("AMP")] = 38; |
| 611 | res[QSL("Aacute")] = 0x00c1; |
| 612 | res[QSL("Acirc")] = 0x00c2; |
| 613 | res[QSL("Agrave")] = 0x00c0; |
| 614 | res[QSL("Alpha")] = 0x0391; |
| 615 | res[QSL("Aring")] = 0x00c5; |
| 616 | res[QSL("Atilde")] = 0x00c3; |
| 617 | res[QSL("Auml")] = 0x00c4; |
| 618 | res[QSL("Beta")] = 0x0392; |
| 619 | res[QSL("Ccedil")] = 0x00c7; |
| 620 | res[QSL("Chi")] = 0x03a7; |
| 621 | res[QSL("Dagger")] = 0x2021; |
| 622 | res[QSL("Delta")] = 0x0394; |
| 623 | res[QSL("ETH")] = 0x00d0; |
| 624 | res[QSL("Eacute")] = 0x00c9; |
| 625 | res[QSL("Ecirc")] = 0x00ca; |
| 626 | res[QSL("Egrave")] = 0x00c8; |
| 627 | res[QSL("Epsilon")] = 0x0395; |
| 628 | res[QSL("Eta")] = 0x0397; |
| 629 | res[QSL("Euml")] = 0x00cb; |
| 630 | res[QSL("GT")] = 62; |
| 631 | res[QSL("Gamma")] = 0x0393; |
| 632 | res[QSL("Iacute")] = 0x00cd; |
| 633 | res[QSL("Icirc")] = 0x00ce; |
| 634 | res[QSL("Igrave")] = 0x00cc; |
| 635 | res[QSL("Iota")] = 0x0399; |
| 636 | res[QSL("Iuml")] = 0x00cf; |
| 637 | res[QSL("Kappa")] = 0x039a; |
| 638 | res[QSL("LT")] = 60; |
| 639 | res[QSL("Lambda")] = 0x039b; |
| 640 | res[QSL("Mu")] = 0x039c; |
| 641 | res[QSL("Ntilde")] = 0x00d1; |
| 642 | res[QSL("Nu")] = 0x039d; |
| 643 | res[QSL("OElig")] = 0x0152; |
| 644 | res[QSL("Oacute")] = 0x00d3; |
| 645 | res[QSL("Ocirc")] = 0x00d4; |
| 646 | res[QSL("Ograve")] = 0x00d2; |
| 647 | res[QSL("Omega")] = 0x03a9; |
| 648 | res[QSL("Omicron")] = 0x039f; |
| 649 | res[QSL("Oslash")] = 0x00d8; |
| 650 | res[QSL("Otilde")] = 0x00d5; |
| 651 | res[QSL("Ouml")] = 0x00d6; |
| 652 | res[QSL("Phi")] = 0x03a6; |
| 653 | res[QSL("Pi")] = 0x03a0; |
| 654 | res[QSL("Prime")] = 0x2033; |
| 655 | res[QSL("Psi")] = 0x03a8; |
| 656 | res[QSL("QUOT")] = 34; |
| 657 | res[QSL("Rho")] = 0x03a1; |
| 658 | res[QSL("Scaron")] = 0x0160; |
| 659 | res[QSL("Sigma")] = 0x03a3; |
| 660 | res[QSL("THORN")] = 0x00de; |
| 661 | res[QSL("Tau")] = 0x03a4; |
| 662 | res[QSL("Theta")] = 0x0398; |
| 663 | res[QSL("Uacute")] = 0x00da; |
| 664 | res[QSL("Ucirc")] = 0x00db; |
| 665 | res[QSL("Ugrave")] = 0x00d9; |
| 666 | res[QSL("Upsilon")] = 0x03a5; |
| 667 | res[QSL("Uuml")] = 0x00dc; |
| 668 | res[QSL("Xi")] = 0x039e; |
| 669 | res[QSL("Yacute")] = 0x00dd; |
| 670 | res[QSL("Yuml")] = 0x0178; |
| 671 | res[QSL("Zeta")] = 0x0396; |
| 672 | res[QSL("aacute")] = 0x00e1; |
| 673 | res[QSL("acirc")] = 0x00e2; |
| 674 | res[QSL("acute")] = 0x00b4; |
| 675 | res[QSL("aelig")] = 0x00e6; |
| 676 | res[QSL("agrave")] = 0x00e0; |
| 677 | res[QSL("alefsym")] = 0x2135; |
| 678 | res[QSL("alpha")] = 0x03b1; |
| 679 | res[QSL("amp")] = 38; |
| 680 | res[QSL("and")] = 0x22a5; |
| 681 | res[QSL("ang")] = 0x2220; |
| 682 | res[QSL("apos")] = 0x0027; |
| 683 | res[QSL("aring")] = 0x00e5; |
| 684 | res[QSL("asymp")] = 0x2248; |
| 685 | res[QSL("atilde")] = 0x00e3; |
| 686 | res[QSL("auml")] = 0x00e4; |
| 687 | res[QSL("bdquo")] = 0x201e; |
| 688 | res[QSL("beta")] = 0x03b2; |
| 689 | res[QSL("brvbar")] = 0x00a6; |
| 690 | res[QSL("bull")] = 0x2022; |
| 691 | res[QSL("cap")] = 0x2229; |
| 692 | res[QSL("ccedil")] = 0x00e7; |
| 693 | res[QSL("cedil")] = 0x00b8; |
| 694 | res[QSL("cent")] = 0x00a2; |
| 695 | res[QSL("chi")] = 0x03c7; |
| 696 | res[QSL("circ")] = 0x02c6; |
| 697 | res[QSL("clubs")] = 0x2663; |
| 698 | res[QSL("cong")] = 0x2245; |
| 699 | res[QSL("copy")] = 0x00a9; |
| 700 | res[QSL("crarr")] = 0x21b5; |
| 701 | res[QSL("cup")] = 0x222a; |
| 702 | res[QSL("curren")] = 0x00a4; |
| 703 | res[QSL("dArr")] = 0x21d3; |
| 704 | res[QSL("dagger")] = 0x2020; |
| 705 | res[QSL("darr")] = 0x2193; |
| 706 | res[QSL("deg")] = 0x00b0; |
| 707 | res[QSL("delta")] = 0x03b4; |
| 708 | res[QSL("diams")] = 0x2666; |
| 709 | res[QSL("divide")] = 0x00f7; |
| 710 | res[QSL("eacute")] = 0x00e9; |
| 711 | res[QSL("ecirc")] = 0x00ea; |
| 712 | res[QSL("egrave")] = 0x00e8; |
| 713 | res[QSL("empty")] = 0x2205; |
| 714 | res[QSL("emsp")] = 0x2003; |
| 715 | res[QSL("ensp")] = 0x2002; |
| 716 | res[QSL("epsilon")] = 0x03b5; |
| 717 | res[QSL("equiv")] = 0x2261; |
| 718 | res[QSL("eta")] = 0x03b7; |
| 719 | res[QSL("eth")] = 0x00f0; |
| 720 | res[QSL("euml")] = 0x00eb; |
| 721 | res[QSL("euro")] = 0x20ac; |
| 722 | res[QSL("exist")] = 0x2203; |
| 723 | res[QSL("fnof")] = 0x0192; |
| 724 | res[QSL("forall")] = 0x2200; |
| 725 | res[QSL("frac12")] = 0x00bd; |
| 726 | res[QSL("frac14")] = 0x00bc; |
| 727 | res[QSL("frac34")] = 0x00be; |
| 728 | res[QSL("frasl")] = 0x2044; |
| 729 | res[QSL("gamma")] = 0x03b3; |
| 730 | res[QSL("ge")] = 0x2265; |
| 731 | res[QSL("gt")] = 62; |
| 732 | res[QSL("hArr")] = 0x21d4; |
| 733 | res[QSL("harr")] = 0x2194; |
| 734 | res[QSL("hearts")] = 0x2665; |
| 735 | res[QSL("hellip")] = 0x2026; |
| 736 | res[QSL("iacute")] = 0x00ed; |
| 737 | res[QSL("icirc")] = 0x00ee; |
| 738 | res[QSL("iexcl")] = 0x00a1; |
| 739 | res[QSL("igrave")] = 0x00ec; |
| 740 | res[QSL("image")] = 0x2111; |
| 741 | res[QSL("infin")] = 0x221e; |
| 742 | res[QSL("int")] = 0x222b; |
| 743 | res[QSL("iota")] = 0x03b9; |
| 744 | res[QSL("iquest")] = 0x00bf; |
| 745 | res[QSL("isin")] = 0x2208; |
| 746 | res[QSL("iuml")] = 0x00ef; |
| 747 | res[QSL("kappa")] = 0x03ba; |
| 748 | res[QSL("lArr")] = 0x21d0; |
| 749 | res[QSL("lambda")] = 0x03bb; |
| 750 | res[QSL("lang")] = 0x2329; |
| 751 | res[QSL("laquo")] = 0x00ab; |
| 752 | res[QSL("larr")] = 0x2190; |
| 753 | res[QSL("lceil")] = 0x2308; |
| 754 | res[QSL("ldquo")] = 0x201c; |
| 755 | res[QSL("le")] = 0x2264; |
| 756 | res[QSL("lfloor")] = 0x230a; |
| 757 | res[QSL("lowast")] = 0x2217; |
| 758 | res[QSL("loz")] = 0x25ca; |
| 759 | res[QSL("lrm")] = 0x200e; |
| 760 | res[QSL("lsaquo")] = 0x2039; |
| 761 | res[QSL("lsquo")] = 0x2018; |
| 762 | res[QSL("lt")] = 60; |
| 763 | res[QSL("macr")] = 0x00af; |
| 764 | res[QSL("mdash")] = 0x2014; |
| 765 | res[QSL("micro")] = 0x00b5; |
| 766 | res[QSL("middot")] = 0x00b7; |
| 767 | res[QSL("minus")] = 0x2212; |
| 768 | res[QSL("mu")] = 0x03bc; |
| 769 | res[QSL("nabla")] = 0x2207; |
| 770 | res[QSL("nbsp")] = 0x00a0; |
| 771 | res[QSL("ndash")] = 0x2013; |
| 772 | res[QSL("ne")] = 0x2260; |
| 773 | res[QSL("ni")] = 0x220b; |
| 774 | res[QSL("not")] = 0x00ac; |
| 775 | res[QSL("notin")] = 0x2209; |
| 776 | res[QSL("nsub")] = 0x2284; |
| 777 | res[QSL("ntilde")] = 0x00f1; |
| 778 | res[QSL("nu")] = 0x03bd; |
| 779 | res[QSL("oacute")] = 0x00f3; |
| 780 | res[QSL("ocirc")] = 0x00f4; |
| 781 | res[QSL("oelig")] = 0x0153; |
| 782 | res[QSL("ograve")] = 0x00f2; |
| 783 | res[QSL("oline")] = 0x203e; |
| 784 | res[QSL("omega")] = 0x03c9; |
| 785 | res[QSL("omicron")] = 0x03bf; |
| 786 | res[QSL("oplus")] = 0x2295; |
| 787 | res[QSL("or")] = 0x22a6; |
| 788 | res[QSL("ordf")] = 0x00aa; |
| 789 | res[QSL("ordm")] = 0x00ba; |
| 790 | res[QSL("oslash")] = 0x00f8; |
| 791 | res[QSL("otilde")] = 0x00f5; |
| 792 | res[QSL("otimes")] = 0x2297; |
| 793 | res[QSL("ouml")] = 0x00f6; |
| 794 | res[QSL("para")] = 0x00b6; |
| 795 | res[QSL("part")] = 0x2202; |
| 796 | res[QSL("percnt")] = 0x0025; |
| 797 | res[QSL("permil")] = 0x2030; |
| 798 | res[QSL("perp")] = 0x22a5; |
| 799 | res[QSL("phi")] = 0x03c6; |
| 800 | res[QSL("pi")] = 0x03c0; |
| 801 | res[QSL("piv")] = 0x03d6; |
| 802 | res[QSL("plusmn")] = 0x00b1; |
| 803 | res[QSL("pound")] = 0x00a3; |
| 804 | res[QSL("prime")] = 0x2032; |
| 805 | res[QSL("prod")] = 0x220f; |
| 806 | res[QSL("prop")] = 0x221d; |
| 807 | res[QSL("psi")] = 0x03c8; |
| 808 | res[QSL("quot")] = 34; |
| 809 | res[QSL("rArr")] = 0x21d2; |
| 810 | res[QSL("radic")] = 0x221a; |
| 811 | res[QSL("rang")] = 0x232a; |
| 812 | res[QSL("raquo")] = 0x00bb; |
| 813 | res[QSL("rarr")] = 0x2192; |
| 814 | res[QSL("rceil")] = 0x2309; |
| 815 | res[QSL("rdquo")] = 0x201d; |
| 816 | res[QSL("real")] = 0x211c; |
| 817 | res[QSL("reg")] = 0x00ae; |
| 818 | res[QSL("rfloor")] = 0x230b; |
| 819 | res[QSL("rho")] = 0x03c1; |
| 820 | res[QSL("rlm")] = 0x200f; |
| 821 | res[QSL("rsaquo")] = 0x203a; |
| 822 | res[QSL("rsquo")] = 0x2019; |
| 823 | res[QSL("sbquo")] = 0x201a; |
| 824 | res[QSL("scaron")] = 0x0161; |
| 825 | res[QSL("sdot")] = 0x22c5; |
| 826 | res[QSL("sect")] = 0x00a7; |
| 827 | res[QSL("shy")] = 0x00ad; |
| 828 | res[QSL("sigma")] = 0x03c3; |
| 829 | res[QSL("sigmaf")] = 0x03c2; |
| 830 | res[QSL("sim")] = 0x223c; |
| 831 | res[QSL("spades")] = 0x2660; |
| 832 | res[QSL("sub")] = 0x2282; |
| 833 | res[QSL("sube")] = 0x2286; |
| 834 | res[QSL("sum")] = 0x2211; |
| 835 | res[QSL("sup")] = 0x2283; |
| 836 | res[QSL("sup1")] = 0x00b9; |
| 837 | res[QSL("sup2")] = 0x00b2; |
| 838 | res[QSL("sup3")] = 0x00b3; |
| 839 | res[QSL("supe")] = 0x2287; |
| 840 | res[QSL("szlig")] = 0x00df; |
| 841 | res[QSL("tau")] = 0x03c4; |
| 842 | res[QSL("there4")] = 0x2234; |
| 843 | res[QSL("theta")] = 0x03b8; |
| 844 | res[QSL("thetasym")] = 0x03d1; |
| 845 | res[QSL("thinsp")] = 0x2009; |
| 846 | res[QSL("thorn")] = 0x00fe; |
| 847 | res[QSL("tilde")] = 0x02dc; |
| 848 | res[QSL("times")] = 0x00d7; |
| 849 | res[QSL("trade")] = 0x2122; |
| 850 | res[QSL("uArr")] = 0x21d1; |
| 851 | res[QSL("uacute")] = 0x00fa; |
| 852 | res[QSL("uarr")] = 0x2191; |
| 853 | res[QSL("ucirc")] = 0x00fb; |
| 854 | res[QSL("ugrave")] = 0x00f9; |
| 855 | res[QSL("uml")] = 0x00a8; |
| 856 | res[QSL("upsih")] = 0x03d2; |
| 857 | res[QSL("upsilon")] = 0x03c5; |
| 858 | res[QSL("uuml")] = 0x00fc; |
| 859 | res[QSL("weierp")] = 0x2118; |
| 860 | res[QSL("xi")] = 0x03be; |
| 861 | res[QSL("yacute")] = 0x00fd; |
| 862 | res[QSL("yen")] = 0x00a5; |
| 863 | res[QSL("yuml")] = 0x00ff; |
| 864 | res[QSL("zeta")] = 0x03b6; |
| 865 | res[QSL("zwj")] = 0x200d; |
| 866 | res[QSL("zwnj")] = 0x200c; |
| 867 | |
| 868 | return res; |
| 869 | } |
| 870 | |
| 871 | QString WebFactory::customUserAgent() const { |
| 872 | return m_customUserAgent; |
| 873 | } |
| 874 | |
| 875 | void WebFactory::setCustomUserAgent(const QString& user_agent) { |
| 876 | m_customUserAgent = user_agent; |
| 877 | } |
| 878 | |
| 879 | #if defined(NO_LITE) |
| 880 | void WebFactory::cleanupCache() { |
| 881 | if (MsgBox::show(nullptr, |
| 882 | QMessageBox::Icon::Question, |
| 883 | tr("Web cache is going to be cleared"), |
| 884 | tr("Do you really want to clear web cache?"), |
| 885 | {}, |
| 886 | {}, |
| 887 | QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No) == |
| 888 | QMessageBox::StandardButton::Yes) { |
| 889 | m_engineProfile->clearHttpCache(); |
| 890 | |
| 891 | // NOTE: Manually clear storage. |
| 892 | try { |
| 893 | IOFactory::removeFolder(m_engineProfile->persistentStoragePath()); |
| 894 | } |
| 895 | catch (const ApplicationException& ex) { |
| 896 | qCriticalNN << LOGSEC_CORE << "Removing of webengine storage failed:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | #endif |