delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / soliduiserver / deviceserviceaction.cpp
blob054992aff1a86f747b57e18dac2af84457303969
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>
23 #include <klocale.h>
24 #include <kmacroexpander.h>
25 #include <krun.h>
26 #include <kdebug.h>
27 #include <solid/storageaccess.h>
28 #include <solid/block.h>
31 class MacroExpander : public KMacroExpanderBase
33 public:
34 MacroExpander(const Solid::Device &device)
35 : KMacroExpanderBase('%'), m_device(device) {}
37 protected:
38 virtual int expandEscapedMacro(const QString &str, int pos, QStringList &ret);
40 private:
41 Solid::Device m_device;
44 class DelayedExecutor : public QObject
46 Q_OBJECT
47 public:
48 DelayedExecutor(const KServiceAction &service, Solid::Device &device);
50 private slots:
51 void _k_storageSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi);
53 private:
54 void delayedExecute(const QString &udi);
56 KServiceAction m_service;
59 DeviceServiceAction::DeviceServiceAction()
60 : DeviceAction()
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()) {
69 return QString();
70 } else {
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,
81 const QString &udi)
83 Q_UNUSED(error);
84 Q_UNUSED(errorData);
86 delayedExecute(udi);
89 void DeviceServiceAction::setService(const KServiceAction& service)
91 DeviceAction::setIconName(service.icon());
92 DeviceAction::setLabel(service.text());
94 m_service = service;
97 KServiceAction DeviceServiceAction::service() const
99 return m_service;
102 int MacroExpander::expandEscapedMacro(const QString &str, int pos, QStringList &ret)
104 uint option = str[pos+1].unicode();
106 switch (option) {
108 case 'f': // Filepath
109 case 'F': // case insensitive
110 if (m_device.is<Solid::StorageAccess>()) {
111 ret << m_device.as<Solid::StorageAccess>()->filePath();
112 } else {
113 kWarning() << "DeviceServiceAction::execute: " << m_device.udi()
114 << " is not a StorageAccess device" << endl;
116 break;
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();
121 } else {
122 kWarning() << "DeviceServiceAction::execute: " << m_device.udi()
123 << " is not a Block device" << endl;
125 break;
126 case 'i': // UDI
127 case 'I': // case insensitive
128 ret << m_device.udi();
129 break;
130 case '%':
131 ret = QStringList(QLatin1String("%"));
132 break;
133 default:
134 return -2; // subst with same and skip
136 return 2;
139 DelayedExecutor::DelayedExecutor(const KServiceAction &service, Solid::Device &device)
140 : m_service(service)
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 &)));
149 access->setup();
151 } else {
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();
165 return;
168 KRun::runCommand(exec, QString(), m_service.icon(), 0);
169 deleteLater();
172 #include "deviceserviceaction.moc"