]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/windows/BrowserWindow.cpp
Implement tabbed browsing.
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
index e44357f25c042bb264ac7384baa00b6ab4b3793c..8d2417179119626908faa1576bb8df5bd41643fd 100644 (file)
@@ -22,7 +22,6 @@
 #include "Settings.h"
 #include "ui_SettingsPrivacy.h"
 #include "ui_SettingsGeneral.h"
-#include "databases/CookiesDatabase.h"
 #include "dialogs/CookiesDialog.h"
 #include "dialogs/DomainSettingsDialog.h"
 #include "helpers/SearchEngineHelper.h"
 BrowserWindow::BrowserWindow() : KXmlGuiWindow()
 {
     // Initialize the variables.
-    cookieListPointer = new std::list<QNetworkCookie>;
     javaScriptEnabled = false;
     localStorageEnabled = false;
 
-    // Instantiate the main view pointer.
-    browserViewPointer = new BrowserView(this);
+    // Instantiate the privacy tab widget pointer.
+    tabWidgetPointer = new TabWidget(this);
 
-    // Set the main view as the central widget.
-    setCentralWidget(browserViewPointer);
+    // Set the privacy tab widget as the central widget.
+    setCentralWidget(tabWidgetPointer);
 
     // Get a handle for the action collection.
     KActionCollection *actionCollectionPointer = this->actionCollection();
 
     // Add the standard actions.
     KStandardAction::openNew(this, SLOT(fileNew()), actionCollectionPointer);
-    KStandardAction::print(browserViewPointer, SLOT(print()), actionCollectionPointer);
-    KStandardAction::printPreview(browserViewPointer, SLOT(printPreview()), actionCollectionPointer);
+    KStandardAction::print(tabWidgetPointer, SLOT(print()), actionCollectionPointer);
+    KStandardAction::printPreview(tabWidgetPointer, SLOT(printPreview()), actionCollectionPointer);
     KStandardAction::quit(qApp, SLOT(closeAllWindows()), actionCollectionPointer);
     KStandardAction::redisplay(this, SLOT(refresh()), actionCollectionPointer);
     fullScreenActionPointer = KStandardAction::fullScreen(this, SLOT(toggleFullScreen()), this, actionCollectionPointer);
@@ -152,7 +150,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     searchEngineBingActionPointer->setText(i18nc("Search engine", "Bing"));
     searchEngineYahooActionPointer->setText(i18nc("Search engine", "Yahoo"));
     domainSettingsActionPointer->setText(i18nc("Domain Settings action", "Domain Settings"));
-    cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
+    cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", 0));
     javaScriptActionPointer->setText(i18nc("JavaScript action", "JavaScript"));
     localStorageActionPointer->setText(i18nc("The Local Storage action", "Local Storage"));
     domStorageActionPointer->setText(i18nc("DOM Storage action", "DOM Storage"));
@@ -180,13 +178,16 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     domStorageActionPointer->setIcon(QIcon::fromTheme(QStringLiteral("view-web-browser-dom-tree")));
 
     // Update the on-the-fly menus.
-    connect(browserViewPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
-    connect(browserViewPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
-    connect(browserViewPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
+    connect(tabWidgetPointer, SIGNAL(updateUserAgentActions(QString, bool)), this, SLOT(updateUserAgentActions(QString, bool)));
+    connect(tabWidgetPointer, SIGNAL(updateZoomFactorAction(double)), this, SLOT(updateZoomFactorAction(double)));
+    connect(tabWidgetPointer, SIGNAL(updateSearchEngineActions(QString, bool)), this, SLOT(updateSearchEngineActions(QString, bool)));
 
     // Apply the on-the-fly settings when selected.
-    connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
-    connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), browserViewPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
+    connect(userAgentActionGroupPointer, SIGNAL(triggered(QAction*)), tabWidgetPointer, SLOT(applyOnTheFlyUserAgent(QAction*)));
+    connect(searchEngineActionGroupPointer, SIGNAL(triggered(QAction*)), tabWidgetPointer, SLOT(applyOnTheFlySearchEngine(QAction*)));
+
+    // Process cookie changes.
+    connect(tabWidgetPointer, SIGNAL(updateCookiesAction(int)), this, SLOT(updateCookiesAction(int)));
 
     // Display dialogs.
     connect(zoomFactorActionPointer, SIGNAL(triggered()), this, SLOT(getZoomFactorFromUser()));
@@ -199,11 +200,11 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     connect(domStorageActionPointer, SIGNAL(triggered()), this, SLOT(toggleDomStorage()));
 
     // Update the URL toolbar actions.
-    connect(browserViewPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
-    connect(browserViewPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
-    connect(browserViewPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
-    connect(browserViewPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
-    connect(browserViewPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
+    connect(tabWidgetPointer, SIGNAL(updateBackAction(bool)), backActionPointer, SLOT(setEnabled(bool)));
+    connect(tabWidgetPointer, SIGNAL(updateForwardAction(bool)), forwardActionPointer, SLOT(setEnabled(bool)));
+    connect(tabWidgetPointer, SIGNAL(updateJavaScriptAction(bool)), this, SLOT(updateJavaScriptAction(bool)));
+    connect(tabWidgetPointer, SIGNAL(updateLocalStorageAction(bool)), this, SLOT(updateLocalStorageAction(bool)));
+    connect(tabWidgetPointer, SIGNAL(updateDomStorageAction(bool)), this, SLOT(updateDomStorageAction(bool)));
 
     // Setup the GUI based on the browser_ui.rc file.
     setupGUI(StandardWindowOption::Default, ("browser_ui.rc"));
@@ -244,7 +245,7 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     connect(urlLineEditPointer, SIGNAL(returnKeyPressed(const QString)), this, SLOT(loadUrlFromLineEdit(const QString)));
 
     // Update the URL line edit on page loads.
-    connect(browserViewPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
+    connect(tabWidgetPointer, SIGNAL(updateUrlLineEdit(QUrl)), this, SLOT(updateUrlLineEdit(QUrl)));
 
     // Get a handle for the status bar.
     QStatusBar *statusBarPointer = statusBar();
@@ -256,14 +257,14 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     statusBarPointer->addPermanentWidget(progressBarPointer);
 
     // Update the status bar with the URL when a link is hovered.
-    connect(browserViewPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
+    connect(tabWidgetPointer, SIGNAL(linkHovered(QString)), statusBarPointer, SLOT(showMessage(QString)));
 
     // Update the progress bar.
-    connect(browserViewPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
-    connect(browserViewPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
+    connect(tabWidgetPointer, SIGNAL(showProgressBar(const int)), this, SLOT(showProgressBar(const int)));
+    connect(tabWidgetPointer, SIGNAL(hideProgressBar()), progressBarPointer, SLOT(hide()));
 
-    // Clear the URL line edit focus when requested.
-    connect(browserViewPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
+    // Update the URL line edit focus.
+    connect(tabWidgetPointer, SIGNAL(clearUrlLineEditFocus()), this, SLOT(clearUrlLineEditFocus()));
 
     // Get the URL line edit palettes.
     noDomainSettingsPalette = urlLineEditPointer->palette();
@@ -273,40 +274,23 @@ BrowserWindow::BrowserWindow() : KXmlGuiWindow()
     domainSettingsPalette.setColor(QPalette::Base, QColor("#C8E6C9"));
 
     // Update the applied palette.
-    connect(browserViewPointer, SIGNAL(updateDomainSettingsIndicator(bool, QString)), this, SLOT(updateDomainSettingsIndicator(bool, QString)));
-
-    // Process cookie changes.
-    connect(browserViewPointer, SIGNAL(addCookie(QNetworkCookie)), this, SLOT(addCookieToList(QNetworkCookie)));
-    connect(browserViewPointer, SIGNAL(removeCookie(QNetworkCookie)), this, SLOT(removeCookieFromList(QNetworkCookie)));
+    connect(tabWidgetPointer, SIGNAL(updateDomainSettingsIndicator(const bool)), this, SLOT(updateDomainSettingsIndicator(const bool)));
 
     // Process full screen requests.
-    connect(browserViewPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
+    connect(tabWidgetPointer, SIGNAL(fullScreenRequested(bool)), this, SLOT(fullScreenRequested(bool)));
 
     // Create keyboard shortcuts.
+    QShortcut *ctrlTShortcutPointer = new QShortcut(QKeySequence(i18nc("The open new tab shortcut.", "Ctrl+t")), this);
     QShortcut *f11ShortcutPointer = new QShortcut(QKeySequence(i18nc("The toggle full screen shortcut.", "F11")), this);
     QShortcut *escapeShortcutPointer = new QShortcut(QKeySequence::Cancel, this);
 
     // Connect the keyboard shortcuts to the actions.
+    connect(ctrlTShortcutPointer, SIGNAL(activated()), tabWidgetPointer, SLOT(addTab()));
     connect(f11ShortcutPointer, SIGNAL(activated()), fullScreenActionPointer, SLOT(trigger()));
     connect(escapeShortcutPointer, SIGNAL(activated()), this, SLOT(escape()));
 
     // Load the initial website.
-    browserViewPointer->loadInitialWebsite();
-}
-
-void BrowserWindow::addCookieToList(const QNetworkCookie &newCookie) const
-{
-    //qDebug() << "Add cookie:  " << newCookie.toRawForm();
-
-    // Add the new cookie to the list.
-    cookieListPointer->push_front(newCookie);
-
-    // Update the action text.
-    cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
-
-    // Update the cookie if it is durable and has new data.
-    if (CookiesDatabase::isUpdate(newCookie))
-        CookiesDatabase::updateCookie(newCookie);
+    tabWidgetPointer->loadInitialWebsite();
 }
 
 void BrowserWindow::addOrEditDomainSettings() const
@@ -317,8 +301,11 @@ void BrowserWindow::addOrEditDomainSettings() const
     // Create the domain settings dialog pointer.
     DomainSettingsDialog *domainSettingsDialogPointer;
 
+    // Get the current domain settings name.
+    QString &currentDomainSettingsName = tabWidgetPointer->getDomainSettingsName();
+
     // Run the commands according to the current domain settings status.
-    if (currentDomainSettingsDomain == "")  // Domain settings are not currently applied.
+    if (currentDomainSettingsName == QStringLiteral(""))  // Domain settings are not currently applied.
     {
         // Instruct the domain settings dialog to add a new domain.
         domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::ADD_DOMAIN, currentUrl.host());
@@ -326,7 +313,7 @@ void BrowserWindow::addOrEditDomainSettings() const
     else  // Domain settings are currently applied.
     {
         // Instruct the domain settings dialog to edit the current domain.
-        domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsDomain);
+        domainSettingsDialogPointer = new DomainSettingsDialog(DomainSettingsDialog::EDIT_DOMAIN, currentDomainSettingsName);
     }
 
     // Set the dialog window title.
@@ -339,7 +326,7 @@ void BrowserWindow::addOrEditDomainSettings() const
     domainSettingsDialogPointer->show();
 
     // Reload the tabs when domain settings are updated.
-    connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
+    connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
 }
 
 void BrowserWindow::back() const
@@ -348,7 +335,7 @@ void BrowserWindow::back() const
     urlLineEditPointer->clearFocus();
 
     // Go back.
-    browserViewPointer->back();
+    tabWidgetPointer->back();
 }
 
 void BrowserWindow::clearUrlLineEditFocus() const
@@ -376,7 +363,7 @@ void BrowserWindow::forward() const
     urlLineEditPointer->clearFocus();
 
     // Go forward.
-    browserViewPointer->forward();
+    tabWidgetPointer->forward();
 }
 
 void BrowserWindow::fullScreenRequested(const bool toggleOn)
@@ -391,6 +378,7 @@ void BrowserWindow::fullScreenRequested(const bool toggleOn)
         menuBar()->setVisible(false);
         navigationToolBarPointer->setVisible(false);
         urlToolBarPointer->setVisible(false);
+        tabWidgetPointer->setTabBarVisible(false);
         statusBar()->setVisible(false);
     }
     else  // Turn full screen mode off.
@@ -402,6 +390,7 @@ void BrowserWindow::fullScreenRequested(const bool toggleOn)
         menuBar()->setVisible(true);
         navigationToolBarPointer->setVisible(true);
         urlToolBarPointer->setVisible(true);
+        tabWidgetPointer->setTabBarVisible(true);
         statusBar()->setVisible(true);
     }
 }
@@ -423,7 +412,7 @@ void BrowserWindow::getZoomFactorFromUser()
         currentZoomFactor = newZoomFactor;
 
         // Set the new zoom factor.
-        browserViewPointer->applyOnTheFlyZoomFactor(newZoomFactor);
+        tabWidgetPointer->applyOnTheFlyZoomFactor(newZoomFactor);
 
         // Update the on-the-fly action text.
         updateZoomFactorAction(newZoomFactor);
@@ -436,7 +425,7 @@ void BrowserWindow::home() const
     urlLineEditPointer->clearFocus();
 
     // Go home.
-    browserViewPointer->home();
+    tabWidgetPointer->home();
 }
 
 void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
@@ -445,7 +434,7 @@ void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
     urlLineEditPointer->clearFocus();
 
     // Load the URL.
-    browserViewPointer->loadUrlFromLineEdit(url);
+    tabWidgetPointer->loadUrlFromLineEdit(url);
 }
 
 void BrowserWindow::refresh() const
@@ -454,18 +443,7 @@ void BrowserWindow::refresh() const
     urlLineEditPointer->clearFocus();
 
     // Refresh the web page.
-    browserViewPointer->refresh();
-}
-
-void BrowserWindow::removeCookieFromList(const QNetworkCookie &cookie) const
-{
-    //qDebug() << "Remove cookie:  " << cookie.toRawForm();
-
-    // Remove the cookie from the list.
-    cookieListPointer->remove(cookie);
-
-    // Update the action text.
-    cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", cookieListPointer->size()));
+    tabWidgetPointer->refresh();
 }
 
 void BrowserWindow::showCookiesDialog()
@@ -474,15 +452,15 @@ void BrowserWindow::showCookiesDialog()
     urlLineEditPointer->clearFocus();
 
     // Instantiate the cookie settings dialog.
-    CookiesDialog *cookiesDialogPointer = new CookiesDialog(cookieListPointer);
+    CookiesDialog *cookiesDialogPointer = new CookiesDialog(tabWidgetPointer->getCookieList());
 
     // Show the dialog.
     cookiesDialogPointer->show();
 
     // Connect the dialog signals.
-    connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), browserViewPointer, SLOT(addCookieToStore(QNetworkCookie)));
-    connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), browserViewPointer, SLOT(deleteAllCookies()));
-    connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), browserViewPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
+    connect(cookiesDialogPointer, SIGNAL(addCookie(QNetworkCookie)), tabWidgetPointer, SLOT(addCookieToStore(QNetworkCookie)));
+    connect(cookiesDialogPointer, SIGNAL(deleteAllCookies()), tabWidgetPointer, SLOT(deleteAllCookies()));
+    connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), tabWidgetPointer, SLOT(deleteCookieFromStore(QNetworkCookie)));
 }
 
 void BrowserWindow::showDownloadLocationBrowseDialog() const
