2 * Copyright 2023-2024 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
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.
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.
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/>.
20 // Application headers.
21 #include "EditBookmarkDialog.h"
22 #include "ui_EditBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
25 // Qt toolkit headers.
26 #include <QFileDialog>
28 // Construct the class.
29 EditBookmarkDialog::EditBookmarkDialog(QWidget *parentWidgetPointer, const int databaseId, QIcon ¤tWebsiteFavoriteIcon) : QDialog(parentWidgetPointer), bookmarkDatabaseId(databaseId)
31 // Set the window title.
32 setWindowTitle(i18nc("The edit bookmark dialog window title.", "Edit Bookmark"));
34 // Set the window modality.
35 setWindowModality(Qt::WindowModality::ApplicationModal);
37 // Instantiate the edit bookmark dialog UI.
38 Ui::EditBookmarkDialog editBookmarkDialogUi;
41 editBookmarkDialogUi.setupUi(this);
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);
54 // Get the bookmark struct.
55 bookmarkStructPointer = BookmarksDatabase::getBookmark(databaseId);
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"))));
62 // Instantiate a folder helper.
63 folderHelperPointer = new FolderHelper();
65 // Set the parent folder tree widget column count.
66 parentFolderTreeWidgetPointer->setColumnCount(2);
68 // Hide the second column.
69 parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
71 // Set the column header.
72 parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
74 // Create a bookmarks tree widget item.
75 QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
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"));
82 // Add the bookmarks tree widget item to the root of the tree.
83 parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
85 // Select the root bookmarks folder if it is the initial parent folder.
86 if (bookmarkStructPointer->parentFolderId == 0)
87 bookmarksTreeWidgetItemPointer->setSelected(true);
89 // Populate the subfolders.
90 folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, bookmarkStructPointer->parentFolderId);
92 // Open all the folders.
93 parentFolderTreeWidgetPointer->expandAll();
95 // Populate the line edits.
96 bookmarkNameLineEditPointer->setText(bookmarkStructPointer->name);
97 bookmarkUrlLineEditPointer->setText(bookmarkStructPointer->url);
99 // Scroll to the beginning of the bookmark URL line edit.
100 bookmarkUrlLineEditPointer->setCursorPosition(0);
102 // Focus the bookmark name line edit.
103 bookmarkNameLineEditPointer->setFocus();
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()));
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()));
114 // Set the initial UI status.
118 void EditBookmarkDialog::browse()
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(*)"));
125 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
126 if (!imageFileString.isEmpty())
128 // Set the custom favorite icon.
129 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
131 // Check the custom favorite icon radio button.
132 customFavoriteIconRadioButtonPointer->setChecked(true);
136 void EditBookmarkDialog::save()
138 // Get the selected folders list.
139 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
141 // Get the selected folder.
142 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
144 // Get the parent folder ID.
145 double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
147 // Get the original display order.
148 int displayOrder = bookmarkStructPointer->displayOrder;
150 // Get the new display order if the parent folder has changed.
151 if (parentFolderId != bookmarkStructPointer->parentFolderId)
152 displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
154 // Create a favorite icon.
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();
165 // Create a bookmark struct.
166 BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
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;
176 // Update the bookmark.
177 BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
179 // Update the display order of all the items in the previous folder.
180 BookmarksDatabase::updateFolderContentsDisplayOrder(bookmarkStructPointer->parentFolderId);
182 // Emit the bookmark saved signal.
183 emit bookmarkSaved();
189 void EditBookmarkDialog::updateUi()
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.
194 // Disable the save button.
195 saveButtonPointer->setEnabled(false);
197 else // Both of the line edits are populated.
199 // Enable the save button.
200 saveButtonPointer->setEnabled(true);