File indexing completed on 2024-11-10 04:40:32

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonaditests_export.h"
0010 #include "collection.h"
0011 #include "specialcollections.h"
0012 #include "transactionsequence.h"
0013 
0014 #include <QVariant>
0015 
0016 #include <memory>
0017 
0018 namespace Akonadi
0019 {
0020 // ===================== ResourceScanJob ============================
0021 
0022 class ResourceScanJobPrivate;
0023 
0024 /**
0025   @internal
0026   Helper job for SpecialCollectionsRequestJob.
0027 
0028   A Job that fetches all the collections of a resource, and returns only
0029   those that have a SpecialCollectionAttribute.
0030 
0031   @author Constantin Berzan <exit3219@gmail.com>
0032   @since 4.4
0033 */
0034 class AKONADI_TESTS_EXPORT ResourceScanJob : public Job
0035 {
0036     Q_OBJECT
0037 
0038 public:
0039     /**
0040       Creates a new ResourceScanJob.
0041     */
0042     explicit ResourceScanJob(const QString &resourceId, KCoreConfigSkeleton *settings, QObject *parent = nullptr);
0043 
0044     /**
0045       Destroys this ResourceScanJob.
0046     */
0047     ~ResourceScanJob() override;
0048 
0049     /**
0050       Returns the resource ID of the resource being scanned.
0051     */
0052     [[nodiscard]] QString resourceId() const;
0053 
0054     /**
0055       Sets the resource ID of the resource to scan.
0056     */
0057     void setResourceId(const QString &resourceId);
0058 
0059     /**
0060       Returns the root collection of the resource being scanned.
0061       This function relies on there being a single top-level collection owned
0062       by this resource.
0063     */
0064     [[nodiscard]] Akonadi::Collection rootResourceCollection() const;
0065 
0066     /**
0067       Returns all the collections of this resource which have a
0068       SpecialCollectionAttribute. These might include the root resource collection.
0069     */
0070     [[nodiscard]] Akonadi::Collection::List specialCollections() const;
0071 
0072 protected:
0073     /* reimpl */
0074     void doStart() override;
0075 
0076 private:
0077     friend class ResourceScanJobPrivate;
0078     std::unique_ptr<ResourceScanJobPrivate> const d;
0079 };
0080 
0081 // ===================== DefaultResourceJob ============================
0082 
0083 class DefaultResourceJobPrivate;
0084 
0085 /**
0086   @internal
0087   Helper job for SpecialCollectionsRequestJob.
0088 
0089   A custom ResourceScanJob for the default local folders resource. This is a
0090   maildir resource stored in ~/.local/share/local-mail.
0091 
0092   This job does two things that a regular ResourceScanJob does not do:
0093   1) It creates and syncs the resource if it is missing. The resource ID is
0094      stored in a config file named specialcollectionsrc.
0095   2) If the resource had to be recreated, but the folders existed on disk
0096      before that, it recovers the folders based on name. For instance, it will
0097      give a folder named outbox a SpecialCollectionAttribute of type Outbox.
0098 
0099   @author Constantin Berzan <exit3219@gmail.com>
0100   @since 4.4
0101 */
0102 class AKONADI_TESTS_EXPORT DefaultResourceJob : public ResourceScanJob
0103 {
0104     Q_OBJECT
0105 
0106 public:
0107     /**
0108      * Creates a new DefaultResourceJob.
0109      */
0110     explicit DefaultResourceJob(KCoreConfigSkeleton *settings, QObject *parent = nullptr);
0111 
0112     /**
0113      * Destroys the DefaultResourceJob.
0114      */
0115     ~DefaultResourceJob() override;
0116 
0117     /**
0118      * Sets the @p type of the resource that shall be created if the requested
0119      * special collection does not exist yet.
0120      */
0121     void setDefaultResourceType(const QString &type);
0122 
0123     /**
0124      * Sets the configuration @p options that shall be applied to the new resource
0125      * that is created if the requested special collection does not exist yet.
0126      */
0127     void setDefaultResourceOptions(const QVariantMap &options);
0128 
0129     /**
0130      * Sets the list of well known special collection @p types.
0131      */
0132     void setTypes(const QList<QByteArray> &types);
0133 
0134     /**
0135      * Sets the @p map of special collection types to display names.
0136      */
0137     void setNameForTypeMap(const QMap<QByteArray, QString> &map);
0138 
0139     /**
0140      * Sets the @p map of special collection types to icon names.
0141      */
0142     void setIconForTypeMap(const QMap<QByteArray, QString> &map);
0143 
0144 protected:
0145     /* reimpl */
0146     void doStart() override;
0147     /* reimpl */
0148     void slotResult(KJob *job) override;
0149 
0150 private:
0151     friend class DefaultResourceJobPrivate;
0152     std::unique_ptr<DefaultResourceJobPrivate> const d;
0153 };
0154 
0155 // ===================== GetLockJob ============================
0156 class GetLockJobPrivate;
0157 
0158 /**
0159   @internal
0160   Helper job for SpecialCollectionsRequestJob.
0161 
0162   If SpecialCollectionsRequestJob needs to create a collection, it sets a lock so
0163   that other instances do not interfere. This lock is an
0164   org.kde.pim.SpecialCollections name registered on D-Bus. This job is used to get
0165   that lock.
0166   This job will give the lock immediately if possible, or wait up to three
0167   seconds for the lock to be released.  If the lock is not released during
0168   that time, this job fails. (This is based on the assumption that
0169   SpecialCollectionsRequestJob operations should not take long.)
0170 
0171   Use the releaseLock() function to release the lock.
0172 
0173   @author Constantin Berzan <exit3219@gmail.com>
0174   @since 4.4
0175 */
0176 class AKONADI_TESTS_EXPORT GetLockJob : public KJob
0177 {
0178     Q_OBJECT
0179 
0180 public:
0181     /**
0182       Creates a new GetLockJob.
0183     */
0184     explicit GetLockJob(QObject *parent = nullptr);
0185 
0186     /**
0187       Destroys the GetLockJob.
0188     */
0189     ~GetLockJob() override;
0190 
0191     /* reimpl */
0192     void start() override;
0193 
0194 private:
0195     friend class GetLockJobPrivate;
0196     std::unique_ptr<GetLockJobPrivate> const d;
0197 
0198     Q_PRIVATE_SLOT(d, void doStart())
0199 };
0200 
0201 // ===================== helper functions ============================
0202 
0203 /**
0204  * Sets on @p col the required attributes of SpecialCollection type @p type
0205  * These are a SpecialCollectionAttribute and an EntityDisplayAttribute.
0206  * @param col collection
0207  * @param type collection type
0208  * @param nameForType collection name for type
0209  * @param iconForType collection icon for type
0210  */
0211 void setCollectionAttributes(Akonadi::Collection &col,
0212                              const QByteArray &type,
0213                              const QMap<QByteArray, QString> &nameForType,
0214                              const QMap<QByteArray, QString> &iconForType);
0215 
0216 /**
0217   Releases the SpecialCollectionsRequestJob lock that was obtained through
0218   GetLockJob.
0219   @return Whether the lock was released successfully.
0220 */
0221 bool AKONADI_TESTS_EXPORT releaseLock();
0222 
0223 } // namespace Akonadi