]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/BookmarksDialog.cpp
Partial bookmark implementation. https://redmine.stoutner.com/issues/968
[PrivacyBrowserPC.git] / src / dialogs / BookmarksDialog.cpp
1 /*
2  * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 // Application headers.
21 #include "BookmarksDialog.h"
22 #include "ui_BookmarksDialog.h"
23 #include "databases/BookmarksDatabase.h"
24
25 // KDE Frameworks headers.
26 #include <KLocalizedString>
27
28 // Qt toolkit headers.
29 #include <QDebug>
30 #include <QStandardItemModel>
31 #include <QTreeView>
32
33 // Construct the class.
34 BookmarksDialog::BookmarksDialog() : QDialog(nullptr)
35 {
36     // Set the dialog window title.
37     setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks"));
38
39     // Set the window modality.
40     setWindowModality(Qt::WindowModality::ApplicationModal);
41
42     // Instantiate the bookmarks settings dialog UI.
43     Ui::BookmarksDialog bookmarksDialogUi;
44
45     // Setup the UI.
46     bookmarksDialogUi.setupUi(this);
47
48     // Get the list of bookmarks.
49     std::list<BookmarkStruct> *bookmarksListPointer = BookmarksDatabase::getBookmarks();
50
51     // Get a handle for the tree view.
52     QTreeView *treeViewPointer = bookmarksDialogUi.treeView;
53
54     // Initialize the tree model.
55     QStandardItemModel *treeModelPointer = new QStandardItemModel();
56
57     // Set the column count.
58     treeModelPointer->setColumnCount(3);
59
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"));
63
64     // Populate the bookmarks tree view.
65     for (BookmarkStruct bookmarkStruct : *bookmarksListPointer)
66     {
67         // Create a list for the bookmark items.
68         QList<QStandardItem*> bookmarkItemList;
69
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);
74
75         // Populate the cookie standard item list.
76         bookmarkItemList.append(idItemPointer);
77         bookmarkItemList.append(nameItemPointer);
78         bookmarkItemList.append(urlItemPointer);
79
80         // Add the cookie to the tree.
81         treeModelPointer->appendRow(bookmarkItemList);
82     }
83
84     // Auto resize the headers.
85     treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
86
87     // Indicate that all the rows are the same height, which improves performance.
88     treeViewPointer->setUniformRowHeights(true);
89
90     // Set the tree model.
91     treeViewPointer->setModel(treeModelPointer);
92
93     // Hide the database ID column.
94     treeViewPointer->setColumnHidden(0, true);
95
96     // Get handles for the buttons.
97     QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox;
98
99     // Connect the buttons.
100     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
101
102     // Monitor editing of data in the tree model.
103     connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*)));
104 }
105
106 void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem)
107 {
108     // Get the model index of the modified item.
109     QModelIndex modifiedItemModelIndex = modifiedStandardItem->index();
110
111     // Get the model index of the database ID.
112     QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0);
113
114     // Get the database ID.
115     int databaseId = databaseIdModelIndex.data().toInt();
116
117     // Check to see if the bookmark name or the URL was edited.
118     if (modifiedStandardItem->column() == 1)  // The bookmark name was edited.
119     {
120         // Update the bookmark name.
121         BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text());
122     }
123     else  // The bookmark URL was edited.
124     {
125         // Update the bookmark URL.
126         BookmarksDatabase::updateBookmarkUrl(databaseId, modifiedStandardItem->text());
127     }
128
129     // Emit the bookmark updated signal.
130     emit bookmarkUpdated();
131 }