File indexing completed on 2024-05-12 05:35:50

0001 /*
0002     SPDX-FileCopyrightText: 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ActionItem.h"
0008 #include "SolidActionData.h"
0009 
0010 #include <QString>
0011 
0012 #include <KConfigGroup>
0013 #include <KDesktopFile>
0014 
0015 #include <Solid/DeviceInterface>
0016 
0017 ActionItem::ActionItem(const QString &pathToDesktop, const QString &action, QObject *parent)
0018     : desktopMasterPath(pathToDesktop)
0019     , actionName(action)
0020 {
0021     Q_UNUSED(parent);
0022 
0023     // Create the desktop file
0024     desktopFileMaster = new KDesktopFile(desktopMasterPath);
0025     desktopWritePath = desktopFileMaster->locateLocal(desktopMasterPath);
0026     desktopFileWrite = new KDesktopFile(desktopWritePath);
0027     // Now we can fill the action groups list
0028     configGroups.append(desktopFileMaster->desktopGroup());
0029     actionGroupIndices.insert(ActionItem::GroupDesktop, configGroups.size() - 1);
0030     configGroups.append(desktopFileMaster->actionGroup(actionName));
0031     actionGroupIndices.insert(ActionItem::GroupAction, configGroups.size() - 1);
0032     configGroups.append(desktopFileWrite->desktopGroup());
0033     actionGroupIndices.insert(ActionItem::GroupDesktop, configGroups.size() - 1);
0034     configGroups.append(desktopFileWrite->actionGroup(actionName));
0035     actionGroupIndices.insert(ActionItem::GroupAction, configGroups.size() - 1);
0036 
0037     const QString predicateString = readKey(ActionItem::GroupDesktop, QStringLiteral("X-KDE-Solid-Predicate"), QString());
0038     predicateItem = Solid::Predicate::fromString(predicateString);
0039 }
0040 
0041 ActionItem::~ActionItem()
0042 {
0043     delete desktopFileWrite;
0044     delete desktopFileMaster;
0045 }
0046 
0047 /// Public functions below
0048 
0049 bool ActionItem::isUserSupplied() const
0050 {
0051     return hasKey(ActionItem::GroupDesktop, QStringLiteral("X-KDE-Action-Custom"));
0052 }
0053 
0054 QString ActionItem::icon() const
0055 {
0056     return readKey(ActionItem::GroupAction, QStringLiteral("Icon"), QString());
0057 }
0058 
0059 QString ActionItem::exec() const
0060 {
0061     return readKey(ActionItem::GroupAction, QStringLiteral("Exec"), QString());
0062 }
0063 
0064 QString ActionItem::name() const
0065 {
0066     return readKey(ActionItem::GroupAction, QStringLiteral("Name"), QString());
0067 }
0068 
0069 Solid::Predicate ActionItem::predicate() const
0070 {
0071     return predicateItem;
0072 }
0073 
0074 QString ActionItem::involvedTypes() const
0075 {
0076     SolidActionData *actData = SolidActionData::instance();
0077     const QSet<Solid::DeviceInterface::Type> devTypeList = predicateItem.usedTypes();
0078     QStringList deviceTypes;
0079     for (Solid::DeviceInterface::Type devType : devTypeList) {
0080         deviceTypes << actData->nameFromInterface(devType);
0081     }
0082 
0083     return deviceTypes.join(QLatin1String(", "));
0084 }
0085 
0086 void ActionItem::setIcon(const QString &nameOfIcon)
0087 {
0088     setKey(ActionItem::GroupAction, QStringLiteral("Icon"), nameOfIcon);
0089 }
0090 
0091 void ActionItem::setName(const QString &nameOfAction)
0092 {
0093     setKey(ActionItem::GroupAction, QStringLiteral("Name"), nameOfAction);
0094 }
0095 
0096 void ActionItem::setExec(const QString &execUrl)
0097 {
0098     setKey(ActionItem::GroupAction, QStringLiteral("Exec"), execUrl);
0099 }
0100 
0101 void ActionItem::setPredicate(const QString &newPredicate)
0102 {
0103     setKey(ActionItem::GroupDesktop, QStringLiteral("X-KDE-Solid-Predicate"), newPredicate);
0104     predicateItem = Solid::Predicate::fromString(newPredicate);
0105 }
0106 
0107 /// Private functions below
0108 
0109 QString ActionItem::readKey(GroupType keyGroup, const QString &keyName, const QString &defaultValue) const
0110 {
0111     return configItem(ActionItem::DesktopRead, keyGroup, keyName).readEntry(keyName, defaultValue);
0112 }
0113 
0114 void ActionItem::setKey(GroupType keyGroup, const QString &keyName, const QString &keyContents)
0115 {
0116     configItem(ActionItem::DesktopWrite, keyGroup).writeEntry(keyName, keyContents);
0117 }
0118 
0119 bool ActionItem::hasKey(GroupType keyGroup, const QString &keyName) const
0120 {
0121     return configItem(ActionItem::DesktopRead, keyGroup, keyName).hasKey(keyName);
0122 }
0123 
0124 KConfigGroup &ActionItem::configItem(DesktopAction actionType, GroupType keyGroup, const QString &keyName)
0125 {
0126     return configGroups[configItemIndex(actionType, keyGroup, keyName)];
0127 }
0128 
0129 const KConfigGroup &ActionItem::configItem(DesktopAction actionType, GroupType keyGroup, const QString &keyName) const
0130 {
0131     return configGroups.at(configItemIndex(actionType, keyGroup, keyName));
0132 }
0133 
0134 qsizetype ActionItem::configItemIndex(DesktopAction actionType, GroupType keyGroup, const QString &keyName) const
0135 {
0136     switch (actionType) {
0137     case ActionItem::DesktopRead: {
0138         const auto indices = actionGroupIndices.values(keyGroup);
0139         for (qsizetype possibleGroupIndex : indices) {
0140             if (configGroups[possibleGroupIndex].hasKey(keyName)) {
0141                 return possibleGroupIndex;
0142             }
0143         }
0144         return indices.first(); // backstop so a valid value is always returned
0145     }
0146     case ActionItem::DesktopWrite: {
0147         int countAccess = isUserSupplied() ? 1 : 0;
0148         return actionGroupIndices.values(keyGroup).at(countAccess);
0149     }
0150     }
0151     Q_UNREACHABLE();
0152 }