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 "AddBookmarkDialog.h"
22 #include "ui_AddBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
25 // KDE Framework headers.
26 #include <KLocalizedString>
28 // Qt toolkit headers.
29 #include <QFileDialog>
30 #include <QPushButton>
32 // Construct the class.
33 AddBookmarkDialog::AddBookmarkDialog(const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon, const double parentFolderId) : QDialog(nullptr)
35 // Set the window title.
36 setWindowTitle(i18nc("The add bookmark dialog window title.", "Add Bookmark"));
38 // Set the window modality.
39 setWindowModality(Qt::WindowModality::ApplicationModal);
41 // Instantiate the add bookmark dialog UI.
42 Ui::AddBookmarkDialog addBookmarkDialogUi;
45 addBookmarkDialogUi.setupUi(this);
47 // Get handles for the widgets.
48 websiteFavoriteIconRadioButtonPointer = addBookmarkDialogUi.websiteFavoriteIconRadioButton;
49 customFavoriteIconRadioButtonPointer = addBookmarkDialogUi.customFavoriteIconRadioButton;
50 parentFolderTreeWidgetPointer = addBookmarkDialogUi.parentFolderTreeWidget;
51 bookmarkNameLineEditPointer = addBookmarkDialogUi.bookmarkNameLineEdit;
52 bookmarkUrlLineEditPointer = addBookmarkDialogUi.bookmarkUrlLineEdit;
53 QPushButton *browseButtonPointer = addBookmarkDialogUi.browseButton;
54 QDialogButtonBox *dialogButtonBoxPointer = addBookmarkDialogUi.dialogButtonBox;
57 websiteFavoriteIconRadioButtonPointer->setIcon(favoriteIcon);
58 customFavoriteIconRadioButtonPointer->setIcon(QIcon::fromTheme(QLatin1String("globe"), QIcon::fromTheme(QLatin1String("applications-internet"))));
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 (parentFolderId == 0)
85 bookmarksTreeWidgetItemPointer->setSelected(true);
87 // Populate the subfolders.
88 folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId);
90 // Open all the folders.
91 parentFolderTreeWidgetPointer->expandAll();
93 // Populate the line edits.
94 bookmarkNameLineEditPointer->setText(bookmarkName);
95 bookmarkUrlLineEditPointer->setText(bookmarkUrl);
97 // Scroll to the beginning of the bookmark URL line edit.
98 bookmarkUrlLineEditPointer->setCursorPosition(0);
100 // Focus the bookmark name line edit.
101 bookmarkNameLineEditPointer->setFocus();
103 // Add buttons to the dialog button box.
104 addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
106 // Set the button icons.
107 addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
109 // Connect the buttons.
110 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
111 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark()));
112 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
114 // Update the UI when the line edits change.
115 connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
116 connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
118 // Set the initial UI status.
122 void AddBookmarkDialog::addBookmark()
124 // Get the selected folders list.
125 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
127 // Get the selected folder.
128 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
130 // Get the favorite icon.
131 QIcon favoriteIcon = websiteFavoriteIconRadioButtonPointer->isChecked() ? websiteFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
133 // Create a bookmark struct.
134 BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
136 // Populate the bookmark struct.
137 bookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
138 bookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
139 bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
140 bookmarkStructPointer->favoriteIcon = favoriteIcon;
143 BookmarksDatabase::addBookmark(bookmarkStructPointer);
145 // Update the list of bookmarks in the menu and toolbar.
146 emit bookmarkAdded();
152 void AddBookmarkDialog::browse()
154 // Get an image file string from the user.
155 QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
156 tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
158 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
159 if (!imageFileString.isEmpty())
161 // Set the custom favorite icon.
162 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
164 // Check the custom favorite icon radio button.
165 customFavoriteIconRadioButtonPointer->setChecked(true);
169 void AddBookmarkDialog::updateUi()
171 // Determine if both line edits are populated.
172 if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty()) // At least one of the line edits is empty.
174 // Disable the add button.
175 addButtonPointer->setEnabled(false);
177 else // Both of the line edits are populated.
179 // Enable the add button.
180 addButtonPointer->setEnabled(true);