]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/TabWidget.cpp
Implement saving of MHT web archives. https://redmine.stoutner.com/issues/1089
[PrivacyBrowserPC.git] / src / widgets / TabWidget.cpp
index 0a7b3ca1d458ae6d0b97d735545f1fd5bfdcc28c..4a6b11c64d2bb7800497c4da9c70a970b4c0c489 100644 (file)
@@ -171,7 +171,7 @@ void TabWidget::addFirstTab()
     qTabWidgetPointer->currentWidget()->setFocus();
 }
 
-PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool backgroundTab)
+PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const bool backgroundTab, const QString urlString)
 {
     // Create a privacy WebEngine view.
     PrivacyWebEngineView *privacyWebEngineViewPointer = new PrivacyWebEngineView();
@@ -344,12 +344,12 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const
         }
     });
 
-    // Update the zoom factor when changed by CTRL-Scrolling.  This can be modified when <https://redmine.stoutner.com/issues/845> is fixed.
+    // Update the zoom actions when changed by CTRL-Scrolling.  This can be modified when <https://redmine.stoutner.com/issues/845> is fixed.
     connect(webEnginePagePointer, &QWebEnginePage::contentsSizeChanged, [webEnginePagePointer, this] ()
     {
-        // Only update the zoom factor action text if this is the current tab.
+        // Only update the zoom actions if this is the current tab.
         if (webEnginePagePointer == currentWebEnginePagePointer)
-            emit updateZoomFactorAction(webEnginePagePointer->zoomFactor());
+            emit updateZoomActions(webEnginePagePointer->zoomFactor());
     });
 
     // Display find text results.
@@ -454,6 +454,9 @@ PrivacyWebEngineView* TabWidget::addTab(const bool removeUrlLineEditFocus, const
     if (removeUrlLineEditFocus)
         emit clearUrlLineEditFocus();
 
+    if (urlString != nullptr)
+        privacyWebEngineViewPointer->load(QUrl::fromUserInput(urlString));
+
     // Return the privacy WebEngine view pointer.
     return privacyWebEngineViewPointer;
 }
@@ -487,8 +490,17 @@ void TabWidget::applyApplicationSettings()
 
 void TabWidget::applyDomainSettingsAndReload()
 {
-    // Apply the domain settings.  `true` reloads the website.
-    currentPrivacyWebEngineViewPointer->applyDomainSettings(currentPrivacyWebEngineViewPointer->url().host(), true);
+    // Get the number of tabs.
+    int numberOfTabs = qTabWidgetPointer->count();
+
+    // Apply the domain settings to each WebEngine.
+    for (int i = 0; i < numberOfTabs; ++i) {
+        // Get the WebEngine view pointer.
+        PrivacyWebEngineView *privacyWebEngineViewPointer = qobject_cast<PrivacyWebEngineView *>(qTabWidgetPointer->widget(i));
+
+        // Apply the spatial navigation settings to each page.
+        privacyWebEngineViewPointer->applyDomainSettings(privacyWebEngineViewPointer->url().host(), true);
+    }
 }
 
 void TabWidget::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
@@ -661,6 +673,24 @@ std::list<QNetworkCookie>* TabWidget::getCookieList() const
     return currentPrivacyWebEngineViewPointer->cookieListPointer;
 }
 
