2 * Copyright 2023 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(const int databaseId, QIcon ¤tWebsiteFavoriteIcon) : QDialog(nullptr), 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);
61 // Instantiate a folder helper.
62 folderHelperPointer = new FolderHelper();
64 // Set the parent folder tree widget column count.
65 parentFolderTreeWidgetPointer->setColumnCount(2);
67 // Hide the second column.
68 parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
70 // Set the column header.
71 parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
73 // Create a bookmarks tree widget item.
74 QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
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"));
81 // Add the bookmarks tree widget item to the root of the tree.
82 parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
84 // Select the root bookmarks folder if it is the initial parent folder.
85 if (bookmarkStructPointer->parentFolderId == 0)
86 bookmarksTreeWidgetItemPointer->setSelected(true);
88 // Populate the subfolders.
89 folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, bookmarkStructPointer->parentFolderId);
91 // Open all the folders.
92 parentFolderTreeWidgetPointer->expandAll();
94 // Populate the line edits.
95 bookmarkNameLineEditPointer->setText(bookmarkStructPointer->name);
96 bookmarkUrlLineEditPointer->setText(bookmarkStructPointer->url);
98 // Scroll to the beginning of the line edits.
99 bookmarkNameLineEditPointer->setCursorPosition(0);
100 bookmarkUrlLineEditPointer->setCursorPosition(0);
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()));
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()));
111 // Set the initial UI status.
115 void EditBookmarkDialog::browse()
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(*)"));
122 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
123 if (!imageFileString.isEmpty())
125 // Set the custom favorite icon.
126 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
128 // Check the custom favorite icon radio button.
129 customFavoriteIconRadioButtonPointer->setChecked(true);
133 void EditBookmarkDialog::save()
135 // Get the selected folders list.
136 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
138 // Get the selected folder.
139 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
141 // Get the parent folder ID.
142 double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
144 // Get the original display order.
145 int displayOrder = bookmarkStructPointer->displayOrder;
147 // Get the new display order if the parent folder has changed.
148 if (parentFolderId != bookmarkStructPointer->parentFolderId)
149 displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
151 // Create a favorite icon.
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();
162 // Create a bookmark struct.
163 BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
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;
173 // Update the bookmark.
174 BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
176 // Update the display order of all the items in the previous folder.
177 BookmarksDatabase::updateFolderContentsDisplayOrder(bookmarkStructPointer->parentFolderId);
179 // Emit the bookmark saved signal.
180 emit bookmarkSaved();
186 void EditBookmarkDialog::updateUi()
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.
191 // Disable the save button.
192 saveButtonPointer->setEnabled(false);
194 else // Both of the line edits are populated.
196 // Enable the save button.
197 saveButtonPointer->setEnabled(true);