]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/views/BrowserView.cpp
Implement printing.
[PrivacyBrowserPC.git] / src / views / BrowserView.cpp
index 591e16aa3ae44361e6f43c70d40b20d51af406fe..b3f593ff93352a1d023a895207cb5832af21e036 100644 (file)
@@ -31,6 +31,9 @@
 
 // Qt framework headers.
 #include <QAction>
+#include <QPrintDialog>
+#include <QPrintPreviewDialog>
+#include <QPrinter>
 
 // Initialize the public static variables.
 QString BrowserView::webEngineDefaultUserAgent = QStringLiteral("");
@@ -59,6 +62,9 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     // Set the WebEngine page.
     webEngineViewPointer->setPage(webEnginePagePointer);
 
+    // Handle full screen requests.
+    connect(webEnginePagePointer, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this, SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
+
     // Get handles for the aspects of the WebEngine.
     webEngineHistoryPointer = webEnginePagePointer->history();
     webEngineSettingsPointer = webEngineViewPointer->settings();
@@ -73,20 +79,43 @@ BrowserView::BrowserView(QWidget *parent) : QWidget(parent)
     // Set the local storage filter.
     webEngineCookieStorePointer->setCookieFilter([this](const QWebEngineCookieStore::FilterRequest &filterRequest)
     {
-        // qDebug() << "Page URL:  " << filterRequest.firstPartyUrl << ", Local storage URL:  " << filterRequest.origin << ",  Is third-party:  " << filterRequest.thirdParty;
+        //qDebug().noquote().nospace() << "Page URL:  " << filterRequest.firstPartyUrl << ", Local storage URL:  " << filterRequest.origin << ",  Is third-party:  " << filterRequest.thirdParty;
 
         // Block all third party local storage requests, including the sneaky ones that don't register a first party URL.
         if (filterRequest.thirdParty || (filterRequest.firstPartyUrl == QStringLiteral("")))
+        {
+            //qDebug() << "Request blocked.";
+
+            // Return false.
             return false;
+        }
 
+        /*  TODO.  Waiting for a solution to <https://redmine.stoutner.com/issues/857>.
         // Check each tab to see if this local storage request should be allowed.
         for (PrivacyWebEngine *privacyWebEnginePointer : *privacyWebEngineListPointer)
         {
+            //qDebug().noquote().nospace() << "Local storage:  " << privacyWebEnginePointer->localStorageEnabled << ".  WebEngine URL:  " << webEngineViewPointer->url().host() << ".  Request Host:  " << filterRequest.firstPartyUrl.host();
+
             // Allow this local storage request if it comes from a tab with local storage enabled.
             if (privacyWebEnginePointer->localStorageEnabled && (webEngineViewPointer->url().host() == filterRequest.firstPartyUrl.host()))
+            {
+                //qDebug() << "Request allowed.";
+
+                // Return true.
                 return true;
+            }
+        }
+        */
+
+        // Allow the request if it is first party and local storage is enabled.
+        if (!filterRequest.thirdParty && currentPrivacyWebEnginePointer->localStorageEnabled)
+        {
+            // Return true.
+            return true;
         }
 
+        //qDebug() << "Request blocked.";
+
         // Block any remaining local storage requests.
         return false;
     });
@@ -187,7 +216,7 @@ void BrowserView::applyApplicationSettings()
     searchEngineUrl = SearchEngineHelper::getSearchUrl(Settings::searchEngine());
 
     // Emit the update search engine actions signal.
-    emit updateSearchEngineActions(Settings::searchEngine());
+    emit updateSearchEngineActions(Settings::searchEngine(), true);
 }
 
 // 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.
@@ -351,14 +380,12 @@ void BrowserView::applyDomainSettings(const QString &hostname, const bool reload
     emit updateJavaScriptAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::JavascriptEnabled));
     emit updateLocalStorageAction(currentPrivacyWebEnginePointer->localStorageEnabled);
     emit updateDomStorageAction(webEngineSettingsPointer->testAttribute(QWebEngineSettings::LocalStorageEnabled));
-    emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent());
+    emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), true);
     emit updateZoomFactorAction(webEngineViewPointer->zoomFactor());
 
     // Reload the website if requested.
     if (reloadWebsite)
