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