@@ -522,7 +500,7 @@ void BrowserWindow::showDomainSettingsDialog() const
     domainSettingsDialogPointer->show();
 
     // Reload the tabs when domain settings are updated.
-    connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
+    connect(domainSettingsDialogPointer, SIGNAL(domainSettingsUpdated()), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
 }
 
 void BrowserWindow::showProgressBar(const int &progress) const
@@ -590,8 +568,8 @@ void BrowserWindow::showSettingsDialog()
     configDialogPointer->resize(1000, 500);
 
     // Apply the settings when they are updated.
-    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyApplicationSettings()));
-    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), browserViewPointer, SLOT(applyDomainSettingsAndReload()));
+    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyApplicationSettings()));
+    connect(configDialogPointer, SIGNAL(settingsChanged(QString)), tabWidgetPointer, SLOT(applyDomainSettingsAndReload()));
 }
 
 QSize BrowserWindow::sizeHint() const
@@ -606,7 +584,7 @@ void BrowserWindow::toggleDomStorage() const
     urlLineEditPointer->clearFocus();
 
     // Toggle DOM storage.
-    browserViewPointer->toggleDomStorage();
+    tabWidgetPointer->toggleDomStorage();
 }
 
 void BrowserWindow::toggleJavaScript() const
@@ -615,7 +593,7 @@ void BrowserWindow::toggleJavaScript() const
     urlLineEditPointer->clearFocus();
 
     // Toggle JavaScript.