-    {
         webEngineViewPointer->reload();
-    }
 }
 
 void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
@@ -371,6 +398,9 @@ void BrowserView::applyOnTheFlySearchEngine(QAction *searchEngineActionPointer)
 
     // Store the search engine string.
     searchEngineUrl = SearchEngineHelper::getSearchUrl(searchEngineName);
+
+    // Update the search engine actionas.
+    emit updateSearchEngineActions(searchEngineName, false);
 }
 
 void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
@@ -384,6 +414,9 @@ void BrowserView::applyOnTheFlyUserAgent(QAction *userAgentActionPointer) const
     // Apply the user agent.
     webEngineProfilePointer->setHttpUserAgent(UserAgentHelper::getUserAgentFromTranslatedName(userAgentName));
 
+    // Update the user agent actions.
+    emit updateUserAgentActions(webEngineProfilePointer->httpUserAgent(), false);
+
     // Reload the website.
     webEngineViewPointer->reload();
 }
@@ -434,6 +467,15 @@ void BrowserView::forward() const
     webEngineViewPointer->forward();
 }
 
+void BrowserView::fullScreenRequested(QWebEngineFullScreenRequest fullScreenRequest) const
+{
+    // Make it so.
+    emit fullScreenRequested(fullScreenRequest.toggleOn());
+
+    // Accept the request.
+    fullScreenRequest.accept();
+}
+
 void BrowserView::home() const
 {
     // Load the homepage.
@@ -509,7 +551,7 @@ void BrowserView::loadUrlFromLineEdit(QString url) const
 void BrowserView::mouseBack() const
 {
     // Go back if possible.
-    if (webEngineHistoryPointer->canGoBack())
+    if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoBack())
     {
         // Clear the URL line edit focus.
         emit clearUrlLineEditFocus();
@@ -522,7 +564,7 @@ void BrowserView::mouseBack() const
 void BrowserView::mouseForward() const
 {
     // Go forward if possible.
-    if (webEngineHistoryPointer->canGoForward())
+    if (webEngineViewPointer->isActiveWindow() && webEngineHistoryPointer->canGoForward())
     {
         // Clear the URL line edit focus.
         emit clearUrlLineEditFocus();
@@ -538,6 +580,60 @@ void BrowserView::pageLinkHovered(const QString &linkUrl) const
     emit linkHovered(linkUrl);
 }
 
+void BrowserView::print() const
+{
+    // Create a printer.
+    QPrinter printer;
+
+    // Set the resolution to be 300 dpi.
+    printer.setResolution(300);
+
+    // Create a printer dialog.
+    QPrintDialog printDialog(&printer, webEngineViewPointer);
+
+    // Display the dialog and print the page if instructed.
+    if (printDialog.exec() == QDialog::Accepted)
+        printWebpage(&printer);
+}
+
+void BrowserView::printPreview() const
+{
+    // Create a printer.
+    QPrinter printer;
+
+    // Set the resolution to be 300 dpi.
+    printer.setResolution(300);
+
+    // Create a print preview dialog.
+    QPrintPreviewDialog printPreviewDialog(&printer, webEngineViewPointer);
+
+    // Generate the print preview.
+    connect(&printPreviewDialog, SIGNAL(paintRequested(QPrinter *)), this, SLOT(printWebpage(QPrinter *)));
+
+    // Display the dialog.
+    printPreviewDialog.exec();
+}
+
+void BrowserView::printWebpage(QPrinter *printerPointer) const
+{
+    // Create an event loop.  For some reason, the print preview doesn't produce any output unless it is run inside an event loop.
+    QEventLoop eventLoop;
+
+    // Print the webpage, converting the callback above into a `QWebEngineCallback<bool>`.
+    // Printing requires that the printer be a pointer, not a reference, or it will crash with much cursing.
+    webEnginePagePointer->print(printerPointer, [&eventLoop](bool printSuccess)
+    {
+        // Instruct the compiler to ignore the unused parameter.
+        (void) printSuccess;
+
+        // Quit the loop.
+        eventLoop.quit();
+    });
+
+    // Execute the loop.
+    eventLoop.exec();
+}
+
 void BrowserView::refresh() const
 {
     // Reload the website.