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) : 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 bookmarks dialog UI.
42 Ui::AddBookmarkDialog addBookmarkDialogUi;
45 addBookmarkDialogUi.setupUi(this);
47 // Get handles for the widgets.
48 defaultFavoriteIconRadioButtonPointer = addBookmarkDialogUi.defaultFavoriteIconRadioButton;
49 customFavoriteIconRadioButtonPointer = addBookmarkDialogUi.customFavoriteIconRadioButton;
50 bookmarkNamePointer = addBookmarkDialogUi.bookmarkNameLineEdit;
51 bookmarkUrlPointer = addBookmarkDialogUi.bookmarkUrlLineEdit;
52 QPushButton *browseButtonPointer = addBookmarkDialogUi.browseButton;
53 QDialogButtonBox *dialogButtonBoxPointer = addBookmarkDialogUi.dialogButtonBox;
55 // Set the default favorite icon.
56 defaultFavoriteIconRadioButtonPointer->setIcon(favoriteIcon);
58 // Populate the line edits.
59 bookmarkNamePointer->setText(bookmarkName);
60 bookmarkUrlPointer->setText(bookmarkUrl);
62 // Scroll to the beginning of the line edits.
63 bookmarkNamePointer->setCursorPosition(0);
64 bookmarkUrlPointer->setCursorPosition(0);
66 // Add buttons to the dialog button box.
67 QPushButton *addBookmarkButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
69 // Set the button icons.
70 addBookmarkButtonPointer->setIcon(QIcon::fromTheme("list-add"));
72 // Connect the buttons.
73 connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
74 connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(addBookmark()));
75 connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
78 void AddBookmarkDialog::addBookmark()
80 // Get the favorite icon.
81 QIcon favoriteIcon = defaultFavoriteIconRadioButtonPointer->isChecked() ? defaultFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
83 // Create a bookmark struct.
84 BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
86 // Populate the bookmark struct.
87 bookmarkStructPointer->bookmarkName = bookmarkNamePointer->text();
88 bookmarkStructPointer->bookmarkUrl = bookmarkUrlPointer->text();
89 bookmarkStructPointer->favoriteIcon = favoriteIcon;
92 BookmarksDatabase::addBookmark(bookmarkStructPointer);
94 // Update the list of bookmarks in the menu and toolbar.
101 void AddBookmarkDialog::browse()
103 // Get an image file string from the user.
104 QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
105 tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
107 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
108 if (!imageFileString.isEmpty())
110 // Set the custom favorite icon.
111 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
113 // Check the custom favorite icon radio button.
114 customFavoriteIconRadioButtonPointer->setChecked(true);