Akonadi

dbus.cpp
1/*
2 SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dbus_p.h"
8#include "instance_p.h"
9
10#include <QList>
11#include <QString>
12#include <QStringList>
13#include <QStringView>
14
15#include <array>
16
17using namespace Akonadi;
18
19#define AKONADI_DBUS_SERVER_SERVICE u"org.freedesktop.Akonadi"
20#define AKONADI_DBUS_CONTROL_SERVICE u"org.freedesktop.Akonadi.Control"
21#define AKONADI_DBUS_CONTROL_SERVICE_LOCK u"org.freedesktop.Akonadi.Control.lock"
22#define AKONADI_DBUS_AGENTSERVER_SERVICE u"org.freedesktop.Akonadi.AgentServer"
23#define AKONADI_DBUS_STORAGEJANITOR_SERVICE u"org.freedesktop.Akonadi.Janitor"
24#define AKONADI_DBUS_SERVER_SERVICE_UPGRADING u"org.freedesktop.Akonadi.upgrading"
25
26static QString makeServiceName(QStringView base)
27{
28 if (!Instance::hasIdentifier()) {
29 return base.toString();
30 }
31 return base + QLatin1Char('.') + Instance::identifier();
32}
33
34QString DBus::serviceName(DBus::ServiceType serviceType)
35{
36 switch (serviceType) {
37 case Server:
38 return makeServiceName(AKONADI_DBUS_SERVER_SERVICE);
39 case Control:
40 return makeServiceName(AKONADI_DBUS_CONTROL_SERVICE);
41 case ControlLock:
42 return makeServiceName(AKONADI_DBUS_CONTROL_SERVICE_LOCK);
43 case AgentServer:
44 return makeServiceName(AKONADI_DBUS_AGENTSERVER_SERVICE);
45 case StorageJanitor:
46 return makeServiceName(AKONADI_DBUS_STORAGEJANITOR_SERVICE);
47 case UpgradeIndicator:
48 return makeServiceName(AKONADI_DBUS_SERVER_SERVICE_UPGRADING);
49 }
50 Q_ASSERT(!"WTF?");
51 return QString();
52}
53
54std::optional<DBus::AgentService> DBus::parseAgentServiceName(const QString &serviceName)
55{
56 if (!serviceName.startsWith(AKONADI_DBUS_SERVER_SERVICE ".")) {
57 return std::nullopt;
58 }
59 const auto parts = QStringView(serviceName).mid(QStringView(AKONADI_DBUS_SERVER_SERVICE ".").length()).split(QLatin1Char('.'));
60 if ((parts.size() == 2 && !Akonadi::Instance::hasIdentifier())
61 || (parts.size() == 3 && Akonadi::Instance::hasIdentifier() && Akonadi::Instance::identifier() == parts.at(2))) {
62 // switch on parts.at( 0 )
63 if (parts.at(0) == QLatin1StringView("Agent")) {
64 return AgentService{parts.at(1).toString(), DBus::Agent};
65 } else if (parts.at(0) == QLatin1StringView("Resource")) {
66 return AgentService{parts.at(1).toString(), DBus::Resource};
67 } else if (parts.at(0) == QLatin1StringView("Preprocessor")) {
68 return AgentService{parts.at(1).toString(), DBus::Preprocessor};
69 } else {
70 return std::nullopt;
71 }
72 }
73
74 return std::nullopt;
75}
76
77QString DBus::agentServiceName(const QString &agentIdentifier, DBus::AgentType agentType)
78{
79 Q_ASSERT(!agentIdentifier.isEmpty());
80 Q_ASSERT(agentType != Unknown);
81 QString serviceName = QStringLiteral(AKONADI_DBUS_SERVER_SERVICE ".");
82 switch (agentType) {
83 case Agent:
84 serviceName += QLatin1StringView("Agent.");
85 break;
86 case Resource:
87 serviceName += QLatin1StringView("Resource.");
88 break;
89 case Preprocessor:
90 serviceName += QLatin1StringView("Preprocessor.");
91 break;
92 default:
93 Q_ASSERT(!"WTF?");
94 }
95 serviceName += agentIdentifier;
96 if (Akonadi::Instance::hasIdentifier()) {
97 serviceName += QLatin1Char('.') % Akonadi::Instance::identifier();
98 }
99 return serviceName;
100}
101
102std::optional<QString> DBus::parseInstanceIdentifier(const QString &serviceName)
103{
104 constexpr std::array<QStringView, 5> services = {QStringView{AKONADI_DBUS_STORAGEJANITOR_SERVICE},
105 QStringView{AKONADI_DBUS_SERVER_SERVICE_UPGRADING},
106 QStringView{AKONADI_DBUS_AGENTSERVER_SERVICE},
107 QStringView{AKONADI_DBUS_CONTROL_SERVICE_LOCK},
108 QStringView{AKONADI_DBUS_CONTROL_SERVICE}};
109 for (const auto &service : services) {
110 if (serviceName.startsWith(service)) {
111 if (serviceName != service) {
112 return serviceName.mid(service.length() + 1); // +1 for the separator "."
113 }
114 return std::nullopt;
115 }
116 }
117
118 if (serviceName.startsWith(QStringView{AKONADI_DBUS_SERVER_SERVICE})) {
119 const auto split = QStringView(serviceName).split(QLatin1Char('.'));
120 if (split.size() <= 3) {
121 return std::nullopt;
122 }
123
124 // [0]org.[1]freedesktop.[2]Akonadi.[3]type.[4]identifier.[5]instance
125 if (split[3] == QStringView{u"Agent"} || split[3] == QStringView{u"Resource"} || split[3] == QStringView{u"Preprocessor"}) {
126 if (split.size() == 6) {
127 return split[5].toString();
128 } else {
129 return std::nullopt;
130 }
131 } else {
132 return split[3].toString();
133 }
134 }
135
136 return std::nullopt;
137}
Provides methods to control the Akonadi server process.
Definition control.h:54
Helper integration between Akonadi and Qt.
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QStringView mid(qsizetype start, qsizetype length) const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:49:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.