-    browserViewPointer->toggleJavaScript();
+    tabWidgetPointer->toggleJavaScript();
 }
 
 void BrowserWindow::toggleLocalStorage() const
@@ -624,7 +602,7 @@ void BrowserWindow::toggleLocalStorage() const
     urlLineEditPointer->clearFocus();
 
     // Toggle local storage.
-    browserViewPointer->toggleLocalStorage();
+    tabWidgetPointer->toggleLocalStorage();
 }
 
 void BrowserWindow::toggleFullScreen()
@@ -646,6 +624,10 @@ void BrowserWindow::toggleFullScreen()
             urlToolBarPointer->setVisible(false);
         }
 
+        // Hide the tab bar if specified.
+        if (Settings::fullScreenHideTabBar())
+            tabWidgetPointer->setTabBarVisible(false);
+
         // Hide the status bar if specified.
         if (Settings::fullScreenHideStatusBar())
             statusBar()->setVisible(false);
@@ -662,27 +644,33 @@ void BrowserWindow::toggleFullScreen()
         navigationToolBarPointer->setVisible(true);
         urlToolBarPointer->setVisible(true);
 
+        // Show the tab bar.
+        tabWidgetPointer->setTabBarVisible(true);
+
         // Show the status bar.
         statusBar()->setVisible(true);
     }
 }
 
