File indexing completed on 2025-01-05 04:46:27

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "agenttype.h"
0008 #include "agentmanager.h"
0009 #include "akonadicontrol_debug.h"
0010 
0011 #include "private/capabilities_p.h"
0012 #include "private/standarddirs_p.h"
0013 
0014 #include <KConfigGroup>
0015 #include <KDesktopFile>
0016 
0017 using namespace Akonadi;
0018 
0019 const QLatin1StringView AgentType::CapabilityUnique = QLatin1StringView(AKONADI_AGENT_CAPABILITY_UNIQUE);
0020 const QLatin1StringView AgentType::CapabilityResource = QLatin1StringView(AKONADI_AGENT_CAPABILITY_RESOURCE);
0021 const QLatin1StringView AgentType::CapabilityAutostart = QLatin1StringView(AKONADI_AGENT_CAPABILITY_AUTOSTART);
0022 const QLatin1StringView AgentType::CapabilityPreprocessor = QLatin1StringView(AKONADI_AGENT_CAPABILITY_PREPROCESSOR);
0023 const QLatin1StringView AgentType::CapabilitySearch = QLatin1StringView(AKONADI_AGENT_CAPABILITY_SEARCH);
0024 
0025 AgentType::AgentType()
0026 {
0027 }
0028 
0029 bool AgentType::load(const QString &fileName, AgentManager *manager)
0030 {
0031     Q_UNUSED(manager)
0032 
0033     if (!KDesktopFile::isDesktopFile(fileName)) {
0034         return false;
0035     }
0036 
0037     KDesktopFile desktopFile(fileName);
0038     KConfigGroup group = desktopFile.desktopGroup();
0039 
0040     const QStringList keyList(group.keyList());
0041     for (const QString &key : keyList) {
0042         if (key.startsWith(QLatin1StringView("X-Akonadi-Custom-"))) {
0043             const QString customKey = key.mid(17, key.length());
0044             const QStringList val = group.readEntry(key, QStringList());
0045             if (val.size() == 1) {
0046                 custom[customKey] = QVariant(val[0]);
0047             } else {
0048                 custom[customKey] = val;
0049             }
0050         }
0051     }
0052 
0053     name = desktopFile.readName();
0054     comment = desktopFile.readComment();
0055     icon = desktopFile.readIcon();
0056     mimeTypes = group.readEntry(QStringLiteral("X-Akonadi-MimeTypes"), QStringList());
0057     capabilities = group.readEntry(QStringLiteral("X-Akonadi-Capabilities"), QStringList());
0058     exec = group.readEntry(QStringLiteral("Exec"));
0059     identifier = group.readEntry(QStringLiteral("X-Akonadi-Identifier"));
0060     launchMethod = Process; // Save default
0061 
0062     const QString method = group.readEntry(QStringLiteral("X-Akonadi-LaunchMethod"));
0063     if (method.compare(QLatin1StringView("AgentProcess"), Qt::CaseInsensitive) == 0) {
0064         launchMethod = Process;
0065     } else if (method.compare(QLatin1StringView("AgentServer"), Qt::CaseInsensitive) == 0) {
0066         launchMethod = Server;
0067     } else if (method.compare(QLatin1StringView("AgentLauncher"), Qt::CaseInsensitive) == 0) {
0068         launchMethod = Launcher;
0069     } else if (!method.isEmpty()) {
0070         qCWarning(AKONADICONTROL_LOG) << "Invalid exec method:" << method << "falling back to AgentProcess";
0071     }
0072 
0073     if (identifier.isEmpty()) {
0074         qCWarning(AKONADICONTROL_LOG) << "Agent desktop file" << fileName << "contains empty identifier";
0075         return false;
0076     }
0077     if (exec.isEmpty()) {
0078         qCWarning(AKONADICONTROL_LOG) << "Agent desktop file" << fileName << "contains empty Exec entry";
0079         return false;
0080     }
0081 
0082     // autostart implies unique
0083     if (capabilities.contains(CapabilityAutostart) && !capabilities.contains(CapabilityUnique)) {
0084         capabilities << CapabilityUnique;
0085     }
0086 
0087     // load instance count if needed
0088     if (!capabilities.contains(CapabilityUnique)) {
0089         QSettings agentrc(StandardDirs::agentsConfigFile(StandardDirs::ReadOnly), QSettings::IniFormat);
0090         instanceCounter = agentrc.value(QStringLiteral("InstanceCounters/%1/InstanceCounter").arg(identifier), 0).toInt();
0091     }
0092 
0093     return true;
0094 }
0095 
0096 void AgentType::save(QSettings *config) const
0097 {
0098     Q_ASSERT(config);
0099     if (!capabilities.contains(CapabilityUnique)) {
0100         config->setValue(QStringLiteral("InstanceCounters/%1/InstanceCounter").arg(identifier), instanceCounter);
0101     }
0102 }