Akonadi

dbus.cpp
1 /*
2  Copyright (c) 2011 Volker Krause <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "dbus_p.h"
21 #include "instance_p.h"
22 
23 #include <QString>
24 #include <QStringView>
25 #include <QStringList>
26 #include <QVector>
27 
28 #include <array>
29 
30 using namespace Akonadi;
31 
32 #define AKONADI_DBUS_SERVER_SERVICE u"org.freedesktop.Akonadi"
33 #define AKONADI_DBUS_CONTROL_SERVICE u"org.freedesktop.Akonadi.Control"
34 #define AKONADI_DBUS_CONTROL_SERVICE_LOCK u"org.freedesktop.Akonadi.Control.lock"
35 #define AKONADI_DBUS_AGENTSERVER_SERVICE u"org.freedesktop.Akonadi.AgentServer"
36 #define AKONADI_DBUS_STORAGEJANITOR_SERVICE u"org.freedesktop.Akonadi.Janitor"
37 #define AKONADI_DBUS_SERVER_SERVICE_UPGRADING u"org.freedesktop.Akonadi.upgrading"
38 
39 static QString makeServiceName(QStringView base)
40 {
41  if (!Instance::hasIdentifier()) {
42  return base.toString();
43  }
44  return base + QLatin1Char('.') + Instance::identifier();
45 }
46 
47 QString DBus::serviceName(DBus::ServiceType serviceType)
48 {
49  switch (serviceType) {
50  case Server:
51  return makeServiceName(AKONADI_DBUS_SERVER_SERVICE);
52  case Control:
53  return makeServiceName(AKONADI_DBUS_CONTROL_SERVICE);
54  case ControlLock:
55  return makeServiceName(AKONADI_DBUS_CONTROL_SERVICE_LOCK);
56  case AgentServer:
57  return makeServiceName(AKONADI_DBUS_AGENTSERVER_SERVICE);
58  case StorageJanitor:
59  return makeServiceName(AKONADI_DBUS_STORAGEJANITOR_SERVICE);
60  case UpgradeIndicator:
61  return makeServiceName(AKONADI_DBUS_SERVER_SERVICE_UPGRADING);
62  }
63  Q_ASSERT(!"WTF?");
64  return QString();
65 }
66 
67 std::optional<DBus::AgentService> DBus::parseAgentServiceName(const QString &serviceName)
68 {
69  if (!serviceName.startsWith(AKONADI_DBUS_SERVER_SERVICE ".")) {
70  return std::nullopt;
71  }
72 
73  const auto parts = serviceName.midRef(QStringView(AKONADI_DBUS_SERVER_SERVICE ".").length()).split(QLatin1Char('.'));
74  if ((parts.size() == 2 && !Akonadi::Instance::hasIdentifier())
75  || (parts.size() == 3 && Akonadi::Instance::hasIdentifier() && Akonadi::Instance::identifier() == parts.at(2))) {
76  // switch on parts.at( 0 )
77  if (parts.at(0) == QLatin1String("Agent")) {
78  return AgentService{parts.at(1).toString(), DBus::Agent};
79  } else if (parts.at(0) == QLatin1String("Resource")) {
80  return AgentService{parts.at(1).toString(), DBus::Resource};
81  } else if (parts.at(0) == QLatin1String("Preprocessor")) {
82  return AgentService{parts.at(1).toString(), DBus::Preprocessor};
83  } else {
84  return std::nullopt;
85  }
86  }
87 
88  return std::nullopt;
89 }
90 
91 QString DBus::agentServiceName(const QString &agentIdentifier, DBus::AgentType agentType)
92 {
93  Q_ASSERT(!agentIdentifier.isEmpty());
94  Q_ASSERT(agentType != Unknown);
95  QString serviceName = QStringLiteral(AKONADI_DBUS_SERVER_SERVICE ".");
96  switch (agentType) {
97  case Agent:
98  serviceName += QLatin1String("Agent.");
99  break;
100  case Resource:
101  serviceName += QLatin1String("Resource.");
102  break;
103  case Preprocessor:
104  serviceName += QLatin1String("Preprocessor.");
105  break;
106  default:
107  Q_ASSERT(!"WTF?");
108  }
109  serviceName += agentIdentifier;
110  if (Akonadi::Instance::hasIdentifier()) {
111  serviceName += QLatin1Char('.') % Akonadi::Instance::identifier();
112  }
113  return serviceName;
114 }
115 
116 std::optional<QString> DBus::parseInstanceIdentifier(const QString &serviceName)
117 {
118  constexpr std::array<QStringView, 5> services = {QStringView{AKONADI_DBUS_STORAGEJANITOR_SERVICE},
119  QStringView{AKONADI_DBUS_SERVER_SERVICE_UPGRADING},
120  QStringView{AKONADI_DBUS_AGENTSERVER_SERVICE},
121  QStringView{AKONADI_DBUS_CONTROL_SERVICE_LOCK},
122  QStringView{AKONADI_DBUS_CONTROL_SERVICE}};
123  for (const auto &service : services) {
124  if (serviceName.startsWith(service)) {
125  if (serviceName != service) {
126  return serviceName.mid(service.length() + 1); // +1 for the separator "."
127  }
128  return std::nullopt;
129  }
130  }
131 
132  if (serviceName.startsWith(QStringView{AKONADI_DBUS_SERVER_SERVICE})) {
133  const auto split = serviceName.splitRef(QLatin1Char('.'));
134  if (split.size() <= 3) {
135  return std::nullopt;
136  }
137 
138  // [0]org.[1]freedesktop.[2]Akonadi.[3]type.[4]identifier.[5]instance
139  if (split[3] == QStringView{u"Agent"} || split[3] == QStringView{u"Resource"} || split[3] == QStringView{u"Preprocessor"}) {
140  if (split.size() == 6) {
141  return split[5].toString();
142  } else {
143  return std::nullopt;
144  }
145  } else {
146  return split[3].toString();
147  }
148  }
149 
150  return std::nullopt;
151 }
QVector< QStringRef > split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Provides methods to control the Akonadi server process.
Definition: control.h:64
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringRef midRef(int position, int n) const const
QString mid(int position, int n) const const
QString toString() const const
QVector< QStringRef > splitRef(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.