File indexing completed on 2022-12-06 15:36:18

0001 /* This file is part of the KDE project
0002    Copyright (C) 2000 David Faure <faure@kde.org>
0003    Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU General Public License as
0007    published by the Free Software Foundation; either version 2 of
0008    the License, or (at your option) version 3.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013    GNU General Public License for more details.
0014 
0015    You should have received a copy of the GNU General Public License
0016    along with this program.  If not, see <http://www.gnu.org/licenses/>
0017 */
0018 
0019 #ifndef kbookmarkmodel__commands_h
0020 #define kbookmarkmodel__commands_h
0021 
0022 #include "kbookmarkmodel_export.h"
0023 #include <KBookmark>
0024 #include <QUndoCommand>
0025 #include <QUrl>
0026 class KBookmarkModel;
0027 
0028 // Interface adds the affectedBookmarks method
0029 // Any command class should on call add those bookmarks which are
0030 // affected by executing or unexecuting the command
0031 // Or a common parent of the affected bookmarks
0032 // see KBookmarkManager::notifyChange(KBookmarkGroup)
0033 class KBOOKMARKMODEL_EXPORT IKEBCommand
0034 {
0035 public:
0036     IKEBCommand()
0037     {
0038     }
0039     virtual ~IKEBCommand()
0040     {
0041     }
0042     virtual QString affectedBookmarks() const = 0;
0043     // If only QUndoCommand had setData(QVariant), we would save a lot of casting...
0044 };
0045 
0046 class KBOOKMARKMODEL_EXPORT KEBMacroCommand : public QUndoCommand, public IKEBCommand
0047 {
0048 public:
0049     explicit KEBMacroCommand(const QString &name, QUndoCommand *parent = nullptr)
0050         : QUndoCommand(name, parent)
0051     {
0052     }
0053     ~KEBMacroCommand() override
0054     {
0055     }
0056     QString affectedBookmarks() const override;
0057 };
0058 
0059 class KBOOKMARKMODEL_EXPORT DeleteManyCommand : public KEBMacroCommand
0060 {
0061 public:
0062     DeleteManyCommand(KBookmarkModel *model, const QString &name, const QList<KBookmark> &bookmarks);
0063     ~DeleteManyCommand() override
0064     {
0065     }
0066 };
0067 
0068 class KBOOKMARKMODEL_EXPORT CreateCommand : public QUndoCommand, public IKEBCommand
0069 {
0070 public:
0071     // separator
0072     CreateCommand(KBookmarkModel *model, const QString &address, QUndoCommand *parent = nullptr);
0073 
0074     // bookmark
0075     CreateCommand(KBookmarkModel *model, const QString &address, const QString &text, const QString &iconPath, const QUrl &url, QUndoCommand *parent = nullptr);
0076 
0077     // folder
0078     CreateCommand(KBookmarkModel *model, const QString &address, const QString &text, const QString &iconPath, bool open, QUndoCommand *parent = nullptr);
0079 
0080     // clone existing bookmark
0081     CreateCommand(KBookmarkModel *model, const QString &address, const KBookmark &original, const QString &name = QString(), QUndoCommand *parent = nullptr);
0082 
0083     QString finalAddress() const;
0084 
0085     ~CreateCommand() override
0086     {
0087     }
0088     void redo() override;
0089     void undo() override;
0090     QString affectedBookmarks() const override;
0091 
0092 private: // TODO move it all to a d pointer
0093     KBookmarkModel *m_model;
0094     QString m_to;
0095     QString m_text;
0096     QString m_iconPath;
0097     QUrl m_url;
0098     bool m_group : 1;
0099     bool m_separator : 1;
0100     bool m_open : 1;
0101     KBookmark m_originalBookmark;
0102     QDomDocument m_originalBookmarkDocRef; // so that it lives at least as long as m_originalBookmark
0103 };
0104 
0105 class KBOOKMARKMODEL_EXPORT EditCommand : public QUndoCommand, public IKEBCommand
0106 {
0107 public:
0108     EditCommand(KBookmarkModel *model, const QString &address, int col, const QString &newValue, QUndoCommand *parent = nullptr);
0109     ~EditCommand() override
0110     {
0111     }
0112     void redo() override;
0113     void undo() override;
0114     QString affectedBookmarks() const override
0115     {
0116         return KBookmark::parentAddress(mAddress);
0117     }
0118     void modify(const QString &newValue);
0119 
0120 private:
0121     KBookmarkModel *m_model;
0122     QString mAddress;
0123     int mCol;
0124     QString mNewValue;
0125     QString mOldValue;
0126 };
0127 
0128 class KBOOKMARKMODEL_EXPORT DeleteCommand : public QUndoCommand, public IKEBCommand
0129 {
0130 public:
0131     explicit DeleteCommand(KBookmarkModel *model, const QString &from, bool contentOnly = false, QUndoCommand *parent = nullptr);
0132     ~DeleteCommand() override
0133     {
0134         delete m_cmd;
0135         delete m_subCmd;
0136     }
0137     void redo() override;
0138     void undo() override;
0139     QString affectedBookmarks() const override;
0140     static KEBMacroCommand *deleteAll(KBookmarkModel *model, const KBookmarkGroup &parentGroup);
0141 
0142 private: // TODO d pointer
0143     KBookmarkModel *m_model;
0144     QString m_from;
0145     QUndoCommand *m_cmd;
0146     KEBMacroCommand *m_subCmd;
0147     bool m_contentOnly;
0148 };
0149 
0150 class SortItem;
0151 
0152 class KBOOKMARKMODEL_EXPORT SortCommand : public KEBMacroCommand
0153 {
0154 public:
0155     SortCommand(KBookmarkModel *model, const QString &name, const QString &groupAddress, QUndoCommand *parent = nullptr);
0156     ~SortCommand() override
0157     {
0158     }
0159     void redo() override;
0160     void undo() override;
0161     QString affectedBookmarks() const override;
0162     // internal
0163     void moveAfter(const SortItem &moveMe, const SortItem &afterMe);
0164 
0165 private:
0166     KBookmarkModel *m_model;
0167     QString m_groupAddress;
0168 };
0169 
0170 // TODO RENAME -- or maybe move these to KBookmarkModel?
0171 class KBOOKMARKMODEL_EXPORT CmdGen
0172 {
0173 public:
0174     static KEBMacroCommand *setAsToolbar(KBookmarkModel *model, const KBookmark &bk);
0175     static KEBMacroCommand *insertMimeSource(KBookmarkModel *model, const QString &cmdName, const QMimeData *data, const QString &addr);
0176     // TODO remove "bool copy"
0177     static KEBMacroCommand *itemsMoved(KBookmarkModel *model, const QList<KBookmark> &items, const QString &newAddress, bool copy);
0178 
0179 private:
0180     CmdGen()
0181     {
0182     }
0183 };
0184 
0185 #endif