File indexing completed on 2025-01-19 03:58:03

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-05-15
0007  * Description : low level manager for bookmarks
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #ifndef DIGIKAM_BOOKMARKS_MNGR_H
0016 #define DIGIKAM_BOOKMARKS_MNGR_H
0017 
0018 // Qt includes
0019 
0020 #include <QObject>
0021 #include <QAbstractItemModel>
0022 #include <QUndoCommand>
0023 #include <QSortFilterProxyModel>
0024 
0025 // Local includes
0026 
0027 #include "searchtextbar.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class BookmarksManager;
0033 class BookmarkNode;
0034 
0035 class RemoveBookmarksCommand : public QUndoCommand
0036 {
0037 public:
0038 
0039     explicit RemoveBookmarksCommand(BookmarksManager* const mngr,
0040                                     BookmarkNode* const parent,
0041                                     int row);
0042     ~RemoveBookmarksCommand() override;
0043 
0044     void undo()                override;
0045     void redo()                override;
0046 
0047 protected:
0048 
0049     int               m_row;
0050     BookmarksManager* m_bookmarkManager;
0051     BookmarkNode*     m_node;
0052     BookmarkNode*     m_parent;
0053     bool              m_done;
0054 };
0055 
0056 //---------------------------------------------------------------------------------
0057 
0058 class InsertBookmarksCommand : public RemoveBookmarksCommand
0059 {
0060 public:
0061 
0062     explicit InsertBookmarksCommand(BookmarksManager* const mngr,
0063                                     BookmarkNode* const parent,
0064                                     BookmarkNode* const node,
0065                                     int row);
0066 
0067     void undo() override;
0068     void redo() override;
0069 };
0070 
0071 //---------------------------------------------------------------------------------
0072 
0073 class ChangeBookmarkCommand : public QUndoCommand
0074 {
0075 public:
0076 
0077     enum BookmarkData
0078     {
0079         Url = 0,
0080         Title,
0081         Desc
0082     };
0083 
0084 public:
0085 
0086     explicit ChangeBookmarkCommand(BookmarksManager* const mngr,
0087                                    BookmarkNode* const node,
0088                                    const QString& newValue,
0089                                    BookmarkData type);
0090     ~ChangeBookmarkCommand()  override;
0091 
0092     void undo()               override;
0093     void redo()               override;
0094 
0095 private:
0096 
0097     class Private;
0098     Private* const d;
0099 };
0100 
0101 /**
0102  * BookmarksModel is a QAbstractItemModel wrapper around the BookmarkManager
0103  */
0104 class BookmarksModel : public QAbstractItemModel
0105 {
0106     Q_OBJECT
0107 
0108 public:
0109 
0110     enum Roles
0111     {
0112         TypeRole      = Qt::UserRole + 1,
0113         UrlRole       = Qt::UserRole + 2,
0114         UrlStringRole = Qt::UserRole + 3,
0115         SeparatorRole = Qt::UserRole + 4,
0116         DateAddedRole = Qt::UserRole + 5
0117     };
0118 
0119 public:
0120 
0121     explicit BookmarksModel(BookmarksManager* const mngr, QObject* const parent = nullptr);
0122     ~BookmarksModel()                                                                              override;
0123 
0124     BookmarksManager* bookmarksManager()                                                      const;
0125     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0126     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole)                       const override;
0127     int columnCount(const QModelIndex& parent = QModelIndex())                                const override;
0128     int rowCount(const QModelIndex& parent = QModelIndex())                                   const override;
0129     QModelIndex index(int, int, const QModelIndex& = QModelIndex())                           const override;
0130     QModelIndex parent(const QModelIndex& index= QModelIndex())                               const override;
0131     Qt::ItemFlags flags(const QModelIndex& index)                                             const override;
0132     Qt::DropActions supportedDropActions ()                                                   const override;
0133     QMimeData* mimeData(const QModelIndexList& indexes)                                       const override;
0134     QStringList mimeTypes()                                                                   const override;
0135     bool hasChildren(const QModelIndex& parent = QModelIndex())                               const override;
0136     BookmarkNode* node(const QModelIndex& index)                                              const;
0137     QModelIndex index(BookmarkNode* node)                                                     const;
0138 
0139     bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row,
0140                       int column, const QModelIndex& parent)                                         override;
0141 
0142     bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex())                   override;
0143     bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole)           override;
0144 
0145 public Q_SLOTS:
0146 
0147     void entryAdded(BookmarkNode* item);
0148     void entryRemoved(BookmarkNode* parent, int row, BookmarkNode* item);
0149     void entryChanged(BookmarkNode* item);
0150 
0151 private:
0152 
0153     class Private;
0154     Private* const d;
0155 };
0156 
0157 /**
0158  *  Proxy model that filters out the bookmarks so only the folders
0159  *  are left behind.  Used in the add bookmark dialog combobox.
0160  */
0161 class AddBookmarkProxyModel : public QSortFilterProxyModel
0162 {
0163     Q_OBJECT
0164 
0165 public:
0166 
0167     explicit AddBookmarkProxyModel(QObject* const parent = nullptr);
0168 
0169     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0170 
0171 protected:
0172 
0173     bool filterAcceptsRow(int srow, const QModelIndex& sparent) const override;
0174 };
0175 
0176 //---------------------------------------------------------------------------------
0177 
0178 class TreeProxyModel : public QSortFilterProxyModel
0179 {
0180     Q_OBJECT
0181 
0182 public:
0183 
0184     explicit TreeProxyModel(QObject* const parent = nullptr);
0185 
0186     int columnCount(const QModelIndex&) const                        override;
0187 
0188 Q_SIGNALS:
0189 
0190     void signalFilterAccepts(bool);
0191 
0192 protected:
0193 
0194     bool filterAcceptsRow(int srow, const QModelIndex& sparent) const override;
0195 
0196 private:
0197 
0198     void emitResult(bool v);
0199 };
0200 
0201 /**
0202  *  Bookmark manager, owner of the bookmarks, loads, saves and basic tasks
0203  */
0204 class BookmarksManager : public QObject
0205 {
0206     Q_OBJECT
0207 
0208 public:
0209 
0210     explicit BookmarksManager(const QString& bookmarksFile, QObject* const parent = nullptr);
0211     ~BookmarksManager() override;
0212 
0213     void addBookmark(BookmarkNode* const parent, BookmarkNode* const node, int row = -1);
0214     void removeBookmark(BookmarkNode* const node);
0215     void setTitle(BookmarkNode* const node, const QString& newTitle);
0216     void setUrl(BookmarkNode* const node, const QString& newUrl);
0217     void setComment(BookmarkNode* const node, const QString& newDesc);
0218     void changeExpanded();
0219 
0220     BookmarkNode*   bookmarks();
0221     BookmarksModel* bookmarksModel();
0222     QUndoStack*     undoRedoStack() const;
0223 
0224     void save();
0225     void load();
0226 
0227 Q_SIGNALS:
0228 
0229     void entryAdded(BookmarkNode* item);
0230     void entryRemoved(BookmarkNode* parent, int row, BookmarkNode* item);
0231     void entryChanged(BookmarkNode* item);
0232 
0233 public Q_SLOTS:
0234 
0235     void importBookmarks();
0236     void exportBookmarks();
0237 
0238 private:
0239 
0240     class Private;
0241     Private* const d;
0242 
0243     friend class RemoveBookmarksCommand;
0244     friend class ChangeBookmarkCommand;
0245 };
0246 
0247 } // namespace Digikam
0248 
0249 #endif // DIGIKAM_BOOKMARKS_MNGR_H