File indexing completed on 2024-05-12 05:37:18

0001 /*
0002     SPDX-FileCopyrightText: 2011 Viranch Mehta <viranch.mehta@gmail.com>
0003     SPDX-FileCopyrightText: 2019 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "hotplugjob.h"
0009 
0010 #include "deviceserviceaction.h"
0011 
0012 #include <KLocalizedString>
0013 #include <KService>
0014 #include <Solid/Device>
0015 
0016 #include <QDebug>
0017 #include <QStandardPaths>
0018 
0019 void HotplugJob::start()
0020 {
0021     if (operationName() == QLatin1String("invokeAction")) {
0022         const QString desktopFile = parameters()[QStringLiteral("predicate")].toString();
0023         const QString filePath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "solid/actions/" + desktopFile);
0024 
0025         auto services = KService(filePath).actions();
0026         if (services.size() < 1) {
0027             qWarning() << "Failed to resolve hotplugjob action" << desktopFile << filePath;
0028             setError(KJob::UserDefinedError);
0029             setErrorText(i18nc("error; %1 is the desktop file name of the service", "Failed to resolve service action for %1.", desktopFile));
0030             setResult(false); // calls emitResult internally.
0031             return;
0032         }
0033         // Cannot be > 1, we only have one filePath, and < 1 was handled as error.
0034         Q_ASSERT(services.size() == 1);
0035 
0036         DeviceServiceAction action;
0037         action.setService(services.takeFirst());
0038 
0039         Solid::Device device(m_dest);
0040         action.execute(device);
0041     }
0042 
0043     emitResult();
0044 }