~lubuntu-dev/lxde/liblxqt-mount

« back to all changes in this revision

Viewing changes to providers_udisks2.cpp

  • Committer: Luís Pereira
  • Date: 2014-06-12 10:52:04 UTC
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: git-v1:09b968dfd1434bfe6eae98496a1f1e38db8d471d
Changes files names

This change has in mind how to include the header using portable headers
way.

Signed-off-by: Luís Pereira <luis.artur.pereira@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* BEGIN_COMMON_COPYRIGHT_HEADER
 
2
 * (c)LGPL2+
 
3
 *
 
4
 * LXQt - The Lightweight Desktop Environment
 
5
 * http://lxqt.org
 
6
 *
 
7
 * Copyright: 2013 Razor team
 
8
 *            2013-2014 LXQt team
 
9
 * Authors:
 
10
 *   Petr Vanek <petr@yarpen.cz>
 
11
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
 
12
 *
 
13
 * This program or library is free software; you can redistribute it
 
14
 * and/or modify it under the terms of the GNU Lesser General Public
 
15
 * License as published by the Free Software Foundation; either
 
16
 * version 2.1 of the License, or (at your option) any later version.
 
17
 *
 
18
 * This library is distributed in the hope that it will be useful,
 
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
21
 * Lesser General Public License for more details.
 
22
 
 
23
 * You should have received a copy of the GNU Lesser General
 
24
 * Public License along with this library; if not, write to the
 
25
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
26
 * Boston, MA 02110-1301 USA
 
27
 *
 
28
 * END_COMMON_COPYRIGHT_HEADER */
 
29
 
 
30
#include <QtDBus>
 
31
#include "providers.h"
 
32
 
 
33
namespace LxQt {
 
34
 
 
35
UDisks2Provider::UDisks2Provider(QObject *parent):
 
36
    MountProvider(parent)
 
37
{
 
38
    QDBusConnection system = QDBusConnection::systemBus();
 
39
 
 
40
    if (!system.isConnected())
 
41
    {
 
42
        qDebug() << "UDisks2Provider::UDisks2Provider - not connected - " << mIsValid;
 
43
        return;
 
44
    }
 
45
 
 
46
 
 
47
    if (!QDBusInterface("org.freedesktop.UDisks2",
 
48
                       "/org/freedesktop/UDisks2",
 
49
                       "org.freedesktop.UDisks2", system).isValid())
 
50
    {
 
51
        qDebug() << "org.freedesktop.UDisks2 - not exists - " << mIsValid;
 
52
        return;
 
53
    }
 
54
 
 
55
    system.connect("org.freedesktop.UDisks2",
 
56
                   "/org/freedesktop/UDisks2",
 
57
                   "org.freedesktop.UDisks2",
 
58
                   "InterfacesAdded",
 
59
                   this,
 
60
                   SLOT(dbusDeviceAdded(QDBusObjectPath,QVariantMap)));
 
61
 
 
62
    system.connect("org.freedesktop.UDisks2",
 
63
                   "/org/freedesktop/UDisks2",
 
64
                   "org.freedesktop.UDisks2",
 
65
                   "InterfacesRemoved",
 
66
                   this,
 
67
                   SLOT(dbusDeviceRemoved(QDBusObjectPath,QStringList)));
 
68
 
 
69
    mIsValid = true;
 
70
    qDebug() << "UDisks2Provider::UDisks2Provider final validity" << mIsValid;
 
71
 
 
72
}
 
73
 
 
74
void UDisks2Provider::update()
 
75
{
 
76
    qDebug() << "UDisks2Provider::update() called";
 
77
 
 
78
    QList<QDBusObjectPath> paths;
 
79
    QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
 
80
                                                       "/org/freedesktop/UDisks2/block_devices",
 
81
                                                       "org.freedesktop.DBus.Introspectable",
 
82
                                                       "Introspect");
 
83
    QDBusPendingReply<QString> reply = QDBusConnection::systemBus().call(call);
 
84
 
 
85
    if (!reply.isValid())
 
86
    {
 
87
        qWarning("UDisks2Manager: error: %s", qPrintable(reply.error().name()));
 
88
        return;
 
89
    }
 
90
 
 
91
    QXmlStreamReader xml(reply.value());
 
92
    while (!xml.atEnd())
 
93
    {
 
94
        xml.readNext();
 
95
        if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name().toString() == "node" )
 
96
        {
 
97
            QString name = xml.attributes().value("name").toString();
 
98
            if(!name.isEmpty())
 
99
                paths << QDBusObjectPath("/org/freedesktop/UDisks2/block_devices/" + name);
 
100
        }
 
101
    }
 
