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 "AddFolderDialog.h"
22 #include "ui_AddFolderDialog.h"
23 #include "databases/BookmarksDatabase.h"
24 #include "helpers/FolderHelper.h"
25 #include "structs/BookmarkStruct.h"
27 // Qt toolkit headers.
28 #include <QFileDialog>
30 // Construct the class.
31 AddFolderDialog::AddFolderDialog(const QIcon ¤tWebsiteFavoriteIcon, const double parentFolderId) : QDialog(nullptr)
33 // Set the window title.
34 setWindowTitle(i18nc("The add folder dialog window title.", "Add Folder"));
36 // Set the window modality.
37 setWindowModality(Qt::WindowModality::ApplicationModal);
39 // Instantiate the add folder dialog UI.
40 Ui::AddFolderDialog addFolderDialogUi;
43 addFolderDialogUi.setupUi(this);
45 // Get handles for the widgets.
46 defaultFolderIconRadioButtonPointer = addFolderDialogUi.defaultFolderIconRadioButton;
47 currentWebsiteFavoriteIconRadioButtonPointer = addFolderDialogUi.currentWebsiteFavoriteIconRadioButton;
48 customFolderIconRadioButtonPointer = addFolderDialogUi.customFolderIconRadioButton;
49 parentFolderTreeWidgetPointer = addFolderDialogUi.parentFolderTreeWidget;
50 folderNameLineEditPointer = addFolderDialogUi.folderNameLineEdit;
51 QPushButton *browseButtonPointer = addFolderDialogUi.browseButton;
52 QDialogButtonBox *dialogButtonBoxPointer = addFolderDialogUi.dialogButtonBox;
54 // Set the default favorite icon.
55 currentWebsiteFavoriteIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
57 // Instantiate a folder helper.
58 folderHelperPointer = new FolderHelper();
60 // Set the parent folder tree widget column count.
61 parentFolderTreeWidgetPointer->setColumnCount(2);
63 // Hide the second column.
64 parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
66 // Set the column header.
67 parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
69 // Create a bookmarks tree widget item.
70 QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
72 // Populate the bookmarks tree widget item.
73 bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_NAME_COLUMN, i18nc("The bookmarks root tree widget name", "Bookmarks"));
74 bookmarksTreeWidgetItemPointer->setIcon(folderHelperPointer->FOLDER_NAME_COLUMN, QIcon::fromTheme(QLatin1String("bookmarks"), QIcon::fromTheme(QLatin1String("bookmark-new"))));
75 bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_ID_COLUMN, QLatin1String("0"));
77 // Add the bookmarks tree widget item to the root of the tree.
78 parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
80 // Select the root bookmarks folder if it is the initial parent folder.
81 if (parentFolderId == 0)
82 bookmarksTreeWidgetItemPointer->setSelected(true);
84 // Populate the subfolders.
85 folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId);
87 // Open all the folders.
88 parentFolderTreeWidgetPointer->expandAll();
90 // Focus the folder name line edit.
91 folderNameLineEditPointer->setFocus();
93 // Add buttons to the dialog button box.
94 addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add folder button", "Add"), QDialogButtonBox::AcceptRole);
96 // Set the button icons.
97 addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
99 // Connect the buttons.
100 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
101 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addFolder()));
102 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
104 // Update the UI when the folder name changes.
105 connect(folderNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi(const QString&)));
107 // Set the initial UI status.
108 updateUi(folderNameLineEditPointer->text());
111 void AddFolderDialog::addFolder()
113 // Get the parent folder ID.
114 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
116 // Get the selected folder.
117 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
119 // Create a favorite icon.
122 // Get the favorite icon.
123 if (defaultFolderIconRadioButtonPointer->isChecked()) // The default folder icon is checked.
124 favoriteIcon = defaultFolderIconRadioButtonPointer->icon();
125 else if (currentWebsiteFavoriteIconRadioButtonPointer->isChecked()) // The current website favorite icon is checked.
126 favoriteIcon = currentWebsiteFavoriteIconRadioButtonPointer->icon();
127 else // The custom folder icon is checked.
128 favoriteIcon = customFolderIconRadioButtonPointer->icon();
130 // Create a bookmark struct.
131 BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
133 // Populate the bookmark struct.
134 bookmarkStructPointer->name = folderNameLineEditPointer->text();
135 bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
136 bookmarkStructPointer->favoriteIcon = favoriteIcon;
139 BookmarksDatabase::addFolder(bookmarkStructPointer);
141 // Update the list of bookmarks in the menu and toolbar.
148 void AddFolderDialog::browse()
150 // Get an image file string from the user.
151 QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
152 tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
154 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
155 if (!imageFileString.isEmpty())
157 // Set the custom favorite icon.
158 customFolderIconRadioButtonPointer->setIcon(QIcon(imageFileString));
160 // Check the custom favorite icon radio button.
161 customFolderIconRadioButtonPointer->setChecked(true);
165 void AddFolderDialog::updateUi(const QString &newFolderName)
167 // Set the status of the add button based on the
168 addButtonPointer->setEnabled(!newFolderName.isEmpty());