Akonadi

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

KDE's Doxygen guidelines are available online.