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