]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddBookmarkDialog.cpp
Add bookmark folders.
[PrivacyBrowserPC.git] / src / dialogs / AddBookmarkDialog.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 "AddBookmarkDialog.h"
22 #include "ui_AddBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
24
25 // KDE Framework headers.
26 #include <KLocalizedString>
27
28 // Qt toolkit headers.
29 #include <QFileDialog>
30 #include <QPushButton>
31
32 // Construct the class.
33 AddBookmarkDialog::AddBookmarkDialog(const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon, const double parentFolderId) : QDialog(nullptr)
34 {
35     // Set the window title.
36     setWindowTitle(i18nc("The add bookmark dialog window title.", "Add Bookmark"));
37
38     // Set the window modality.
39     setWindowModality(Qt::WindowModality::ApplicationModal);
40
41     // Instantiate the add bookmark dialog UI.
42     Ui::AddBookmarkDialog addBookmarkDialogUi;
43
44     // Setup the UI.
45     addBookmarkDialogUi.setupUi(this);
46
47     // Get handles for the widgets.
48     defaultFavoriteIconRadioButtonPointer = addBookmarkDialogUi.defaultFavoriteIconRadioButton;
49     customFavoriteIconRadioButtonPointer = addBookmarkDialogUi.customFavoriteIconRadioButton;
50     parentFolderTreeWidgetPointer = addBookmarkDialogUi.parentFolderTreeWidget;
51     bookmarkNameLineEditPointer = addBookmarkDialogUi.bookmarkNameLineEdit;
52     bookmarkUrlLineEditPointer = addBookmarkDialogUi.bookmarkUrlLineEdit;
53     QPushButton *browseButtonPointer = addBookmarkDialogUi.browseButton;
54     QDialogButtonBox *dialogButtonBoxPointer = addBookmarkDialogUi.dialogButtonBox;
55
56     // Set the default favorite icon.
57     defaultFavoriteIconRadioButtonPointer->setIcon(favoriteIcon);
58
59     // Instantiate a folder helper.
60     folderHelperPointer = new FolderHelper();
61
62     // Set the parent folder tree widget column count.
63     parentFolderTreeWidgetPointer->setColumnCount(2);
64
65     // Hide the second column.
66     parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
67
68     // Set the column header.
69     parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
70
71     // Create a bookmarks tree widget item.
72     QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
73
74     // Populate the bookmarks tree widget item.
75     bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_NAME_COLUMN, i18nc("The bookmarks root tree widget name", "Bookmarks"));
76     bookmarksTreeWidgetItemPointer->setIcon(folderHelperPointer->FOLDER_NAME_COLUMN, QIcon::fromTheme("bookmarks"));
77     bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_ID_COLUMN, QLatin1String("0"));
78
79     // Add the bookmarks tree widget item to the root of the tree.
80     parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
81
82     // Select the root bookmarks folder if it is the initial parent folder.
83     if (parentFolderId == 0)
84         bookmarksTreeWidgetItemPointer->setSelected(true);
85
86     // Populate the subfolders.
87     folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId);
88
89     // Open all the folders.
90     parentFolderTreeWidgetPointer->expandAll();
91
92     // Populate the line edits.
93     bookmarkNameLineEditPointer->setText(bookmarkName);
94     bookmarkUrlLineEditPointer->setText(bookmarkUrl);
95
96     // Scroll to the beginning of the line edits.
97     bookmarkNameLineEditPointer->setCursorPosition(0);
98     bookmarkUrlLineEditPointer->setCursorPosition(0);
99
100     // Add buttons to the dialog button box.
101     addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
102
103     // Set the button icons.
104     addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
105
106     // Connect the buttons.
107     connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
108     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark()));
109     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
110
111     // Update the UI when the line edits change.
112     connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
113     connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
114
115     // Set the initial UI status.
116     updateUi();
117 }
118
119 void AddBookmarkDialog::addBookmark()
120 {
121     // Get the selected folders list.
122     QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
123
124     // Get the selected folder.
125     QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
126
127     // Get the favorite icon.
128     QIcon favoriteIcon = defaultFavoriteIconRadioButtonPointer->isChecked() ? defaultFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
129
130     // Create a bookmark struct.
131     BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
132
133     // Populate the bookmark struct.
134     bookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
135     bookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
136     bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
137     bookmarkStructPointer->favoriteIcon = favoriteIcon;
138
139     // Add the bookmark.
140     BookmarksDatabase::addBookmark(bookmarkStructPointer);
141
142     // Update the list of bookmarks in the menu and toolbar.
143     emit bookmarkAdded();
144
145     // Close the dialog.
146     close();
147 }
148
149 void AddBookmarkDialog::browse()
150 {
151     // Get an image file string from the user.
152     QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
153                                                            tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
154
155     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
156     if (!imageFileString.isEmpty())
157     {
158         // Set the custom favorite icon.
159         customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
160
161         // Check the custom favorite icon radio button.
162         customFavoriteIconRadioButtonPointer->setChecked(true);
163     }
164 }
165
166 void AddBookmarkDialog::updateUi()
167 {
168     // Determine if both line edits are populated.
169     if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty())  // At least one of the line edits is empty.
170     {
171         // Disable the add button.
172         addButtonPointer->setEnabled(false);
173     }
174     else  // Both of the line edits are populated.
175     {
176         // Enable the add button.
177         addButtonPointer->setEnabled(true);
178     }
179 }