1
/***************************************************************************
2
* Copyright (C) 2009 by Ben Cooksley <ben@eclipse.endoftheinternet.org> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
18
***************************************************************************/
20
#include "ActionItem.h"
21
#include "SolidActionData.h"
25
#include <kdesktopfileactions.h>
26
#include <KDesktopFile>
27
#include <KConfigGroup>
29
#include <Solid/DeviceInterface>
31
ActionItem::ActionItem(const QString& pathToDesktop, const QString& action, QObject *parent)
35
desktopMasterPath = pathToDesktop;
37
// Create the desktop file
38
desktopFileMaster = new KDesktopFile(desktopMasterPath);
39
desktopWritePath = desktopFileMaster->locateLocal(desktopMasterPath);
40
desktopFileWrite = new KDesktopFile(desktopWritePath);
41
// Now we can fill the action groups list
42
configGroups.append(desktopFileMaster->desktopGroup());
43
actionGroups.insertMulti(ActionItem::GroupDesktop, &configGroups.last());
44
configGroups.append(desktopFileMaster->actionGroup(actionName));
45
actionGroups.insertMulti(ActionItem::GroupAction, &configGroups.last());
46
configGroups.append(desktopFileWrite->desktopGroup());
47
actionGroups.insertMulti(ActionItem::GroupDesktop, &configGroups.last());
48
configGroups.append(desktopFileWrite->actionGroup(actionName));
49
actionGroups.insertMulti(ActionItem::GroupAction, &configGroups.last());
51
QString predicateString = readKey(ActionItem::GroupDesktop, "X-KDE-Solid-Predicate", "");
52
predicateItem = Solid::Predicate::fromString( predicateString );
55
ActionItem::~ActionItem()
57
delete desktopFileWrite;
58
delete desktopFileMaster;
61
/// Public functions below
63
bool ActionItem::isUserSupplied()
65
return hasKey(ActionItem::GroupDesktop, "X-KDE-Action-Custom");
68
QString ActionItem::icon()
70
return readKey(ActionItem::GroupAction, "Icon", "");
73
QString ActionItem::exec()
75
return readKey(ActionItem::GroupAction, "Exec", "");
78
QString ActionItem::name()
80
return readKey(ActionItem::GroupAction, "Name", "");
83
Solid::Predicate ActionItem::predicate() const
88
QString ActionItem::involvedTypes() const
90
SolidActionData * actData = SolidActionData::instance();
91
QSet<Solid::DeviceInterface::Type> devTypeList = predicateItem.usedTypes();
92
QStringList deviceTypes;
93
foreach( Solid::DeviceInterface::Type devType, devTypeList ) {
94
deviceTypes << actData->nameFromInterface( devType );
97
return deviceTypes.join(", ");
100
void ActionItem::setIcon(const QString& nameOfIcon)
102
setKey(ActionItem::GroupAction, "Icon", nameOfIcon);
105
void ActionItem::setName(const QString& nameOfAction)
107
setKey(ActionItem::GroupAction, "Name", nameOfAction);
110
void ActionItem::setExec(const QString& execUrl)
112
setKey(ActionItem::GroupAction, "Exec", execUrl);
115
void ActionItem::setPredicate( const QString& newPredicate )
117
setKey(ActionItem::GroupDesktop, "X-KDE-Solid-Predicate", newPredicate);
118
predicateItem = Solid::Predicate::fromString( newPredicate );
121
/// Private functions below
123
QString ActionItem::readKey(GroupType keyGroup, const QString& keyName, const QString& defaultValue)
125
return configItem(ActionItem::DesktopRead, keyGroup, keyName)->readEntry(keyName, defaultValue);
128
void ActionItem::setKey(GroupType keyGroup, const QString& keyName, const QString& keyContents)
130
configItem(ActionItem::DesktopWrite, keyGroup)->writeEntry(keyName, keyContents);
133
bool ActionItem::hasKey(GroupType keyGroup, const QString& keyName)
135
return configItem(ActionItem::DesktopRead, keyGroup, keyName)->hasKey(keyName);
138
KConfigGroup * ActionItem::configItem(DesktopAction actionType, GroupType keyGroup, const QString& keyName)
142
if (actionType == ActionItem::DesktopRead) {
143
foreach(KConfigGroup * possibleGroup, actionGroups.values(keyGroup)) {
144
if (possibleGroup->hasKey(keyName)) {
145
return possibleGroup;
149
} else if (actionType == ActionItem::DesktopWrite) {
150
if (isUserSupplied()) {
153
return actionGroups.values(keyGroup)[countAccess];
155
return actionGroups.values(keyGroup)[0]; // Implement a backstop so a valid value is always returned
158
#include "ActionItem.moc"