102
 
 
103
    foreach (QDBusObjectPath i, paths)
 
104
    {
 
105
//        qDebug() << "UDisks2Provider::update() path:" << i.path();
 
106
        if (mDevicesByPath.contains(i.path()))
 
107
            dbusDeviceChanged(i);
 
108
        else
 
109
            dbusDeviceAdded(i, QVariantMap());
 
110
    }
 
111
 
 
112
//    qDebug() << mDevicesByPath;
 
113
}
 
114
 
 
115
UDisks2MountDevice *UDisks2Provider::getDevice(const QDBusObjectPath &path) const
 
116
{
 
117
    if (mDevicesByPath.contains(path.path()))
 
118
        return mDevicesByPath[path.path()];
 
119
    else
 
120
        return 0;
 
121
}
 
122
 
 
123
void UDisks2Provider::addDevice(UDisks2MountDevice *device)
 
124
{
 
125
    mDevicesByPath.insert(device->path().path(), device);
 
126
    mDevices.append(device);
 
127
}
 
128
 
 
129
void UDisks2Provider::delDevice(UDisks2MountDevice *device)
 
130
{
 
131
    mDevices.removeAll(device);
 
132
    mDevicesByPath.remove(device->path().path());
 
133
    device->deleteLater();
 
134
}
 
135
 
 
136
void UDisks2Provider::dbusDeviceAdded(const QDBusObjectPath &path, const QVariantMap &)
 
137
{
 
138
    if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
 
139
        return;
 
140
 
 
141
    UDisks2MountDevice *device = new UDisks2MountDevice(path);
 
142
 
 
143
    addDevice(device);
 
144
    emit deviceAdded(device);
 
145
}
 
146
 
 
147
void UDisks2Provider::dbusDeviceRemoved(const QDBusObjectPath &path, const QStringList &)
 
148
{
 
149
    if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
 
150
        return;
 
151
 
 
152
    UDisks2MountDevice *device = getDevice(path);
 
153
    if (device)
 
154
    {
 
155
        emit deviceRemoved(device);
 
156
        delDevice(device);
 
157
    }
 
158
}
 
159
 
 
160
void UDisks2Provider::dbusDeviceChanged(const QDBusObjectPath &path)
 
161
{
 
162
#if 0
 
163
    if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
 
164
        return;
 
165
 
 
166
    UDisks2MountDevice *device = getDevice(path);
 
167
    if (!device)
 
168
        return;
 
169
 
 
170
    if (device->update())
 
171
        emit deviceChanged(device);
 
172
#endif
 
173
}
 
174
 
 
175
UDisks2MountDevice::UDisks2MountDevice(const QDBusObjectPath &path):
 
176
    MountDevice(),
 
177
    mPath(path)
 
