Akonadi

akonadicontrol/agenttype.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "agenttype.h"
8 #include "agentmanager.h"
9 #include "akonadicontrol_debug.h"
10 
11 #include <private/capabilities_p.h>
12 #include <private/standarddirs_p.h>
13 
14 #include <KConfigGroup>
15 #include <KDesktopFile>
16 
17 using namespace Akonadi;
18 
19 const QLatin1String AgentType::CapabilityUnique = QLatin1String(AKONADI_AGENT_CAPABILITY_UNIQUE);
20 const QLatin1String AgentType::CapabilityResource = QLatin1String(AKONADI_AGENT_CAPABILITY_RESOURCE);
21 const QLatin1String AgentType::CapabilityAutostart = QLatin1String(AKONADI_AGENT_CAPABILITY_AUTOSTART);
22 const QLatin1String AgentType::CapabilityPreprocessor = QLatin1String(AKONADI_AGENT_CAPABILITY_PREPROCESSOR);
23 const QLatin1String AgentType::CapabilitySearch = QLatin1String(AKONADI_AGENT_CAPABILITY_SEARCH);
24 
26 {
27 }
28 
29 bool AgentType::load(const QString &fileName, AgentManager *manager)
30 {
31  Q_UNUSED(manager)
32 
33  if (!KDesktopFile::isDesktopFile(fileName)) {
34  return false;
35  }
36 
37  KDesktopFile desktopFile(fileName);
38  KConfigGroup group = desktopFile.desktopGroup();
39 
40  const QStringList keyList(group.keyList());
41  for (const QString &key : keyList) {
42  if (key.startsWith(QLatin1String("X-Akonadi-Custom-"))) {
43  const QString customKey = key.mid(17, key.length());
44  const QStringList val = group.readEntry(key, QStringList());
45  if (val.size() == 1) {
46  custom[customKey] = QVariant(val[0]);
47  } else {
48  custom[customKey] = val;
49  }
50  }
51  }
52 
53  name = desktopFile.readName();
54  comment = desktopFile.readComment();
55  icon = desktopFile.readIcon();
56  mimeTypes = group.readEntry(QStringLiteral("X-Akonadi-MimeTypes"), QStringList());
57  capabilities = group.readEntry(QStringLiteral("X-Akonadi-Capabilities"), QStringList());
58  exec = group.readEntry(QStringLiteral("Exec"));
59  identifier = group.readEntry(QStringLiteral("X-Akonadi-Identifier"));
60  launchMethod = Process; // Save default
61 
62  const QString method = group.readEntry(QStringLiteral("X-Akonadi-LaunchMethod"));
63  if (method.compare(QLatin1String("AgentProcess"), Qt::CaseInsensitive) == 0) {
64  launchMethod = Process;
65  } else if (method.compare(QLatin1String("AgentServer"), Qt::CaseInsensitive) == 0) {
66  launchMethod = Server;
67  } else if (method.compare(QLatin1String("AgentLauncher"), Qt::CaseInsensitive) == 0) {
68  launchMethod = Launcher;
69  } else if (!method.isEmpty()) {
70  qCWarning(AKONADICONTROL_LOG) << "Invalid exec method:" << method << "falling back to AgentProcess";
71  }
72 
73  if (identifier.isEmpty()) {
74  qCWarning(AKONADICONTROL_LOG) << "Agent desktop file" << fileName << "contains empty identifier";
75  return false;
76  }
77  if (exec.isEmpty()) {
78  qCWarning(AKONADICONTROL_LOG) << "Agent desktop file" << fileName << "contains empty Exec entry";
79  return false;
80  }
81 
82  // autostart implies unique
83  if (capabilities.contains(CapabilityAutostart) && !capabilities.contains(CapabilityUnique)) {
84  capabilities << CapabilityUnique;
85  }
86 
87  // load instance count if needed
88  if (!capabilities.contains(CapabilityUnique)) {
89  QSettings agentrc(StandardDirs::agentsConfigFile(StandardDirs::ReadOnly), QSettings::IniFormat);
90  instanceCounter = agentrc.value(QStringLiteral("InstanceCounters/%1/InstanceCounter").arg(identifier), 0).toInt();
91  }
92 
93  return true;
94 }
95 
96 void AgentType::save(QSettings *config) const
97 {
98  Q_ASSERT(config);
99  if (!capabilities.contains(CapabilityUnique)) {
100  config->setValue(QStringLiteral("InstanceCounters/%1/InstanceCounter").arg(identifier), instanceCounter);
101  }
102 }
QString readEntry(const char *key, const char *aDefault=nullptr) const
CaseInsensitive
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QStringList capabilities() const
Returns the list of supported capabilities of the agent type.
int size() const const
Provides an interface to retrieve agent types and manage agent instances.
bool isEmpty() const const
static bool isDesktopFile(const QString &path)
KSharedConfigPtr config()
QString identifier() const
Returns the unique identifier of the agent type.
QStringList keyList() const
QIcon icon() const
Returns the icon of the agent type.
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString mid(int position, int n) const const
QStringList mimeTypes() const
Returns the list of supported mime types of the agent type.
AgentType()
Creates a new agent type.
Helper integration between Akonadi and Qt.
QString name() const
Returns the i18n'ed name of the agent type.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.