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 "EditBookmarkDialog.h"
22 #include "ui_EditBookmarkDialog.h"
23 #include "databases/BookmarksDatabase.h"
25 // Qt toolkit headers.
26 #include <QFileDialog>
28 // Construct the class.
29 EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon ¤tWebsiteFavoriteIcon) : QDialog(nullptr), bookmarkDatabaseId(bookmarkId)
31 // Set the window title.
32 setWindowTitle(i18nc("The edit bookmark dialog window title.", "Edit Bookmark"));
34 // Set the window modality.
35 setWindowModality(Qt::WindowModality::ApplicationModal);
37 // Instantiate the bookmarks dialog UI.
38 Ui::EditBookmarkDialog editBookmarkDialogUi;
41 editBookmarkDialogUi.setupUi(this);
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;
52 // Get the bookmark struct.
53 bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
55 // Set the favorite icons.
56 currentFavoriteIconRadioButtonPointer->setIcon(bookmarkStructPointer->favoriteIcon);
57 currentWebsiteFavoritIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
59 // Populate the line edits.
60 bookmarkNamePointer->setText(bookmarkStructPointer->bookmarkName);
61 bookmarkUrlPointer->setText(bookmarkStructPointer->bookmarkUrl);
63 // Scroll to the beginning of the line edits.
64 bookmarkNamePointer->setCursorPosition(0);
65 bookmarkUrlPointer->setCursorPosition(0);
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()));
73 void EditBookmarkDialog::browse()
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 (*)"));
79 // Check to see if an image file string was returned. This will be empty if the user selected cancel.
80 if (!imageFileString.isEmpty())
82 // Set the custom favorite icon.
83 customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
85 // Check the custom favorite icon radio button.
86 customFavoriteIconRadioButtonPointer->setChecked(true);
90 void EditBookmarkDialog::save()
92 // Create a favorite icon.
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();
103 qDebug() << "Favorite icon: " << favoriteIcon;
105 // Create a bookmark struct.
106 BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
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;
115 // Update the bookmark.
116 BookmarksDatabase::updateBookmark(bookmarkStructPointer);
118 // Emit the bookmark saved signal.
119 emit bookmarkSaved();