File indexing completed on 2024-06-23 05:07:02

0001 /***************************************************************************
0002  *   SPDX-FileCopyrightText: 2006-2009 Tobias Koenig <tokoe@kde.org>       *
0003  *                                                                         *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later                            *
0005  ***************************************************************************/
0006 
0007 #pragma once
0008 
0009 #include "commandcontext.h"
0010 #include "storage/countquerybuilder.h"
0011 #include "storage/datastore.h"
0012 #include "storage/itemretriever.h"
0013 
0014 #include "private/imapset_p.h"
0015 #include "private/protocol_p.h"
0016 #include "private/scope_p.h"
0017 
0018 #include <functional>
0019 
0020 class ItemFetchHelperTest;
0021 
0022 namespace Akonadi
0023 {
0024 namespace Server
0025 {
0026 class Connection;
0027 class AkonadiServer;
0028 
0029 class ItemFetchHelper
0030 {
0031 public:
0032     ItemFetchHelper(Connection *connection,
0033                     const Scope &scope,
0034                     const Protocol::ItemFetchScope &itemFetchScope,
0035                     const Protocol::TagFetchScope &tagFagScope,
0036                     AkonadiServer &akonadi,
0037                     const Protocol::FetchLimit &itemsLimit = Protocol::FetchLimit());
0038     ItemFetchHelper(Connection *connection,
0039                     const CommandContext &context,
0040                     const Scope &scope,
0041                     const Protocol::ItemFetchScope &itemFetchScope,
0042                     const Protocol::TagFetchScope &tagFetchScope,
0043                     AkonadiServer &akonadi,
0044                     const Protocol::FetchLimit &itemsLimit = Protocol::FetchLimit());
0045 
0046     bool fetchItems(std::function<void(Protocol::FetchItemsResponse &&)> &&callback = {});
0047 
0048     void disableATimeUpdates();
0049 
0050 private:
0051     enum ItemQueryColumns {
0052         ItemQueryPimItemIdColumn,
0053         ItemQueryPimItemRidColumn,
0054         ItemQueryMimeTypeIdColumn,
0055         ItemQueryRevColumn,
0056         ItemQueryRemoteRevisionColumn,
0057         ItemQuerySizeColumn,
0058         ItemQueryDatetimeColumn,
0059         ItemQueryCollectionIdColumn,
0060         ItemQueryPimItemGidColumn,
0061         ItemQueryColumnCount
0062     };
0063 
0064     void updateItemAccessTime();
0065     void triggerOnDemandFetch();
0066     QSqlQuery buildItemQuery();
0067     QSqlQuery buildPartQuery(const QList<QByteArray> &partList, bool allPayload, bool allAttrs);
0068     QSqlQuery buildFlagQuery();
0069     QSqlQuery buildTagQuery();
0070     QSqlQuery buildVRefQuery();
0071 
0072     QList<Protocol::Ancestor> ancestorsForItem(Collection::Id parentColId);
0073     static bool needsAccessTimeUpdate(const QList<QByteArray> &parts);
0074     QVariant extractQueryResult(const QSqlQuery &query, ItemQueryColumns column) const;
0075     bool isScopeLocal(const Scope &scope);
0076     DataStore *storageBackend() const;
0077 
0078 private:
0079     Connection *mConnection = nullptr;
0080     const CommandContext &mContext;
0081     QHash<Collection::Id, QList<Protocol::Ancestor>> mAncestorCache;
0082     Scope mScope;
0083     Protocol::ItemFetchScope mItemFetchScope;
0084     Protocol::TagFetchScope mTagFetchScope;
0085     int mItemQueryColumnMap[ItemQueryColumnCount];
0086     bool mUpdateATimeEnabled = true;
0087     AkonadiServer &mAkonadi;
0088     Protocol::FetchLimit mItemsLimit;
0089     QueryBuilder mItemQuery;
0090     QString mPimItemQueryAlias;
0091 
0092     friend class ::ItemFetchHelperTest;
0093 };
0094 
0095 } // namespace Server
0096 } // namespace Akonadi