]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/helpers/FolderHelper.cpp
Add bookmark folders.
[PrivacyBrowserPC.git] / src / helpers / FolderHelper.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 "FolderHelper.h"
22 #include "databases/BookmarksDatabase.h"
23 #include "structs/BookmarkStruct.h"
24
25 // Construct the class.
26 FolderHelper::FolderHelper() {}
27
28 void FolderHelper::populateSubfolders(QTreeWidgetItem *treeWidgetItemPointer, const double initialParentFolderId)
29 {
30     // Get the list of subfolders.
31     QList<BookmarkStruct> *subfoldersList = BookmarksDatabase::getSubfolders(treeWidgetItemPointer->text(FOLDER_ID_COLUMN).toDouble());
32
33     // Populate each subfolder.
34     for (BookmarkStruct bookmarkStruct : *subfoldersList)
35     {
36         // Create a tree widget item.
37         QTreeWidgetItem *subfolderWidgetItemPointer = new QTreeWidgetItem();
38
39         // Populate the tree widget item.
40         subfolderWidgetItemPointer->setText(FOLDER_NAME_COLUMN, bookmarkStruct.name);
41         subfolderWidgetItemPointer->setIcon(FOLDER_NAME_COLUMN, bookmarkStruct.favoriteIcon);
42         subfolderWidgetItemPointer->setText(FOLDER_ID_COLUMN, QString::number(bookmarkStruct.folderId, 'f', 0));  // Format the folder ID as a floating point with no trailing zeros.
43
44         // Add the subfolder to the tree widget item.
45         treeWidgetItemPointer->addChild(subfolderWidgetItemPointer);
46
47         // Select the folder if it is the initial parent folder.
48         if (bookmarkStruct.folderId == initialParentFolderId)
49             subfolderWidgetItemPointer->setSelected(true);
50
51         // Add any subfolders.
52         populateSubfolders(subfolderWidgetItemPointer, initialParentFolderId);
53     }
54 }
55
56 void FolderHelper::populateSubfoldersExcept(const double exceptSubfolderDatabaseId, QTreeWidgetItem *treeWidgetItemPointer, const double initialParentFolderId)
57 {
58     // Get the list of subfolders.
59     QList<BookmarkStruct> *subfoldersList = BookmarksDatabase::getSubfolders(treeWidgetItemPointer->text(FOLDER_ID_COLUMN).toDouble());
60
61     // Populate each subfolder.
62     for (BookmarkStruct bookmarkStruct : *subfoldersList)
63     {
64         // Only populate the subfolder if it is not excepted.
65         if (bookmarkStruct.databaseId != exceptSubfolderDatabaseId)
66         {
67             // Create a tree widget item.
68             QTreeWidgetItem *subfolderWidgetItemPointer = new QTreeWidgetItem();
69
70             // Populate the tree widget item.
71             subfolderWidgetItemPointer->setText(FOLDER_NAME_COLUMN, bookmarkStruct.name);
72             subfolderWidgetItemPointer->setIcon(FOLDER_NAME_COLUMN, bookmarkStruct.favoriteIcon);
73             subfolderWidgetItemPointer->setText(FOLDER_ID_COLUMN, QString::number(bookmarkStruct.folderId, 'f', 0));  // Format the folder ID as a floating point with no trailing zeros.
74
75             // Add the subfolder to the tree widget item.
76             treeWidgetItemPointer->addChild(subfolderWidgetItemPointer);
77
78             // Select the folder if it is the initial parent folder.
79             if (bookmarkStruct.folderId == initialParentFolderId)
80                 subfolderWidgetItemPointer->setSelected(true);
81
82             // Add any subfolders.
83             populateSubfoldersExcept(exceptSubfolderDatabaseId, subfolderWidgetItemPointer, initialParentFolderId);
84         }
85     }
86 }