~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/runners/solid/devicewrapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 *   Copyright 2009 by Jacopo De Simoi <wilderkde@gmail.com>               *
 
3
 *   This program is free software; you can redistribute it and/or modify  *
 
4
 *   it under the terms of the GNU General Public License as published by  *
 
5
 *   the Free Software Foundation; either version 2 of the License, or     *
 
6
 *   (at your option) any later version.                                   *
 
7
 *                                                                         *
 
8
 *   This program is distributed in the hope that it will be useful,       *
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
11
 *   GNU General Public License for more details.                          *
 
12
 *                                                                         *
 
13
 *   You should have received a copy of the GNU General Public License     *
 
14
 *   along with this program; if not, write to the                         *
 
15
 *   Free Software Foundation, Inc.,                                       *
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
17
 ***************************************************************************/
 
18
 
 
19
//own
 
20
#include "devicewrapper.h"
 
21
 
 
22
//Qt
 
23
#include <QAction>
 
24
#include <QTimer>
 
25
#include <QtDBus/QDBusInterface>
 
26
#include <QtDBus/QDBusReply>
 
27
 
 
28
//Solid
 
29
#include <Solid/Device>
 
30
#include <Solid/StorageVolume>
 
31
#include <Solid/StorageAccess>
 
32
#include <Solid/OpticalDrive>
 
33
#include <Solid/OpticalDisc>
 
34
 
 
35
//KDE
 
36
#include <KIcon>
 
37
#include <KMessageBox>
 
38
#include <KStandardDirs>
 
39
#include <kdesktopfileactions.h>
 
40
 
 
41
//Plasma
 
42
#include <Plasma/DataEngine>
 
43
 
 
44
DeviceWrapper::DeviceWrapper(const QString& udi)
 
45
  : m_device(udi),
 
46
    m_isStorageAccess(false),
 
47
    m_isAccessible(false),
 
48
    m_isEncryptedContainer(false)
 
49
{
 
50
    m_udi = m_device.udi();
 
51
}
 
52
 
 
53
DeviceWrapper::~DeviceWrapper()
 
54
{
 
55
}
 
56
 
 
57
void DeviceWrapper::dataUpdated(const QString &source, Plasma::DataEngine::Data data)
 
58
{
 
59
    Q_UNUSED(source)
 
60
 
 
61
    if (data.isEmpty()) {
 
62
        return;
 
63
    }
 
64
    if (data["text"].isValid()) {
 
65
        m_actionIds.clear();
 
66
        foreach (const QString &desktop, data["predicateFiles"].toStringList()) {
 
67
            QString filePath = KStandardDirs::locate("data", "solid/actions/" + desktop);
 
68
            QList<KServiceAction> services = KDesktopFileActions::userDefinedServices(filePath, true);
 
69
 
 
70
            foreach (KServiceAction serviceAction, services) {
 
71
                QString actionId = id()+'_'+desktop+'_'+serviceAction.name();
 
72
                m_actionIds << actionId;
 
73
                emit registerAction(actionId,  serviceAction.icon(), serviceAction.text(), desktop);
 
74
            }
 
75
        }
 
76
        m_isEncryptedContainer = data["isEncryptedContainer"].toBool();
 
77
    } else {
 
78
        if (data["Device Types"].toStringList().contains("Storage Access")) {
 
79
            m_isStorageAccess = true;
 
80
            if (data["Accessible"].toBool() == true) {
 
81
                m_isAccessible = true;
 
82
            } else {
 
83
                m_isAccessible = false;
 
84
            }
 
85
        } else {
 
86
            m_isStorageAccess = false;
 
87
        }
 
88
        if (data["Device Types"].toStringList().contains("OpticalDisc")) {
 
89
            m_isOpticalDisc = true;
 
90
        } else {
 
91
            m_isOpticalDisc = false;
 
92
        }
 
93
    }
 
94
 
 
95
    m_emblems = m_device.emblems();
 
96
 
 
97
    emit refreshMatch(m_udi);
 
98
 
 
99
}
 