178
{
 
179
//    qDebug() << "UDisks2MountDevice::UDisks2MountDevice path" << mPath.path();
 
180
    mBlockIface = new QDBusInterface("org.freedesktop.UDisks2",
 
181
                                     mPath.path(),
 
182
                                     "org.freedesktop.UDisks2.Block",
 
183
                                     QDBusConnection::systemBus(),
 
184
                                     this);
 
185
    Q_ASSERT(mBlockIface);
 
186
 
 
187
    QDBusObjectPath drive_object = mBlockIface->property("Drive").value<QDBusObjectPath>();
 
188
//    qDebug() << "UDisks2MountDevice::UDisks2MountDevice drive_object" << drive_object.path();
 
189
 
 
190
    QDBusConnection::systemBus().connect("org.freedesktop.UDisks2",
 
191
                                         mPath.path(),
 
192
                                         "org.freedesktop.DBus.Properties","PropertiesChanged",
 
193
                                         this,
 
194
                                         SIGNAL(update()));
 
195
 
 
196
    mDriveIface = new QDBusInterface("org.freedesktop.UDisks2",
 
197
                                     drive_object.path(),
 
198
                                     "org.freedesktop.UDisks2.Drive",
 
199
                                     QDBusConnection::systemBus(),
 
200
                                     this);
 
201
    Q_ASSERT(mDriveIface);
 
202
 
 
203
    update();
 
204
}
 
205
 
 
206
void UDisks2MountDevice::update()
 
207
{
 
208
 
 
209
    bool res = false;
 
210
    res = setDevFile(mBlockIface->property("Device").toByteArray().data()) || res;
 
211
 
 
212
    res = setMediaType(calcMediaType()) || res;
 
213
    res = setLabel(calcLabel()) || res;
 
214
    res = setIsExternal(calcIsExternal()) || res;
 
215
    res = setIconName(calcIconName()) || res;
 
216
 
 
217
    QStringList mounts = mountPoints();
 
218
    res = setIsMounted(mounts.count() != 0) || res;
 
219
 
 
220
    res = setIsEjectable(mDriveIface->property("Ejectable").toBool()) || res;
 
221
    res = setSize(mBlockIface->property("Size").toULongLong()) || res;
 
222
    res = setVendor(mDriveIface->property("Vendor").toString()) || res;
 
223
    res = setModel(mDriveIface->property("Model").toString()) || res;
 
224
    res = setFileSystem(mBlockIface->property("IdType").toString()) || res;
 
225
 
 
226
    if (!mounts.empty())
 
227
        res = setMountPath(mounts.first()) || res;
 
228
 
 
229
    if (res)
 
230
        emit changed();
 
231
}
 
232
 
 
233
QStringList UDisks2MountDevice::mountPoints() const
 
234
{
 
235
    QStringList points;
 
236
    QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
 
237
                                                          mPath.path(),
 
238
                                                          "org.freedesktop.DBus.Properties",
 
239
                                                          "Get");
 
240
 
 
241
    QList<QVariant> args;
 
242
    args << "org.freedesktop.UDisks2.Filesystem" << "MountPoints";
 
243
    message.setArguments(args);
 
244
 
 
245
    QDBusMessage reply = QDBusConnection::systemBus().call(message);
 
246
 
 
247
    QList<QByteArray> l;
 
248
    foreach (QVariant arg, reply.arguments())
 
249
        arg.value<QDBusVariant>().variant().value<QDBusArgument>() >> l;
 
250
 
 
251
    foreach (QByteArray p, l)
 
252
        points.append(p);
 
253
 
 
254
    qDebug() << "UDisks2MountDevice::mountPoints()" << points;
 
255
    return points;
 
256
}
 
257
 
 
258
MountDevice::MediaType UDisks2MountDevice::calcMediaType()
 