+QIcon TabWidget::getCurrentTabFavoritIcon() const
+{
+    // Return the current Privacy WebEngine favorite icon.
+    return currentPrivacyWebEngineViewPointer->favoriteIcon;
+}
+
+QString TabWidget::getCurrentTabTitle() const
+{
+    // Return the current Privacy WebEngine title.
+    return currentPrivacyWebEngineViewPointer->title();
+}
+
+QString TabWidget::getCurrentTabUrl() const
+{
+    // Return the current Privacy WebEngine URL as a string.
+    return currentPrivacyWebEngineViewPointer->url().toString();
+}
+
 QString& TabWidget::getDomainSettingsName() const
 {
     // Return the domain settings name.
@@ -706,7 +736,7 @@ void TabWidget::loadInitialWebsite()
 void TabWidget::loadUrlFromLineEdit(QString url) const
 {
     // Decide if the text is more likely to be a URL or a search.
-    if (url.startsWith("file://"))  // The text is likely a file URL.
+    if (url.startsWith("file://") || url.startsWith("view-source:"))  // The text is likely a file or view source URL.
     {
         // Load the URL.
         currentPrivacyWebEngineViewPointer->load(QUrl::fromUserInput(url));
@@ -828,6 +858,38 @@ void TabWidget::refresh() const
     currentPrivacyWebEngineViewPointer->reload();
 }
 
+void TabWidget::reloadAndBypassCache() const
+{
+    // Reload the website, bypassing the cache.
+    currentWebEnginePagePointer->triggerAction(QWebEnginePage::ReloadAndBypassCache);
+}
+
+void TabWidget::saveArchive()
+{
+    // Get the suggested file name.
+    QString suggestedFileName = currentPrivacyWebEngineViewPointer->url().host() + ".mht";
+
+    // Get the download directory.
+    QString downloadDirectory = Settings::downloadLocation();
+
+    // Resolve the system download directory if specified.
+    if (downloadDirectory == QLatin1String("System Download Directory"))
+        downloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
+
+    // Get a file path from the file picker.
+    QString saveFilePath = QFileDialog::getSaveFileName(this, i18nc("Save file dialog caption", "Save File"), downloadDirectory + QLatin1Char('/') + suggestedFileName);
+
+    // Save the webpage as an archive if the file save path is populated.
+    if (!saveFilePath.isEmpty())
+    {
+        // Set the saving archive flag.  Otherwise, a second download tries to run.
+        savingArchive = true;
+
+        // Save the archive.
+        currentWebEnginePagePointer->save(saveFilePath);
+    }
+}
+
 void TabWidget::setTabBarVisible(const bool visible) const
 {
     // Set the tab bar visibility.
@@ -836,163 +898,170 @@ void TabWidget::setTabBarVisible(const bool visible) const
 
 void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPointer)
 {
-    // Get the download attributes.
-    QUrl downloadUrl = webEngineDownloadItemPointer->url();
-    QString mimeTypeString = webEngineDownloadItemPointer->mimeType();
-    QString suggestedFileName = webEngineDownloadItemPointer->suggestedFileName();
-    int totalBytes = webEngineDownloadItemPointer->totalBytes();
-
-    // Check to see if Privacy Browser is not running KDE or if local storage (cookies) is enabled.
-    if (!isRunningKde || currentPrivacyWebEngineViewPointer->localStorageEnabled)  // KDE is not running or local storage (cookies) is enabled.  Use WebEngine's downloader.
+    // Only show the save dialog if an archive is not currently being saved.  Otherwise, two save dialogs will be shown.
+    if (!savingArchive)
     {
-        // Instantiate the save dialog.
-        SaveDialog *saveDialogPointer = new SaveDialog(downloadUrl, mimeTypeString, totalBytes);
-
-        // Display the save dialog.
-        int saveDialogResult = saveDialogPointer->exec();
-
-        // Process the save dialog results.
-        if (saveDialogResult == QDialog::Accepted)  // Save was selected.
+        // Get the download attributes.
+        QUrl downloadUrl = webEngineDownloadItemPointer->url();
+        QString mimeTypeString = webEngineDownloadItemPointer->mimeType();
+        QString suggestedFileName = webEngineDownloadItemPointer->suggestedFileName();
+        int totalBytes = webEngineDownloadItemPointer->totalBytes();
+
+        // Check to see if Privacy Browser is not running KDE or if local storage (cookies) is enabled.
+        if (!isRunningKde || currentPrivacyWebEngineViewPointer->localStorageEnabled)  // KDE is not running or local storage (cookies) is enabled.  Use WebEngine's downloader.
         {
-            // Get the download directory.
-            QString downloadDirectory = Settings::downloadLocation();
-
-            // Resolve the system download directory if specified.
-            if (downloadDirectory == QLatin1String("System Download Directory"))
-                downloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
+            // Instantiate the save dialog.
+            SaveDialog *saveDialogPointer = new SaveDialog(downloadUrl, mimeTypeString, totalBytes);
 
-            // Get a file path from the file picker.
-            QString saveFilePath = QFileDialog::getSaveFileName(this, i18nc("Save file dialog caption", "Save File"), downloadDirectory + QLatin1Char('/') + suggestedFileName);
+            // Display the save dialog.
+            int saveDialogResult = saveDialogPointer->exec();
 
-            // Process the save file path.
-            if (!saveFilePath.isEmpty())  // The file save path is populated.
+            // Process the save dialog results.
+            if (saveDialogResult == QDialog::Accepted)  // Save was selected.
             {
-                // Create a save file path file info.
-                QFileInfo saveFilePathFileInfo = QFileInfo(saveFilePath);
+                // Get the download directory.
+                QString downloadDirectory = Settings::downloadLocation();
 
-                // Get the canonical save path and file name.
-                QString absoluteSavePath = saveFilePathFileInfo.absolutePath();
-                QString saveFileName = saveFilePathFileInfo.fileName();
+                // Resolve the system download directory if specified.
+                if (downloadDirectory == QLatin1String("System Download Directory"))
+                    downloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
 
-                // Set the download directory and file name.
-                webEngineDownloadItemPointer->setDownloadDirectory(absoluteSavePath);
-                webEngineDownloadItemPointer->setDownloadFileName(saveFileName);
+                // Get a file path from the file picker.
+                QString saveFilePath = QFileDialog::getSaveFileName(this, i18nc("Save file dialog caption", "Save File"), downloadDirectory + QLatin1Char('/') + suggestedFileName);
 
-                // Create a file download notification.
-                KNotification *fileDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload"));
+                // Process the save file path.
+                if (!saveFilePath.isEmpty())  // The file save path is populated.
+                {
+                    // Create a save file path file info.
+                    QFileInfo saveFilePathFileInfo = QFileInfo(saveFilePath);
 
-                // Set the notification title.
-                fileDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download"));
+                    // Get the canonical save path and file name.
+                    QString absoluteSavePath = saveFilePathFileInfo.absolutePath();
+                    QString saveFileName = saveFilePathFileInfo.fileName();
 
-                // Set the notification text.
-                fileDownloadNotificationPointer->setText(i18nc("Downloading notification text", "Downloading %1", saveFileName));
+                    // Set the download directory and file name.
+                    webEngineDownloadItemPointer->setDownloadDirectory(absoluteSavePath);
+                    webEngineDownloadItemPointer->setDownloadFileName(saveFileName);
 
-                // Set the notification icon.
-                fileDownloadNotificationPointer->setIconName(QLatin1String("download"));
+                    // Create a file download notification.
+                    KNotification *fileDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload"));
 
-                // Set the action list cancel button.
-                fileDownloadNotificationPointer->setActions(QStringList({i18nc("Download notification action","Cancel")}));
+                    // Set the notification title.
+                    fileDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download"));
 
-                // Set the notification to display indefinitely.
-                fileDownloadNotificationPointer->setFlags(KNotification::Persistent);
+                    // Set the notification text.
+                    fileDownloadNotificationPointer->setText(i18nc("Downloading notification text", "Downloading %1", saveFileName));
 
-                // Prevent the notification from being autodeleted if it is closed.  Otherwise, the updates to the notification below cause a crash.
-                fileDownloadNotificationPointer->setAutoDelete(false);
+                    // Get the download icon from the theme.
+                    QIcon downloadIcon = QIcon::fromTheme(QLatin1String("download"), QIcon::fromTheme(QLatin1String("document-save")));
 
-                // Display the notification.
-                fileDownloadNotificationPointer->sendEvent();
+                    // Set the notification icon.
+                    fileDownloadNotificationPointer->setIconName(downloadIcon.name());
 
-                // Handle clicks on the cancel button.
-                connect(fileDownloadNotificationPointer, &KNotification::action1Activated, [webEngineDownloadItemPointer, saveFileName] ()
-                {
-                    // Cancel the download.
-                    webEngineDownloadItemPointer->cancel();
+                    // Set the action list cancel button.
+                    fileDownloadNotificationPointer->setActions(QStringList({i18nc("Download notification action","Cancel")}));
 
-                    // Create a file download notification.
-                    KNotification *canceledDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload"));
+                    // Prevent the notification from being autodeleted if it is closed.  Otherwise, the updates to the notification below cause a crash.
+                    fileDownloadNotificationPointer->setAutoDelete(false);
 
-                    // Set the notification title.
-                    canceledDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download"));
+                    // Handle clicks on the cancel button.
+                    connect(fileDownloadNotificationPointer, &KNotification::action1Activated, [webEngineDownloadItemPointer, saveFileName] ()
+                    {
+                        // Cancel the download.
+                        webEngineDownloadItemPointer->cancel();
 
-                    // Set the new text.
-                    canceledDownloadNotificationPointer->setText(i18nc("Download canceled notification", "%1 download canceled", saveFileName));
+                        // Create a file download notification.
+                        KNotification *canceledDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload"));
 
-                    // Set the notification icon.
-                    canceledDownloadNotificationPointer->setIconName(QLatin1String("download"));
+                        // Set the notification title.
+                        canceledDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download"));
 
-                    // Display the notification.
-                    canceledDownloadNotificationPointer->sendEvent();
-                });
+                        // Set the new text.
+                        canceledDownloadNotificationPointer->setText(i18nc("Download canceled notification", "%1 download canceled", saveFileName));
 
-                // Update the notification when the download progresses.
-                connect(webEngineDownloadItemPointer, &QWebEngineDownloadItem::downloadProgress, [fileDownloadNotificationPointer, saveFileName] (qint64 bytesReceived, qint64 totalBytes)
-                {
-                    // Set the new text.  Total bytes will be 0 if the download size is unknown.
-                    if (totalBytes > 0)
+                        // Set the notification icon.
+                        canceledDownloadNotificationPointer->setIconName(QLatin1String("download"));
+
+                        // Display the notification.
+                        canceledDownloadNotificationPointer->sendEvent();
+                    });
+
+                    // Update the notification when the download progresses.
+                    connect(webEngineDownloadItemPointer, &QWebEngineDownloadItem::downloadProgress, [fileDownloadNotificationPointer, saveFileName] (qint64 bytesReceived, qint64 totalBytes)
                     {
-                        // Calculate the download percentage.
-                        int downloadPercentage = 100 * bytesReceived / totalBytes;
-
-                        // Set the file download notification text.
-                        fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1\% of %2 downloaded (%3 of %4 bytes)", downloadPercentage, saveFileName,
-                                                                    bytesReceived, totalBytes));
-                    }
-                    else
+                        // Set the new text.  Total bytes will be 0 if the download size is unknown.
+                        if (totalBytes > 0)
+                        {
+                            // Calculate the download percentage.
+                            int downloadPercentage = 100 * bytesReceived / totalBytes;
+
+                            // Set the file download notification text.
+                            fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1\% of %2 downloaded (%3 of %4 bytes)", downloadPercentage, saveFileName,
+                                                                        bytesReceived, totalBytes));
+                        }
+                        else
+                        {
+                            // Set the file download notification text.
+                            fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1:  %2 bytes downloaded", saveFileName, bytesReceived));
+                        }
+
+                        // Display the updated notification.
+                        fileDownloadNotificationPointer->update();
+                    });
+
+                    // Update the notification when the download finishes.  The save file name must be copied into the lambda or a crash occurs.
+                    connect(webEngineDownloadItemPointer, &QWebEngineDownloadItem::finished, [fileDownloadNotificationPointer, saveFileName, saveFilePath] ()
                     {
-                        // Set the file download notification text.
-                        fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1:  %2 bytes downloaded", saveFileName, bytesReceived));
-                    }
-
-                    // Display the updated notification.
-                    fileDownloadNotificationPointer->update();
-                });
+                        // Set the new text.
+                        fileDownloadNotificationPointer->setText(i18nc("Download finished notification text", "%1 download finished", saveFileName));
 
-                // Update the notification when the download finishes.  The save file name must be copied into the lambda or a crash occurs.
-                connect(webEngineDownloadItemPointer, &QWebEngineDownloadItem::finished, [fileDownloadNotificationPointer, saveFileName, saveFilePath] ()
-                {
-                    // Set the new text.
-                    fileDownloadNotificationPointer->setText(i18nc("Download finished notification text", "%1 download finished", saveFileName));
+                        // Set the URL so the file options will be displayed.
+                        fileDownloadNotificationPointer->setUrls(QList<QUrl> {QUrl(saveFilePath)});
 
-                    // Set the URL so the file options will be displayed.
-                    fileDownloadNotificationPointer->setUrls(QList<QUrl> {QUrl(saveFilePath)});
+                        // Remove the actions from the notification.
+                        fileDownloadNotificationPointer->setActions(QStringList());
 
-                    // Remove the actions from the notification.
-                    fileDownloadNotificationPointer->setActions(QStringList());
+                        // Set the notification to disappear after a timeout.
+                        fileDownloadNotificationPointer->setFlags(KNotification::CloseOnTimeout);
 
-                    // Set the notification to disappear after a timeout.
-                    fileDownloadNotificationPointer->setFlags(KNotification::CloseOnTimeout);
+                        // Display the updated notification.
+                        fileDownloadNotificationPointer->update();
+                    });
 
-                    // Display the updated notification.
-                    fileDownloadNotificationPointer->update();
-                });
+                    // Display the notification.
+                    fileDownloadNotificationPointer->sendEvent();
 
-                // Start the download.
-                webEngineDownloadItemPointer->accept();
+                    // Start the download.
+                    webEngineDownloadItemPointer->accept();
+                }
+                else  // The file save path is not populated.
+                {
+                    // Cancel the download.
+                    webEngineDownloadItemPointer->cancel();
+                }
             }
