protocolhelper_p.h 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (c) 2008 Volker Krause <vkrause@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

20 21
#ifndef AKONADI_PROTOCOLHELPER_P_H
#define AKONADI_PROTOCOLHELPER_P_H
22

Daniel Vrátil's avatar
Daniel Vrátil committed
23 24
#include "cachepolicy.h"
#include "collection.h"
25
#include "collectionutils.h"
Daniel Vrátil's avatar
Daniel Vrátil committed
26 27 28 29 30
#include "item.h"
#include "itemfetchscope.h"
#include "sharedvaluepool_p.h"
#include "attributeentity.h"
#include "tag.h"
31

32 33
#include <akonadi/private/imapparser_p.h>
#include <akonadi/private/protocol_p.h>
34
#include <akonadi/private/scope_p.h>
35
#include <akonadi/private/tristate_p.h>
36

37 38
#include <QString>

39
#include <algorithm>
40 41
#include <type_traits>
#include <functional>
42
#include <cassert>
43

Laurent Montel's avatar
Laurent Montel committed
44
namespace Akonadi
45
{
Laurent Montel's avatar
Laurent Montel committed
46 47

struct ProtocolHelperValuePool {
Guy Maurel's avatar
Guy Maurel committed
48 49
    typedef Internal::SharedValuePool<QByteArray, QVector> FlagPool;
    typedef Internal::SharedValuePool<QString, QVector> MimeTypePool;
50

Guy Maurel's avatar
Guy Maurel committed
51 52 53
    FlagPool flagPool;
    MimeTypePool mimeTypePool;
    QHash<Collection::Id, Collection> ancestorCollections;
54
};
55

56 57 58 59 60 61
/**
  @internal
  Helper methods for converting between libakonadi objects and their protocol
  representation.

  @todo Add unit tests for this.
62
  @todo Use exceptions for a useful error handling
63 64 65
*/
class ProtocolHelper
{
Guy Maurel's avatar
Guy Maurel committed
66
public:
67 68
    /** Part namespaces. */
    enum PartNamespace {
Guy Maurel's avatar
Guy Maurel committed
69 70 71
        PartGlobal,
        PartPayload,
        PartAttribute
72 73
    };

74 75
    /**
      Parse a cache policy definition.
76
      @param policy The parsed cache policy.
77
      @returns Akonadi::CachePolicy
78
    */
79
    static CachePolicy parseCachePolicy(const Protocol::CachePolicy &policy);
80 81 82 83

    /**
      Convert a cache policy object into its protocol representation.
    */
84
    static Protocol::CachePolicy cachePolicyToProtocol(const CachePolicy &policy);
85

86 87 88
    /**
      Convert a ancestor chain from its protocol representation into an Entity object.
    */
89
    static void parseAncestors(const QVector<Protocol::Ancestor> &ancestors, Entity *entity);
90

91 92 93 94 95 96
    /**
      Convert a ancestor chain from its protocol representation into an Entity object.

      This method allows to pass a @p valuePool which acts as cache, so ancestor paths for the
      same @p parentCollection don't have to be parsed twice.
    */
97 98 99 100
    static void parseAncestorsCached(const QVector<Protocol::Ancestor> &ancestors,
                                     Entity *entity,
                                     Collection::Id parentCollection,
                                     ProtocolHelperValuePool *valuePool = 0);
101

102 103 104
    /**
      Parse a collection description.
      @param data The input data.
Yuri Chornoivan's avatar
Yuri Chornoivan committed
105
      @param requireParent Whether or not we require a parent as part of the data.
106
      @returns The parsed collection
107
    */
108 109 110 111 112 113
    static Collection parseCollection(const Protocol::FetchCollectionsResponse &data, bool requireParent = true);

    static void parseAttributes(const Protocol::Attributes &attributes, Entity *entity);
    static void parseAttributes(const Protocol::Attributes &attributes, AttributeEntity *entity);

    static CollectionStatistics parseCollectionStatistics(const Protocol::FetchCollectionStatsResponse &stats);
114

115 116 117
    /**
      Convert attributes to their protocol representation.
    */
118 119
    static Protocol::Attributes attributesToProtocol(const Entity &entity, bool ns = false);
    static Protocol::Attributes attributesToProtocol(const AttributeEntity &entity, bool ns = false);
120 121 122 123

    /**
      Encodes part label and namespace.
    */
124
    static QByteArray encodePartIdentifier(PartNamespace ns, const QByteArray &label);
125

126 127 128
    /**
      Decode part label and namespace.
    */
Guy Maurel's avatar
Guy Maurel committed
129
    static QByteArray decodePartIdentifier(const QByteArray &data, PartNamespace &ns);
130 131 132 133 134

    /**
      Converts the given set of items into a protocol representation.
      @throws A Akonadi::Exception if the item set contains items with missing/invalid identifiers.
    */
135 136
    template<typename T, template<typename> class Container>
    static Scope entitySetToScope(const Container<T> &_objects)
137
    {
Guy Maurel's avatar
Guy Maurel committed
138 139
        if (_objects.isEmpty()) {
            throw Exception("No objects specified");
140
        }
Guy Maurel's avatar
Guy Maurel committed
141

142
        Container<T> objects(_objects);
143 144
        using namespace std::placeholders;
        std::sort(objects.begin(), objects.end(),
145 146 147
                  [](const T &a, const T &b) -> bool {
                      return a.id() < b.id();
                  });
148
        if (objects.at(0).isValid()) {
Guy Maurel's avatar
Guy Maurel committed
149
            QVector<typename T::Id>  uids;
150 151
            uids.reserve(objects.size());
            for (const T &object : objects) {
Guy Maurel's avatar
Guy Maurel committed
152 153 154 155
                uids << object.id();
            }
            ImapSet set;
            set.add(uids);
156
            return Scope(set);
Guy Maurel's avatar
Guy Maurel committed
157 158
        }

159 160 161 162 163 164
        qDebug() << entitySetHasGID(_objects);
        if (entitySetHasGID(_objects)) {
            return entitySetToGID(_objects);
        }

        if (!entitySetHasRemoteIdentifier(_objects, std::mem_fn(&T::remoteId))) {
Guy Maurel's avatar
Guy Maurel committed
165 166 167 168
            throw Exception("No remote identifier specified");
        }

        // check if we have RIDs or HRIDs
169
        if (entitySetHasHRID(_objects)) {
170
            return hierarchicalRidToScope(objects.first());
Guy Maurel's avatar
Guy Maurel committed
171 172
        }

173
        return entitySetToRemoteIdentifier(Scope::Rid, _objects, std::mem_fn(&T::remoteId));
174
    }
175

176
    static Protocol::ScopeContext commandContextToProtocol(const Akonadi::Collection &collection, const Akonadi::Tag &tag,
Laurent Montel's avatar
Laurent Montel committed
177
            const Item::List &requestedItems);
178

Volker Krause's avatar
Volker Krause committed
179 180 181 182 183
    /**
      Converts the given object identifier into a protocol representation.
      @throws A Akonadi::Exception if the item set contains items with missing/invalid identifiers.
    */
    template <typename T>
184
    static Scope entityToScope(const T &object)
Volker Krause's avatar
Volker Krause committed
185
    {
186
        return entitySetToScope(QVector<T>() << object);
Volker Krause's avatar
Volker Krause committed
187 188
    }

189 190 191 192
    /**
      Converts the given collection's hierarchical RID into a protocol representation.
      Assumes @p col has a valid hierarchical RID, so check that before!
    */
193
    static Scope hierarchicalRidToScope(const Collection &col);
194

Volker Krause's avatar
Volker Krause committed
195 196 197 198
    /**
      Converts the HRID of the given item into an ASAP protocol representation.
      Assumes @p item has a valid HRID.
    */
199
    static Scope hierarchicalRidToScope(const Item &item);
Volker Krause's avatar
Volker Krause committed
200

201 202 203 204 205 206
    static Scope hierarchicalRidToScope(const Tag &/*tag*/)
    {
        assert(false);
        return Scope();
    }

207 208 209
    /**
      Converts a given ItemFetchScope object into a protocol representation.
    */
210
    static Protocol::FetchScope itemFetchScopeToProtocol(const ItemFetchScope &fetchScope);
211

Christian Mollekopf's avatar
Christian Mollekopf committed
212 213 214
    /**
      Converts a given TagFetchScope object into a protocol representation.
    */
215
    static QVector<QByteArray> tagFetchScopeToProtocol(const TagFetchScope &fetchScope);
Christian Mollekopf's avatar
Christian Mollekopf committed
216

217 218 219
    /**
      Parses a single line from an item fetch job result into an Item object.
     */
220 221 222
    static Item parseItemFetchResult(const Protocol::FetchItemsResponse &data, ProtocolHelperValuePool *valuePool = 0);
    static Tag parseTagFetchResult(const Protocol::FetchTagsResponse &data);
    static Relation parseRelationFetchResult(const Protocol::FetchRelationsResponse &data);
Christian Mollekopf's avatar
Christian Mollekopf committed
223

224
    static bool streamPayloadToFile(const QString &file, const QByteArray &data, QByteArray &error);
225

226
    static Akonadi::Tristate listPreference(const Collection::ListPreference pref);
227 228 229 230

private:
    template<typename T, template<typename> class Container>
    inline static
Laurent Montel's avatar
Laurent Montel committed
231
    typename std::enable_if < !std::is_same<T, Akonadi::Collection>::value, bool >::type
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    entitySetHasGID(const Container<T> &objects)
    {
        return entitySetHasRemoteIdentifier(objects, std::mem_fn(&T::gid));
    }

    template<typename T, template<typename> class Container>
    inline static
    typename std::enable_if<std::is_same<T, Akonadi::Collection>::value, bool>::type
    entitySetHasGID(const Container<T> &/*objects*/, int */*dummy*/ = 0)
    {
        return false;
    }

    template<typename T, template<typename> class Container>
    inline static
Laurent Montel's avatar
Laurent Montel committed
247
    typename std::enable_if < !std::is_same<T, Akonadi::Collection>::value, Scope >::type
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    entitySetToGID(const Container<T> &objects)
    {
        return entitySetToRemoteIdentifier(Scope::Gid, objects, std::mem_fn(&T::gid));
    }

    template<typename T, template<typename> class Container>
    inline static
    typename std::enable_if<std::is_same<T, Akonadi::Collection>::value, Scope>::type
    entitySetToGID(const Container<T> &/*objects*/, int */*dummy*/ = 0)
    {
        return Scope();
    }

    template<typename T, template<typename> class Container, typename RIDFunc>
    inline static
    bool entitySetHasRemoteIdentifier(const Container<T> &objects, const RIDFunc &ridFunc)
    {
        return std::find_if(objects.constBegin(), objects.constEnd(),
266 267 268
                            [=](const T &obj) {
                                return ridFunc(obj).isEmpty();
                            })
Laurent Montel's avatar
Laurent Montel committed
269
               == objects.constEnd();
270 271 272 273 274 275 276 277 278 279
    }

    template<typename T, template<typename> class Container, typename RIDFunc>
    inline static
    typename std::enable_if<std::is_same<QString, typename RIDFunc:: result_type>::value, Scope>::type
    entitySetToRemoteIdentifier(Scope::SelectionScope scope, const Container<T> &objects, const RIDFunc &ridFunc)
    {
        QStringList rids;
        rids.reserve(objects.size());
        std::transform(objects.cbegin(), objects.cend(),
280 281 282
                       std::back_inserter(rids), [=](const T &obj) -> QString {
                            return ridFunc(obj);
                       });
283 284 285 286 287 288 289 290 291 292 293
        return Scope(scope, rids);
    }

    template<typename T, template<typename> class Container, typename RIDFunc>
    inline static
    typename std::enable_if<std::is_same<QByteArray, typename RIDFunc:: result_type>::value, Scope>::type
    entitySetToRemoteIdentifier(Scope::SelectionScope scope, const Container<T> &objects, const RIDFunc &ridFunc, int */*dummy*/ = 0)
    {
        QStringList rids;
        rids.reserve(objects.size());
        std::transform(objects.cbegin(), objects.cend(),
294 295 296
                       std::back_inserter(rids), [=](const T &obj) -> QString {
                           return QString::fromLatin1(ridFunc(obj));
                       });
297 298 299 300 301
        return Scope(scope, rids);
    }

    template<typename T, template<typename> class Container>
    inline static
302
    typename std::enable_if<!std::is_same<T, Tag>::value, bool>::type
303 304 305
    entitySetHasHRID(const Container<T> &objects)
    {
        return objects.size() == 1 &&
Laurent Montel's avatar
Laurent Montel committed
306
               std::find_if(objects.constBegin(), objects.constEnd(),
307 308 309
                            [](const T &obj) -> bool {
                                return !CollectionUtils::hasValidHierarchicalRID(obj);
                            })
Laurent Montel's avatar
Laurent Montel committed
310
               == objects.constEnd();  // ### HRID sets are not yet specified
311 312 313 314 315 316 317 318 319
    }

    template<typename T, template<typename> class Container>
    inline static
    typename std::enable_if<std::is_same<T, Tag>::value, bool>::type
    entitySetHasHRID(const Container<T> &/*objects*/, int */*dummy*/ = 0)
    {
        return false;
    }
320 321 322 323 324
};

}

#endif