]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/dialogs/EditBookmarkDialog.cpp
Add dragging and dropping of bookmarks.
[PrivacyBrowserPC.git] / src / dialogs / EditBookmarkDialog.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 "EditBookmarkDialog.h"
22 #include "ui_EditBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
24
25 // Qt toolkit headers.
26 #include <QFileDialog>
27
28 // Construct the class.
29 EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon &currentWebsiteFavoriteIcon) : QDialog(nullptr), bookmarkDatabaseId(bookmarkId)
30 {
31     // Set the window title.
32     setWindowTitle(i18nc("The edit bookmark dialog window title.", "Edit Bookmark"));
33
34     // Set the window modality.
35     setWindowModality(Qt::WindowModality::ApplicationModal);
36
37     // Instantiate the bookmarks dialog UI.
38     Ui::EditBookmarkDialog editBookmarkDialogUi;
39
40     // Setup the UI.
41     editBookmarkDialogUi.setupUi(this);
42
43     // Get handles for the widgets.
44     currentFavoriteIconRadioButtonPointer = editBookmarkDialogUi.currentFavoriteIconRadioButton;
45     currentWebsiteFavoritIconRadioButtonPointer = editBookmarkDialogUi.currentWebsiteFavoriteIconRadioButton;
46     customFavoriteIconRadioButtonPointer = editBookmarkDialogUi.customFavoriteIconRadioButton;
47     bookmarkNamePointer = editBookmarkDialogUi.bookmarkNameLineEdit;
48     bookmarkUrlPointer = editBookmarkDialogUi.bookmarkUrlLineEdit;
49     QPushButton *browseButtonPointer = editBookmarkDialogUi.browseButton;
50     QDialogButtonBox *dialogButtonBoxPointer = editBookmarkDialogUi.dialogButtonBox;
51
52     // Get the bookmark struct.
53     bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
54
55     // Set the favorite icons.
56     currentFavoriteIconRadioButtonPointer->setIcon(bookmarkStructPointer->favoriteIcon);
57     currentWebsiteFavoritIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
58
59     // Populate the line edits.
60     bookmarkNamePointer->setText(bookmarkStructPointer->bookmarkName);
61     bookmarkUrlPointer->setText(bookmarkStructPointer->bookmarkUrl);
62
63     // Scroll to the beginning of the line edits.
64     bookmarkNamePointer->setCursorPosition(0);
65     bookmarkUrlPointer->setCursorPosition(0);
66
67     // Connect the buttons.
68     connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
69     connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(save()));
70     connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
71 }
72
73 void EditBookmarkDialog::browse()
74 {
75     // Get an image file string from the user.
76     QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
77                                                            tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
78
79     // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
80     if (!imageFileString.isEmpty())
81     {
82         // Set the custom favorite icon.
83         customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
84
85         // Check the custom favorite icon radio button.
86         customFavoriteIconRadioButtonPointer->setChecked(true);
87     }
88 }
89
90 void EditBookmarkDialog::save()
91 {
92     // Create a favorite icon.
93     QIcon favoriteIcon;
94
95     // Get the favorite icon.
96     if (currentFavoriteIconRadioButtonPointer->isChecked())  // The current favorite icon is checked.
97         favoriteIcon = currentFavoriteIconRadioButtonPointer->icon();
98     else if (currentWebsiteFavoritIconRadioButtonPointer->isChecked())  // The current website favorite icon is checked.
99         favoriteIcon = currentWebsiteFavoritIconRadioButtonPointer->icon();
100     else  // The custom favorite icon is checked.
101         favoriteIcon = customFavoriteIconRadioButtonPointer->icon();
102
103     qDebug() << "Favorite icon:  " << favoriteIcon;
104
105     // Create a bookmark struct.
106     BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
107
108     // Populate the bookmark struct.
109     bookmarkStructPointer->id = bookmarkDatabaseId;
110     bookmarkStructPointer->bookmarkName = bookmarkNamePointer->text();
111     bookmarkStructPointer->bookmarkUrl = bookmarkUrlPointer->text();
112     bookmarkStructPointer->displayOrder = bookmarkStructPointer->displayOrder;
113     bookmarkStructPointer->favoriteIcon = favoriteIcon;
114
115     // Update the bookmark.
116     BookmarksDatabase::updateBookmark(bookmarkStructPointer);
117
118     // Emit the bookmark saved signal.
119     emit bookmarkSaved();
120
121     // Close the dialog.
122     close();
123 }