1/***************************************************************************
2 * Copyright (C) 2009 by Ben Cooksley <ben@eclipse.endoftheinternet.org> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20#include "ActionItem.h"
21#include "SolidActionData.h"
22
23#include <QString>
24
25#include <kdesktopfileactions.h>
26#include <KDesktopFile>
27#include <KConfigGroup>
28
29#include <Solid/DeviceInterface>
30
31ActionItem::ActionItem(const QString& pathToDesktop, const QString& action, QObject *parent)
32{
33 Q_UNUSED(parent);
34
35 desktopMasterPath = pathToDesktop;
36 actionName = action;
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());
50
51 QString predicateString = readKey(ActionItem::GroupDesktop, "X-KDE-Solid-Predicate", "");
52 predicateItem = Solid::Predicate::fromString( predicateString );
53}
54
55ActionItem::~ActionItem()
56{
57 delete desktopFileWrite;
58 delete desktopFileMaster;
59}
60
61/// Public functions below
62
63bool ActionItem::isUserSupplied()
64{
65 return hasKey(ActionItem::GroupDesktop, "X-KDE-Action-Custom");
66}
67
68QString ActionItem::icon()
69{
70 return readKey(ActionItem::GroupAction, "Icon", "");
71}
72
73QString ActionItem::exec()
74{
75 return readKey(ActionItem::GroupAction, "Exec", "");
76}
77
78QString ActionItem::name()
79{
80 return readKey(ActionItem::GroupAction, "Name", "");
81}
82
83Solid::Predicate ActionItem::predicate() const
84{
85 return predicateItem;
86}
87
88QString ActionItem::involvedTypes() const
89{
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 );
95 }
96
97 return deviceTypes.join(", ");
98}
99
100void ActionItem::setIcon(const QString& nameOfIcon)
101{
102 setKey(ActionItem::GroupAction, "Icon", nameOfIcon);
103}
104
105void ActionItem::setName(const QString& nameOfAction)
106{
107 setKey(ActionItem::GroupAction, "Name", nameOfAction);
108}
109
110void ActionItem::setExec(const QString& execUrl)
111{
112 setKey(ActionItem::GroupAction, "Exec", execUrl);
113}
114
115void ActionItem::setPredicate( const QString& newPredicate )
116{
117 setKey(ActionItem::GroupDesktop, "X-KDE-Solid-Predicate", newPredicate);
118 predicateItem = Solid::Predicate::fromString( newPredicate );
119}
120
121/// Private functions below
122
123QString ActionItem::readKey(GroupType keyGroup, const QString& keyName, const QString& defaultValue)
124{
125 return configItem(ActionItem::DesktopRead, keyGroup, keyName)->readEntry(keyName, defaultValue);
126}
127
128void ActionItem::setKey(GroupType keyGroup, const QString& keyName, const QString& keyContents)
129{
130 configItem(ActionItem::DesktopWrite, keyGroup)->writeEntry(keyName, keyContents);
131}
132
133bool ActionItem::hasKey(GroupType keyGroup, const QString& keyName)
134{
135 return configItem(ActionItem::DesktopRead, keyGroup, keyName)->hasKey(keyName);
136}
137
138KConfigGroup * ActionItem::configItem(DesktopAction actionType, GroupType keyGroup, const QString& keyName)
139{
140 int countAccess = 0;
141
142 if (actionType == ActionItem::DesktopRead) {
143 foreach(KConfigGroup * possibleGroup, actionGroups.values(keyGroup)) {
144 if (possibleGroup->hasKey(keyName)) {
145 return possibleGroup;
146 break;
147 }
148 }
149 } else if (actionType == ActionItem::DesktopWrite) {
150 if (isUserSupplied()) {
151 countAccess = 1;
152 }
153 return actionGroups.values(keyGroup)[countAccess];
154 }
155 return actionGroups.values(keyGroup)[0]; // Implement a backstop so a valid value is always returned
156}
157
158#include "ActionItem.moc"
159