X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fwidgets%2FTabWidget.cpp;h=9e843ef306da564f362d7c5e434a0589b7dda21d;hp=eb923336797868fd23e95206c1f20d7e2b6ccae2;hb=refs%2Fheads%2Fmaster;hpb=e5ec85accb02116b08c33b574805c132475d90bc diff --git a/src/widgets/TabWidget.cpp b/src/widgets/TabWidget.cpp index eb92333..0728ecc 100644 --- a/src/widgets/TabWidget.cpp +++ b/src/widgets/TabWidget.cpp @@ -1,7 +1,7 @@ /* - * Copyright 2022-2023 Soren Stoutner . + * Copyright 2022-2024 Soren Stoutner . * - * This file is part of Privacy Browser PC . + * This file is part of Privacy Browser PC . * * Privacy Browser PC is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -178,7 +178,7 @@ void TabWidget::addFirstTab() qTabWidgetPointer->currentWidget()->setFocus(); } -PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool backgroundTab, const QString urlString) +PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool adjacent, const bool backgroundTab, const QString urlString) { // Create a splitter widget. QSplitter *splitterPointer = new QSplitter(); @@ -197,8 +197,14 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const splitterPointer->addWidget(privacyWebEngineViewPointer); splitterPointer->addWidget(devToolsWebEngineViewPointer); + // Initialize the new tab index. + int newTabIndex = 0; + // Add a new tab. - int newTabIndex = qTabWidgetPointer->addTab(splitterPointer, i18nc("New tab label.", "New Tab")); + if (adjacent) // Add the new tab adjacent to the current tab. + newTabIndex = qTabWidgetPointer->insertTab((qTabWidgetPointer->currentIndex() + 1), splitterPointer, i18nc("New tab label.", "New Tab")); + else // Add the new tab at the end of the list. + newTabIndex = qTabWidgetPointer->addTab(splitterPointer, i18nc("New tab label.", "New Tab")); // Set the default tab icon. qTabWidgetPointer->setTabIcon(newTabIndex, defaultFavoriteIcon); @@ -446,12 +452,20 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const // Plugins must be enabled for the PDF viewer to work. webEngineSettingsPointer->setAttribute(QWebEngineSettings::PluginsEnabled, true); + // Update the blocked requests action. + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::requestBlocked, [this, privacyWebEngineViewPointer] (const int blockedRequests) + { + // Update the blocked requests action if the specified privacy WebEngine view is the current privacy WebEngine view. + if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) + emit blockedRequestsUpdated(blockedRequests); + }); + // Update the cookies action. - connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::updateCookiesAction, [this, privacyWebEngineViewPointer] (const int numberOfCookies) + connect(privacyWebEngineViewPointer, &PrivacyWebEngineView::numberOfCookiesChanged, [this, privacyWebEngineViewPointer] (const int numberOfCookies) { // Update the cookie action if the specified privacy WebEngine view is the current privacy WebEngine view. if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer) - emit updateCookiesAction(numberOfCookies); + emit cookiesChanged(numberOfCookies); }); // Process cookie changes. @@ -567,10 +581,10 @@ void TabWidget::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const currentPrivacyWebEngineViewPointer->reload(); } -void TabWidget::applyOnTheFlyZoomFactor(const double &zoomFactor) const +void TabWidget::applyOnTheFlyZoomFactor(const double zoomFactorDouble) const { // Set the zoom factor. - currentPrivacyWebEngineViewPointer->setZoomFactor(zoomFactor); + currentPrivacyWebEngineViewPointer->setZoomFactor(zoomFactorDouble); } void TabWidget::applySpellCheckLanguages() const @@ -731,6 +745,12 @@ QString TabWidget::getCurrentTabUrl() const return currentPrivacyWebEngineViewPointer->url().toString(); } +QString TabWidget::getCurrentUserAgent() const +{ + // Return the current WebEngine user agent. + return currentWebEngineProfilePointer->httpUserAgent(); +} + QString& TabWidget::getDomainSettingsName() const { // Return the domain settings name. @@ -894,6 +914,9 @@ void TabWidget::printWebpage(QPrinter *printerPointer) const void TabWidget::refresh() const { + // Reset the HTTP authentication dialog counter. + currentPrivacyWebEngineViewPointer->httpAuthenticationDialogsDisplayed = 0; + // Reload the website. currentPrivacyWebEngineViewPointer->reload(); } @@ -907,10 +930,10 @@ void TabWidget::reloadAndBypassCache() const void TabWidget::saveArchive() { // Get the suggested file name. - QString suggestedFileName = currentPrivacyWebEngineViewPointer->url().host() + ".mht"; + QString suggestedFileName = currentPrivacyWebEngineViewPointer->title() + ".mht"; // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -922,6 +945,10 @@ void TabWidget::saveArchive() // Save the webpage as an archive if the file save path is populated. if (!saveFilePath.isEmpty()) { + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveFilePath); + // Set the saving archive flag. Otherwise, a second download tries to run. savingArchive = true; @@ -951,7 +978,7 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin if (!isRunningKde || currentPrivacyWebEngineViewPointer->localStorageEnabled) // KDE is not running or local storage (cookies) is enabled. Use WebEngine's downloader. { // Instantiate the save dialog. - SaveDialog *saveDialogPointer = new SaveDialog(downloadUrl, mimeTypeString, totalBytes); + SaveDialog *saveDialogPointer = new SaveDialog(this, downloadUrl, mimeTypeString, totalBytes); // Display the save dialog. int saveDialogResult = saveDialogPointer->exec(); @@ -960,7 +987,7 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin if (saveDialogResult == QDialog::Accepted) // Save was selected. { // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -972,6 +999,10 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin // Process the save file path. if (!saveFilePath.isEmpty()) // The file save path is populated. { + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveFilePath); + // Create a save file path file info. QFileInfo saveFilePathFileInfo = QFileInfo(saveFilePath); @@ -1090,7 +1121,7 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin // This must use the show command to launch a separate dialog which cancels WebEngine's automatic background download of the file to a temporary location. { // Instantiate the save dialog. `true` instructs it to use the native downloader - SaveDialog *saveDialogPointer = new SaveDialog(downloadUrl, mimeTypeString, totalBytes, suggestedFileName, true); + SaveDialog *saveDialogPointer = new SaveDialog(this, downloadUrl, mimeTypeString, totalBytes, suggestedFileName, true); // Connect the save button. connect(saveDialogPointer, SIGNAL(useNativeKdeDownloader(QUrl &, QString &)), this, SLOT(useNativeKdeDownloader(QUrl &, QString &))); @@ -1104,6 +1135,12 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin savingArchive = false; } +void TabWidget::stop() const +{ + // Stop the loading of the current privacy WebEngine. + currentPrivacyWebEngineViewPointer->stop(); +} + void TabWidget::toggleDeveloperTools(const bool enabled) const { // Get a handle for the current developer tools WebEngine. @@ -1187,6 +1224,21 @@ void TabWidget::toggleLocalStorage() currentPrivacyWebEngineViewPointer->reload(); } +void TabWidget::updateDownloadDirectory(QString newDownloadDirectory) const +{ + // Remove the file name from the save file path. + newDownloadDirectory.truncate(newDownloadDirectory.lastIndexOf(QLatin1Char('/'))); + + // Update the download location. + Settings::setDownloadDirectory(newDownloadDirectory); + + // Get a handle for the KConfig skeleton. + KConfigSkeleton *kConfigSkeletonPointer = Settings::self(); + + // Write the settings to disk. + kConfigSkeletonPointer->save(); +} + void TabWidget::updateUiFromWebEngineView(const PrivacyWebEngineView *privacyWebEngineViewPointer) const { // Only update the UI if the signal was emitted from the current privacy WebEngine. @@ -1220,9 +1272,10 @@ void TabWidget::updateUiWithTabSettings() DevToolsWebEngineView *devToolsWebEngineViewPointer = qTabWidgetPointer->currentWidget()->findChild(); // Update the actions. + emit blockedRequestsUpdated(currentPrivacyWebEngineViewPointer->blockedRequests); + emit cookiesChanged(currentPrivacyWebEngineViewPointer->cookieListPointer->size()); emit updateDefaultZoomFactor(currentPrivacyWebEngineViewPointer->defaultZoomFactor); emit updateBackAction(currentWebEngineHistoryPointer->canGoBack()); - emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size()); emit updateDeveloperToolsAction(devToolsWebEngineViewPointer->isVisible()); emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled)); emit updateForwardAction(currentWebEngineHistoryPointer->canGoForward()); @@ -1250,7 +1303,7 @@ void TabWidget::updateUiWithTabSettings() void TabWidget::useNativeKdeDownloader(QUrl &downloadUrl, QString &suggestedFileName) { // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -1269,11 +1322,15 @@ void TabWidget::useNativeKdeDownloader(QUrl &downloadUrl, QString &suggestedFile saveFileDialogPointer->setWindowModality(Qt::WindowModal); // Process the saving of the file. The save file dialog pointer must be captured directly instead of by reference or nasty crashes occur. - auto saveFile = [saveFileDialogPointer, downloadUrl] () + auto saveFile = [saveFileDialogPointer, downloadUrl, this] () { // Get the save location. The dialog box should only allow the selecting of one file location. QUrl saveLocation = saveFileDialogPointer->selectedUrls().value(0); + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveLocation.toLocalFile()); + // Create a file copy job. `-1` creates the file with default permissions. KIO::FileCopyJob *fileCopyJobPointer = KIO::file_copy(downloadUrl, saveLocation, -1, KIO::Overwrite);