Messagelib

tagmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "tagmanager.h"
8#include "messagelist_debug.h"
9#include <Akonadi/Monitor>
10#include <Akonadi/TagAttribute>
11#include <Akonadi/TagFetchJob>
12#include <Akonadi/TagFetchScope>
13using namespace MessageList::Core;
14TagManager::TagManager(QObject *parent)
15 : QObject{parent}
16 , mMonitor(new Akonadi::Monitor(this))
17{
18 init();
19}
20
21TagManager::~TagManager() = default;
22
23TagManager *TagManager::self()
24{
25 static TagManager s_self;
26 return &s_self;
27}
28
29void TagManager::init()
30{
31 mMonitor->setObjectName(QLatin1StringView("MessageListTagMonitor"));
32 mMonitor->setTypeMonitored(Akonadi::Monitor::Tags);
33 connect(mMonitor, &Akonadi::Monitor::tagAdded, this, &TagManager::slotTagsChanged);
34 connect(mMonitor, &Akonadi::Monitor::tagRemoved, this, &TagManager::slotTagsChanged);
35 connect(mMonitor, &Akonadi::Monitor::tagChanged, this, &TagManager::slotTagsChanged);
36 slotTagsChanged();
37}
38
39void TagManager::slotTagsChanged()
40{
41 auto fetchJob = new Akonadi::TagFetchJob(this);
42 fetchJob->fetchScope().fetchAttribute<Akonadi::TagAttribute>();
43 connect(fetchJob, &Akonadi::TagFetchJob::result, this, &TagManager::slotTagsFetched);
44}
45
46QMap<QString, QString> TagManager::mapTag() const
47{
48 return mMapTag;
49}
50
51void TagManager::setMapTag(const QMap<QString, QString> &newMapTag)
52{
53 mMapTag = newMapTag;
54}
55
56void TagManager::slotTagsFetched(KJob *job)
57{
58 if (job->error()) {
59 qCWarning(MESSAGELIST_LOG) << "Failed to load tags " << job->errorString();
60 return;
61 }
62 mMapTag.clear();
63 auto fetchJob = static_cast<Akonadi::TagFetchJob *>(job);
64 const auto tags{fetchJob->tags()};
65 Q_EMIT tagsFetched(tags);
66
67 for (const Akonadi::Tag &akonadiTag : tags) {
68 const QString tagUrl = akonadiTag.url().url();
69 mMapTag.insert(akonadiTag.name(), tagUrl);
70 }
71}
72
73QString TagManager::tagFromName(const QString &name) const
74{
75 return mMapTag.value(name);
76}
77
78#include "moc_tagmanager.cpp"
void tagRemoved(const Akonadi::Tag &tag)
void tagChanged(const Akonadi::Tag &tag)
void tagAdded(const Akonadi::Tag &tag)
virtual QString errorString() const
int error() const
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void clear()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:49:15 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.