1 /* This file is part of the KDE Project
2 Copyright (c) 2005 Jean-Remy Falleri <jr.falleri@laposte.net>
3 Copyright (c) 2005-2007 Kevin Ottens <ervin@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "deviceserviceaction.h"
22 #include <kdesktopfile.h>
24 #include <kmacroexpander.h>
27 #include <solid/storageaccess.h>
28 #include <solid/block.h>
31 class MacroExpander
: public KMacroExpanderBase
34 MacroExpander(const Solid::Device
&device
)
35 : KMacroExpanderBase('%'), m_device(device
) {}
38 virtual int expandEscapedMacro(const QString
&str
, int pos
, QStringList
&ret
);
41 Solid::Device m_device
;
44 class DelayedExecutor
: public QObject
48 DelayedExecutor(const KServiceAction
&service
, Solid::Device
&device
);
51 void _k_storageSetupDone(Solid::ErrorType error
, QVariant errorData
, const QString
&udi
);
54 void delayedExecute(const QString
&udi
);
56 KServiceAction m_service
;
59 DeviceServiceAction::DeviceServiceAction()
62 DeviceAction::setIconName("dialog-cancel");
63 DeviceAction::setLabel(i18nc("A default name for an action without proper label", "Unknown"));
66 QString
DeviceServiceAction::id() const
68 if (m_service
.name().isEmpty() && m_service
.exec().isEmpty()) {
71 return "#Service:"+m_service
.name()+m_service
.exec();
75 void DeviceServiceAction::execute(Solid::Device
&device
)
77 new DelayedExecutor(m_service
, device
);
80 void DelayedExecutor::_k_storageSetupDone(Solid::ErrorType error
, QVariant errorData
,
89 void DeviceServiceAction::setService(const KServiceAction
& service
)
91 DeviceAction::setIconName(service
.icon());
92 DeviceAction::setLabel(service
.text());
97 KServiceAction
DeviceServiceAction::service() const
102 int MacroExpander::expandEscapedMacro(const QString
&str
, int pos
, QStringList
&ret
)
104 uint option
= str
[pos
+1].unicode();
108 case 'f': // Filepath
109 case 'F': // case insensitive
110 if (m_device
.is
<Solid::StorageAccess
>()) {
111 ret
<< m_device
.as
<Solid::StorageAccess
>()->filePath();
113 kWarning() << "DeviceServiceAction::execute: " << m_device
.udi()
114 << " is not a StorageAccess device" << endl
;
117 case 'd': // Device node
118 case 'D': // case insensitive
119 if (m_device
.is
<Solid::Block
>()) {
120 ret
<< m_device
.as
<Solid::Block
>()->device();
122 kWarning() << "DeviceServiceAction::execute: " << m_device
.udi()
123 << " is not a Block device" << endl
;
127 case 'I': // case insensitive
128 ret
<< m_device
.udi();
131 ret
= QStringList(QLatin1String("%"));
134 return -2; // subst with same and skip
139 DelayedExecutor::DelayedExecutor(const KServiceAction
&service
, Solid::Device
&device
)
142 if (device
.is
<Solid::StorageAccess
>()
143 && !device
.as
<Solid::StorageAccess
>()->isAccessible()) {
144 Solid::StorageAccess
*access
= device
.as
<Solid::StorageAccess
>();
146 connect(access
, SIGNAL(setupDone(Solid::ErrorType
, QVariant
, const QString
&)),
147 this, SLOT(_k_storageSetupDone(Solid::ErrorType
, QVariant
, const QString
&)));
152 delayedExecute(device
.udi());
156 void DelayedExecutor::delayedExecute(const QString
&udi
)
158 Solid::Device
device(udi
);
160 QString exec
= m_service
.exec();
161 MacroExpander
mx(device
);
163 if (!mx
.expandMacrosShellQuote(exec
)) {
164 kWarning() << ", Syntax error:" << m_service
.exec();
168 KRun::runCommand(exec
, QString(), m_service
.icon(), 0);
172 #include "deviceserviceaction.moc"