-            else  // The file save path is not populated.
+            else  // Cancel was selected.
             {
                 // Cancel the download.
                 webEngineDownloadItemPointer->cancel();
             }
         }
-        else  // Cancel was selected.
+        else  // KDE is running and local storage (cookies) is disabled.  Use KDE's native downloader.
+            // 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.
         {
-            // Cancel the download.
-            webEngineDownloadItemPointer->cancel();
-        }
-    }
-    else  // KDE is running and local storage (cookies) is disabled.  Use KDE's native downloader.
-          // 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);
+            // Instantiate the save dialog.  `true` instructs it to use the native downloader
+            SaveDialog *saveDialogPointer = new SaveDialog(downloadUrl, mimeTypeString, totalBytes, suggestedFileName, true);
 
-        // Connect the save button.
-        connect(saveDialogPointer, SIGNAL(useNativeKdeDownloader(QUrl &, QString &)), this, SLOT(useNativeKdeDownloader(QUrl &, QString &)));
+            // Connect the save button.
+            connect(saveDialogPointer, SIGNAL(useNativeKdeDownloader(QUrl &, QString &)), this, SLOT(useNativeKdeDownloader(QUrl &, QString &)));
 
-        // Show the dialog.
-        saveDialogPointer->show();
+            // Show the dialog.
+            saveDialogPointer->show();
+        }
     }
