~lubuntu-dev/lxde/liblxqt-mount

« back to all changes in this revision

Viewing changes to rzmountproviders.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: 2012 Razor team
8
 
 *            2013-2014 LXQt team
9
 
 * Authors:
10
 
 *   Alexander Sokoloff <sokoloff.a@gmail.com>
11
 
 *   Petr Vanek <petr@scribus.info>
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 "rzmountproviders.h"
31
 
#include <QtDBus/QDBusConnection>
32
 
#include <QtDBus/QDBusArgument>
33
 
#include <QtDBus/QDBusInterface>
34
 
#include <QtDBus/QDBusMessage>
35
 
#include <QtCore/QDebug>
36
 
 
37
 
namespace LxQt {
38
 
 
39
 
/************************************************
40
 
 
41
 
 ************************************************/
42
 
MountProvider::MountProvider(QObject *parent):
43
 
    QObject(parent),
44
 
    mIsValid(false)
45
 
{
46
 
}
47
 
 
48
 
 
49
 
/************************************************
50
 
 
51
 
 ************************************************/
52
 
UDiskProvider::UDiskProvider(QObject *parent):
53
 
    MountProvider(parent)
54
 
{
55
 
    QDBusConnection system = QDBusConnection::systemBus();
56
 
 
57
 
    if (!system.isConnected())
58
 
    {
59
 
        return;
60
 
    }
61
 
 
62
 
    if (!QDBusInterface("org.freedesktop.UDisks",
63
 
                       "/org/freedesktop/UDisks",
64
 
                       "org.freedesktop.UDisks", system).isValid())
65
 
    {
66
 
        qDebug() << "org.freedesktop.UDisks - not exists - " << mIsValid;
67
 
        return;
68
 
    }
69
 
 
70
 
    system.connect("org.freedesktop.UDisks",
71
 
                   "/org/freedesktop/UDisks",
72
 
                   "org.freedesktop.UDisks",
73
 
                   "DeviceAdded",
74
 
                   this,
75
 
                   SLOT(dbusDeviceAdded(QDBusObjectPath)));
76
 
 
77
 
    system.connect("org.freedesktop.UDisks",
78
 
                   "/org/freedesktop/UDisks",
79
 
                   "org.freedesktop.UDisks",
80
 
                   "DeviceRemoved",
81
 
                   this,
82
 
                   SLOT(dbusDeviceRemoved(QDBusObjectPath)));
83
 
 
84
 
    system.connect("org.freedesktop.UDisks",
85
 
                   "/org/freedesktop/UDisks",
86
 
                   "org.freedesktop.UDisks",
87
 
                   "DeviceChanged",
88
 
                   this,
89
 
                   SLOT(dbusDeviceChanged(QDBusObjectPath)));
90
 
 
91
 
    mIsValid = true;
92
 
}
93
 
 
94
 
 
95
 
/************************************************
96
 
 
97
 
 ************************************************/
98
 
void UDiskProvider::update()
99
 
{
100
 
 
101
 
    QDBusInterface devEnum("org.freedesktop.UDisks",
102
 
                           "/org/freedesktop/UDisks",
103
 
                           "org.freedesktop.UDisks",
104
 
                           QDBusConnection::systemBus());
105
 
 
106
 
    QDBusMessage enumRes = devEnum.call("EnumerateDevices");
107
 
    if (enumRes.type() != QDBusMessage::ReplyMessage || !enumRes.arguments().at(0).canConvert<QDBusArgument>())
108
 
    {
109
 
        if (enumRes.type() == QDBusMessage::ErrorMessage)
110
 
            qWarning() << "ERROR: Can't call EnumerateDevices"
111
 
                       <<  qPrintable(enumRes.errorName()) << qPrintable(enumRes.errorMessage());
112
 
        else
113
 
            qWarning() << "ERROR: Unexpected result type of EnumerateDevices call";
114
 
        return;
115
 
    }
116
 
 
117
 
    const QDBusArgument enumArg = enumRes.arguments().at(0).value<QDBusArgument>();
118
 
    if (enumArg.currentType() != QDBusArgument::ArrayType)
119
 
    {
120
 
        qWarning() << "ERROR: Unexpected argument type of EnumerateDevices call";
121
 
        return;
122
 
    }
123
 
 
124
 
    enumArg.beginArray();
125
 
    while (!enumArg.atEnd())
126
 
    {
127
 
        QDBusObjectPath path = qdbus_cast<QDBusObjectPath>(enumArg);
128
 
 
129
 
        if (mDevicesByPath.contains(path.path()))
130
 
            dbusDeviceChanged(path);
131
 
        else
132
 
            dbusDeviceAdded(path);
133
 
    }
134
 
    enumArg.endArray();
135
 
 
136
 
}
137
 
 
138
 
 
139
 
/************************************************
140
 
 
141
 
 ************************************************/
142
 
UDiskMountDevice *UDiskProvider::getDevice(const QDBusObjectPath &path) const
143
 
{
144
 
    if (mDevicesByPath.contains(path.path()))
145
 
        return mDevicesByPath[path.path()];
146
 
    else
147
 
        return 0;
148
 
}
149
 
 
150
 
 
151
 
/************************************************
152
 
 
153
 
 ************************************************/
154
 
void UDiskProvider::addDevice(UDiskMountDevice *device)
155
 
{
156
 
    mDevicesByPath.insert(device->udiskPath(), device);
157
 
    mDevices.append(device);
158
 
}
159
 
 
160
 
 
161
 
/************************************************
162
 
 
163
 
 ************************************************/
164
 
void UDiskProvider::delDevice(UDiskMountDevice *device)
165
 
{
166
 
    mDevices.removeAll(device);
167
 
    mDevicesByPath.remove(device->udiskPath());
168
 
    device->deleteLater();
169
 
}
170
 
 
171
 
 
172
 
/************************************************
173
 
 
174
 
 ************************************************/
175
 
void UDiskProvider::dbusDeviceAdded(const QDBusObjectPath &path)
176
 
{
177
 
    UDiskMountDevice *device = new UDiskMountDevice(path);
178
 
     addDevice(device);
179
 
     emit deviceAdded(device);
180
 
}
181
 
 
182
 
 
183
 
/************************************************
184
 
 
185
 
 ************************************************/
186
 
void UDiskProvider::dbusDeviceRemoved(const QDBusObjectPath &path)
187
 
{
188
 
    UDiskMountDevice *device = getDevice(path);
189
 
    if (device)
190
 
    {
191
 
        emit deviceRemoved(device);
192
 
        delDevice(device);
193
 
    }
194
 
}
195
 
 
196
 
 
197
 
/************************************************
198
 
 
199
 
 ************************************************/
200
 
void UDiskProvider::dbusDeviceChanged(const QDBusObjectPath &path)
201
 
{
202
 
    UDiskMountDevice *device = getDevice(path);
203
 
    if (!device)
204
 
        return;
205
 
 
206
 
    if (device->update())
207
 
        emit deviceChanged(device);
208
 
}
209
 
 
210
 
 
211
 
/************************************************
212
 
 
213
 
 ************************************************/
214
 
UDiskMountDevice::UDiskMountDevice(const QDBusObjectPath &path):
215
 
