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 "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(QWidget *parentWidgetPointer, const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon, const double parentFolderId) :
34 QDialog(parentWidgetPointer)
36 // Set the window title.
37 setWindowTitle(i18nc("The add bookmark dialog window title.", "Add Bookmark"));
39 // Set the window modality.
40 setWindowModality(Qt::WindowModality::ApplicationModal);
42 // Instantiate the add bookmark dialog UI.
43 Ui::AddBookmarkDialog addBookmarkDialogUi;
46 addBookmarkDialogUi.setupUi(this);
48 // Get handles for the widgets.
49 websiteFavoriteIconRadioButtonPointer = addBookmarkDialogUi.websiteFavoriteIconRadioButton;
50 customFavoriteIconRadioButtonPointer = addBookmarkDialogUi.customFavoriteIconRadioButton;
51 parentFolderTreeWidgetPointer = addBookmarkDialogUi.parentFolderTreeWidget;
52 bookmarkNameLineEditPointer = addBookmarkDialogUi.bookmarkNameLineEdit;
53 bookmarkUrlLineEditPointer = addBookmarkDialogUi.bookmarkUrlLineEdit;
54 QPushButton *browseButtonPointer = addBookmarkDialogUi.browseButton;
55 QDialogButtonBox *dialogButtonBoxPointer = addBookmarkDialogUi.dialogButtonBox;
58 websiteFavoriteIconRadioButtonPointer->setIcon(favoriteIcon);
59 customFavoriteIconRadioButtonPointer->setIcon(QIcon::fromTheme(QLatin1String("globe"), QIcon::fromTheme(QLatin1String("applications-internet"))));
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 (parentFolderId == 0)
86 bookmarksTreeWidgetItemPointer->setSelected(true);
88 // Populate the subfolders.
89 folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, parentFolderId);
91 // Open all the folders.
92 parentFolderTreeWidgetPointer->expandAll();
94 // Populate the line edits.
95 bookmarkNameLineEditPointer->setText(bookmarkName);
96 bookmarkUrlLineEditPointer->setText(bookmarkUrl);
98 // Scroll to the beginning of the bookmark URL line edit.
99 bookmarkUrlLineEditPointer->setCursorPosition(0);
101 // Focus the bookmark name line edit.
102 bookmarkNameLineEditPointer->setFocus();
104 // Add buttons to the dialog button box.
105 addButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
107 // Set the button icons.
108 addButtonPointer->setIcon(QIcon::fromTheme("list-add"));
110 // Connect the buttons.
111 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
112 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark()));
113 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
115 // Update the UI when the line edits change.
116 connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
117 connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
119 // Set the initial UI status.
123 void AddBookmarkDialog::addBookmark()
125 // Get the selected folders list.
126 QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
128 // Get the selected folder.
129 QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
131 // Get the favorite icon.
132 QIcon favoriteIcon = websiteFavoriteIconRadioButtonPointer->isChecked() ? websiteFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
134 // Create a bookmark struct.
135 BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
137 // Populate the bookmark struct.
138 bookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
139 bookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
140 bookmarkStructPointer->parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
141 bookmarkStructPointer->favoriteIcon = favoriteIcon;
144 BookmarksDatabase::addBookmark(bookmarkStructPointer);
146 // Update the list of bookmarks in the menu and toolbar.
147 emit bookmarkAdded();
153 void AddBookmarkDialog::browse()
155 // Get an image file string from the user.
156 QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
157 tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
159 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
160 if (!imageFileString.isEmpty())
162 // Set the custom favorite icon.
163 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
165 // Check the custom favorite icon radio button.
166 customFavoriteIconRadioButtonPointer->setChecked(true);
170 void AddBookmarkDialog::updateUi()
172 // Update the add button status
173 if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty()) // At least one of the line edits is empty.
175 // Disable the add button.
176 addButtonPointer->setEnabled(false);
178 else // Both of the line edits are populated.
180 // Enable the add button.
181 addButtonPointer->setEnabled(true);