+void BrowserWindow::updateCookiesAction(const int numberOfCookies) const
+{
+    // Update the action text.
+    cookiesActionPointer->setText(i18nc("The Cookies action, which also displays the number of cookies", "Cookies - %1", numberOfCookies));
+}
+
 void BrowserWindow::updateDomStorageAction(const bool &isEnabled) const
 {
     // Set the action checked status.
     domStorageActionPointer->setChecked(isEnabled);
 }
 
-void BrowserWindow::updateDomainSettingsIndicator(const bool &status, const QString &domainSettingsDomain)
+void BrowserWindow::updateDomainSettingsIndicator(const bool status)
 {
     // Set the domain palette according to the status.
     if (status)
         urlLineEditPointer->setPalette(domainSettingsPalette);
     else
         urlLineEditPointer->setPalette(noDomainSettingsPalette);
-
-    // Store the domain.
-    currentDomainSettingsDomain = domainSettingsDomain;
 }
 
 void BrowserWindow::updateJavaScriptAction(const bool &isEnabled)
@@ -823,7 +811,7 @@ void BrowserWindow::updateUserAgentActions(const QString &userAgent, const bool
         // Update the user agent menu action text.
         userAgentMenuActionPointer->setText(i18nc("The main user agent menu action", "User Agent - Privacy Browser"));
     }
-    else if (userAgent == BrowserView::webEngineDefaultUserAgent)  // WebEngine default.
+    else if (userAgent == TabWidget::webEngineDefaultUserAgent)  // WebEngine default.
     {
         // check the WebEngine default user agent action.
         userAgentWebEngineDefaultActionPointer->setChecked(true);
@@ -935,8 +923,15 @@ void BrowserWindow::updateUrlLineEdit(const QUrl &newUrl)
     // Update the URL line edit if it does not have focus.
     if (!urlLineEditPointer->hasFocus())
     {
+        // Get the new URL string.
+        QString newUrlString = newUrl.toString();
+
         // Update the URL line edit.
-        urlLineEditPointer->setText(newUrl.toString());
+        urlLineEditPointer->setText(newUrlString);
+
+        // Set the focus if the new URL is blank.
+        if (newUrlString == QStringLiteral(""))
+            urlLineEditPointer->setFocus();
     }
 
     // Store the current URL.