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 "EditFolderDialog.h"
22 #include "ui_EditFolderDialog.h"
23 #include "databases/BookmarksDatabase.h"
25 // Qt toolkit headers.
26 #include <QFileDialog>
28 // Construct the class.
29 EditFolderDialog::EditFolderDialog(const int databaseId, QIcon ¤tWebsiteFavoriteIcon) : QDialog(nullptr), folderDatabaseId(databaseId)
31 // Set the window title.
32 setWindowTitle(i18nc("The edit folder dialog window title.", "Edit Folder"));
34 // Set the window modality.
35 setWindowModality(Qt::WindowModality::ApplicationModal);
37 // Instantiate the edit folder dialog UI.
38 Ui::EditFolderDialog editFolderDialogUi;
41 editFolderDialogUi.setupUi(this);
43 // Get handles for the widgets.
44 currentFolderIconRadioButtonPointer = editFolderDialogUi.currentFolderIconRadioButton;
45 currentWebsiteFavoriteIconRadioButtonPointer = editFolderDialogUi.currentWebsiteFavoriteIconRadioButton;
46 customFolderIconRadioButtonPointer = editFolderDialogUi.customFolderIconRadioButton;
47 parentFolderTreeWidgetPointer = editFolderDialogUi.parentFolderTreeWidget;
48 folderNameLineEditPointer = editFolderDialogUi.folderNameLineEdit;
49 QPushButton *browseButtonPointer = editFolderDialogUi.browseButton;
50 QDialogButtonBox *dialogButtonBoxPointer = editFolderDialogUi.dialogButtonBox;
51 saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
53 // Get the bookmark struct.
54 bookmarkStructPointer = BookmarksDatabase::getBookmark(databaseId);
56 // Set the folder icons.
57 currentFolderIconRadioButtonPointer->setIcon(bookmarkStructPointer->favoriteIcon);
58 currentWebsiteFavoriteIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
60 // Instantiate a folder helper.
61 folderHelperPointer = new FolderHelper();
63 // Set the parent folder tree widget column count.
64 parentFolderTreeWidgetPointer->setColumnCount(2);
66 // Hide the second column.
67 parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
69 // Set the column header.
70 parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
72 // Create a bookmarks tree widget item.
73 QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
75 // Populate the bookmarks tree widget item.
76 bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_NAME_COLUMN, i18nc("The bookmarks root tree widget name", "Bookmarks"));
77 bookmarksTreeWidgetItemPointer->setIcon(folderHelperPointer->FOLDER_NAME_COLUMN, QIcon::fromTheme(QLatin1String("bookmarks"), QIcon::fromTheme(QLatin1String("bookmark-new"))));
78 bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_ID_COLUMN, QLatin1String("0"));
80 // Add the bookmarks tree widget item to the root of the tree.
81 parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
83 // Select the root bookmarks folder if it is the initial parent folder.
84 if (bookmarkStructPointer->parentFolderId == 0)
85 bookmarksTreeWidgetItemPointer->setSelected(true);
87 // Populate the subfolders, except for the one being edited.
88 folderHelperPointer->populateSubfoldersExcept(databaseId, bookmarksTreeWidgetItemPointer, bookmarkStructPointer->parentFolderId);
90 // Open all the folders.
91 parentFolderTreeWidgetPointer->expandAll();
93 // Populate the line edits.
94 folderNameLineEditPointer->setText(bookmarkStructPointer->name);
96 // Scroll to the beginning of the line edits.
97 folderNameLineEditPointer->setCursorPosition(0);
99 // Connect the buttons.
100 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
101 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(save()));
102 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
104 // Update the UI when the line edit changes.
105 connect(folderNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
107 // Set the initial UI status.
111 void EditFolderDialog::browse()
113 // Get an image file string from the user.
114 QString imageFileString = QFileDialog::getOpenFileName(this, i18nc("The browse for folder icon dialog header", "Folder Icon Image"), QDir::homePath(),
115 i18nc("The browse for image files filter", "Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg(*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files(*)"));
117 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
118 if (!imageFileString.isEmpty())
120 // Set the custom folder icon.
121 customFolderIconRadioButtonPointer->setIcon(QIcon(imageFileString));
123 // Check the custom folder icon radio button.
124 customFolderIconRadioButtonPointer->setChecked(true);
128 void EditFolderDialog::save()
130 // Get the selected folders list.
131 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
133 // Get the selected folder.
134 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
136 // Get the parent folder ID.
137 double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
139 // Get the original display order.
140 int displayOrder = bookmarkStructPointer->displayOrder;
142 // Get the new display order if the parent folder has changed.
143 if (parentFolderId != bookmarkStructPointer->parentFolderId)
144 displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
146 // Create a favorite icon.
149 // Get the favorite icon.
150 if (currentFolderIconRadioButtonPointer->isChecked()) // The current folder icon is checked.
151 favoriteIcon = currentFolderIconRadioButtonPointer->icon();
152 else if (currentWebsiteFavoriteIconRadioButtonPointer->isChecked()) // The current website favorite icon is checked.
153 favoriteIcon = currentWebsiteFavoriteIconRadioButtonPointer->icon();
154 else // The custom favorite icon is checked.
155 favoriteIcon = customFolderIconRadioButtonPointer->icon();
157 // Create a bookmark struct.
158 BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
160 // Populate the bookmark struct.
161 updatedBookmarkStructPointer->databaseId = folderDatabaseId;
162 updatedBookmarkStructPointer->name = folderNameLineEditPointer->text();
163 updatedBookmarkStructPointer->parentFolderId = parentFolderId;
164 updatedBookmarkStructPointer->displayOrder = displayOrder;
165 updatedBookmarkStructPointer->favoriteIcon = favoriteIcon;
167 // Update the folder.
168 BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
170 // Update the display order of all the items in the previous folder.
171 BookmarksDatabase::updateFolderContentsDisplayOrder(bookmarkStructPointer->parentFolderId);
173 // Emit the folder saved signal.
180 void EditFolderDialog::updateUi()
182 // Set the status of the save button.
183 saveButtonPointer->setEnabled(!folderNameLineEditPointer->text().isEmpty());