1 /***************************************************************************
2  *   SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>            *
3  *                                                                         *
4  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
5  ***************************************************************************/
6 
7 #pragma once
8 
9 #include "entities.h"
10 
11 #include <QByteArray>
12 #include <QStack>
13 #include <QString>
14 
15 namespace Akonadi
16 {
17 class Scope;
18 class ImapSet;
19 
20 namespace Protocol
21 {
22 class Ancestor;
23 class CachePolicy;
24 class FetchCollectionsResponse;
25 class TagFetchScope;
26 class FetchTagsResponse;
27 using FetchTagsResponsePtr = QSharedPointer<FetchTagsResponse>;
28 class FetchRelationsResponse;
29 using FetchRelationsResponsePtr = QSharedPointer<FetchRelationsResponse>;
30 }
31 
32 namespace Server
33 {
34 class CommandContext;
35 class Connection;
36 class AkonadiServer;
37 
38 /**
39   Helper functions for command handlers.
40 */
41 class HandlerHelper
42 {
43 public:
44     /**
45       Returns the collection identified by the given id or path.
46     */
47     static Collection collectionFromIdOrName(const QByteArray &id);
48 
49     /**
50       Returns the full path for the given collection.
51     */
52     static QString pathForCollection(const Collection &col);
53 
54     /**
55       Returns the protocol representation of the cache policy of the given
56       Collection object.
57     */
58     static Protocol::CachePolicy cachePolicyResponse(const Collection &col);
59 
60     /**
61       Returns the protocol representation of the given collection.
62       Make sure DataStore::activeCachePolicy() has been called before to include
63       the effective cache policy
64     */
65     static Protocol::FetchCollectionsResponse fetchCollectionsResponse(AkonadiServer &akonadi, const Collection &col);
66 
67     /**
68       Returns the protocol representation of the given collection.
69       Make sure DataStore::activeCachePolicy() has been called before to include
70       the effective cache policy
71     */
72     static Protocol::FetchCollectionsResponse
73     fetchCollectionsResponse(AkonadiServer &akonadi,
74                              const Collection &col,
75                              const CollectionAttribute::List &attributeList,
76                              bool includeStatistics = false,
77                              int ancestorDepth = 0,
78                              const QStack<Collection> &ancestors = QStack<Collection>(),
79                              const QStack<CollectionAttribute::List> &ancestorAttributes = QStack<CollectionAttribute::List>(),
80                              const QStringList &mimeTypes = QStringList());
81 
82     /**
83       Returns the protocol representation of a collection ancestor chain.
84     */
85     static QVector<Protocol::Ancestor> ancestorsResponse(int ancestorDepth,
86                                                          const QStack<Collection> &ancestors,
87                                                          const QStack<CollectionAttribute::List> &_ancestorsAttributes = QStack<CollectionAttribute::List>());
88 
89     static Protocol::FetchTagsResponse fetchTagsResponse(const Tag &tag, const Protocol::TagFetchScope &tagFetchScope, Connection *connection = nullptr);
90 
91     static Protocol::FetchRelationsResponse fetchRelationsResponse(const Relation &relation);
92 
93     /**
94       Converts a bytearray list of flag names into flag records.
95       @throws HandlerException on errors during database operations
96     */
97     static Flag::List resolveFlags(const QSet<QByteArray> &flagNames);
98 
99     /**
100       Converts a imap set of tags into tag records.
101       @throws HandlerException on errors during database operations
102     */
103     static Tag::List resolveTagsByUID(const ImapSet &tags);
104 
105     static Tag::List resolveTagsByGID(const QStringList &tagsGIDs);
106 
107     static Tag::List resolveTagsByRID(const QStringList &tagsRIDs, const CommandContext &context);
108 
109     static Collection collectionFromScope(const Scope &scope, const CommandContext &context);
110 
111     static Tag::List tagsFromScope(const Scope &scope, const CommandContext &context);
112 };
113 
114 } // namespace Server
115 } // namespace Akonadi
116 
117