]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/dialogs/EditBookmarkDialog.cpp
Add bookmark folders.
[PrivacyBrowserPC.git] / src / dialogs / EditBookmarkDialog.cpp
index 35582f18b3811907ee3e04585e41027e4e6e80a1..6dd3f712471247b9e4bd0f33b02b723184fb3976 100644 (file)
@@ -26,7 +26,7 @@
 #include <QFileDialog>
 
 // Construct the class.
-EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon &currentWebsiteFavoriteIcon) : QDialog(nullptr), bookmarkDatabaseId(bookmarkId)
+EditBookmarkDialog::EditBookmarkDialog(const int databaseId, QIcon &currentWebsiteFavoriteIcon) : QDialog(nullptr), bookmarkDatabaseId(databaseId)
 {
     // Set the window title.
     setWindowTitle(i18nc("The edit bookmark dialog window title.", "Edit Bookmark"));
@@ -34,7 +34,7 @@ EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon &currentWebsi
     // Set the window modality.
     setWindowModality(Qt::WindowModality::ApplicationModal);
 
-    // Instantiate the bookmarks dialog UI.
+    // Instantiate the edit bookmark dialog UI.
     Ui::EditBookmarkDialog editBookmarkDialogUi;
 
     // Setup the UI.
@@ -42,39 +42,82 @@ EditBookmarkDialog::EditBookmarkDialog(const int bookmarkId, QIcon &currentWebsi
 
     // Get handles for the widgets.
     currentFavoriteIconRadioButtonPointer = editBookmarkDialogUi.currentFavoriteIconRadioButton;
-    currentWebsiteFavoritIconRadioButtonPointer = editBookmarkDialogUi.currentWebsiteFavoriteIconRadioButton;
+    currentWebsiteFavoriteIconRadioButtonPointer = editBookmarkDialogUi.currentWebsiteFavoriteIconRadioButton;
     customFavoriteIconRadioButtonPointer = editBookmarkDialogUi.customFavoriteIconRadioButton;
-    bookmarkNamePointer = editBookmarkDialogUi.bookmarkNameLineEdit;
-    bookmarkUrlPointer = editBookmarkDialogUi.bookmarkUrlLineEdit;
+    parentFolderTreeWidgetPointer = editBookmarkDialogUi.parentFolderTreeWidget;
+    bookmarkNameLineEditPointer = editBookmarkDialogUi.bookmarkNameLineEdit;
+    bookmarkUrlLineEditPointer = editBookmarkDialogUi.bookmarkUrlLineEdit;
     QPushButton *browseButtonPointer = editBookmarkDialogUi.browseButton;
     QDialogButtonBox *dialogButtonBoxPointer = editBookmarkDialogUi.dialogButtonBox;
+    saveButtonPointer = dialogButtonBoxPointer->button(QDialogButtonBox::Save);
 
     // Get the bookmark struct.
-    bookmarkStructPointer = BookmarksDatabase::getBookmark(bookmarkId);
+    bookmarkStructPointer = BookmarksDatabase::getBookmark(databaseId);
 
     // Set the favorite icons.
     currentFavoriteIconRadioButtonPointer->setIcon(bookmarkStructPointer->favoriteIcon);
-    currentWebsiteFavoritIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
+    currentWebsiteFavoriteIconRadioButtonPointer->setIcon(currentWebsiteFavoriteIcon);
+
+    // Instantiate a folder helper.
+    folderHelperPointer = new FolderHelper();
+
+    // Set the parent folder tree widget column count.
+    parentFolderTreeWidgetPointer->setColumnCount(2);
+
+    // Hide the second column.
+    parentFolderTreeWidgetPointer->hideColumn(folderHelperPointer->FOLDER_ID_COLUMN);
+
+    // Set the column header.
+    parentFolderTreeWidgetPointer->setHeaderLabel(i18nc("The folder tree widget header", "Select Parent Folder"));
+
+    // Create a bookmarks tree widget item.
+    QTreeWidgetItem *bookmarksTreeWidgetItemPointer = new QTreeWidgetItem();
+
+    // Populate the bookmarks tree widget item.
+    bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_NAME_COLUMN, i18nc("The bookmarks root tree widget name", "Bookmarks"));
+    bookmarksTreeWidgetItemPointer->setIcon(folderHelperPointer->FOLDER_NAME_COLUMN, QIcon::fromTheme("bookmarks"));
+    bookmarksTreeWidgetItemPointer->setText(folderHelperPointer->FOLDER_ID_COLUMN, QLatin1String("0"));
+
+    // Add the bookmarks tree widget item to the root of the tree.
+    parentFolderTreeWidgetPointer->addTopLevelItem(bookmarksTreeWidgetItemPointer);
+
+    // Select the root bookmarks folder if it is the initial parent folder.
+    if (bookmarkStructPointer->parentFolderId == 0)
+        bookmarksTreeWidgetItemPointer->setSelected(true);
+
+    // Populate the subfolders.
+    folderHelperPointer->populateSubfolders(bookmarksTreeWidgetItemPointer, bookmarkStructPointer->parentFolderId);
+
+    // Open all the folders.
+    parentFolderTreeWidgetPointer->expandAll();
 
     // Populate the line edits.
-    bookmarkNamePointer->setText(bookmarkStructPointer->bookmarkName);
-    bookmarkUrlPointer->setText(bookmarkStructPointer->bookmarkUrl);
+    bookmarkNameLineEditPointer->setText(bookmarkStructPointer->name);
+    bookmarkUrlLineEditPointer->setText(bookmarkStructPointer->url);
 
     // Scroll to the beginning of the line edits.
-    bookmarkNamePointer->setCursorPosition(0);
-    bookmarkUrlPointer->setCursorPosition(0);
+    bookmarkNameLineEditPointer->setCursorPosition(0);
+    bookmarkUrlLineEditPointer->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()));
+
+    // Update the UI when the line edits change.
+    connect(bookmarkNameLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
+    connect(bookmarkUrlLineEditPointer, SIGNAL(textEdited(const QString&)), this, SLOT(updateUi()));
+
+    // Set the initial UI status.
+    updateUi();
 }
 
 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 (*)"));
