]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddBookmarkDialog.cpp
Additional fix for notifications on Xfce. https://redmine.stoutner.com/issues/1017
[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 line edits.
98     bookmarkNameLineEditPointer->setCursorPosition(0);
99     bookmarkUrlLineEditPointer->setCursorPosition(0);
100
101     // Add buttons to the dialog button box.
102     addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
103
104     // Set the button icons.
105     addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
106
107     // Connect the buttons.
108     connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
109     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark()));
110     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
111
112     // Update the UI when the line edits change.
113     connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
114     connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
115
116     // Set the initial UI status.
117     updateUi();
118 }
119
120 void AddBookmarkDialog::addBookmark()
121 {
122     // Get the selected folders list.
123     QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
124
125     // Get the selected folder.
126     QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
127
128     // Get the favorite icon.
129     QIcon favoriteIcon = websiteFavoriteIconRadioButtonPointer->isChecked() ? websiteFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
130
131     // Create a bookmark struct.
132     BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
133
134     // Populate the bookmark struct.
135     bookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
136     bookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
137     bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
138     bookmarkStructPointer->favoriteIcon = favoriteIcon;
139
140     // Add the bookmark.
141     BookmarksDatabase::addBookmark(bookmarkStructPointer);
142
143     // Update the list of bookmarks in the menu and toolbar.
144     emit bookmarkAdded();
145
146     // Close the dialog.
147     close();
148 }
149
150 void AddBookmarkDialog::browse()
151 {
152     // Get an image file string from the user.
153     QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
154                                                            tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
155
156     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
157     if (!imageFileString.isEmpty())
158     {
159         // Set the custom favorite icon.
160         customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
161
162         // Check the custom favorite icon radio button.
163         customFavoriteIconRadioButtonPointer->setChecked(true);
164     }
165 }
166
167 void AddBookmarkDialog::updateUi()
168 {
169     // Determine if both line edits are populated.
170     if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty())  // At least one of the line edits is empty.
171     {
172         // Disable the add button.
173         addButtonPointer->setEnabled(false);
174     }
175     else  // Both of the line edits are populated.
176     {
177         // Enable the add button.
178         addButtonPointer->setEnabled(true);
179     }
180 }