1
/* BEGIN_COMMON_COPYRIGHT_HEADER
4
* LXQt - The Lightweight Desktop Environment
7
* Copyright: 2012 Razor team
10
* Alexander Sokoloff <sokoloff.a@gmail.com>
11
* Petr Vanek <petr@scribus.info>
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.
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.
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
28
* END_COMMON_COPYRIGHT_HEADER */
30
#include "rzmountproviders.h"
31
#include <QtDBus/QDBusConnection>
32
#include <QtDBus/QDBusArgument>
33
#include <QtDBus/QDBusInterface>
34
#include <QtDBus/QDBusMessage>
35
#include <QtCore/QDebug>
39
/************************************************
41
************************************************/
42
MountProvider::MountProvider(QObject *parent):
49
/************************************************
51
************************************************/
52
UDiskProvider::UDiskProvider(QObject *parent):
55
QDBusConnection system = QDBusConnection::systemBus();
57
if (!system.isConnected())
62
if (!QDBusInterface("org.freedesktop.UDisks",
63
"/org/freedesktop/UDisks",
64
"org.freedesktop.UDisks", system).isValid())
66
qDebug() << "org.freedesktop.UDisks - not exists - " << mIsValid;
70
system.connect("org.freedesktop.UDisks",
71
"/org/freedesktop/UDisks",
72
"org.freedesktop.UDisks",
75
SLOT(dbusDeviceAdded(QDBusObjectPath)));
77
system.connect("org.freedesktop.UDisks",
78
"/org/freedesktop/UDisks",
79
"org.freedesktop.UDisks",
82
SLOT(dbusDeviceRemoved(QDBusObjectPath)));
84
system.connect("org.freedesktop.UDisks",
85
"/org/freedesktop/UDisks",
86
"org.freedesktop.UDisks",
89
SLOT(dbusDeviceChanged(QDBusObjectPath)));
95
/************************************************
97
************************************************/
98
void UDiskProvider::update()
101
QDBusInterface devEnum("org.freedesktop.UDisks",
102
"/org/freedesktop/UDisks",
103
"org.freedesktop.UDisks",
104
QDBusConnection::systemBus());
106
QDBusMessage enumRes = devEnum.call("EnumerateDevices");
107
if (enumRes.type() != QDBusMessage::ReplyMessage || !enumRes.arguments().at(0).canConvert<QDBusArgument>())
109
if (enumRes.type() == QDBusMessage::ErrorMessage)
110
qWarning() << "ERROR: Can't call EnumerateDevices"
111
<< qPrintable(enumRes.errorName()) << qPrintable(enumRes.errorMessage());
113
qWarning() << "ERROR: Unexpected result type of EnumerateDevices call";
117
const QDBusArgument enumArg = enumRes.arguments().at(0).value<QDBusArgument>();
118
if (enumArg.currentType() != QDBusArgument::ArrayType)
120
qWarning() << "ERROR: Unexpected argument type of EnumerateDevices call";
124
enumArg.beginArray();
125
while (!enumArg.atEnd())
127
QDBusObjectPath path = qdbus_cast<QDBusObjectPath>(enumArg);
129
if (mDevicesByPath.contains(path.path()))
130
dbusDeviceChanged(path);
132
dbusDeviceAdded(path);
139
/************************************************
141
************************************************/
142
UDiskMountDevice *UDiskProvider::getDevice(const QDBusObjectPath &path) const
144
if (mDevicesByPath.contains(path.path()))
145
return mDevicesByPath[path.path()];
151
/************************************************
153
************************************************/
154
void UDiskProvider::addDevice(UDiskMountDevice *device)
156
mDevicesByPath.insert(device->udiskPath(), device);
157
mDevices.append(device);
161
/************************************************
163
************************************************/
164
void UDiskProvider::delDevice(UDiskMountDevice *device)
166
mDevices.removeAll(device);
167
mDevicesByPath.remove(device->udiskPath());
168
device->deleteLater();
172
/************************************************
174
************************************************/
175
void UDiskProvider::dbusDeviceAdded(const QDBusObjectPath &path)
177
UDiskMountDevice *device = new UDiskMountDevice(path);
179
emit deviceAdded(device);
183
/************************************************
185
************************************************/
186
void UDiskProvider::dbusDeviceRemoved(const QDBusObjectPath &path)
188
UDiskMountDevice *device = getDevice(path);
191
emit deviceRemoved(device);
197
/************************************************
199
************************************************/
200
void UDiskProvider::dbusDeviceChanged(const QDBusObjectPath &path)
202
UDiskMountDevice *device = getDevice(path);
206
if (device->update())
207
emit deviceChanged(device);
211
/************************************************
213
************************************************/
214
UDiskMountDevice::UDiskMountDevice(const QDBusObjectPath &path):
216
mUdiskPath(path.path())
218
mDbus = new QDBusInterface("org.freedesktop.UDisks",
220
"org.freedesktop.UDisks.Device",
221
QDBusConnection::systemBus(),
227
/************************************************
229
************************************************/
230
bool UDiskMountDevice::update()
233
res = setDevFile(mDbus->property("DeviceFile").toString()) || res;
235
res = setMediaType(calcMediaType()) || res;
236
res = setLabel(calcLabel()) || res;
237
res = setIsExternal(calcIsExternal()) || res;
238
res = setIconName(calcIconName()) || res;
240
res = setIsMounted(mDbus->property("DeviceIsMounted").toBool()) || res;
242
if (mDbus->property("DeviceIsDrive").toBool())
243
res = setIsEjectable(mDbus->property("DriveIsMediaEjectable").toBool());
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;
250
QStringList paths = mDbus->property("DeviceMountPaths").toStringList();
252
res = setMountPath(paths.first()) || res;
261
/************************************************
263
************************************************/
264
MountDevice::MediaType UDiskMountDevice::calcMediaType()
266
if (mDbus->property("DeviceIsOpticalDisc").toBool())
267
return MountDevice::MediaTypeOptical;
269
const QString media = mDbus->property("DriveMedia").toString();
270
const QString mediaCompat = mDbus->property("DriveMediaCompatibility").toString();
271
const QString idUsage = mDbus->property("IdUsage").toString();
273
if (mDbus->property("DeviceIsDrive").toBool())
275
if (mediaCompat == "floppy")
276
return MountDevice::MediaTypeFdd;
278
if (idUsage == "filesystem")
279
return MountDevice::MediaTypeDrive;
281
return MountDevice::MediaTypeUnknown;
284
if (mDbus->property("DeviceIsPartition").toBool())
286
if (idUsage == "filesystem")
287
return MountDevice::MediaTypePartition;
289
return MountDevice::MediaTypeUnknown;
292
return MountDevice::MediaTypeUnknown;
296
/************************************************
298
************************************************/
299
QString UDiskMountDevice::calcLabel()
301
const QString idLabel = mDbus->property("IdLabel").toString();
303
if (mMediaType == MediaTypeFdd)
304
return tr("Floppy drive");
306
if (mMediaType == MediaTypeOptical)
310
const QString driveVendor = mDbus->property("DriveVendor").toString();
311
const QString driveModel = mDbus->property("DriveModel").toString();
312
const qulonglong size = mDbus->property("DeviceSize").toULongLong();
315
if (!idLabel.isEmpty())
321
if (!driveVendor.isEmpty())
324
if (!driveModel.isEmpty())
325
label += QString(" - %1").arg(driveModel);
332
label += QString(" [%3]").arg(sizeToString(size));
338
/************************************************
340
************************************************/
341
bool UDiskMountDevice::calcIsExternal()
343
return ! mDbus->property("DeviceIsSystemInternal").toBool();
344
// if (mDbus->property("DeviceIsSystemInternal").toBool())
347
// if (mDbus->property("DeviceIsMediaAvailable").toBool()
348
// && mDbus->property("IdUsage").toString() == "filesystem")
351
// if (mDbus->property("DeviceIsDrive").toBool())
360
/************************************************
362
************************************************/
363
QString UDiskMountDevice::calcIconName()
365
const QString media = mDbus->property( "DriveMedia" ).toString();
369
// ..............................................
371
case MediaTypePartition:
374
const QString conn = mDbus->property( "DriveConnectionInterface" ).toString();
377
return "drive-removable-media-usb";
379
return "drive-removable-media";
382
// ..............................................
385
return "media-floppy";
389
// ..............................................
390
// case MediaTypeFlash:
392
// if ( media == "flash_ms" ) // Flash & Co.
393
// return "media-flash-memory-stick";
395
// if ( media == "flash_sd" ||
396
// media == "flash_sdhc" ||
397
// media == "flash_mmc" )
398
// return "media-flash-sd-mmc";
400
// if ( media == "flash_sm" )
401
// return "media-flash-smart-media";
403
// return "media-flash";
407
// ..............................................
408
case MediaTypeOptical:
410
bool isWritable = mDbus->property( "OpticalDiscIsBlank" ).toBool() ||
411
mDbus->property("OpticalDiscIsAppendable").toBool();
414
return "media-optical-recordable";
416
if (media.startsWith("optical_dvd") ||
417
media.startsWith( "optical_hddvd")) // DVD
418
return "media-optical-dvd";
420
if (media.startsWith("optical_bd")) // BluRay
421
return "media-optical-blu-ray";
423
return "media-optical";
426
// ..............................................
427
case MediaTypeUnknown:
429
return "drive-harddisk";
433
return "drive-harddisk";
437
/************************************************
439
************************************************/
440
void UDiskMountDevice::dbusError(const QDBusError &err, const QDBusMessage &msg)
442
qWarning() << "UdisksInfo::mDbus_error" << err.message();
443
emit error(err.message());
447
/************************************************
449
************************************************/
450
bool UDiskMountDevice::mount()
455
QList<QVariant> args;
456
args << QVariant(QString()) << QVariant(QStringList());
459
ret = mDbus->callWithCallback("FilesystemMount", args, this,
460
//SLOT(dbusSuccess(QDBusMessage)),
462
SLOT(dbusError(QDBusError, QDBusMessage)));
464
QStringList paths = mDbus->property("DeviceMountPaths").toStringList();
467
mMountPath = paths.at(0);
475
/************************************************
477
************************************************/
478
bool UDiskMountDevice::unmount()
483
QList<QVariant> args;
484
args << QVariant(QStringList());
487
ret = mDbus->callWithCallback("FilesystemUnmount", args, this,
488
// SLOT(dbusSuccess(QDBusMessage)),
490
SLOT(dbusError(QDBusError, QDBusMessage)));
495
/************************************************
497
************************************************/
498
bool UDiskMountDevice::eject()
503
QList<QVariant> args;
504
args << QVariant(QStringList());
506
return mDbus->callWithCallback("DriveEject", args, this,
507
SLOT(dbusSuccess(QDBusMessage)),
508
SLOT(dbusError(QDBusError, QDBusMessage)));