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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #include "agentprocessinstance.h"
0008 
0009 #include "agenttype.h"
0010 #include "akonadicontrol_debug.h"
0011 #include "private/standarddirs_p.h"
0012 #include "processcontrol.h"
0013 
0014 #include <QStandardPaths>
0015 
0016 using namespace Akonadi;
0017 
0018 AgentProcessInstance::AgentProcessInstance(AgentManager &manager)
0019     : AgentInstance(manager)
0020 {
0021 }
0022 
0023 bool AgentProcessInstance::start(const AgentType &agentInfo)
0024 {
0025     Q_ASSERT(!identifier().isEmpty());
0026     if (identifier().isEmpty()) {
0027         return false;
0028     }
0029 
0030     setAgentType(agentInfo.identifier);
0031 
0032     Q_ASSERT(agentInfo.launchMethod == AgentType::Process || agentInfo.launchMethod == AgentType::Launcher);
0033 
0034     const QString executable = (agentInfo.launchMethod == AgentType::Process) ? Akonadi::StandardDirs::findExecutable(agentInfo.exec) : agentInfo.exec;
0035 
0036     if (executable.isEmpty()) {
0037         qCWarning(AKONADICONTROL_LOG) << "Unable to find agent executable" << agentInfo.exec;
0038         return false;
0039     }
0040 
0041     mController = std::make_unique<Akonadi::ProcessControl>();
0042     connect(mController.get(), &ProcessControl::unableToStart, this, &AgentProcessInstance::failedToStart);
0043 
0044     if (agentInfo.launchMethod == AgentType::Process) {
0045         const QStringList arguments = {QStringLiteral("--identifier"), identifier()};
0046         mController->start(executable, arguments);
0047     } else {
0048         Q_ASSERT(agentInfo.launchMethod == AgentType::Launcher);
0049         const QStringList arguments = QStringList() << executable << identifier();
0050         const QString agentLauncherExec = Akonadi::StandardDirs::findExecutable(QStringLiteral("akonadi_agent_launcher"));
0051         mController->start(agentLauncherExec, arguments);
0052     }
0053     return true;
0054 }
0055 
0056 void AgentProcessInstance::quit()
0057 {
0058     mController->setCrashPolicy(Akonadi::ProcessControl::StopOnCrash);
0059     AgentInstance::quit();
0060 }
0061 
0062 void AgentProcessInstance::cleanup()
0063 {
0064     mController->setCrashPolicy(Akonadi::ProcessControl::StopOnCrash);
0065     AgentInstance::cleanup();
0066 }
0067 
0068 void AgentProcessInstance::restartWhenIdle()
0069 {
0070     if (mController->isRunning()) {
0071         if (status() != 1) {
0072             mController->restartOnceWhenFinished();
0073             quit();
0074         }
0075     } else {
0076         mController->start();
0077     }
0078 }
0079 
0080 void Akonadi::AgentProcessInstance::configure(qlonglong windowId)
0081 {
0082     controlInterface()->configure(windowId);
0083 }
0084 
0085 void AgentProcessInstance::failedToStart()
0086 {
0087     statusChanged(2 /*Broken*/, QStringLiteral("Unable to start."));
0088 }
0089 
0090 #include "moc_agentprocessinstance.cpp"