+
+    // Reset the saving archive flag.
+    savingArchive = false;
 }
 
 void TabWidget::toggleDomStorage() const
@@ -1052,12 +1121,13 @@ void TabWidget::updateUiFromWebEngineView(const PrivacyWebEngineView *privacyWeb
     if (privacyWebEngineViewPointer == currentPrivacyWebEngineViewPointer)
     {
         // Update the UI.
+        emit updateDefaultZoomFactor(currentPrivacyWebEngineViewPointer->defaultZoomFactor);
         emit updateDomainSettingsIndicator(currentPrivacyWebEngineViewPointer->domainSettingsName != QLatin1String(""));
         emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
         emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
         emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
         emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true);
-        emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor());
+        emit updateZoomActions(currentPrivacyWebEngineViewPointer->zoomFactor());
     }
 }
 
@@ -1075,6 +1145,7 @@ void TabWidget::updateUiWithTabSettings()
     emit clearUrlLineEditFocus();
 
     // Update the actions.
+    emit updateDefaultZoomFactor(currentPrivacyWebEngineViewPointer->defaultZoomFactor);
     emit updateBackAction(currentWebEngineHistoryPointer->canGoBack());
     emit updateCookiesAction(currentPrivacyWebEngineViewPointer->cookieListPointer->size());
     emit updateDomStorageAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
@@ -1082,7 +1153,7 @@ void TabWidget::updateUiWithTabSettings()
     emit updateJavaScriptAction(currentWebEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
     emit updateLocalStorageAction(currentPrivacyWebEngineViewPointer->localStorageEnabled);
     emit updateUserAgentActions(currentWebEngineProfilePointer->httpUserAgent(), true);
-    emit updateZoomFactorAction(currentPrivacyWebEngineViewPointer->zoomFactor());
+    emit updateZoomActions(currentPrivacyWebEngineViewPointer->zoomFactor());
 
     // Update the URL.
     emit updateWindowTitle(currentPrivacyWebEngineViewPointer->title());