1 /* SPDX-FileCopyrightText: 2010 Thomas McGuire <mcguire@kde.org>
2 
3    SPDX-FileCopyrightText: 2011-2021 Laurent Montel <montel@kde.org>
4 
5    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
6 */
7 #pragma once
8 
9 #include "kmail_export.h"
10 #include "mailcommon/tag.h"
11 #include <Akonadi/Item>
12 #include <QMap>
13 #include <QVector>
14 class KActionCollection;
15 class KXMLGUIClient;
16 class KToggleAction;
17 class QAction;
18 namespace Akonadi
19 {
20 class Item;
21 class Tag;
22 }
23 
24 namespace KMail
25 {
26 class MessageActions;
27 
28 /**
29  * Creates actions related to the existing Akonadi tags and plugs them into the GUI.
30  *
31  * The tag manager reads all tags from Akonadi and adds each to the action collection
32  * and to the message status menu.
33  * For tags that should be in the toolbar, it plugs the action list
34  * toolbar_messagetag_actions.
35  *
36  * The actions are automatically updated when a Akonadi tag changes.
37  */
38 class KMAIL_EXPORT TagActionManager : public QObject
39 {
40     Q_OBJECT
41 public:
42     /**
43      * Does not take ownership of the action collection, the GUI client or the message actions.
44      * Does not yet create the actions.
45      *
46      * @param parent The parent QObject.
47      * @param actionCollection: Each tag action is added here
48      * @param messageActions: Each action is added to the message status menu
49      * @param guiClient: The action list with the toolbar action is plugged here
50      */
51     TagActionManager(QObject *parent, KActionCollection *actionCollection, MessageActions *messageActions, KXMLGUIClient *guiClient);
52 
53     ~TagActionManager() override;
54 
55     /**
56      * Removes all actions from the GUI again
57      */
58     void clearActions();
59 
60     /**
61      * Creates and plugs all tag actions
62      */
63     void createActions();
64 
65     /**
66      * Updates the state of the toggle actions of all tags.
67      * The state of the action depends on the number of selected messages, for example
68      * all actions are disabled when no message is selected.
69      *
70      * This function is async
71      *
72      * @param numberOfSelectedMessages The number of selected messages
73      * @param selectedItem if exactly one item is selected, it should be passed here
74      */
75     void updateActionStates(int numberOfSelectedMessages, const Akonadi::Item &selectedItem);
76 
77 Q_SIGNALS:
78 
79     /**
80      * Emitted when one of the tagging actions was triggered. The user of this class
81      * should connect to this signal and change the tags of the messages
82      */
83     void tagActionTriggered(const Akonadi::Tag &tag);
84     /**
85      * Emitted when we want to select more action
86      */
87     void tagMoreActionClicked();
88 
89 private:
90     Q_DISABLE_COPY(TagActionManager)
91     void newTagActionClicked();
92     void onSignalMapped(const QString &tag);
93 
94     void fillTagList();
95     void createTagAction(const MailCommon::Tag::Ptr &tag, bool addToMenu);
96     void createTagActions(const QVector<MailCommon::Tag::Ptr> &);
97     void checkTags(const QList<qint64> &tags);
98     Q_REQUIRED_RESULT QList<qint64> checkedTags() const;
99 
100     KActionCollection *const mActionCollection;
101     MessageActions *const mMessageActions;
102     KXMLGUIClient *const mGUIClient;
103 
104     QAction *mSeparatorMoreAction = nullptr;
105     QAction *mSeparatorNewTagAction = nullptr;
106     QAction *mMoreAction = nullptr;
107     QAction *mNewTagAction = nullptr;
108     // Maps the id of a tag to the action of a tag.
109     // Contains all existing tags
110     QMap<qint64, KToggleAction *> mTagActions;
111 
112     // The actions of all tags that are in the toolbar
113     QList<QAction *> mToolbarActions;
114 
115     // Uri of a newly created tag
116     qint64 mNewTagId = -1;
117 };
118 }
119 
120