deviceserviceaction.cpp 4.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* This file is part of the KDE Project
   Copyright (c) 2005 Jean-Remy Falleri <jr.falleri@laposte.net>
   Copyright (c) 2005-2007 Kevin Ottens <ervin@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "deviceserviceaction.h"

Lukáš Tinkl's avatar
Lukáš Tinkl committed
22
23
24
#include <QDebug>

#include <KLocalizedString>
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <kmacroexpander.h>
#include <krun.h>
#include <solid/storageaccess.h>
#include <solid/block.h>


class MacroExpander : public KMacroExpanderBase
{
public:
    MacroExpander(const Solid::Device &device)
        : KMacroExpanderBase('%'), m_device(device) {}

protected:
38
    int expandEscapedMacro(const QString &str, int pos, QStringList &ret) Q_DECL_OVERRIDE;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

private:
    Solid::Device m_device;
};

class DelayedExecutor : public QObject
{
    Q_OBJECT
public:
    DelayedExecutor(const KServiceAction &service, Solid::Device &device);

private slots:
    void _k_storageSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi);

private:
    void delayedExecute(const QString &udi);

    KServiceAction m_service;
};

DeviceServiceAction::DeviceServiceAction()
    : DeviceAction()
{
62
    DeviceAction::setIconName(QStringLiteral("dialog-cancel"));
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    DeviceAction::setLabel(i18nc("A default name for an action without proper label", "Unknown"));
}

QString DeviceServiceAction::id() const
{
    if (m_service.name().isEmpty() && m_service.exec().isEmpty()) {
        return QString();
    } else {
        return "#Service:"+m_service.name()+m_service.exec();
    }
}

void DeviceServiceAction::execute(Solid::Device &device)
{
    new DelayedExecutor(m_service, device);
}

void DelayedExecutor::_k_storageSetupDone(Solid::ErrorType error, QVariant errorData,
                                          const QString &udi)
{
    Q_UNUSED(errorData);

    if (!error) {
        delayedExecute(udi);
    }
}

void DeviceServiceAction::setService(const KServiceAction& service)
{
    DeviceAction::setIconName(service.icon());
    DeviceAction::setLabel(service.text());

    m_service = service;
}

KServiceAction DeviceServiceAction::service() const
{
    return m_service;
}

int MacroExpander::expandEscapedMacro(const QString &str, int pos, QStringList &ret)
{
105
    ushort option = str[pos+1].unicode();
106
107
108
109
110
111
112

    switch (option) {
    case 'f': // Filepath
    case 'F': // case insensitive
        if (m_device.is<Solid::StorageAccess>()) {
            ret << m_device.as<Solid::StorageAccess>()->filePath();
        } else {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
113
114
            qWarning() << "DeviceServiceAction::execute: " << m_device.udi()
                       << " is not a StorageAccess device";
115
116
117
118
119
120
121
        }
        break;
    case 'd': // Device node
    case 'D': // case insensitive
        if (m_device.is<Solid::Block>()) {
            ret << m_device.as<Solid::Block>()->device();
        } else {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
122
123
            qWarning() << "DeviceServiceAction::execute: " << m_device.udi()
                       << " is not a Block device";
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        }
        break;
    case 'i': // UDI
    case 'I': // case insensitive
        ret << m_device.udi();
        break;
    case '%':
        ret = QStringList(QLatin1String("%"));
        break;
    default:
        return -2; // subst with same and skip
    }
    return 2;
}

DelayedExecutor::DelayedExecutor(const KServiceAction &service, Solid::Device &device)
    : m_service(service)
{
    if (device.is<Solid::StorageAccess>()
Lukáš Tinkl's avatar
Lukáš Tinkl committed
143
            && !device.as<Solid::StorageAccess>()->isAccessible()) {
144
145
        Solid::StorageAccess *access = device.as<Solid::StorageAccess>();

146
147
        connect(access, &Solid::StorageAccess::setupDone,
                this, &DelayedExecutor::_k_storageSetupDone);
148
149
150
151
152
153
154
155
156
157
158
159
160

        access->setup();
    } else {
        delayedExecute(device.udi());
    }
}

void DelayedExecutor::delayedExecute(const QString &udi)
{
    Solid::Device device(udi);

    QString exec = m_service.exec();
    MacroExpander mx(device);
161
    mx.expandMacrosShellQuote(exec);
162
163
164
165
166
167

    KRun::runCommand(exec, QString(), m_service.icon(), 0);
    deleteLater();
}

#include "deviceserviceaction.moc"