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 "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(QWidget *parentWidgetPointer, const int databaseId, QIcon ¤tWebsiteFavoriteIcon) : QDialog(parentWidgetPointer), 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 defaultFolderIconRadioButtonPointer = editFolderDialogUi.defaultFolderIconRadioButton;
46 currentWebsiteFavoriteIconRadioButtonPointer = editFolderDialogUi.currentWebsiteFavoriteIconRadioButton;
47 customFolderIconRadioButtonPointer = editFolderDialogUi.customFolderIconRadioButton;
48 parentFolderTreeWidgetPointer = editFolderDialogUi.parentFolderTreeWidget;
49 folderNameLineEditPointer = editFolderDialogUi.folderNameLineEdit;
50 QPushButton *browseButtonPointer = editFolderDialogUi.browseButton;
51 QDialogButtonBox *dialogButtonBoxPointer = editFolderDialogUi.dialogButtonBox;
52 saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
54 // Get the folder bookmark struct.
55 folderBookmarkStructPointer = BookmarksDatabase::getBookmark(databaseId);
57 // Set the folder icons.
58 currentFolderIconRadioButtonPointer->setIcon(folderBookmarkStructPointer->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(QLatin1String("bookmarks"), QIcon::fromTheme(QLatin1String("bookmark-new"))));
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 (folderBookmarkStructPointer->parentFolderId == 0)
86 bookmarksTreeWidgetItemPointer->setSelected(true);
88 // Populate the subfolders, except for the one being edited.
89 folderHelperPointer->populateSubfoldersExcept(databaseId, bookmarksTreeWidgetItemPointer, folderBookmarkStructPointer->parentFolderId);
91 // Open all the folders.
92 parentFolderTreeWidgetPointer->expandAll();
94 // Populate the line edits.
95 folderNameLineEditPointer->setText(folderBookmarkStructPointer->name);
97 // Focus the folder name line edit.
98 folderNameLineEditPointer->setFocus();
100 // Connect the buttons.
101 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
102 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(save()));
103 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
105 // Update the UI when the line edit changes.
106 connect(folderNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
108 // Set the initial UI status.
112 void EditFolderDialog::browse()
114 // Get an image file string from the user.
115 QString imageFileString = QFileDialog::getOpenFileName(this, i18nc("The browse for folder icon dialog header", "Folder Icon Image"), QDir::homePath(),
116 i18nc("The browse for image files filter", "Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg(*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files(*)"));
118 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
119 if (!imageFileString.isEmpty())
121 // Set the custom folder icon.
122 customFolderIconRadioButtonPointer->setIcon(QIcon(imageFileString));
124 // Check the custom folder icon radio button.
125 customFolderIconRadioButtonPointer->setChecked(true);
129 void EditFolderDialog::save()
131 // Get the selected folders list.
132 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
134 // Get the selected folder.
135 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
137 // Get the parent folder ID.
138 double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
140 // Determine if it has moved to a new folder.
141 bool movedToNewFolder = parentFolderId != folderBookmarkStructPointer->parentFolderId;
143 // Get the original display order.
144 int displayOrder = folderBookmarkStructPointer->displayOrder;
146 // Get the new display order if the parent folder has changed.
147 if (movedToNewFolder)
148 displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
150 // Create a favorite icon.
153 // Get the favorite icon.
154 if (currentFolderIconRadioButtonPointer->isChecked()) // The current folder icon is checked.
155 favoriteIcon = currentFolderIconRadioButtonPointer->icon();
156 else if (defaultFolderIconRadioButtonPointer->isChecked()) // The default folder icon is checked.
157 favoriteIcon = defaultFolderIconRadioButtonPointer->icon();
158 else if (currentWebsiteFavoriteIconRadioButtonPointer->isChecked()) // The current website favorite icon is checked.
159 favoriteIcon = currentWebsiteFavoriteIconRadioButtonPointer->icon();
160 else // The custom favorite icon is checked.
161 favoriteIcon = customFolderIconRadioButtonPointer->icon();
163 // Create a bookmark struct.
164 BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
166 // Populate the bookmark struct.
167 updatedBookmarkStructPointer->databaseId = folderDatabaseId;
168 updatedBookmarkStructPointer->name = folderNameLineEditPointer->text();
169 updatedBookmarkStructPointer->parentFolderId = parentFolderId;
170 updatedBookmarkStructPointer->displayOrder = displayOrder;
171 updatedBookmarkStructPointer->favoriteIcon = favoriteIcon;
173 // Update the folder.
174 BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
176 // Update the display order of all the items in the previous folder if it has moved to a new folder.
177 if (movedToNewFolder)
178 BookmarksDatabase::updateFolderContentsDisplayOrder(folderBookmarkStructPointer->parentFolderId);
180 // Emit the folder saved signal.
187 void EditFolderDialog::updateUi()
189 // Set the status of the save button.
190 saveButtonPointer->setEnabled(!folderNameLineEditPointer->text().isEmpty());