]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddFolderDialog.cpp
Add a default folder icon to the edit folder dialog. https://redmine.stoutner.com...
[PrivacyBrowserPC.git] / src / dialogs / AddFolderDialog.cpp
1 /*
2  * Copyright 2023-2024 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
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"
26
27 // Qt toolkit headers.
28 #include <QFileDialog>
29
30 // Construct the class.
31 AddFolderDialog::AddFolderDialog(QWidget *parentWidgetPointer, const QIcon &currentWebsiteFavoriteIcon, const double parentFolderId) : QDialog(parentWidgetPointer)
32 {
33     // Set the window title.
34     setWindowTitle(i18nc("The add folder dialog window title.", "Add Folder"));
35
36     // Set the window modality.
37     setWindowModality(Qt::WindowModality::ApplicationModal);
38
39     // Instantiate the add folder dialog UI.
40     Ui::AddFolderDialog addFolderDialogUi;
41
42     // Setup the UI.
43     addFolderDialogUi.setupUi(this);
44
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;
53
54     // Set the default favorite icon.
55     currentWebsiteFavoriteIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
56
57     // Instantiate a folder helper.
58     folderHelperPointer = new FolderHelper();
59
60     // Set the parent folder tree widget column count.
61     parentFolderTreeWidgetPointer->setColumnCount(2);
62
63     // Hide the second column.
64     parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
65
66     // Set the column header.
67     parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
68
69     // Create a bookmarks tree widget item.
70     QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
71
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"));
76
77     // Add the bookmarks tree widget item to the root of the tree.
78     parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
79
80     // Select the root bookmarks folder if it is the initial parent folder.
81     if (parentFolderId == 0)
82         bookmarksTreeWidgetItemPointer->setSelected(true);
83
84     // Populate the subfolders.
85     folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId);
86
87     // Open all the folders.
88     parentFolderTreeWidgetPointer->expandAll();
89
90     // Focus the folder name line edit.
91     folderNameLineEditPointer->setFocus();
92
93     // Add buttons to the dialog button box.
94     addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add folder button", "Add"), QDialogButtonBox::AcceptRole);
95
96     // Set the button icons.
97     addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
98
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()));
103
104     // Update the UI when the folder name changes.
105     connect(folderNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi(const QString&)));
106
107     // Set the initial UI status.
108     updateUi(folderNameLineEditPointer->text());
109 }
110
111 void AddFolderDialog::addFolder()
112 {
113     // Get the parent folder ID.
114     QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
115
116     // Get the selected folder.
117     QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
118
119     // Create a favorite icon.
120     QIcon favoriteIcon;
121
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();
129
130     // Create a bookmark struct.
131     BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
132
133     // Populate the bookmark struct.
134     bookmarkStructPointer->name = folderNameLineEditPointer->text();
135     bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
136     bookmarkStructPointer->favoriteIcon = favoriteIcon;
137
138     // Add the folder.
139     BookmarksDatabase::addFolder(bookmarkStructPointer);
140
141     // Update the list of bookmarks in the menu and toolbar.
142     emit folderAdded();
143
144     // Close the dialog.
145     close();
146 }
147
148 void AddFolderDialog::browse()
149 {
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 (*)"));
153
154     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
155     if (!imageFileString.isEmpty())
156     {
157         // Set the custom favorite icon.
158         customFolderIconRadioButtonPointer->setIcon(QIcon(imageFileString));
159
160         // Check the custom favorite icon radio button.
161         customFolderIconRadioButtonPointer->setChecked(true);
162     }
163 }
164
165 void AddFolderDialog::updateUi(const QString &newFolderName)
166 {
167     // Set the status of the add button based on the
168     addButtonPointer->setEnabled(!newFolderName.isEmpty());
169 }