]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/windows/BrowserWindow.cpp
Add context menus to the bookmarks toolbar.
[PrivacyBrowserPC.git] / src / windows / BrowserWindow.cpp
index c8c766da4e2586ead8fbfa4bf23f620cf0339f9e..0fdfe26375043dd813985d38200ebf64e36c3570 100644 (file)
@@ -28,6 +28,7 @@
 #include "dialogs/BookmarksDialog.h"
 #include "dialogs/CookiesDialog.h"
 #include "dialogs/DomainSettingsDialog.h"
+#include "dialogs/EditBookmarkDialog.h"
 #include "helpers/SearchEngineHelper.h"
 #include "helpers/UserAgentHelper.h"
 #include "structs/BookmarkStruct.h"
@@ -38,6 +39,8 @@
 #include <KXMLGUIFactory>
 
 // Qt toolkit headers.
+#include <QClipboard>
+#include <QContextMenuEvent>
 #include <QFileDialog>
 #include <QInputDialog>
 #include <QNetworkCookie>
@@ -47,7 +50,7 @@
 #include <QWebEngineFindTextResult>
 
 // Construct the class.
-BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow()
+BrowserWindow::BrowserWindow(bool firstWindow, QString *initialUrlStringPointer) : KXmlGuiWindow()
 {
     // Initialize the variables.
     javaScriptEnabled = false;
@@ -73,7 +76,7 @@ BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow()
     QAction *backActionPointer = KStandardAction::back(this, SLOT(back()), actionCollectionPointer);
     QAction *forwardActionPointer = KStandardAction::forward(this, SLOT(forward()), actionCollectionPointer);
     KStandardAction::home(this, SLOT(home()), actionCollectionPointer);
-    KStandardAction::addBookmark(this, SLOT(addBookmark()), actionCollectionPointer);
+    KStandardAction::addBookmark(this, SLOT(showAddBookmarkDialog()), actionCollectionPointer);
     QAction *editBookmarksActionPointer = KStandardAction::editBookmarks(this, SLOT(editBookmarks()), actionCollectionPointer);
     KStandardAction::preferences(this, SLOT(showSettingsDialog()), actionCollectionPointer);
     KStandardAction::find(this, SLOT(showFindTextActions()), actionCollectionPointer);
@@ -509,6 +512,12 @@ BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow()
     bookmarksMenuCurrentActionList = QList<QAction*>();
     bookmarksToolBarCurrentActionList = QList<QAction*>();
 
+    // Set the bookmarks toolbar context menu policy.
+    bookmarksToolBarPointer->setContextMenuPolicy(Qt::CustomContextMenu);
+
+    // Show the custom bookmark context menu when requested.
+    connect(bookmarksToolBarPointer, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showBookmarkContextMenu(const QPoint&)));
+
     // Populate the bookmarks.
     populateBookmarks();
 
@@ -520,18 +529,17 @@ BrowserWindow::BrowserWindow(bool firstWindow) : KXmlGuiWindow()
     updateUserAgentActions(UserAgentHelper::getUserAgentFromDatabaseName(Settings::userAgent()), true);
     updateZoomActions(Settings::zoomFactor());
 
-    // Load the initial website if this is the first window.
-    if (firstWindow)
+    // Populate the first tab.
+    if (firstWindow)  // This is the first window.
+    {
+        // Load the initial website.
         tabWidgetPointer->loadInitialWebsite();
-}
-
-void BrowserWindow::addBookmark() const
-{
-    // Instantiate an add bookmark dialog.
-    AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(), tabWidgetPointer->getCurrentTabFavoritIcon());
-
-    // Show the dialog.
-    addBookmarkDialogPointer->show();
+    }
+    else if (initialUrlStringPointer)  // An initial URL was specified.
+    {
+        // Load the initial URL.
+        tabWidgetPointer->loadUrlFromLineEdit(*initialUrlStringPointer);
+    }
 }
 
 void BrowserWindow::addOrEditDomainSettings() const
@@ -598,7 +606,7 @@ void BrowserWindow::decrementZoom()
 void BrowserWindow::editBookmarks() const
 {
     // Instantiate an edit bookmarks dialog.
-    BookmarksDialog *bookmarksDialogPointer = new BookmarksDialog();
+    BookmarksDialog *bookmarksDialogPointer = new BookmarksDialog(tabWidgetPointer->getCurrentTabFavoritIcon());
 
     // Update the displayed bookmarks when edited.
     connect(bookmarksDialogPointer, SIGNAL(bookmarkUpdated()), this, SLOT(populateBookmarks()));
@@ -779,8 +787,11 @@ void BrowserWindow::loadUrlFromLineEdit(const QString &url) const
 
 void BrowserWindow::newWindow() const
 {
-    // Display a new instance of Privacy Browser.
-    (new BrowserWindow)->show();
+    // Create a new browser window.
+    BrowserWindow *browserWindowPointer = new BrowserWindow();
+
+    // Show the new browser window.
+    browserWindowPointer->show();
 }
 
 void BrowserWindow::populateBookmarks()
@@ -834,6 +845,9 @@ void BrowserWindow::populateBookmarks()
             }
         );
 
+        // Add the bookmark database ID to the toolbar action.
+        toolBarBookmarkActionPointer->setData(bookmarkStruct.id);
+
         // Add the actions to the current bookmarks lists.
         bookmarksMenuCurrentActionList.append(menuBookmarkActionPointer);
         bookmarksToolBarCurrentActionList.append(toolBarBookmarkActionPointer);
