]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/BookmarksDialog.cpp
Partial bookmark implementation. https://redmine.stoutner.com/issues/968
[PrivacyBrowserPC.git] / src / dialogs / BookmarksDialog.cpp
diff --git a/src/dialogs/BookmarksDialog.cpp b/src/dialogs/BookmarksDialog.cpp
new file mode 100644 (file)
index 0000000..509fade
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ *
+ * Privacy Browser PC is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser PC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Application headers.
+#include "BookmarksDialog.h"
+#include "ui_BookmarksDialog.h"
+#include "databases/BookmarksDatabase.h"
+
+// KDE Frameworks headers.
+#include <KLocalizedString>
+
+// Qt toolkit headers.
+#include <QDebug>
+#include <QStandardItemModel>
+#include <QTreeView>
+
+// Construct the class.
+BookmarksDialog::BookmarksDialog() : QDialog(nullptr)
+{
+    // Set the dialog window title.
+    setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks"));
+
+    // Set the window modality.
+    setWindowModality(Qt::WindowModality::ApplicationModal);
+
+    // Instantiate the bookmarks settings dialog UI.
+    Ui::BookmarksDialog bookmarksDialogUi;
+
+    // Setup the UI.
+    bookmarksDialogUi.setupUi(this);
+
+    // Get the list of bookmarks.
+    std::list<BookmarkStruct> *bookmarksListPointer = BookmarksDatabase::getBookmarks();
+
+    // Get a handle for the tree view.
+    QTreeView *treeViewPointer = bookmarksDialogUi.treeView;
+
+    // Initialize the tree model.
+    QStandardItemModel *treeModelPointer = new QStandardItemModel();
+
+    // Set the column count.
+    treeModelPointer->setColumnCount(3);
+
+    // Set the tree header data.  The first column is the database ID, which is not displayed.
+    treeModelPointer->setHeaderData(1, Qt::Horizontal, i18nc("The bookmark Name header.", "Name"));
+    treeModelPointer->setHeaderData(2, Qt::Horizontal, i18nc("The bookmark URL header.", "URL"));
+
+    // Populate the bookmarks tree view.
+    for (BookmarkStruct bookmarkStruct : *bookmarksListPointer)
+    {
+        // Create a list for the bookmark items.
+        QList<QStandardItem*> bookmarkItemList;
+
+        // Create the bookmark items.
+        QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id));
+        QStandardItem *nameItemPointer = new QStandardItem(bookmarkStruct.favoriteIcon, bookmarkStruct.bookmarkName);
+        QStandardItem *urlItemPointer = new QStandardItem(bookmarkStruct.bookmarkUrl);
+
+        // Populate the cookie standard item list.
+        bookmarkItemList.append(idItemPointer);
+        bookmarkItemList.append(nameItemPointer);
+        bookmarkItemList.append(urlItemPointer);
+
+        // Add the cookie to the tree.
+        treeModelPointer->appendRow(bookmarkItemList);
+    }
+
+    // Auto resize the headers.
+    treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
+
+    // Indicate that all the rows are the same height, which improves performance.
+    treeViewPointer->setUniformRowHeights(true);
+
+    // Set the tree model.
+    treeViewPointer->setModel(treeModelPointer);
+
+    // Hide the database ID column.
+    treeViewPointer->setColumnHidden(0, true);
+
+    // Get handles for the buttons.
+    QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox;
+
+    // Connect the buttons.
+    connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+
+    // Monitor editing of data in the tree model.
+    connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*)));
+}
+
+void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem)
+{
+    // Get the model index of the modified item.
+    QModelIndex modifiedItemModelIndex = modifiedStandardItem->index();
+
+    // Get the model index of the database ID.
+    QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0);
+
+    // Get the database ID.
+    int databaseId = databaseIdModelIndex.data().toInt();
+
+    // Check to see if the bookmark name or the URL was edited.
+    if (modifiedStandardItem->column() == 1)  // The bookmark name was edited.
+    {
+        // Update the bookmark name.
+        BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text());
+    }
+    else  // The bookmark URL was edited.
+    {
+        // Update the bookmark URL.
+        BookmarksDatabase::updateBookmarkUrl(databaseId, modifiedStandardItem->text());
+    }
+
+    // Emit the bookmark updated signal.
+    emit bookmarkUpdated();
+}