]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/EditBookmarkDialog.cpp
Add dragging and dropping of bookmarks.
[PrivacyBrowserPC.git] / src / dialogs / EditBookmarkDialog.cpp
diff --git a/src/dialogs/EditBookmarkDialog.cpp b/src/dialogs/EditBookmarkDialog.cpp
new file mode 100644 (file)
index 0000000..35582f1
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2023 Soren Stoutner <soren@stoutner.com>.
+ *
+ * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
+ *
+ * Privacy Browser PC is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Privacy Browser PC is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Application headers.
+#include "EditBookmarkDialog.h"
+#include "ui_EditBookmarkDialog.h"
+#include "databases/BookmarksDatabase.h"
+
+// Qt toolkit headers.
+#include <QFileDialog>
+
+// Construct the class.
+EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon &currentWebsiteFavoriteIcon) : QDialog(nullptr), bookmarkDatabaseId(bookmarkId)
+{
+    // Set the window title.
+    setWindowTitle(i18nc("The edit bookmark dialog window title.", "Edit Bookmark"));
+
+    // Set the window modality.
+    setWindowModality(Qt::WindowModality::ApplicationModal);
+
+    // Instantiate the bookmarks dialog UI.
+    Ui::EditBookmarkDialog editBookmarkDialogUi;
+
+    // Setup the UI.
+    editBookmarkDialogUi.setupUi(this);
+
+    // Get handles for the widgets.
+    currentFavoriteIconRadioButtonPointer = editBookmarkDialogUi.currentFavoriteIconRadioButton;
+    currentWebsiteFavoritIconRadioButtonPointer = editBookmarkDialogUi.currentWebsiteFavoriteIconRadioButton;
+    customFavoriteIconRadioButtonPointer = editBookmarkDialogUi.customFavoriteIconRadioButton;
+    bookmarkNamePointer = editBookmarkDialogUi.bookmarkNameLineEdit;
+    bookmarkUrlPointer = editBookmarkDialogUi.bookmarkUrlLineEdit;
+    QPushButton *browseButtonPointer = editBookmarkDialogUi.browseButton;
+    QDialogButtonBox *dialogButtonBoxPointer = editBookmarkDialogUi.dialogButtonBox;
+
+    // Get the bookmark struct.
+    bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+
+    // Set the favorite icons.
+    currentFavoriteIconRadioButtonPointer->setIcon(bookmarkStructPointer->favoriteIcon);
+    currentWebsiteFavoritIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
+
+    // Populate the line edits.
+    bookmarkNamePointer->setText(bookmarkStructPointer->bookmarkName);
+    bookmarkUrlPointer->setText(bookmarkStructPointer->bookmarkUrl);
+
+    // Scroll to the beginning of the line edits.
+    bookmarkNamePointer->setCursorPosition(0);
+    bookmarkUrlPointer->setCursorPosition(0);
+
+    // Connect the buttons.
+    connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(browse()));
+    connect(dialogButtonBoxPointer, SIGNAL(accepted()), this, SLOT(save()));
+    connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject()));
+}
+
+void EditBookmarkDialog::browse()
+{
+    // Get an image file string from the user.
+    QString imageFileString = QFileDialog::getOpenFileName(this, tr("Favorite Icon Image"), QDir::homePath(),
+                                                           tr("Image Files — *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.svg (*.bmp *.gif *.jpg *.jpeg *.png *.svg);;All Files (*)"));
+
+    // Check to see if an image file string was returned.  This will be empty if the user selected cancel.
+    if (!imageFileString.isEmpty())
+    {
+        // Set the custom favorite icon.
+        customFavoriteIconRadioButtonPointer->setIcon(QIcon(imageFileString));
+
+        // Check the custom favorite icon radio button.
+        customFavoriteIconRadioButtonPointer->setChecked(true);
+    }
+}
+
+void EditBookmarkDialog::save()
+{
+    // Create a favorite icon.
+    QIcon favoriteIcon;
+
+    // Get the favorite icon.
+    if (currentFavoriteIconRadioButtonPointer->isChecked())  // The current favorite icon is checked.
+        favoriteIcon = currentFavoriteIconRadioButtonPointer->icon();
+    else if (currentWebsiteFavoritIconRadioButtonPointer->isChecked())  // The current website favorite icon is checked.
+        favoriteIcon = currentWebsiteFavoritIconRadioButtonPointer->icon();
+    else  // The custom favorite icon is checked.
+        favoriteIcon = customFavoriteIconRadioButtonPointer->icon();
+
+    qDebug() << "Favorite icon:  " << favoriteIcon;
+
+    // Create a bookmark struct.
+    BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
+
+    // Populate the bookmark struct.
+    bookmarkStructPointer->id = bookmarkDatabaseId;
+    bookmarkStructPointer->bookmarkName = bookmarkNamePointer->text();
+    bookmarkStructPointer->bookmarkUrl = bookmarkUrlPointer->text();
+    bookmarkStructPointer->displayOrder = bookmarkStructPointer->displayOrder;
+    bookmarkStructPointer->favoriteIcon = favoriteIcon;
+
+    // Update the bookmark.
+    BookmarksDatabase::updateBookmark(bookmarkStructPointer);
+
+    // Emit the bookmark saved signal.
+    emit bookmarkSaved();
+
+    // Close the dialog.
+    close();
+}