2 * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "TabWidget.h"
23 #include "ui_AddTabWidget.h"
24 #include "ui_TabWidget.h"
25 #include "databases/CookiesDatabase.h"
26 #include "databases/DomainsDatabase.h"
27 #include "dialogs/SaveDialog.h"
28 #include "filters/MouseEventFilter.h"
29 #include "helpers/SearchEngineHelper.h"
30 #include "helpers/UserAgentHelper.h"
31 #include "interceptors/UrlRequestInterceptor.h"
32 #include "windows/BrowserWindow.h"
34 // KDE Framework headers.
35 #include <KIO/FileCopyJob>
36 #include <KIO/JobUiDelegate>
38 // Qt toolkit headers.
40 #include <QFileDialog>
41 #include <QGraphicsScene>
42 #include <QGraphicsView>
43 #include <QPrintDialog>
44 #include <QPrintPreviewDialog>
47 // Initialize the public static variables.
48 QString TabWidget::webEngineDefaultUserAgent = QStringLiteral("");
50 // Construct the class.
51 TabWidget::TabWidget(QWidget *parent) : QWidget(parent)
53 // Instantiate the UIs.
54 Ui::TabWidget tabWidgetUi;
55 Ui::AddTabWidget addTabWidgetUi;
58 tabWidgetUi.setupUi(this);
60 // Get a handle for the tab widget.
61 tabWidgetPointer = tabWidgetUi.tabWidget;
63 // Setup the add tab UI.
64 addTabWidgetUi.setupUi(tabWidgetPointer);
66 // Get handles for the add tab widgets.
67 QWidget *addTabWidgetPointer = addTabWidgetUi.addTabQWidget;
68 QPushButton *addTabButtonPointer = addTabWidgetUi.addTabButton;
70 // Display the add tab widget.
71 tabWidgetPointer->setCornerWidget(addTabWidgetPointer);
76 // Process tab events.
77 connect(tabWidgetPointer, SIGNAL(currentChanged(int)), this, SLOT(updateUiWithTabSettings()));
78 connect(addTabButtonPointer, SIGNAL(clicked()), this, SLOT(addTab()));
79 connect(tabWidgetPointer, SIGNAL(tabCloseRequested(int)), this, SLOT(deleteTab(int)));
81 // Store a copy of the WebEngine default user agent.
82 webEngineDefaultUserAgent = currentWebEngineProfilePointer->httpUserAgent();
84 // Instantiate the mouse event filter pointer.
85 MouseEventFilter *mouseEventFilterPointer = new MouseEventFilter();
87 // Install the mouse event filter.
88 qApp->installEventFilter(mouseEventFilterPointer);
90 // Process mouse forward and back commands.
91 connect(mouseEventFilterPointer, SIGNAL(mouseBack()), this, SLOT(mouseBack()));
92 connect(mouseEventFilterPointer, SIGNAL(mouseForward()), this, SLOT(mouseForward()));
95 TabWidget::~TabWidget()
97 // Manually delete each WebEngine page.
98 for (int i = 0; i < tabWidgetPointer->count(); ++i)
100 // Get the privacy WebEngine view.
101 PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast<PrivacyWebEngineView *>(tabWidgetPointer->widget(i));
103 // Deletion the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
104 delete privacyWebEngineViewPointer->page();
108 // The cookie is copied instead of referenced so that changes made to the cookie do not create a race condition with the display of the cookie in the dialog.
109 void TabWidget::addCookieToStore(QNetworkCookie cookie, QWebEngineCookieStore *webEngineCookieStorePointer) const
114 // Check to see if the domain does not start with a `.` because Qt makes this harder than it should be. <https://doc.qt.io/qt-5/qwebenginecookiestore.html#setCookie>
115 if (!cookie.domain().startsWith(QStringLiteral(".")))
118 url.setHost(cookie.domain());
119 url.setScheme(QStringLiteral("https"));
121 // Clear the domain from the cookie.
122 cookie.setDomain(QStringLiteral(""));
125 // Add the cookie to the store.
126 if (webEngineCookieStorePointer == nullptr)
127 currentWebEngineCookieStorePointer->setCookie(cookie, url);
129 webEngineCookieStorePointer->setCookie(cookie, url);
132 void TabWidget::addFirstTab()
134 // Create the first tab.
137 // Update the UI with the tab settings.
138 updateUiWithTabSettings();
140 // Set the focus on the current tab widget. This prevents the tab bar from showing a blue bar under the label of the first tab.
141 tabWidgetPointer->currentWidget()->setFocus();
144 PrivacyWebEngineView* TabWidget::addTab(const bool focusNewWebEngineView)
146 // Create a privacy WebEngine view.
147 PrivacyWebEngineView *privacyWebEngineViewPointer = new PrivacyWebEngineView();
150 int newTabIndex = tabWidgetPointer->addTab(privacyWebEngineViewPointer, i18nc("New tab label.", "New Tab"));
152 // Set the default tab icon.
153 tabWidgetPointer->setTabIcon(newTabIndex, defaultTabIcon);
155 // Create an off-the-record profile (the default when no profile name is specified).
156 QWebEngineProfile *webEngineProfilePointer = new QWebEngineProfile(QStringLiteral(""));
158 // Create a WebEngine page.
159 QWebEnginePage *webEnginePagePointer = new QWebEnginePage(webEngineProfilePointer);
161 // Set the WebEngine page.
162 privacyWebEngineViewPointer->setPage(webEnginePagePointer);
164 // Get handles for the web engine elements.
165 QWebEngineCookieStore *webEngineCookieStorePointer = webEngineProfilePointer->cookieStore();
166 QWebEngineSettings *webEngineSettingsPointer = webEnginePagePointer->settings();
168 // Update the URL line edit when the URL changes.
169 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::urlChanged, [privacyWebEngineViewPointer, this] (const QUrl &newUrl)
171 // Only update the UI if this is the current tab.
172 if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
174 // Update the URL line edit.
175 emit updateUrlLineEdit(newUrl);
177 // Update the status of the forward and back buttons.
178 emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
179 emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward());
182 // Reapply the zoom factor. This is a bug in QWebEngineView that resets the zoom with every load. It can be removed once <https://redmine.stoutner.com/issues/799> is fixed.
183 privacyWebEngineViewPointer->setZoomFactor(currentZoomFactor);
186 // Update the progress bar when a load is started.
187 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadStarted, [privacyWebEngineViewPointer, this] ()
189 // Store the load progress.
190 privacyWebEngineViewPointer->loadProgressInt = 0;
192 // Show the progress bar if this is the current tab.
193 if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
194 emit showProgressBar(0);
197 // Update the progress bar when a load progresses.
198 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadProgress, [privacyWebEngineViewPointer, this] (const int progress)
200 // Store the load progress.
201 privacyWebEngineViewPointer->loadProgressInt = progress;
203 // Update the progress bar if this is the current tab.
204 if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
205 emit showProgressBar(progress);
208 // Update the progress bar when a load finishes.
209 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::loadFinished, [privacyWebEngineViewPointer, this] ()
211 // Store the load progress.
212 privacyWebEngineViewPointer->loadProgressInt = -1;
214 // Hide the progress bar if this is the current tab.
215 if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
216 emit hideProgressBar();
219 // Handle full screen requests.
220 connect(webEnginePagePointer, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this, SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
222 // Listen for hovered link URLs.
223 connect(webEnginePagePointer, SIGNAL(linkHovered(const QString)), this, SLOT(pageLinkHovered(const QString)));
225 // Handle file downloads.
226 connect(webEngineProfilePointer, SIGNAL(downloadRequested(QWebEngineDownloadItem *)), this, SLOT(showSaveDialog(QWebEngineDownloadItem *)));
228 // Instantiate the URL request interceptor.
229 UrlRequestInterceptor *urlRequestInterceptorPointer = new UrlRequestInterceptor();
231 // Set the URL request interceptor.
232 webEngineProfilePointer->setUrlRequestInterceptor(urlRequestInterceptorPointer);
234 // Reapply the domain settings when the host changes.
235 connect(urlRequestInterceptorPointer, SIGNAL(applyDomainSettings(QString)), this, SLOT(applyDomainSettingsWithoutReloading(QString)));
237 // Set the local storage filter.
238 webEngineCookieStorePointer->setCookieFilter([privacyWebEngineViewPointer](const QWebEngineCookieStore::FilterRequest &filterRequest)
240 // Block all third party local storage requests, including the sneaky ones that don't register a first party URL.
241 if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral("")))
243 //qDebug().noquote().nospace() << "Third-party request blocked: " << filterRequest.origin;
249 // Allow the request if local storage is enabled.
250 if (privacyWebEngineViewPointer->localStorageEnabled)
252 //qDebug().noquote().nospace() << "Request allowed by local storage: " << filterRequest.origin;
258 //qDebug().noquote().nospace() << "Request blocked by default: " << filterRequest.origin;
260 // Block any remaining local storage requests.
264 // Disable JavaScript by default (this prevetns JavaScript from being enabled on a new tab before domain settings are loaded).
265 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
267 // Don't allow JavaScript to open windows.
268 webEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptCanOpenWindows, false);
270 // Allow keyboard navigation.
271 webEngineSettingsPointer->setAttribute(QWebEngineSettings::SpatialNavigationEnabled, true);
273 // Enable full screen support.
274 webEngineSettingsPointer->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
276 // Require user interaction to play media.
277 webEngineSettingsPointer->setAttribute(QWebEngineSettings::PlaybackRequiresUserGesture, true);
279 // Limit WebRTC to public IP addresses.
280 webEngineSettingsPointer->setAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly, true);
282 // Update the cookies action.
283 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, [privacyWebEngineViewPointer, this] (const int numberOfCookies)
285 // Update the cookie action if the specified privacy WebEngine view is the current privacy WebEngine view.
286 if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
287 emit updateCookiesAction(numberOfCookies);
290 // Process cookie changes.
291 connect(webEngineCookieStorePointer, SIGNAL(cookieAdded(QNetworkCookie)), privacyWebEngineViewPointer, SLOT(addCookieToList(QNetworkCookie)));
292 connect(webEngineCookieStorePointer, SIGNAL(cookieRemoved(QNetworkCookie)), privacyWebEngineViewPointer, SLOT(removeCookieFromList(QNetworkCookie)));
294 // Get a list of durable cookies.
295 QList<QNetworkCookie*> *durableCookiesListPointer = CookiesDatabase::getCookies();
297 // Add the durable cookies to the store.
298 for (QNetworkCookie *cookiePointer : *durableCookiesListPointer)
299 addCookieToStore(*cookiePointer, webEngineCookieStorePointer);
301 // Update the title when it changes.
302 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::titleChanged, [this, privacyWebEngineViewPointer] (const QString &title)
304 // Get the index for this tab.
305 int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer);
307 // Update the title for this tab.
308 tabWidgetPointer->setTabText(tabIndex, title);
310 // Update the window title if this is the current tab.
311 if (tabIndex == tabWidgetPointer->currentIndex())
312 emit updateWindowTitle(title);
315 // Update the icon when it changes.
316 connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::iconChanged, [privacyWebEngineViewPointer, this] (const QIcon &icon)
318 // Get the index for this tab.
319 int tabIndex = tabWidgetPointer->indexOf(privacyWebEngineViewPointer);
321 // Update the icon for this tab.
323 tabWidgetPointer->setTabIcon(tabIndex, defaultTabIcon);
325 tabWidgetPointer->setTabIcon(tabIndex, icon);
328 // Move to the new tab.
329 tabWidgetPointer->setCurrentIndex(newTabIndex);
331 // Clear the URL line edit focus so that it populates correctly when opening a new tab from the context menu.
332 if (focusNewWebEngineView)
333 emit clearUrlLineEditFocus();
335 // Return the privacy WebEngine view pointer.
336 return privacyWebEngineViewPointer;
339 void TabWidget::applyApplicationSettings()
341 // Set the tab position.
342 if (Settings::tabsOnTop())
343 tabWidgetPointer->setTabPosition(QTabWidget::North);
345 tabWidgetPointer->setTabPosition(QTabWidget::South);
347 // Set the search engine URL.
348 searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
350 // Emit the update search engine actions signal.
351 emit updateSearchEngineActions(Settings::searchEngine(), true);
354 // This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument.
355 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
356 void TabWidget::applyDomainSettingsAndReload()
358 // Apply the domain settings. `true` reloads the website.
359 applyDomainSettings(currentPrivacyWebEngineViewPointer->url().host(), true);
362 // This exists as a separate function from `applyDomainSettings()` so it can be listed as a slot and function without the need for a boolean argument.
363 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
364 void TabWidget::applyDomainSettingsWithoutReloading(const QString &hostname)
366 // Apply the domain settings `false` does not reload the website.
367 applyDomainSettings(hostname, false);
370 // Once <https://redmine.stoutner.com/issues/799> has been resolved this can be `const`.
371 void TabWidget::applyDomainSettings(const QString &hostname, const bool reloadWebsite)
373 // Get the record for the hostname.
374 QSqlQuery domainQuery = DomainsDatabase::getDomainQuery(hostname);
376 // Check if the hostname has domain settings.
377 if (domainQuery.isValid()) // The hostname has domain settings.
379 // Get the domain record.
380 QSqlRecord domainRecord = domainQuery.record();
382 // Store the domain settings name.
383 currentPrivacyWebEngineViewPointer->domainSettingsName = domainRecord.field(DomainsDatabase::DOMAIN_NAME).value().toString();
385 // Set the JavaScript status.
386 switch (domainRecord.field(DomainsDatabase::JAVASCRIPT).value().toInt())
388 // Set the default JavaScript status.
389 case (DomainsDatabase::SYSTEM_DEFAULT):
391 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
396 // Disable JavaScript.
397 case (DomainsDatabase::DISABLED):
399 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
404 // Enable JavaScript.
405 case (DomainsDatabase::ENABLED):
407 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, true);
413 // Set the local storage status.
414 switch (domainRecord.field(DomainsDatabase::LOCAL_STORAGE).value().toInt())
416 // Set the default local storage status.
417 case (DomainsDatabase::SYSTEM_DEFAULT):
419 currentPrivacyWebEngineViewPointer->localStorageEnabled = Settings::localStorageEnabled();
424 // Disable local storage.
425 case (DomainsDatabase::DISABLED):
427 currentPrivacyWebEngineViewPointer->localStorageEnabled = false;
432 // Enable local storage.
433 case (DomainsDatabase::ENABLED):
435 currentPrivacyWebEngineViewPointer->localStorageEnabled = true;
441 // Set the DOM storage status.
442 switch (domainRecord.field(DomainsDatabase::DOM_STORAGE).value().toInt())
444 // Set the default DOM storage status.
445 case (DomainsDatabase::SYSTEM_DEFAULT):
447 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
452 // Disable DOM storage.
453 case (DomainsDatabase::DISABLED):
455 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, false);
460 // Enable DOM storage.
461 case (DomainsDatabase::ENABLED):
463 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, true);
469 // Set the user agent.
470 currentWebEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getResultingDomainSettingsUserAgent(domainRecord.field(DomainsDatabase::USER_AGENT).value().toString()));
472 // Check if a custom zoom factor is set.
473 if (domainRecord.field(DomainsDatabase::ZOOM_FACTOR).value().toInt())
475 // Store the current zoom factor.
476 currentZoomFactor = domainRecord.field(DomainsDatabase::CUSTOM_ZOOM_FACTOR).value().toDouble();
480 // Reset the current zoom factor.
481 currentZoomFactor = Settings::zoomFactor();
484 // Set the zoom factor. The use of `currentZoomFactor` can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
485 currentPrivacyWebEngineViewPointer->setZoomFactor(currentZoomFactor);
487 else // The hostname does not have domain settings.
489 // Reset the domain settings name.
490 currentPrivacyWebEngineViewPointer->domainSettingsName = QStringLiteral("");
492 // Set the JavaScript status.
493 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, Settings::javaScriptEnabled());
495 // Set the local storage status.
496 currentPrivacyWebEngineViewPointer->localStorageEnabled = Settings::localStorageEnabled();
498 // Set DOM storage. In QWebEngineSettings it is called Local Storage.
499 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, Settings::domStorageEnabled());
501 // Set the user agent.
502 currentWebEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()));
504 // Store the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
505 currentZoomFactor = Settings::zoomFactor();
507 // Set the zoom factor.
508 currentPrivacyWebEngineViewPointer->setZoomFactor(Settings::zoomFactor());
512 emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QStringLiteral(""));
513 emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
514 emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
515 emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
516 emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true);
517 emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor());
519 // Reload the website if requested.
521 currentPrivacyWebEngineViewPointer->reload();
524 void TabWidget::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
526 // Store the search engine name.
527 QString searchEngineName = searchEngineActionPointer->text();
529 // Strip out any `&` characters.
530 searchEngineName.remove('&');
532 // Store the search engine string.
533 searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
535 // Update the search engine actionas.
536 emit updateSearchEngineActions(searchEngineName, false);
539 void TabWidget::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
541 // Get the user agent name.
542 QString userAgentName = userAgentActionPointer->text();
544 // Strip out any `&` characters.
545 userAgentName.remove('&');
547 // Apply the user agent.
548 currentWebEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
550 // Update the user agent actions.
551 emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), false);
553 // Reload the website.
554 currentPrivacyWebEngineViewPointer->reload();
557 // This can be const once <https://redmine.stoutner.com/issues/799> has been resolved.
558 void TabWidget::applyOnTheFlyZoomFactor(const double &zoomFactor)
560 // Update the current zoom factor. This can be removed once <https://redmine.stoutner.com/issues/799> has been resolved.
561 currentZoomFactor = zoomFactor;
563 // Set the zoom factor.
564 currentPrivacyWebEngineViewPointer->setZoomFactor(zoomFactor);
567 void TabWidget::back() const
570 currentPrivacyWebEngineViewPointer->back();
573 void TabWidget::deleteAllCookies() const
575 // Delete all the cookies.
576 currentWebEngineCookieStorePointer->deleteAllCookies();
579 void TabWidget::deleteCookieFromStore(const QNetworkCookie &cookie) const
581 // Delete the cookie.
582 currentWebEngineCookieStorePointer->deleteCookie(cookie);
585 void TabWidget::deleteTab(const int tabIndex)
587 // Get the privacy WebEngine view.
588 PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast<PrivacyWebEngineView *>(tabWidgetPointer->widget(tabIndex));
590 // Proccess the tab delete according to the number of tabs.
591 if (tabWidgetPointer->count() > 1) // There is more than one tab.
594 tabWidgetPointer->removeTab(tabIndex);
596 // Delete the WebEngine page to prevent the following error: `Release of profile requested but WebEnginePage still not deleted. Expect troubles !`
597 delete privacyWebEngineViewPointer->page();
599 // Delete the privacy WebEngine view.
600 delete privacyWebEngineViewPointer;
602 else // There is only one tab.
604 // Close Privacy Browser.
609 void TabWidget::forward() const
612 currentPrivacyWebEngineViewPointer->forward();
615 void TabWidget::fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest) const
618 emit fullScreenRequested(fullScreenRequest.toggleOn());
620 // Accept the request.
621 fullScreenRequest.accept();
624 std::list<QNetworkCookie>* TabWidget::getCookieList() const
626 // Return the current cookie list.
627 return currentPrivacyWebEngineViewPointer->cookieListPointer;
630 QString& TabWidget::getDomainSettingsName() const
632 // Return the domain settings name.
633 return currentPrivacyWebEngineViewPointer->domainSettingsName;
636 void TabWidget::home() const
638 // Load the homepage.
639 currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(Settings::homepage()));
642 PrivacyWebEngineView* TabWidget::loadBlankInitialWebsite()
644 // Apply the application settings.
645 applyApplicationSettings();
647 // Return the current privacy WebEngine view pointer.
648 return currentPrivacyWebEngineViewPointer;
651 void TabWidget::loadInitialWebsite()
653 // Apply the application settings.
654 applyApplicationSettings();
656 // Get the arguments.
657 QStringList argumentsStringList = qApp->arguments();
659 // Check to see if the arguments lists contains a URL.
660 if (argumentsStringList.size() > 1)
662 // Load the URL from the arguments list.
663 currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(argumentsStringList.at(1)));
667 // Load the homepage.
672 void TabWidget::loadUrlFromLineEdit(QString url) const
674 // Decide if the text is more likely to be a URL or a search.
675 if (url.startsWith("file://")) // The text is likely a file URL.
678 currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(url));
680 else if (url.contains(".")) // The text is likely a URL.
682 // Check if the URL does not start with a valid protocol.
683 if (!url.startsWith("http"))
685 // Add `https://` to the beginning of the URL.
686 url = "https://" + url;
690 currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(url));
692 else // The text is likely a search.
695 currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(searchEngineUrl + url));
699 void TabWidget::mouseBack() const
701 // Go back if possible.
702 if (currentPrivacyWebEngineViewPointer->isActiveWindow() && currentWebEngineHistoryPointer->canGoBack())
704 // Clear the URL line edit focus.
705 emit clearUrlLineEditFocus();
708 currentPrivacyWebEngineViewPointer->back();
712 void TabWidget::mouseForward() const
714 // Go forward if possible.
715 if (currentPrivacyWebEngineViewPointer->isActiveWindow() && currentWebEngineHistoryPointer->canGoForward())
717 // Clear the URL line edit focus.
718 emit clearUrlLineEditFocus();
721 currentPrivacyWebEngineViewPointer->forward();
725 void TabWidget::pageLinkHovered(const QString &linkUrl) const
727 // Emit a signal so that the browser window can update the status bar.
728 emit linkHovered(linkUrl);
731 void TabWidget::print() const
736 // Set the resolution to be 300 dpi.
737 printer.setResolution(300);
739 // Create a printer dialog.
740 QPrintDialog printDialog(&printer, currentPrivacyWebEngineViewPointer);
742 // Display the dialog and print the page if instructed.
743 if (printDialog.exec() == QDialog::Accepted)
744 printWebpage(&printer);
747 void TabWidget::printPreview() const
752 // Set the resolution to be 300 dpi.
753 printer.setResolution(300);
755 // Create a print preview dialog.
756 QPrintPreviewDialog printPreviewDialog(&printer, currentPrivacyWebEngineViewPointer);
758 // Generate the print preview.
759 connect(&printPreviewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printWebpage(QPrinter *)));
761 // Display the dialog.
762 printPreviewDialog.exec();
765 void TabWidget::printWebpage(QPrinter *printerPointer) const
767 // Create an event loop. For some reason, the print preview doesn't produce any output unless it is run inside an event loop.
768 QEventLoop eventLoop;
770 // Print the webpage, converting the callback above into a `QWebEngineCallback<bool>`.
771 // Printing requires that the printer be a pointer, not a reference, or it will crash with much cursing.
772 currentWebEnginePagePointer->print(printerPointer, [&eventLoop](bool printSuccess)
774 // Instruct the compiler to ignore the unused parameter.
785 void TabWidget::refresh() const
787 // Reload the website.
788 currentPrivacyWebEngineViewPointer->reload();
791 void TabWidget::setTabBarVisible(const bool visible) const
793 // Set the tab bar visibility.
794 tabWidgetPointer->tabBar()->setVisible(visible);
797 void TabWidget::showSaveDialog(QWebEngineDownloadItem *downloadItemPointer) const
799 // Instantiate the save dialog.
800 SaveDialog *saveDialogPointer = new SaveDialog(downloadItemPointer);
802 // Connect the save button.
803 connect(saveDialogPointer, SIGNAL(showSaveFilePickerDialog(QUrl &, QString &)), this, SLOT(showSaveFilePickerDialog(QUrl &, QString &)));
806 saveDialogPointer->show();
809 void TabWidget::showSaveFilePickerDialog(QUrl &downloadUrl, QString &suggestedFileName)
811 // Get the download location.
812 QString downloadDirectory = Settings::downloadLocation();
814 // Resolve the system download directory if specified.
815 if (downloadDirectory == QStringLiteral("System Download Directory"))
816 downloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
818 // Create a save file dialog.
819 QFileDialog *saveFileDialogPointer = new QFileDialog(this, i18nc("Save file dialog caption", "Save File"), downloadDirectory);
821 // Tell the dialog to use a save button.
822 saveFileDialogPointer->setAcceptMode(QFileDialog::AcceptSave);
824 // Populate the file name from the download item pointer.
825 saveFileDialogPointer->selectFile(suggestedFileName);
827 // Prevent interaction with the parent window while the dialog is open.
828 saveFileDialogPointer->setWindowModality(Qt::WindowModal);
830 // Process the saving of the file. The save file dialog pointer must be captured directly instead of by reference or nasty crashes occur.
831 auto saveFile = [saveFileDialogPointer, &downloadUrl] () {
832 // Get the save location. The dialog box should only allow the selecting of one file location.
833 QUrl saveLocation = saveFileDialogPointer->selectedUrls().value(0);
835 // Create a file copy job. `-1` creates the file with default permissions.
836 KIO::FileCopyJob *fileCopyJobPointer = KIO::file_copy(downloadUrl, saveLocation, -1, KIO::Overwrite);
838 // Set the download job to display any error messages.
839 fileCopyJobPointer->uiDelegate()->setAutoErrorHandlingEnabled(true);
841 // Start the download.
842 fileCopyJobPointer->start();
845 // Handle clicks on the save button.
846 connect(saveFileDialogPointer, &QDialog::accepted, this, saveFile);
849 saveFileDialogPointer->show();
852 void TabWidget::toggleDomStorage() const
854 // Toggle DOM storage.
855 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::LocalStorageEnabled, !currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
857 // Update the DOM storage action.
858 emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
860 // Reload the website.
861 currentPrivacyWebEngineViewPointer->reload();
864 void TabWidget::toggleJavaScript() const
866 // Toggle JavaScript.
867 currentWebEngineSettingsPointer->setAttribute(QWebEngineSettings::JavascriptEnabled, !currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
869 // Update the JavaScript action.
870 emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
872 // Reload the website.
873 currentPrivacyWebEngineViewPointer->reload();
876 void TabWidget::toggleLocalStorage()
878 // Toggle local storeage.
879 currentPrivacyWebEngineViewPointer->localStorageEnabled = !currentPrivacyWebEngineViewPointer->localStorageEnabled;
881 // Update the local storage action.
882 emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
884 // Reload the website.
885 currentPrivacyWebEngineViewPointer->reload();
888 void TabWidget::updateUiWithTabSettings()
890 // Update the current WebEngine pointers.
891 currentPrivacyWebEngineViewPointer = qobject_cast<PrivacyWebEngineView *>(tabWidgetPointer->currentWidget());
892 currentWebEngineSettingsPointer = currentPrivacyWebEngineViewPointer->settings();
893 currentWebEnginePagePointer = currentPrivacyWebEngineViewPointer->page();
894 currentWebEngineProfilePointer = currentWebEnginePagePointer->profile();
895 currentWebEngineHistoryPointer = currentWebEnginePagePointer->history();
896 currentWebEngineCookieStorePointer = currentWebEngineProfilePointer->cookieStore();
898 // Clear the URL line edit focus.
899 emit clearUrlLineEditFocus();
902 emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
903 emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size());
904 emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QStringLiteral(""));
905 emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
906 emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward());
907 emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
908 emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
909 emit updateWindowTitle(currentPrivacyWebEngineViewPointer->title());
910 emit updateUrlLineEdit(currentPrivacyWebEngineViewPointer->url());
911 emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true);
912 emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor());
914 // Update the progress bar.
915 if (currentPrivacyWebEngineViewPointer->loadProgressInt >= 0)
916 emit showProgressBar(currentPrivacyWebEngineViewPointer->loadProgressInt);
918 emit hideProgressBar();