+    QString imageFileString = QFileDialog::getOpenFileName(this, i18nc("The browse for favorite icon dialog header", "Favorite Icon Image"), QDir::homePath(),
+                              i18nc("The browse for image files filter", "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())
@@ -89,31 +132,49 @@ void EditBookmarkDialog::browse()
 
 void EditBookmarkDialog::save()
 {
+    // Get the selected folders list.
+    QList<QTreeWidgetItem*> selectedFoldersList = parentFolderTreeWidgetPointer->selectedItems();
+
+    // Get the selected folder.
+    QTreeWidgetItem *selectedFolderPointer = selectedFoldersList.first();
+
+    // Get the parent folder ID.
+    double parentFolderId = selectedFolderPointer->text(folderHelperPointer->FOLDER_ID_COLUMN).toDouble();
+
+    // Get the original display order.
+    int displayOrder = bookmarkStructPointer->displayOrder;
+
+    // Get the new display order if the parent folder has changed.
+    if (parentFolderId != bookmarkStructPointer->parentFolderId)
+        displayOrder = BookmarksDatabase::getFolderItemCount(parentFolderId);
+
     // 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 if (currentWebsiteFavoriteIconRadioButtonPointer->isChecked())  // The current website favorite icon is checked.
+        favoriteIcon = currentWebsiteFavoriteIconRadioButtonPointer->icon();
     else  // The custom favorite icon is checked.
         favoriteIcon = customFavoriteIconRadioButtonPointer->icon();
 
-    qDebug() << "Favorite icon:  " << favoriteIcon;
-
     // Create a bookmark struct.
-    BookmarkStruct *bookmarkStructPointer = new BookmarkStruct;
+    BookmarkStruct *updatedBookmarkStructPointer = new BookmarkStruct;
 
     // Populate the bookmark struct.
-    bookmarkStructPointer->id = bookmarkDatabaseId;
-    bookmarkStructPointer->bookmarkName = bookmarkNamePointer->text();
-    bookmarkStructPointer->bookmarkUrl = bookmarkUrlPointer->text();
-    bookmarkStructPointer->displayOrder = bookmarkStructPointer->displayOrder;
-    bookmarkStructPointer->favoriteIcon = favoriteIcon;
+    updatedBookmarkStructPointer->databaseId = bookmarkDatabaseId;
+    updatedBookmarkStructPointer->name = bookmarkNameLineEditPointer->text();
+    updatedBookmarkStructPointer->url = bookmarkUrlLineEditPointer->text();
+    updatedBookmarkStructPointer->parentFolderId = parentFolderId;
+    updatedBookmarkStructPointer->displayOrder = displayOrder;
+    updatedBookmarkStructPointer->favoriteIcon = favoriteIcon;
 
     // Update the bookmark.
-    BookmarksDatabase::updateBookmark(bookmarkStructPointer);
+    BookmarksDatabase::updateBookmark(updatedBookmarkStructPointer);
+
+    // Update the display order of all the items in the previous folder.
+    BookmarksDatabase::updateFolderContentsDisplayOrder(bookmarkStructPointer->parentFolderId);
 
     // Emit the bookmark saved signal.
     emit bookmarkSaved();
@@ -121,3 +182,18 @@ void EditBookmarkDialog::save()
     // Close the dialog.
     close();
 }
+
+void EditBookmarkDialog::updateUi()
+{
+    // Determine if both line edits are populated.
+    if (bookmarkNameLineEditPointer->text().isEmpty() || bookmarkUrlLineEditPointer->text().isEmpty())  // At least one of the line edits is empty.
+    {
+        // Disable the save button.
+        saveButtonPointer->setEnabled(false);
+    }
+    else  // Both of the line edits are populated.
+    {
+        // Enable the save button.
+        saveButtonPointer->setEnabled(true);
+    }
+}