]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/AddBookmarkDialog.cpp
Add dragging and dropping of bookmarks.
[PrivacyBrowserPC.git] / src / dialogs / AddBookmarkDialog.cpp
1 /*
2  * Copyright 2023 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 "AddBookmarkDialog.h"
22 #include "ui_AddBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
24
25 // KDE Framework headers.
26 #include <KLocalizedString>
27
28 // Qt toolkit headers.
29 #include <QFileDialog>
30 #include <QPushButton>
31
32 // Construct the class.
33 AddBookmarkDialog::AddBookmarkDialog(const QString &bookmarkName, const QString &bookmarkUrl, const QIcon &favoriteIcon) : QDialog(nullptr)
34 {
35     // Set the window title.
36     setWindowTitle(i18nc("The add bookmark dialog window title.", "Add Bookmark"));
37
38     // Set the window modality.
39     setWindowModality(Qt::WindowModality::ApplicationModal);
40
41     // Instantiate the bookmarks dialog UI.
42     Ui::AddBookmarkDialog addBookmarkDialogUi;
43
44     // Setup the UI.
45     addBookmarkDialogUi.setupUi(this);
46
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;
54
55     // Set the default favorite icon.
56     defaultFavoriteIconRadioButtonPointer->setIcon(favoriteIcon);
57
58     // Populate the line edits.
59     bookmarkNamePointer->setText(bookmarkName);
60     bookmarkUrlPointer->setText(bookmarkUrl);
61
62     // Scroll to the beginning of the line edits.
63     bookmarkNamePointer->setCursorPosition(0);
64     bookmarkUrlPointer->setCursorPosition(0);
65
66     // Add buttons to the dialog button box.
67     QPushButton *addBookmarkButtonPointer = dialogButtonBoxPointer->addButton(i18nc("The add bookmark button", "Add"), QDialogButtonBox::AcceptRole);
68
69     // Set the button icons.
70     addBookmarkButtonPointer->setIcon(QIcon::fromTheme("list-add"));
71
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()));
76 }
77
78 void AddBookmarkDialog::addBookmark()
79 {
80     // Get the favorite icon.
81     QIcon favoriteIcon = defaultFavoriteIconRadioButtonPointer->isChecked() ? defaultFavoriteIconRadioButtonPointer->icon() : customFavoriteIconRadioButtonPointer->icon();
82
83     // Create a bookmark struct.
84     BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
85
86     // Populate the bookmark struct.
87     bookmarkStructPointer->bookmarkName = bookmarkNamePointer->text();
88     bookmarkStructPointer->bookmarkUrl = bookmarkUrlPointer->text();
89     bookmarkStructPointer->favoriteIcon = favoriteIcon;
90
91     // Add the bookmark.
92     BookmarksDatabase::addBookmark(bookmarkStructPointer);
93
94     // Update the list of bookmarks in the menu and toolbar.
95     emit bookmarkAdded();
96
97     // Close the dialog.
98     close();
99 }
100
101 void AddBookmarkDialog::browse()
102 {
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 (*)"));
106
107     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
108     if (!imageFileString.isEmpty())
109     {
110         // Set the custom favorite icon.
111         customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
112
113         // Check the custom favorite icon radio button.
114         customFavoriteIconRadioButtonPointer->setChecked(true);
115     }
116 }