]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/EditBookmarkDialog.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[PrivacyBrowserPC.git] / src / dialogs / EditBookmarkDialog.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 "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(QWidget *parentWidgetPointer, const int databaseId, QIcon &currentWebsiteFavoriteIcon) : QDialog(parentWidgetPointer), 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 bookmark URL line edit.
100     bookmarkUrlLineEditPointer->setCursorPosition(0);
101
102     // Focus the bookmark name line edit.
103     bookmarkNameLineEditPointer->setFocus();
104
105     // Connect the buttons.
106     connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
107     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(save()));
108     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
109
110     // Update the UI when the line edits change.
111     connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
112     connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
113
114     // Set the initial UI status.
115     updateUi();
116 }
117
118 void EditBookmarkDialog::browse()
119 {
120     // Get an image file string from the user.
121     QString imageFileString = QFileDialog::getOpenFileName(this, i18nc("The browse for favorite icon dialog header", "Favorite Icon Image"), QDir::homePath(),
122                               i18nc("The browse for image files filter", "Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg(*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files(*)"));
123
124
125     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
126     if (!imageFileString.isEmpty())
127     {
128         // Set the custom favorite icon.
129         customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
130
131         // Check the custom favorite icon radio button.
132         customFavoriteIconRadioButtonPointer->setChecked(true);
133     }
134 }
135
136 void EditBookmarkDialog::save()
137 {
138     // Get the selected folders list.
139     QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
140
141     // Get the selected folder.
142     QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
143
144     // Get the parent folder ID.
145     double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
146
147     // Get the original display order.
148     int displayOrder = bookmarkStructPointer->displayOrder;
149
150     // Get the new display order if the parent folder has changed.
151     if (parentFolderId != bookmarkStructPointer->parentFolderId)
152         displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
153
154     // Create a favorite icon.
155     QIcon favoriteIcon;
156
157     // Get the favorite icon.
158     if (currentFavoriteIconRadioButtonPointer->isChecked())  // The current favorite icon is checked.
159         favoriteIcon = currentFavoriteIconRadioButtonPointer->icon();
160     else if (currentWebsiteFavoriteIconRadioButtonPointer->isChecked())  // The current website favorite icon is checked.
161         favoriteIcon = currentWebsiteFavoriteIconRadioButtonPointer->icon();
162     else  // The custom favorite icon is checked.
163         favoriteIcon = customFavoriteIconRadioButtonPointer->icon();
164
165     // Create a bookmark struct.
166     BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
167
168     // Populate the bookmark struct.
169     updatedBookmarkStructPointer->databaseId = bookmarkDatabaseId;
170     updatedBookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
171     updatedBookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
172     updatedBookmarkStructPointer->parentFolderId = parentFolderId;
173     updatedBookmarkStructPointer->displayOrder = displayOrder;
174     updatedBookmarkStructPointer->favoriteIcon = favoriteIcon;
175
176     // Update the bookmark.
177     BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
178
179     // Update the display order of all the items in the previous folder.
180     BookmarksDatabase::updateFolderContentsDisplayOrder(bookmarkStructPointer->parentFolderId);
181
182     // Emit the bookmark saved signal.
183     emit bookmarkSaved();
184
185     // Close the dialog.
186     close();
187 }
188
189 void EditBookmarkDialog::updateUi()
190 {
191     // Determine if both line edits are populated.
192     if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty())  // At least one of the line edits is empty.
193     {
194         // Disable the save button.
195         saveButtonPointer->setEnabled(false);
196     }
197     else  // Both of the line edits are populated.
198     {
199         // Enable the save button.
200         saveButtonPointer->setEnabled(true);
201     }
202 }