    MountDevice(),
216
 
    mUdiskPath(path.path())
217
 
{
218
 
    mDbus = new QDBusInterface("org.freedesktop.UDisks",
219
 
                               path.path(),
220
 
                               "org.freedesktop.UDisks.Device",
221
 
                               QDBusConnection::systemBus(),
222
 
                               this);
223
 
    update();
224
 
}
225
 
 
226
 
 
227
 
/************************************************
228
 
 
229
 
 ************************************************/
230
 
bool UDiskMountDevice::update()
231
 
{
232
 
    bool res = false;
233
 
    res = setDevFile(mDbus->property("DeviceFile").toString()) || res;
234
 
 
235
 
    res = setMediaType(calcMediaType()) || res;
236
 
    res = setLabel(calcLabel()) || res;
237
 
    res = setIsExternal(calcIsExternal()) || res;
238
 
    res = setIconName(calcIconName()) || res;
239
 
 
240
 
    res = setIsMounted(mDbus->property("DeviceIsMounted").toBool()) || res;
241
 
 
242
 
    if (mDbus->property("DeviceIsDrive").toBool())
243
 
        res = setIsEjectable(mDbus->property("DriveIsMediaEjectable").toBool());
244
 
 
245
 
    res = setSize(mDbus->property("DeviceSize").toULongLong()) || res;
246
 
    res = setVendor(mDbus->property("DriveVendor").toString()) || res;
247
 
    res = setModel(mDbus->property("DriveModel").toString()) || res;
248
 
    res = setFileSystem(mDbus->property("IdType").toString()) || res;
249
 
 
250
 
    QStringList paths = mDbus->property("DeviceMountPaths").toStringList();
251
 
    if (!paths.empty())
252
 
        res = setMountPath(paths.first()) || res;
253
 
 
254
 
    if (res)
255
 
        emit changed();
256
 
 
257
 
    return res;
258
 
}
259
 
 
260
 
 
261
 
/************************************************
262
 
 
263
 
 ************************************************/
264
 
MountDevice::MediaType UDiskMountDevice::calcMediaType()
265
 
{
266
 
    if (mDbus->property("DeviceIsOpticalDisc").toBool())
267
 
        return MountDevice::MediaTypeOptical;
268
 
 
269
 
    const QString media = mDbus->property("DriveMedia").toString();
270
 
    const QString mediaCompat = mDbus->property("DriveMediaCompatibility").toString();
271
 
    const QString idUsage = mDbus->property("IdUsage").toString();
272
 
 
273
 
    if (mDbus->property("DeviceIsDrive").toBool())
274
 
    {
275
 
        if (mediaCompat == "floppy")
276
 
            return MountDevice::MediaTypeFdd;
277
 
 
278
 
        if (idUsage == "filesystem")
279
 
            return MountDevice::MediaTypeDrive;
280
 
 
281
 
        return MountDevice::MediaTypeUnknown;
282
 
    }
283
 
 
284
 
    if (mDbus->property("DeviceIsPartition").toBool())
285
 
    {
286
 
        if (idUsage == "filesystem")
287
 
            return MountDevice::MediaTypePartition;
288
 
 
289
 
        return MountDevice::MediaTypeUnknown;
290
 
    }
291
 
 
292
 
    return MountDevice::MediaTypeUnknown;
293
 
}
294
 
 
295
 
 
296
 
/************************************************
297
 
 
298
 
 ************************************************/
299
 
QString UDiskMountDevice::calcLabel()
300
 
{
301
 
    const QString idLabel = mDbus->property("IdLabel").toString();
302
 
 
303
 
    if (mMediaType ==  MediaTypeFdd)
304
 
        return tr("Floppy drive");
305
 
 
306
 
    if (mMediaType ==  MediaTypeOptical)
307
 
        return idLabel;
308
 
 
309
 
 
310
 
    const QString driveVendor = mDbus->property("DriveVendor").toString();
311
 
    const QString driveModel  = mDbus->property("DriveModel").toString();
312
 
    const qulonglong size = mDbus->property("DeviceSize").toULongLong();
313
 
 
314
 
    QString label;
315
 
    if (!idLabel.isEmpty())
316
 
    {
317
 
        label = idLabel;
318
 
    }
319
 
    else
320
 
    {
321
 
       if (!driveVendor.isEmpty())
322
 
            label = driveVendor;
323
 
 
324
 
       if (!driveModel.isEmpty())
325
 
            label += QString(" - %1").arg(driveModel);
326
 
    }
327
 
 
328
 
    if (label.isEmpty())
329
 
        label = mDevFile;
330
 
 
331
 
    if (mSize)
332
 
        label += QString(" [%3]").arg(sizeToString(size));
333
 
 
334
 
    return label;
335
 
}
336
 
 
337
 
 
338
 
/************************************************
339
 
 
340
 
 ************************************************/
341
 
bool UDiskMountDevice::calcIsExternal()
342
 
{
343
 
    return ! mDbus->property("DeviceIsSystemInternal").toBool();
344
 
//    if (mDbus->property("DeviceIsSystemInternal").toBool())
345
 
//        return false;
346
 
 
347
 
//    if (mDbus->property("DeviceIsMediaAvailable").toBool()
348
 
//        && mDbus->property("IdUsage").toString() == "filesystem")
349
 
//        return true;
350
 
 
351
 
//    if (mDbus->property("DeviceIsDrive").toBool())
352
 
//    {
353
 
//        return true;
354
 
//    }
355
 
 
356
 
 //   return false;
357
 
}
358
 
 
359
 
 
360
 
/************************************************
361
 
 
362
 
 ************************************************/
363
 
QString UDiskMountDevice::calcIconName()
364
 
{
365
 
    const QString media = mDbus->property( "DriveMedia" ).toString();
366
 
 
367
 
    switch (mMediaType)
368
 
    {
369
 
    // ..............................................
370
 
    case MediaTypeDrive:
371
 
    case MediaTypePartition:
372
 
        {
373
 
            // handle drives
374
 
            const QString conn = mDbus->property( "DriveConnectionInterface" ).toString();
375
 
 
376
 
            if (conn == "usb")
377
 
                return "drive-removable-media-usb";
378
 
 
379
 
            return "drive-removable-media";
380
 
        }
381
 
 
382
 
    // ..............................................
383
 
    case MediaTypeFdd:
384
 
        {
385
 
            return "media-floppy";
386
 
        }
387
 
 
388
 
 
389
 
    // ..............................................
390
 
//    case MediaTypeFlash:
391
 
//        {
392
 
//            if ( media == "flash_ms" ) // Flash & Co.
393
 
//                return "media-flash-memory-stick";
394
 
 
395
 
//            if ( media == "flash_sd" ||
396
 
//                 media == "flash_sdhc" ||
397
 
//                 media == "flash_mmc" )
398
 
//                return "media-flash-sd-mmc";
399
 
 
400
 
//            if ( media == "flash_sm" )
401
 
//                return "media-flash-smart-media";
402
 
 
403
 
//            return "media-flash";
404
 
//        }
405
 
 
406
 
 
407
 
    // ..............................................
408
 
    case MediaTypeOptical:
409
 
        {
410
 
            bool isWritable = mDbus->property( "OpticalDiscIsBlank" ).toBool() ||
411
 
                              mDbus->property("OpticalDiscIsAppendable").toBool();
412
 
 
413
 
            if (isWritable)
414
 
                return "media-optical-recordable";
415
 
 
416
 
            if (media.startsWith("optical_dvd") ||
417
 
                media.startsWith( "optical_hddvd")) // DVD
418
 
                return "media-optical-dvd";
419
 
 
420
 
            if (media.startsWith("optical_bd")) // BluRay
421
 
                return "media-optical-blu-ray";
422
 
 
423
 
            return "media-optical";
424
 
        }
425
 
 
426
 
    // ..............................................
427
 
    case MediaTypeUnknown:
428
 
        {
429
 
            return "drive-harddisk";
430
 
        }
431
 
    }
432
 
 
433
 
     return "drive-harddisk";
434
 
}
435
 
 
436
 
 
437
 
/************************************************
438
 
 
439
 
 ************************************************/
440
 
void UDiskMountDevice::dbusError(const QDBusError &err, const QDBusMessage &msg)
441
 
{
442
 
    qWarning() << "UdisksInfo::mDbus_error" << err.message();
443
 
    emit error(err.message());
444
 
}
445
 
 
446
 
 
447
 
/************************************************
448
 
 
449
 
 ************************************************/
450
 
bool UDiskMountDevice::mount()
451
 
{
452
 
    if (mIsMounted)
453
 
        return true;
454
 
 
455
 
    QList<QVariant> args;
456
 
    args << QVariant(QString()) << QVariant(QStringList());
457
 
 
458
 
    bool ret;
459
 
    ret = mDbus->callWithCallback("FilesystemMount", args, this,
460
 
                             //SLOT(dbusSuccess(QDBusMessage)),
461
 
                             SIGNAL(mounted()),
462
 
                             SLOT(dbusError(QDBusError, QDBusMessage)));
463
 
 
464
 
    QStringList paths = mDbus->property("DeviceMountPaths").toStringList();
465
 
 
466
 
    if (!paths.empty())
467
 
        mMountPath = paths.at(0);
468
 
    else
469
 
        mMountPath = "";
470
 
 
471
 
    return ret;
472
 
}
473
 
 
474
 
 
475
 
/************************************************
476
 
 
477
 
 ************************************************/
478
 
bool UDiskMountDevice::unmount()
479
 
{
480
 
    if (!mIsMounted)
481
 
        return true;
482
 
 
483
 
    QList<QVariant> args;
484
 
    args << QVariant(QStringList());
485
 
 
486
 
    bool ret;
487
 
    ret = mDbus->callWithCallback("FilesystemUnmount", args, this,
488
 
//                             SLOT(dbusSuccess(QDBusMessage)),
489
 
                             SIGNAL(unmounted()),
490
 
                             SLOT(dbusError(QDBusError, QDBusMessage)));
491
 
    return ret;
492
 
}
493
 
 
494
 
 
495
 
/************************************************
496
 
 
497
 
 ************************************************/
498
 
bool UDiskMountDevice::eject()
499
 
{
500
 
    if (!mIsMounted)
501
 
        return false;
502
 
 
503
 
    QList<QVariant> args;
504
 
    args << QVariant(QStringList());
505
 
 
506
 
    return mDbus->callWithCallback("DriveEject", args, this,
507
 
                             SLOT(dbusSuccess(QDBusMessage)),
508
 
                             SLOT(dbusError(QDBusError, QDBusMessage)));
509
 
}
510
 
 
511
 
} // namespace LxQt