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