X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserPC.git;a=blobdiff_plain;f=src%2Fdialogs%2FBookmarksDialog.cpp;fp=src%2Fdialogs%2FBookmarksDialog.cpp;h=509fade9f7796b3e6f240c8ac30a7b958fa1cf4a;hp=0000000000000000000000000000000000000000;hb=7c6edb3608791950c6146ac242e2b6f493ca8e8c;hpb=697f5cae65577dcdf01cfa85840de8d44d835358 diff --git a/src/dialogs/BookmarksDialog.cpp b/src/dialogs/BookmarksDialog.cpp new file mode 100644 index 0000000..509fade --- /dev/null +++ b/src/dialogs/BookmarksDialog.cpp @@ -0,0 +1,131 @@ +/* + * Copyright 2023 Soren Stoutner . + * + * This file is part of 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 . + */ + +// Application headers. +#include "BookmarksDialog.h" +#include "ui_BookmarksDialog.h" +#include "databases/BookmarksDatabase.h" + +// KDE Frameworks headers. +#include + +// Qt toolkit headers. +#include +#include +#include + +// Construct the class. +BookmarksDialog::BookmarksDialog() : QDialog(nullptr) +{ + // Set the dialog window title. + setWindowTitle(i18nc("The bookmarks dialog window title", "Bookmarks")); + + // Set the window modality. + setWindowModality(Qt::WindowModality::ApplicationModal); + + // Instantiate the bookmarks settings dialog UI. + Ui::BookmarksDialog bookmarksDialogUi; + + // Setup the UI. + bookmarksDialogUi.setupUi(this); + + // Get the list of bookmarks. + std::list *bookmarksListPointer = BookmarksDatabase::getBookmarks(); + + // Get a handle for the tree view. + QTreeView *treeViewPointer = bookmarksDialogUi.treeView; + + // Initialize the tree model. + QStandardItemModel *treeModelPointer = new QStandardItemModel(); + + // Set the column count. + treeModelPointer->setColumnCount(3); + + // Set the tree header data. The first column is the database ID, which is not displayed. + treeModelPointer->setHeaderData(1, Qt::Horizontal, i18nc("The bookmark Name header.", "Name")); + treeModelPointer->setHeaderData(2, Qt::Horizontal, i18nc("The bookmark URL header.", "URL")); + + // Populate the bookmarks tree view. + for (BookmarkStruct bookmarkStruct : *bookmarksListPointer) + { + // Create a list for the bookmark items. + QList bookmarkItemList; + + // Create the bookmark items. + QStandardItem *idItemPointer = new QStandardItem(QString::number(bookmarkStruct.id)); + QStandardItem *nameItemPointer = new QStandardItem(bookmarkStruct.favoriteIcon, bookmarkStruct.bookmarkName); + QStandardItem *urlItemPointer = new QStandardItem(bookmarkStruct.bookmarkUrl); + + // Populate the cookie standard item list. + bookmarkItemList.append(idItemPointer); + bookmarkItemList.append(nameItemPointer); + bookmarkItemList.append(urlItemPointer); + + // Add the cookie to the tree. + treeModelPointer->appendRow(bookmarkItemList); + } + + // Auto resize the headers. + treeViewPointer->header()->setSectionResizeMode(QHeaderView::ResizeToContents); + + // Indicate that all the rows are the same height, which improves performance. + treeViewPointer->setUniformRowHeights(true); + + // Set the tree model. + treeViewPointer->setModel(treeModelPointer); + + // Hide the database ID column. + treeViewPointer->setColumnHidden(0, true); + + // Get handles for the buttons. + QDialogButtonBox *dialogButtonBoxPointer = bookmarksDialogUi.dialogButtonBox; + + // Connect the buttons. + connect(dialogButtonBoxPointer, SIGNAL(rejected()), this, SLOT(reject())); + + // Monitor editing of data in the tree model. + connect(treeModelPointer, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(updateBookmarkFromTree(QStandardItem*))); +} + +void BookmarksDialog::updateBookmarkFromTree(QStandardItem *modifiedStandardItem) +{ + // Get the model index of the modified item. + QModelIndex modifiedItemModelIndex = modifiedStandardItem->index(); + + // Get the model index of the database ID. + QModelIndex databaseIdModelIndex = modifiedItemModelIndex.siblingAtColumn(0); + + // Get the database ID. + int databaseId = databaseIdModelIndex.data().toInt(); + + // Check to see if the bookmark name or the URL was edited. + if (modifiedStandardItem->column() == 1) // The bookmark name was edited. + { + // Update the bookmark name. + BookmarksDatabase::updateBookmarkName(databaseId, modifiedStandardItem->text()); + } + else // The bookmark URL was edited. + { + // Update the bookmark URL. + BookmarksDatabase::updateBookmarkUrl(databaseId, modifiedStandardItem->text()); + } + + // Emit the bookmark updated signal. + emit bookmarkUpdated(); +}