259
{
 
260
    if (mDriveIface->property("Optical").toBool())
 
261
        return MountDevice::MediaTypeOptical;
 
262
 
 
263
    const QString media = mDriveIface->property("Media").toString();
 
264
    const QStringList mediaCompat = mDriveIface->property("MediaCompatibility").toStringList();
 
265
    const QString idUsage = mBlockIface->property("IdUsage").toString();
 
266
 
 
267
    // TODO/FIXME: how to get it from udisks2?!
 
268
//    if (mDbus->property("DeviceIsDrive").toBool())
 
269
//    {
 
270
        // TODO/FIXME: just guessing...
 
271
        if (mediaCompat.contains("floppy"))
 
272
            return MountDevice::MediaTypeFdd;
 
273
 
 
274
        if (idUsage == "filesystem")
 
275
            return MountDevice::MediaTypeDrive;
 
276
 
 
277
        return MountDevice::MediaTypeUnknown;
 
278
//    }
 
279
 
 
280
//    if (mDbus->property("DeviceIsPartition").toBool())
 
281
//    {
 
282
//        if (idUsage == "filesystem")
 
283
//            return MountDevice::MediaTypePartition;
 
284
 
 
285
//      return MountDevice::MediaTypeUnknown;
 
286
//    }
 
287
 
 
288
    return MountDevice::MediaTypeUnknown;
 
289
 
 
290
}
 
291
 
 
292
QString UDisks2MountDevice::calcLabel()
 
293
{
 
294
    const QString idLabel = mBlockIface->property("IdLabel").toString();
 
295
 
 
296
    if (mMediaType == MediaTypeFdd)
 
297
        return tr("Floppy drive");
 
298
 
 
299
    if (mMediaType == MediaTypeOptical)
 
300
        return idLabel;
 
301
 
 
302
 
 
303
    const QString driveVendor = mDriveIface->property("Vendor").toString();
 
304
    const QString driveModel  = mDriveIface->property("Model").toString();
 
305
    const qulonglong size = mBlockIface->property("Size").toULongLong();
 
306
 
 
307
    QString label;
 
308
    if (!idLabel.isEmpty())
 
309
    {
 
310
        label = idLabel;
 
311
    }
 
312
    else
 
313
    {
 
314
       if (!driveVendor.isEmpty())
 
315
            label = driveVendor;
 
316
 
 
317
       if (!driveModel.isEmpty())
 
318
            label += QString(" - %1").arg(driveModel);
 
319
    }
 
320
 
 
321
    if (label.isEmpty())
 
322
        label = mDevFile;
 
323
 
 
324
    if (mSize)
 
325
        label += QString(" [%3]").arg(sizeToString(size));
 
326
 
 
327
    qDebug() << "LABEL" << mPath.path() << label;
 
328
    return label;
 
329
}
 
330
 
 
331
bool UDisks2MountDevice::calcIsExternal()
 
332
{
 
333
    // TODO/FIXME: I'm really unsure here...
 
334
    return mDriveIface->property("Removable").toBool();
 
335
}
 
336
 
 
337
QString UDisks2MountDevice::calcIconName()
 
338
{
 
339
    const QString media = mDriveIface->property( "Media" ).toString();
 
340
 
 
341
    switch (mMediaType)
 
342
    {
 
343
    // ..............................................
 
344
    case MediaTypeDrive:
 
345
    case MediaTypePartition:
 
346
        {
 
347
            // handle drives
 
348
            const QString conn = mDriveIface->property( "ConnectionBus" ).toString();
 
349
 
 
350
            if (conn == "usb")
 
351
                return "drive-removable-media-usb";
 
352
 
 
353
            return "drive-removable-media";
 
354
        }
 
355
 
 
356
    // ..............................................
 
357
    case MediaTypeFdd:
 
358
        {
 
359
            return "media-floppy";
 
360
        }
 
361
 
 
362
 
 
363
    // ..............................................
 
364
//    case MediaTypeFlash:
 
365
//        {
 
366
//            if ( media == "flash_ms" ) // Flash & Co.
 
367
//                return "media-flash-memory-stick";
 
368
 
 
369
//            if ( media == "flash_sd" ||
 
370
//                 media == "flash_sdhc" ||
 
371
//                 media == "flash_mmc" )
 
372
//                return "media-flash-sd-mmc";
 
373
 
 
374
//            if ( media == "flash_sm" )
 
375
//                return "media-flash-smart-media";
 
376
 
 
377
//            return "media-flash";
 
378
//        }
 
379
 
 
380
 
 
381
    case MediaTypeOptical:
 
382
        {
 
383
            bool isWritable = mDriveIface->property( "OpticalBlank" ).toBool()
 
384
                                // TODO/FIXME: how in udisks2?
 
385
                             //||
 
386
                             // mDbus->property("OpticalDiscIsAppendable").toBool();
 
387
                                ;
 
388
 
 
389
            if (isWritable)
 
390
                return "media-optical-recordable";
 
391
 
 
392
            if (media.startsWith("optical_dvd") ||
 
393
                media.startsWith( "optical_hddvd")) // DVD
 
394
                return "media-optical-dvd";
 
395
 
 
396
            if (media.startsWith("optical_bd")) // BluRay
 
397
                return "media-optical-blu-ray";
 
398
 
 
399
            return "media-optical";
 
400
        }
 
401
 
 
402
    case MediaTypeUnknown:
 
403
        {
 
404
            return "drive-harddisk";
 
405
        }
 
406
    }
 
407
 
 
408
    return "drive-harddisk";
 
409
}
 
