]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/commitdiff
Update bookmarks in all windows when modified. https://redmine.stoutner.com/issues...
authorSoren Stoutner <soren@stoutner.com>
Mon, 30 Oct 2023 22:50:12 +0000 (15:50 -0700)
committerSoren Stoutner <soren@stoutner.com>
Mon, 30 Oct 2023 22:50:12 +0000 (15:50 -0700)
src/windows/BrowserWindow.cpp
src/windows/BrowserWindow.h

index 8382e0085806359ba141d0e34cc0d8dba820cddf..b90f132d3704e399b8f3b78d9299b62d13f91220 100644 (file)
@@ -43,6 +43,9 @@
 // Qt toolkit headers.
 #include <QClipboard>
 #include <QContextMenuEvent>
+#include <QDBusConnection>
+#include <QDBusConnectionInterface>
+#include <QDBusMessage>
 #include <QFileDialog>
 #include <QInputDialog>
 #include <QNetworkCookie>
@@ -531,8 +534,8 @@ BrowserWindow::BrowserWindow(bool firstWindow, QString *initialUrlStringPointer)
     // Show the custom bookmark context menu when requested.
     connect(bookmarksToolBarPointer, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showBookmarkContextMenu(const QPoint&)));
 
-    // Populate the bookmarks.
-    populateBookmarks();
+    // Populate the bookmarks in this window.
+    populateBookmarksInThisWindow();
 
     // Populate the UI.
     // This must be done here, because otherwise, if a URL is loaded, like a local file, that does not trigger a call to TabWidget::applyDomainSettings, the UI will not be fully populated.
@@ -571,7 +574,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
                                                                                 tabWidgetPointer->getCurrentTabFavoritIcon(), folderId);
 
             // Update the displayed bookmarks when a new one is added.
-            connect(addBookmarkDialogPointer, SIGNAL(bookmarkAdded()), this, SLOT(populateBookmarks()));
+            connect(addBookmarkDialogPointer, SIGNAL(bookmarkAdded()), this, SLOT(populateBookmarksInAllWindows()));
 
             // Show the dialog.
             addBookmarkDialogPointer->show();
@@ -585,7 +588,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
             AddFolderDialog *addFolderDialogPointer = new AddFolderDialog(tabWidgetPointer->getCurrentTabFavoritIcon(), folderId);
 
             // Update the displayed bookmarks when a folder is added.
-            connect(addFolderDialogPointer, SIGNAL(folderAdded()), this, SLOT(populateBookmarks()));
+            connect(addFolderDialogPointer, SIGNAL(folderAdded()), this, SLOT(populateBookmarksInAllWindows()));
 
             // Show the dialog.
             addFolderDialogPointer->show();
@@ -659,7 +662,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
                 editFolderDialogPointer->show();
 
                 // Process bookmark events.
-                connect(editFolderDialogPointer, SIGNAL(folderSaved()), this, SLOT(populateBookmarks()));
+                connect(editFolderDialogPointer, SIGNAL(folderSaved()), this, SLOT(populateBookmarksInAllWindows()));
             }
         );
 
@@ -718,7 +721,7 @@ void BrowserWindow::addFinalBookmarkFolderMenuActions(QMenu *menuPointer, double
                 BookmarksDatabase::updateFolderContentsDisplayOrder(parentFolderId);
 
                 // Repopulate the bookmarks.
-                populateBookmarks();
+                populateBookmarksInAllWindows();
             }
         }
     );
@@ -811,7 +814,7 @@ void BrowserWindow::editBookmarks() const
     BookmarksDialog *bookmarksDialogPointer = new BookmarksDialog(tabWidgetPointer->getCurrentTabTitle(), tabWidgetPointer->getCurrentTabUrl(), tabWidgetPointer->getCurrentTabFavoritIcon());
 
     // Update the displayed bookmarks when edited.
-    connect(bookmarksDialogPointer, SIGNAL(bookmarkUpdated()), this, SLOT(populateBookmarks()));
+    connect(bookmarksDialogPointer, SIGNAL(bookmarkUpdated()), this, SLOT(populateBookmarksInAllWindows()));
 
     // Show the dialog.
     bookmarksDialogPointer->show();
@@ -996,8 +999,29 @@ void BrowserWindow::newWindow() const
     browserWindowPointer->show();
 }
 
-void BrowserWindow::populateBookmarks()
+void BrowserWindow::populateBookmarksInAllWindows() const
 {
+    // Get a list of all the registered service names.
+    QStringList registeredServiceNames = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
+
+    // Get a list of all the Privacy Browser windows, which will be `com.stoutner.privacybrowser-` with the PID appended.
+    QStringList privacyBrowserServiceNames = registeredServiceNames.filter("com.stoutner.privacybrowser");
+
+    // Repopulate the bookmarks in each window.
+    for (QString privacyBrowserServiceName : privacyBrowserServiceNames)
+    {
+        // Prepare the D-Bus message.
+        QDBusMessage dBusMessage = QDBusMessage::createMethodCall(privacyBrowserServiceName, "/privacybrowser/MainWindow_1", "com.stoutner.privacybrowser.BrowserWindow", "populateBookmarksInThisWindow");
+
+        // Make it so.
+        QDBusConnection::sessionBus().send(dBusMessage);
+    }
+}
+
+void BrowserWindow::populateBookmarksInThisWindow()
+{
+    qDebug() << "Populating bookmarks.";
+
     // Remove all the final bookmark folder menu actions.
     for (QPair<QMenu *, QAction *> *finalBookmarkFolderMenuActionPair : finalBookmarkFolderMenuActionList)
     {
@@ -1311,7 +1335,7 @@ void BrowserWindow::showBookmarkContextMenu(const QPoint &point)
                     editBookmarkDialogPointer->show();
 
                     // Process bookmark events.
-                    connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(populateBookmarks()));
+                    connect(editBookmarkDialogPointer, SIGNAL(bookmarkSaved()), this, SLOT(populateBookmarksInAllWindows()));
                 }
             );
 
@@ -1345,7 +1369,7 @@ void BrowserWindow::showBookmarkContextMenu(const QPoint &point)
                     BookmarksDatabase::updateFolderContentsDisplayOrder(parentFolderId);
 
                     // Repopulate the bookmarks.
-                    populateBookmarks();
+                    populateBookmarksInAllWindows();
                 }
             );
         }
@@ -1660,7 +1684,7 @@ void BrowserWindow::toggleBookmark()
     }
 
     // Repopulate the bookmarks.
-    populateBookmarks();
+    populateBookmarksInAllWindows();
 }
 
 void BrowserWindow::toggleDomStorage() const
index c4e04645371d0f9f86a8f76d4d116cf4102a5bfa..6910f8ae474c968fbbab68a173508fea62690909 100644 (file)
@@ -49,6 +49,10 @@ public:
     // The public variables.
     TabWidget *tabWidgetPointer;
 
+public Q_SLOTS:
+    // The public slots.
+    void populateBookmarksInThisWindow();  // This is public so that the bookmarks can be repopulated over D-Bus when changed by another instance of Privacy Browser.
+
 private Q_SLOTS:
     // The private slots.
     void addOrEditDomainSettings() const;
@@ -67,7 +71,7 @@ private Q_SLOTS:
     void incrementZoom();
     void loadUrlFromLineEdit(const QString &url) const;
     void newWindow() const;
-    void populateBookmarks();
+    void populateBookmarksInAllWindows() const;
     void refresh() const;
     void reloadAndBypassCache() const;
     void showBookmarkContextMenu(const QPoint &point);