2 * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
6 * Privacy Browser PC is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser PC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser PC. If not, see <http://www.gnu.org/licenses/>.
20 // Application headers.
21 #include "BookmarksDialog.h"
22 #include "ui_BookmarksDialog.h"
23 #include "databases/BookmarksDatabase.h"
25 // KDE Frameworks headers.
26 #include <KLocalizedString>
28 // Qt toolkit headers.
30 #include <QStandardItemModel>
33 // Construct the class.
34 BookmarksDialog::BookmarksDialog() : QDialog(nullptr)
36 // Set the dialog window title.
37 setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks"));
39 // Set the window modality.
40 setWindowModality(Qt::WindowModality::ApplicationModal);
42 // Instantiate the bookmarks settings dialog UI.
43 Ui::BookmarksDialog bookmarksDialogUi;
46 bookmarksDialogUi.setupUi(this);
48 // Get the list of bookmarks.
49 std::list<BookmarkStruct> *bookmarksListPointer = BookmarksDatabase::getBookmarks();
51 // Get a handle for the tree view.
52 QTreeView *treeViewPointer = bookmarksDialogUi.treeView;
54 // Initialize the tree model.
55 QStandardItemModel *treeModelPointer = new QStandardItemModel();
57 // Set the column count.
58 treeModelPointer->setColumnCount(3);
60 // Set the tree header data. The first column is the database ID, which is not displayed.
61 treeModelPointer->setHeaderData(1, Qt::Horizontal, i18nc("The bookmark Name header.", "Name"));
62 treeModelPointer->setHeaderData(2, Qt::Horizontal, i18nc("The bookmark URL header.", "URL"));
64 // Populate the bookmarks tree view.
65 for (BookmarkStruct bookmarkStruct : *bookmarksListPointer)
67 // Create a list for the bookmark items.
68 QList<QStandardItem*> bookmarkItemList;
70 // Create the bookmark items.
71 QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id));
72 QStandardItem *nameItemPointer = new QStandardItem(bookmarkStruct.favoriteIcon, bookmarkStruct.bookmarkName);
73 QStandardItem *urlItemPointer = new QStandardItem(bookmarkStruct.bookmarkUrl);
75 // Populate the cookie standard item list.
76 bookmarkItemList.append(idItemPointer);
77 bookmarkItemList.append(nameItemPointer);
78 bookmarkItemList.append(urlItemPointer);
80 // Add the cookie to the tree.
81 treeModelPointer->appendRow(bookmarkItemList);
84 // Auto resize the headers.
85 treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
87 // Indicate that all the rows are the same height, which improves performance.
88 treeViewPointer->setUniformRowHeights(true);
90 // Set the tree model.
91 treeViewPointer->setModel(treeModelPointer);
93 // Hide the database ID column.
94 treeViewPointer->setColumnHidden(0, true);
96 // Get handles for the buttons.
97 QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox;
99 // Connect the buttons.
100 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
102 // Monitor editing of data in the tree model.
103 connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*)));
106 void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem)
108 // Get the model index of the modified item.
109 QModelIndex modifiedItemModelIndex = modifiedStandardItem->index();
111 // Get the model index of the database ID.
112 QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0);
114 // Get the database ID.
115 int databaseId = databaseIdModelIndex.data().toInt();
117 // Check to see if the bookmark name or the URL was edited.
118 if (modifiedStandardItem->column() == 1) // The bookmark name was edited.
120 // Update the bookmark name.
121 BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text());
123 else // The bookmark URL was edited.
125 // Update the bookmark URL.
126 BookmarksDatabase::updateBookmarkUrl(databaseId, modifiedStandardItem->text());
129 // Emit the bookmark updated signal.
130 emit bookmarkUpdated();