100
 
 
101
QString DeviceWrapper::id() const {
 
102
     return m_udi;
 
103
}
 
104
 
 
105
Solid::Device DeviceWrapper::device() const {
 
106
    return m_device;
 
107
}
 
108
 
 
109
KIcon DeviceWrapper::icon() const {
 
110
    return KIcon(m_device.icon(), NULL, m_emblems);
 
111
}
 
112
 
 
113
bool DeviceWrapper::isStorageAccess() const {
 
114
    return m_isStorageAccess;
 
115
}
 
116
 
 
117
bool DeviceWrapper::isAccessible() const {
 
118
    return m_isAccessible;
 
119
}
 
120
 
 
121
bool DeviceWrapper::isEncryptedContainer() const {
 
122
    return m_isEncryptedContainer;
 
123
}
 
124
 
 
125
bool DeviceWrapper::isOpticalDisc() const {
 
126
    return m_isOpticalDisc;
 
127
}
 
128
 
 
129
QString DeviceWrapper::description() const {
 
130
    return m_device.description();
 
131
}
 
132
 
 
133
void DeviceWrapper::setForceEject(bool force)
 
134
{
 
135
    m_forceEject = force;
 
136
}
 
137
 
 
138
QString DeviceWrapper::defaultAction() const {
 
139
 
 
140
    QString actionString;
 
141
 
 
142
    if (m_isOpticalDisc && m_forceEject) {
 
143
        actionString = i18n("Eject medium");
 
144
    } else if (m_isStorageAccess) {
 
145
        if (!m_isEncryptedContainer) {
 
146
            if (!m_isAccessible) {
 
147
                actionString = i18n("Mount the device");
 
148
            } else {
 
149
                actionString = i18n("Unmount the device");
 
150
            }
 
151
        } else {
 
152
            if (!m_isAccessible) {
 
153
                actionString = i18nc("Unlock the encrypted container; will ask for a password; partitions inside will appear as they had been plugged in","Unlock the container");
 
154
            } else {
 
155
                actionString = i18nc("Close the encrypted container; partitions inside will disappear as they had been unplugged", "Lock the container");
 
156
            }
 
157
        }
 
158
    } else {
 
159
            actionString = i18n("Eject medium");
 
160
    }
 
161
    return actionString;
 
162
}
 
163
 
 
164
void DeviceWrapper::runAction(QAction * action)
 
165
{
 
166
    if (action) {
 
167
        QString desktopAction = action->data().toString();
 
168
        if (!desktopAction.isEmpty()) {
 
169
            QStringList desktopFiles;
 
170
            desktopFiles.append(desktopAction);
 
171
            QDBusInterface soliduiserver("org.kde.kded", "/modules/soliduiserver", "org.kde.SolidUiServer");
 
172
            soliduiserver.asyncCall("showActionsDialog", id(), desktopFiles);
 
173
        }
 
174
    } else {
 
175
        if (isOpticalDisc() && m_forceEject) {
 
176
            Solid::OpticalDrive *drive = m_device.parent().as<Solid::OpticalDrive>();
 
177
            if (drive) {
 
178
                drive->eject();
 
179
            }
 
180
            return;
 
181
        }
 
182
 
 
183
        if (m_device.is<Solid::StorageVolume>()) {
 
184
            Solid::StorageAccess *access = m_device.as<Solid::StorageAccess>();
 
185
            if (access) {
 
186
                if (access->isAccessible()) {
 
187
                    access->teardown();
 
188
                } else {
 
189
                    access->setup();
 
190
                }
 
191
                return;
 
192
            }
 
193
        }
 
194
 
 
195
        if (isOpticalDisc()) {
 
196
            Solid::OpticalDrive *drive = m_device.parent().as<Solid::OpticalDrive>();
 
197
            if (drive) {
 
198
                drive->eject();
 
199
            }
 
200
        }
 
201
    }
 
202
}
 
203
 
 
204
QStringList DeviceWrapper::actionIds() const
 
205
{
 
206
    return m_actionIds;
 
207
}
 
208
 
 
209
#include "devicewrapper.moc"