1
/* BEGIN_COMMON_COPYRIGHT_HEADER
4
* LXQt - The Lightweight Desktop Environment
7
* Copyright: 2013 Razor team
10
* Petr Vanek <petr@yarpen.cz>
11
* Alexander Sokoloff <sokoloff.a@gmail.com>
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 */
31
#include "providers.h"
35
UDisks2Provider::UDisks2Provider(QObject *parent):
38
QDBusConnection system = QDBusConnection::systemBus();
40
if (!system.isConnected())
42
qDebug() << "UDisks2Provider::UDisks2Provider - not connected - " << mIsValid;
47
if (!QDBusInterface("org.freedesktop.UDisks2",
48
"/org/freedesktop/UDisks2",
49
"org.freedesktop.UDisks2", system).isValid())
51
qDebug() << "org.freedesktop.UDisks2 - not exists - " << mIsValid;
55
system.connect("org.freedesktop.UDisks2",
56
"/org/freedesktop/UDisks2",
57
"org.freedesktop.UDisks2",
60
SLOT(dbusDeviceAdded(QDBusObjectPath,QVariantMap)));
62
system.connect("org.freedesktop.UDisks2",
63
"/org/freedesktop/UDisks2",
64
"org.freedesktop.UDisks2",
67
SLOT(dbusDeviceRemoved(QDBusObjectPath,QStringList)));
70
qDebug() << "UDisks2Provider::UDisks2Provider final validity" << mIsValid;
74
void UDisks2Provider::update()
76
qDebug() << "UDisks2Provider::update() called";
78
QList<QDBusObjectPath> paths;
79
QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
80
"/org/freedesktop/UDisks2/block_devices",
81
"org.freedesktop.DBus.Introspectable",
83
QDBusPendingReply<QString> reply = QDBusConnection::systemBus().call(call);
87
qWarning("UDisks2Manager: error: %s", qPrintable(reply.error().name()));
91
QXmlStreamReader xml(reply.value());
95
if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name().toString() == "node" )
97
QString name = xml.attributes().value("name").toString();
99
paths << QDBusObjectPath("/org/freedesktop/UDisks2/block_devices/" + name);
103
foreach (QDBusObjectPath i, paths)
105
// qDebug() << "UDisks2Provider::update() path:" << i.path();
106
if (mDevicesByPath.contains(i.path()))
107
dbusDeviceChanged(i);
109
dbusDeviceAdded(i, QVariantMap());
112
// qDebug() << mDevicesByPath;
115
UDisks2MountDevice *UDisks2Provider::getDevice(const QDBusObjectPath &path) const
117
if (mDevicesByPath.contains(path.path()))
118
return mDevicesByPath[path.path()];
123
void UDisks2Provider::addDevice(UDisks2MountDevice *device)
125
mDevicesByPath.insert(device->path().path(), device);
126
mDevices.append(device);
129
void UDisks2Provider::delDevice(UDisks2MountDevice *device)
131
mDevices.removeAll(device);
132
mDevicesByPath.remove(device->path().path());
133
device->deleteLater();
136
void UDisks2Provider::dbusDeviceAdded(const QDBusObjectPath &path, const QVariantMap &)
138
if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
141
UDisks2MountDevice *device = new UDisks2MountDevice(path);
144
emit deviceAdded(device);
147
void UDisks2Provider::dbusDeviceRemoved(const QDBusObjectPath &path, const QStringList &)
149
if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
152
UDisks2MountDevice *device = getDevice(path);
155
emit deviceRemoved(device);
160
void UDisks2Provider::dbusDeviceChanged(const QDBusObjectPath &path)
163
if(path.path().startsWith("/org/freedesktop/UDisks2/jobs"))
166
UDisks2MountDevice *device = getDevice(path);
170
if (device->update())
171
emit deviceChanged(device);
175
UDisks2MountDevice::UDisks2MountDevice(const QDBusObjectPath &path):
179
// qDebug() << "UDisks2MountDevice::UDisks2MountDevice path" << mPath.path();
180
mBlockIface = new QDBusInterface("org.freedesktop.UDisks2",
182
"org.freedesktop.UDisks2.Block",
183
QDBusConnection::systemBus(),
185
Q_ASSERT(mBlockIface);
187
QDBusObjectPath drive_object = mBlockIface->property("Drive").value<QDBusObjectPath>();
188
// qDebug() << "UDisks2MountDevice::UDisks2MountDevice drive_object" << drive_object.path();
190
QDBusConnection::systemBus().connect("org.freedesktop.UDisks2",
192
"org.freedesktop.DBus.Properties","PropertiesChanged",
196
mDriveIface = new QDBusInterface("org.freedesktop.UDisks2",
198
"org.freedesktop.UDisks2.Drive",
199
QDBusConnection::systemBus(),
201
Q_ASSERT(mDriveIface);
206
void UDisks2MountDevice::update()
210
res = setDevFile(mBlockIface->property("Device").toByteArray().data()) || res;
212
res = setMediaType(calcMediaType()) || res;
213
res = setLabel(calcLabel()) || res;
214
res = setIsExternal(calcIsExternal()) || res;
215
res = setIconName(calcIconName()) || res;
217
QStringList mounts = mountPoints();
218
res = setIsMounted(mounts.count() != 0) || res;
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;
227
res = setMountPath(mounts.first()) || res;
233
QStringList UDisks2MountDevice::mountPoints() const
236
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
238
"org.freedesktop.DBus.Properties",
241
QList<QVariant> args;
242
args << "org.freedesktop.UDisks2.Filesystem" << "MountPoints";
243
message.setArguments(args);
245
QDBusMessage reply = QDBusConnection::systemBus().call(message);
248
foreach (QVariant arg, reply.arguments())
249
arg.value<QDBusVariant>().variant().value<QDBusArgument>() >> l;
251
foreach (QByteArray p, l)
254
qDebug() << "UDisks2MountDevice::mountPoints()" << points;
258
MountDevice::MediaType UDisks2MountDevice::calcMediaType()
260
if (mDriveIface->property("Optical").toBool())
261
return MountDevice::MediaTypeOptical;
263
const QString media = mDriveIface->property("Media").toString();
264
const QStringList mediaCompat = mDriveIface->property("MediaCompatibility").toStringList();
265
const QString idUsage = mBlockIface->property("IdUsage").toString();
267
// TODO/FIXME: how to get it from udisks2?!
268
// if (mDbus->property("DeviceIsDrive").toBool())
270
// TODO/FIXME: just guessing...
271
if (mediaCompat.contains("floppy"))
272
return MountDevice::MediaTypeFdd;
274
if (idUsage == "filesystem")
275
return MountDevice::MediaTypeDrive;
277
return MountDevice::MediaTypeUnknown;
280
// if (mDbus->property("DeviceIsPartition").toBool())
282
// if (idUsage == "filesystem")
283
// return MountDevice::MediaTypePartition;
285
// return MountDevice::MediaTypeUnknown;
288
return MountDevice::MediaTypeUnknown;
292
QString UDisks2MountDevice::calcLabel()
294
const QString idLabel = mBlockIface->property("IdLabel").toString();
296
if (mMediaType == MediaTypeFdd)
297
return tr("Floppy drive");
299
if (mMediaType == MediaTypeOptical)
303
const QString driveVendor = mDriveIface->property("Vendor").toString();
304
const QString driveModel = mDriveIface->property("Model").toString();
305
const qulonglong size = mBlockIface->property("Size").toULongLong();
308
if (!idLabel.isEmpty())
314
if (!driveVendor.isEmpty())
317
if (!driveModel.isEmpty())
318
label += QString(" - %1").arg(driveModel);
325
label += QString(" [%3]").arg(sizeToString(size));
327
qDebug() << "LABEL" << mPath.path() << label;
331
bool UDisks2MountDevice::calcIsExternal()
333
// TODO/FIXME: I'm really unsure here...
334
return mDriveIface->property("Removable").toBool();
337
QString UDisks2MountDevice::calcIconName()
339
const QString media = mDriveIface->property( "Media" ).toString();
343
// ..............................................
345
case MediaTypePartition:
348
const QString conn = mDriveIface->property( "ConnectionBus" ).toString();
351
return "drive-removable-media-usb";
353
return "drive-removable-media";
356
// ..............................................
359
return "media-floppy";
363
// ..............................................
364
// case MediaTypeFlash:
366
// if ( media == "flash_ms" ) // Flash & Co.
367
// return "media-flash-memory-stick";
369
// if ( media == "flash_sd" ||
370
// media == "flash_sdhc" ||
371
// media == "flash_mmc" )
372
// return "media-flash-sd-mmc";
374
// if ( media == "flash_sm" )
375
// return "media-flash-smart-media";
377
// return "media-flash";
381
case MediaTypeOptical:
383
bool isWritable = mDriveIface->property( "OpticalBlank" ).toBool()
384
// TODO/FIXME: how in udisks2?
386
// mDbus->property("OpticalDiscIsAppendable").toBool();
390
return "media-optical-recordable";
392
if (media.startsWith("optical_dvd") ||
393
media.startsWith( "optical_hddvd")) // DVD
394
return "media-optical-dvd";
396
if (media.startsWith("optical_bd")) // BluRay
397
return "media-optical-blu-ray";
399
return "media-optical";
402
case MediaTypeUnknown:
404
return "drive-harddisk";
408
return "drive-harddisk";
411
void UDisks2MountDevice::dbusError(const QDBusError &err, const QDBusMessage &msg)
413
qWarning() << "UDisks2MountDevice::mDbus_error" << err.message();
414
emit error(err.message());
417
bool UDisks2MountDevice::mount()
419
qDebug() << "MOUNT" << mPath.path();
421
QDBusConnection c = QDBusConnection::systemBus();
422
QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
424
"org.freedesktop.UDisks2.Filesystem",
427
if (fileSystem() == "vfat")
428
options.insert("options", "flush");
432
return c.callWithCallback(msg,
434
SLOT(aboutToMount()),
435
SLOT(dbusError(QDBusError,QDBusMessage)));
438
bool UDisks2MountDevice::unmount()
440
qDebug() << "UNMOUNT" << mPath.path();
445
QDBusConnection c = QDBusConnection::systemBus();
446
QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks2",
448
"org.freedesktop.UDisks2.Filesystem",
451
if (fileSystem() == "vfat")
452
options.insert("options", "flush");
456
return c.callWithCallback(msg,
458
SLOT(aboutToUnmount()),
459
SLOT(dbusError(QDBusError,QDBusMessage)));
462
bool UDisks2MountDevice::eject()
464
qDebug() << "EJECT" << mPath.path();
466
return mDriveIface->callWithCallback("Eject",
469
SLOT(aboutToEject()),
470
SLOT(dbusError(QDBusError,QDBusMessage)));
473
void UDisks2MountDevice::aboutToMount()
479
void UDisks2MountDevice::aboutToUnmount()
485
void UDisks2MountDevice::aboutToEject()
487
qDebug() << "UDisks2MountDevice::aboutToEject success";