@@ -868,6 +882,138 @@ void BrowserWindow::reloadAndBypassCache() const
     tabWidgetPointer->refresh();
 }
 
+void BrowserWindow::showAddBookmarkDialog() const
+{
+    // Instantiate an add bookmark dialog.
+    AddBookmarkDialog *addBookmarkDialogPointer = new AddBookmarkDialog(tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(), tabWidgetPointer->getCurrentTabFavoritIcon());
+
+    // Update the displayed bookmarks when a new one is added.
+    connect(addBookmarkDialogPointer, SIGNAL(bookmarkAdded()), this, SLOT(populateBookmarks()));
+
+    // Show the dialog.
+    addBookmarkDialogPointer->show();
+}
+
+void BrowserWindow::showBookmarkContextMenu(const QPoint &point)
+{
+    // Get the bookmark action.
+    QAction *bookmarkActionPointer = bookmarksToolBarPointer->actionAt(point);
+
+    // Check to see if an bookmark was clicked.
+    if (bookmarkActionPointer)  // A bookmark was clicked.
+    {
+        // Create a bookmark context menu.
+        QMenu *bookmarkContextMenuPointer = new QMenu();
+
+        // Get the bookmark ID from the action.
+        int bookmarkId = bookmarkActionPointer->data().toInt();
+
+        // Add the open in new tab action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("tab-new")), i18nc("Open bookmark in new tab context menu entry", "Open in New Tab"), [=]
+            {
+                // Get the bookmark.
+                BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+
+                // Open the bookmark in a new tab.  `true` removes the URL line edit focus, `false` does not load a background tab.
+                tabWidgetPointer->addTab(true, false, bookmarkStructPointer->bookmarkUrl);
+            }
+        );
+
+        // Add the open in background tab action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("tab-new")), i18nc("Open bookmark in background tab context menu entry", "Open in Background Tab"), [=]
+            {
+                // Get the bookmark.
+                BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+
+                // Open the bookmark in a new tab.  `true` removes the URL line edit focus, `true` loads a background tab.
+                tabWidgetPointer->addTab(true, true, bookmarkStructPointer->bookmarkUrl);
+            }
+        );
+
+        // Add the open in new window action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("window-new")), i18nc("Open bookmark in new window context menu entry", "Open in New Window"), [=]
+            {
+                // Get the bookmark.
+                BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+
+                // Create a new browser window.
+                BrowserWindow *browserWindowPointer = new BrowserWindow(false, &bookmarkStructPointer->bookmarkUrl);
+
+                // Show the new browser window.
+                browserWindowPointer->show();
+            }
+        );
+
+        // Add a separator.
+        bookmarkContextMenuPointer->addSeparator();
+
+        // Add the edit action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("edit-entry")), i18nc("Edit bookmark context menu entry", "Edit"), [=]
+            {
+                // Get the current tab favorite icon.
+                QIcon currentTabFavoriteIcon = tabWidgetPointer->getCurrentTabFavoritIcon();
+
+                // Instantiate an edit bookmark dialog.
+                QDialog *editBookmarkDialogPointer = new EditBookmarkDialog(bookmarkId, currentTabFavoriteIcon);
+
+                // Show the dialog.
+                editBookmarkDialogPointer->show();
+
+                // Process bookmark events.
+                connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(populateBookmarks()));
+            }
+        );
+
+        // Add the copy URL action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("edit-copy")), i18nc("Copy bookmark url context menu entry", "Copy URL"), [=]
+            {
+                // Get the bookmark.
+                BookmarkStruct *bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+
+                // Get a handle for the clipboard.
+                QClipboard *clipboard = QGuiApplication::clipboard();
+
+                // Place the URL on the keyboard.
+                clipboard->setText(bookmarkStructPointer->bookmarkUrl);
+            }
+        );
+
+        // Add a separator.
+        bookmarkContextMenuPointer->addSeparator();
+
+        // Add the delete action to the menu.
+        bookmarkContextMenuPointer->addAction(QIcon::fromTheme(QLatin1String("delete")), i18nc("Delete bookmark context menu entry", "Delete"), [=]
+            {
+                // Delete the bookmark.
+                BookmarksDatabase::deleteBookmark(bookmarkId);
+
+                // Repopulate the bookmarks.
+                populateBookmarks();
+            }
+        );
+
+        // Delete the menu from memory when it is closed.
+        bookmarkContextMenuPointer->setAttribute(Qt::WA_DeleteOnClose);
+
+        // Display the context menu.
+        bookmarkContextMenuPointer->popup(bookmarksToolBarPointer->mapToGlobal(point));
+    }
+    else  // The toolbar background was clicked.
+    {
+        // Temporarily set the context menu policy to the default.
+        bookmarksToolBarPointer->setContextMenuPolicy(Qt::DefaultContextMenu);
+
+        // Create a context menu event with the same position.
+        QContextMenuEvent *contextMenuEventPointer = new QContextMenuEvent(QContextMenuEvent::Mouse, point);
+
+        // Send the context menu event to the toolbar.
+        QCoreApplication::sendEvent(bookmarksToolBarPointer, contextMenuEventPointer);
+
+        // Reset the context menu policy.
+        bookmarksToolBarPointer->setContextMenuPolicy(Qt::CustomContextMenu);
+    }
+}
+
 void BrowserWindow::showCookiesDialog()
 {
     // Remove the focus from the URL line edit.