410
 
 
411
void UDisks2MountDevice::dbusError(const QDBusError &err, const QDBusMessage &msg)
 
412
{
 
413
    qWarning() << "UDisks2MountDevice::mDbus_error" << err.message();
 
414
    emit error(err.message());
 
415
}
 
416
 
 
417
bool UDisks2MountDevice::mount()
 
418
{
 
419
    qDebug() << "MOUNT" << mPath.path();
 
420
 
 
421
    QDBusConnection c = QDBusConnection::systemBus();
 
422
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
 
423
                                                      mPath.path(),
 
424
                                                      "org.freedesktop.UDisks2.Filesystem",
 
425
                                                      "Mount");
 
426
    QVariantMap options;
 
427
    if (fileSystem() == "vfat")
 
428
        options.insert("options", "flush");
 
429
 
 
430
    msg << options;
 
431
 
 
432
    return c.callWithCallback(msg,
 
433
                              this,
 
434
                              SLOT(aboutToMount()),
 
435
                              SLOT(dbusError(QDBusError,QDBusMessage)));
 
436
}
 
437
 
 
438
bool UDisks2MountDevice::unmount()
 
439
{
 
440
    qDebug() << "UNMOUNT" << mPath.path();
 
441
 
 
442
    if (!mIsMounted)
 
443
        return true;
 
444
 
 
445
    QDBusConnection c = QDBusConnection::systemBus();
 
446
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
 
447
                                                      mPath.path(),
 
448
                                                      "org.freedesktop.UDisks2.Filesystem",
 
449
                                                      "Unmount");
 
450
    QVariantMap options;
 
451
    if (fileSystem() == "vfat")
 
452
        options.insert("options", "flush");
 
453
 
 
454
    msg << options;
 
455
 
 
456
    return c.callWithCallback(msg,
 
457
                              this,
 
458
                              SLOT(aboutToUnmount()),
 
459
                              SLOT(dbusError(QDBusError,QDBusMessage)));
 
460
}
 
461
 
 
462
bool UDisks2MountDevice::eject()
 
463
{
 
464
    qDebug() << "EJECT" << mPath.path();
 
465
 
 
466
    return mDriveIface->callWithCallback("Eject",
 
467
                                         QVariantList(),
 
468
                                         this,
 
469
                                         SLOT(aboutToEject()),
 
470
                                         SLOT(dbusError(QDBusError,QDBusMessage)));
 
471
}
 
472
 
 
473
void UDisks2MountDevice::aboutToMount()
 
474
{
 
475
    update();
 
476
    emit mounted();
 
477
}
 
478
 
 
479
void UDisks2MountDevice::aboutToUnmount()
 
480
{
 
481
    update();
 
482
    emit unmounted();
 
483
}
 
484
 
 
485
void UDisks2MountDevice::aboutToEject()
 
486
{
 
487
    qDebug() << "UDisks2MountDevice::aboutToEject success";
 
488
}
 
489
 
 
490
} // namespace LxQt