File indexing completed on 2024-12-22 04:57:33

0001 /*
0002     SPDX-FileCopyrightText: 2007 Till Adam <adam@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <Akonadi/Collection>
0010 #include <Akonadi/ResourceBase>
0011 
0012 class QTimer;
0013 class QFileInfo;
0014 class KDirWatch;
0015 namespace Akonadi_Maildir_Resource
0016 {
0017 class MaildirSettings;
0018 }
0019 namespace KPIM
0020 {
0021 class Maildir;
0022 }
0023 
0024 class MaildirResource : public Akonadi::ResourceBase, public Akonadi::AgentBase::ObserverV2
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     explicit MaildirResource(const QString &id);
0030     ~MaildirResource() override;
0031 
0032     virtual QString defaultResourceType();
0033 
0034 protected Q_SLOTS:
0035     void retrieveCollections() override;
0036     void retrieveItems(const Akonadi::Collection &col) override;
0037     bool retrieveItems(const Akonadi::Item::List &items, const QSet<QByteArray> &parts) override;
0038 
0039 protected:
0040     virtual QString itemMimeType() const;
0041 
0042     void aboutToQuit() override;
0043 
0044     void itemAdded(const Akonadi::Item &item, const Akonadi::Collection &collection) override;
0045     void itemChanged(const Akonadi::Item &item, const QSet<QByteArray> &parts) override;
0046     void itemMoved(const Akonadi::Item &item, const Akonadi::Collection &source, const Akonadi::Collection &dest) override;
0047     void itemRemoved(const Akonadi::Item &item) override;
0048 
0049     void collectionAdded(const Akonadi::Collection &collection, const Akonadi::Collection &parent) override;
0050     void collectionChanged(const Akonadi::Collection &collection) override;
0051     // do not hide the other variant, use implementation from base class
0052     // which just forwards to the one above
0053     using Akonadi::AgentBase::ObserverV2::collectionChanged;
0054     void collectionMoved(const Akonadi::Collection &collection, const Akonadi::Collection &source, const Akonadi::Collection &dest) override;
0055     void collectionRemoved(const Akonadi::Collection &collection) override;
0056 
0057 private Q_SLOTS:
0058     void configurationChanged();
0059     void slotItemsRetrievalResult(KJob *job);
0060     void slotDirChanged(const QString &dir);
0061     void slotFileChanged(const QFileInfo &fileInfo);
0062     void fsWatchDirFetchResult(KJob *job);
0063     void fsWatchFileFetchResult(KJob *job);
0064     void fsWatchFileModifyResult(KJob *job);
0065     // Try to restore some config values from Akonadi data
0066     void attemptConfigRestoring(KJob *job);
0067     void changedCleaner();
0068 
0069 private:
0070     bool ensureDirExists();
0071     bool ensureSaneConfiguration();
0072     Akonadi::Collection::List listRecursive(const Akonadi::Collection &root, const KPIM::Maildir &dir);
0073     /** Creates a maildir object for the collection @p col, given it has the full ancestor chain set. */
0074     KPIM::Maildir maildirForCollection(const Akonadi::Collection &col);
0075     /** Creates a collection object for the given maildir @p md. */
0076     Akonadi::Collection collectionForMaildir(const KPIM::Maildir &md) const;
0077 
0078     QString maildirPathForCollection(const Akonadi::Collection &collection) const;
0079     void stopMaildirScan(const KPIM::Maildir &maildir);
0080     void restartMaildirScan(const KPIM::Maildir &maildir);
0081 
0082 private:
0083     Akonadi_Maildir_Resource::MaildirSettings *mSettings = nullptr;
0084     KDirWatch *const mFsWatcher;
0085     QHash<QString, KPIM::Maildir> mMaildirsForCollection;
0086     QSet<QString> mChangedFiles; // files changed by the resource and that should be ignored in slotFileChanged
0087     QTimer *const mChangedCleanerTimer;
0088 };