Akonadi

akonadicontrol/agenttype.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
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
17using namespace Akonadi;
18
19const QLatin1StringView AgentType::CapabilityUnique = QLatin1StringView(AKONADI_AGENT_CAPABILITY_UNIQUE);
20const QLatin1StringView AgentType::CapabilityResource = QLatin1StringView(AKONADI_AGENT_CAPABILITY_RESOURCE);
21const QLatin1StringView AgentType::CapabilityAutostart = QLatin1StringView(AKONADI_AGENT_CAPABILITY_AUTOSTART);
22const QLatin1StringView AgentType::CapabilityPreprocessor = QLatin1StringView(AKONADI_AGENT_CAPABILITY_PREPROCESSOR);
23const QLatin1StringView AgentType::CapabilitySearch = QLatin1StringView(AKONADI_AGENT_CAPABILITY_SEARCH);
24
25AgentType::AgentType()
26{
27}
28
29bool 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(QLatin1StringView("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(QLatin1StringView("AgentProcess"), Qt::CaseInsensitive) == 0) {
64 launchMethod = Process;
65 } else if (method.compare(QLatin1StringView("AgentServer"), Qt::CaseInsensitive) == 0) {
66 launchMethod = Server;
67 } else if (method.compare(QLatin1StringView("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
96void 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}
The agent manager has knowledge about all available agents (it scans for .desktop files in the agent ...
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList keyList() const
static bool isDesktopFile(const QString &path)
Helper integration between Akonadi and Qt.
qsizetype size() const const
void setValue(QAnyStringView key, const QVariant &value)
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
QString mid(qsizetype position, qsizetype n) const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:31:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.