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