File indexing completed on 2024-05-26 05:22:22

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "tagmonitormanager.h"
0008 #include "kmail_debug.h"
0009 
0010 #include <Akonadi/Monitor>
0011 #include <Akonadi/TagAttribute>
0012 #include <Akonadi/TagFetchJob>
0013 #include <Akonadi/TagFetchScope>
0014 
0015 TagMonitorManager::TagMonitorManager(QObject *parent)
0016     : QObject(parent)
0017     , mMonitor(new Akonadi::Monitor(this))
0018 {
0019     mMonitor->setObjectName(QLatin1StringView("TagActionManagerMonitor"));
0020     mMonitor->setTypeMonitored(Akonadi::Monitor::Tags);
0021     mMonitor->tagFetchScope().fetchAttribute<Akonadi::TagAttribute>();
0022     connect(mMonitor, &Akonadi::Monitor::tagAdded, this, &TagMonitorManager::onTagAdded);
0023     connect(mMonitor, &Akonadi::Monitor::tagRemoved, this, &TagMonitorManager::onTagRemoved);
0024     connect(mMonitor, &Akonadi::Monitor::tagChanged, this, &TagMonitorManager::onTagChanged);
0025     createActions();
0026 }
0027 
0028 TagMonitorManager::~TagMonitorManager() = default;
0029 
0030 TagMonitorManager *TagMonitorManager::self()
0031 {
0032     static TagMonitorManager s_self;
0033     return &s_self;
0034 }
0035 
0036 void TagMonitorManager::createActions()
0037 {
0038     if (mTags.isEmpty()) {
0039         auto fetchJob = new Akonadi::TagFetchJob(this);
0040         fetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
0041         connect(fetchJob, &Akonadi::TagFetchJob::result, this, &TagMonitorManager::finishedTagListing);
0042     }
0043 }
0044 
0045 void TagMonitorManager::finishedTagListing(KJob *job)
0046 {
0047     if (job->error()) {
0048         qCWarning(KMAIL_LOG) << job->errorString();
0049     }
0050     auto fetchJob = static_cast<Akonadi::TagFetchJob *>(job);
0051     const Akonadi::Tag::List lstTags = fetchJob->tags();
0052     for (const Akonadi::Tag &result : lstTags) {
0053         mTags.append(MailCommon::Tag::fromAkonadi(result));
0054     }
0055     std::sort(mTags.begin(), mTags.end(), MailCommon::Tag::compare);
0056     Q_EMIT fetchTagDone();
0057 }
0058 
0059 QList<MailCommon::Tag::Ptr> TagMonitorManager::tags() const
0060 {
0061     return mTags;
0062 }
0063 
0064 void TagMonitorManager::onTagAdded(const Akonadi::Tag &akonadiTag)
0065 {
0066     mTags.append(MailCommon::Tag::fromAkonadi(akonadiTag));
0067     std::sort(mTags.begin(), mTags.end(), MailCommon::Tag::compare);
0068     Q_EMIT tagAdded();
0069 }
0070 
0071 void TagMonitorManager::onTagRemoved(const Akonadi::Tag &akonadiTag)
0072 {
0073     for (const MailCommon::Tag::Ptr &tag : std::as_const(mTags)) {
0074         if (tag->id() == akonadiTag.id()) {
0075             mTags.removeAll(tag);
0076             break;
0077         }
0078     }
0079     Q_EMIT tagRemoved();
0080 }
0081 
0082 void TagMonitorManager::onTagChanged(const Akonadi::Tag &akonadiTag)
0083 {
0084     for (const MailCommon::Tag::Ptr &tag : std::as_const(mTags)) {
0085         if (tag->id() == akonadiTag.id()) {
0086             mTags.removeAll(tag);
0087             break;
0088         }
0089     }
0090     mTags.append(MailCommon::Tag::fromAkonadi(akonadiTag));
0091     std::sort(mTags.begin(), mTags.end(), MailCommon::Tag::compare);
0092     Q_EMIT tagChanged();
0093 }
0094 
0095 #include "moc_tagmonitormanager.cpp"