Pimcommon

imapresourcecapabilitiesmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2015-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "imapresourcecapabilitiesmanager.h"
8 #include "pimcommonakonadi_debug.h"
9 #include "util/pimutil.h"
10 
11 #include <Akonadi/AgentManager>
12 #include <Akonadi/ServerManager>
13 
14 #include <QDBusConnection>
15 #include <QDBusInterface>
16 #include <QDBusPendingCall>
17 #include <QDBusPendingReply>
18 
19 using namespace PimCommon;
20 ImapResourceCapabilitiesManager::ImapResourceCapabilitiesManager(QObject *parent)
21  : QObject(parent)
22 {
23  init();
24 }
25 
26 ImapResourceCapabilitiesManager::~ImapResourceCapabilitiesManager() = default;
27 
28 void ImapResourceCapabilitiesManager::slotInstanceAdded(const Akonadi::AgentInstance &instance)
29 {
30  searchCapabilities(instance.identifier());
31 }
32 
33 void ImapResourceCapabilitiesManager::slotInstanceRemoved(const Akonadi::AgentInstance &instance)
34 {
35  mImapResource.remove(instance.identifier());
36 }
37 
38 void ImapResourceCapabilitiesManager::searchCapabilities(const QString &identifier)
39 {
40  // By default makes it as true.
41  mImapResource.insert(identifier, true);
42  const QString service = Akonadi::ServerManager::agentServiceName(Akonadi::ServerManager::Resource, identifier);
43  QDBusInterface iface(service, QStringLiteral("/"), QStringLiteral("org.kde.Akonadi.ImapResourceBase"), QDBusConnection::sessionBus(), this);
44 
45  if (iface.isValid()) {
46  QDBusPendingCall call = iface.asyncCall(QStringLiteral("serverCapabilities"));
47  auto watcher = new QDBusPendingCallWatcher(call, this);
48  watcher->setProperty("identifier", identifier);
49  connect(watcher, &QDBusPendingCallWatcher::finished, this, &ImapResourceCapabilitiesManager::slotCapabilities);
50  } else {
51  qCDebug(PIMCOMMONAKONADI_LOG) << "interface not valid";
52  }
53 }
54 
55 void ImapResourceCapabilitiesManager::slotCapabilities(QDBusPendingCallWatcher *watcher)
56 {
57  QDBusPendingReply<QStringList> reply = *watcher;
58  if (reply.isValid()) {
59  if (watcher->property("identifier").isValid()) {
60  const QStringList capabilities = reply.value();
61  mImapResource.insert(watcher->property("identifier").toString(), capabilities.contains(QLatin1String("ANNOTATEMORE")));
62  }
63  }
64  watcher->deleteLater();
65  watcher = nullptr;
66 }
67 
68 void ImapResourceCapabilitiesManager::init()
69 {
71  for (const Akonadi::AgentInstance &instance : lstInstances) {
72  const QString identifier = instance.identifier();
73  if (PimCommon::Util::isImapResource(identifier)) {
74  searchCapabilities(identifier);
75  }
76  }
77  connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceAdded, this, &ImapResourceCapabilitiesManager::slotInstanceAdded);
78  connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceRemoved, this, &ImapResourceCapabilitiesManager::slotInstanceRemoved);
79 }
80 
81 bool ImapResourceCapabilitiesManager::hasAnnotationSupport(const QString &identifier) const
82 {
83  if (!PimCommon::Util::isImapResource(identifier)) {
84  return false;
85  }
86  return mImapResource.value(identifier, true);
87 }
void instanceRemoved(const Akonadi::AgentInstance &instance)
QString identifier() const
bool isValid() const const
void finished(QDBusPendingCallWatcher *self)
const T value(const Key &key) const const
bool isValid() const const
folderdialogacltab.h
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static QString agentServiceName(ServiceAgentType agentType, const QString &identifier)
QHash::iterator insert(const Key &key, const T &value)
void deleteLater()
QDBusConnection sessionBus()
void instanceAdded(const Akonadi::AgentInstance &instance)
void init(KXmlGuiWindow *window, KgDifficulty *difficulty=nullptr)
T1 value() const const
int remove(const Key &key)
Capabilities capabilities()
static AgentManager * self()
AgentInstance::List instances() const
QString toString() const const
QVariant property(const char *name) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 9 2022 03:56:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.