1
//MountManager - the program for easy mounting of storage devices in Linux
2
//Copyright (C) 2007-2008 Tikhonov Sergey
4
//This file is part of MountManager Core
6
//This program is free software; you can redistribute it and/or
7
//modify it under the terms of the GNU General Public License
8
//as published by the Free Software Foundation; either version 2
9
//of the License, or (at your option) any later version.
11
//This program is distributed in the hope that it will be useful,
12
//but WITHOUT ANY WARRANTY; without even the implied warranty of
13
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
//GNU General Public License for more details.
16
//You should have received a copy of the GNU General Public License
17
//along with this program; if not, write to the Free Software
18
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
#include <QDBusInterface>
21
#include <QDBusMetaType>
22
#include <QDBusArgument>
26
#include <QTextStream>
27
#include "diskdevice.h"
28
#include "diskfstab.h"
29
#include "../gui/const.h"
31
Q_DECLARE_METATYPE(ChangeStruct)
32
Q_DECLARE_METATYPE(QList<ChangeStruct>)
34
const QDBusArgument &operator<<( QDBusArgument &arg, const ChangeStruct &change ) {
36
arg << change.propertyName << change.added << change.removed;
41
const QDBusArgument &operator>>( const QDBusArgument &arg, ChangeStruct &change ) {
43
arg >> change.propertyName >> change.added >> change.removed;
48
DiskDevice::DiskDevice(QDBusInterface *interface, const QString& udi) {
52
cdOrFloppyChildDevice = 0;
54
qDBusRegisterMetaType<ChangeStruct>();
55
qDBusRegisterMetaType< QList<ChangeStruct> >();
57
informationCenter = interface;
60
QDBusReply<QString> categoryReply = informationCenter->call("GetPropertyString","info.category");
61
deviceCategory = categoryReply.value();
62
disk = categoryReply.value() == "volume" ? false : true;
64
QDBusReply<QString> parentReply = informationCenter->call("GetPropertyString","info.parent");
65
deviceParentUdi = parentReply.value();
67
QDBusReply<QString> blockReply = informationCenter->call("GetPropertyString","block.device");
68
deviceBlockName = blockReply.value();
70
QDBusReply<QString> uuidReply = informationCenter->call("GetPropertyString","volume.uuid");
71
deviceUuid = uuidReply.value();
73
QDBusReply<QString> labelReply = informationCenter->call("GetPropertyString","volume.label");
74
deviceLabel = labelReply.value();
76
QDBusReply<QString> typeReply = informationCenter->call("GetPropertyString","storage.drive_type");
77
deviceType = typeReply.value();
78
cdOrFloppy = deviceType == "cdrom" || deviceType == "floppy";
80
QDBusReply<QString> fileSystemReply = informationCenter->call("GetPropertyString","volume.fstype");
81
deviceFileSystem = fileSystemReply.value();
82
// Floppy disks always have fat file system
83
if (deviceType == "floppy")
84
deviceFileSystem = "vfat";
86
QDBusReply<bool> removableReply = informationCenter->call("GetPropertyBoolean","storage.removable");
87
removable = removableReply.isValid() ? removableReply.value() : false;
89
QDBusReply<int> blocksReply = informationCenter->call("GetPropertyInteger","volume.num_blocks");
90
deviceBlocks = blocksReply.isValid() ? blocksReply.value() : 0;
92
QDBusReply<QString> productReply = informationCenter->call("GetPropertyString","info.product");
93
if (productReply.isValid()) deviceProduct = productReply.value();
95
QDBusReply<QString> vendorReply = informationCenter->call("GetPropertyString","info.vendor");
96
if (vendorReply.isValid()) deviceVendor = vendorReply.value();
98
QDBusReply<QString> storageBusReply = informationCenter->call("GetPropertyString","storage.bus");
99
if (storageBusReply.isValid()) deviceBus = storageBusReply.value();
100
usb = deviceBus == "usb";
102
QDBusReply<QString> modelReply = informationCenter->call("GetPropertyString","storage.model");
103
if (modelReply.isValid()) deviceModel = modelReply.value();
105
QDBusReply<bool> supportDvdReply = informationCenter->call("GetPropertyBoolean","storage.cdrom.dvd");
106
supportDvd = supportDvdReply.isValid() ? supportDvdReply.value() : false;
108
QDBusReply<qulonglong> sizeReply;
109
sizeReply = isDisk() ? informationCenter->call("GetProperty","storage.size") : informationCenter->call("GetProperty","volume.size");
110
deviceSize = sizeReply.isValid() ? sizeReply.value() : 0;
112
QDBusReply<int> majorReply = informationCenter->call("GetPropertyInteger","block.major");
113
major = majorReply.isValid() ? majorReply.value() : 0;
115
QDBusReply<int> minorReply = informationCenter->call("GetPropertyInteger","block.minor");
116
minor = minorReply.isValid() ? minorReply.value() : 0;
119
informationCenter->connection().connect(HAL_SERVICE,deviceUdi,HAL_DEVICE_INTERFACE,"PropertyModified",
120
this,SLOT(propertyModified(int, const QList<ChangeStruct>&)));
123
DiskDevice::~DiskDevice() {
124
delete informationCenter;
127
QString& DiskDevice::blockName() {
128
return deviceBlockName;
131
const QString& DiskDevice::blockName() const {
132
return deviceBlockName;
135
QString& DiskDevice::udi() {
139
const QString& DiskDevice::udi() const {
143
QString& DiskDevice::parentUdi() {
144
return deviceParentUdi;
147
const QString& DiskDevice::parentUdi() const {
148
return deviceParentUdi;
151
QString& DiskDevice::uuid() {
152
if (cdOrFloppyChildDevice)
153
return cdOrFloppyChildDevice->uuid();
157
const QString& DiskDevice::uuid() const {
161
QString& DiskDevice::label() {
165
const QString& DiskDevice::label() const {
169
QString& DiskDevice::fileSystem() {
170
if (cdOrFloppyChildDevice)
171
return cdOrFloppyChildDevice->fileSystem();
172
return deviceFileSystem;
175
const QString& DiskDevice::fileSystem() const {
179
QString& DiskDevice::fstabFileSystem() {
180
return deviceFstabFileSystem;
183
const QString DiskDevice::fstabFileSystem() const {
184
return deviceFstabFileSystem;
187
QString& DiskDevice::type() {
191
const QString& DiskDevice::type() const {
195
QString& DiskDevice::category() {
196
return deviceCategory;
199
const QString& DiskDevice::category() const {
200
return deviceCategory;
203
QString& DiskDevice::fstabOptions() {
204
return deviceFstabOptions;
207
const QString& DiskDevice::fstabOptions() const {
208
return deviceFstabOptions;
211
QString& DiskDevice::currentMountPoint() {
212
if (cdOrFloppyChildDevice)
213
return cdOrFloppyChildDevice->currentMountPoint();
214
QDBusReply<QString> mountPointReply = informationCenter->call("GetPropertyString","volume.mount_point");
215
deviceCurrentMountPoint = mountPointReply.value();
216
return deviceCurrentMountPoint;
219
const QString& DiskDevice::currentMountPoint() const {
220
return currentMountPoint();
223
QString& DiskDevice::fstabMountPoint() {
224
return deviceFstabMountPoint;
227
const QString& DiskDevice::fstabMountPoint() const {
228
return deviceFstabMountPoint;
231
QString DiskDevice::size(int format) {
232
if (cdOrFloppyChildDevice)
233
return cdOrFloppyChildDevice->size(format);
236
return QString::number(deviceSize) + " Bytes";
238
return QString::number(round(deviceSize/1024.0*10)/10.0) + "Kb";
240
return QString::number(round(deviceSize/1048576.0*10)/10.0) + "Mb";
242
return QString::number(round(deviceSize/1073741824.0*10)/10.0) + "Gb";
244
if (deviceSize/1073741824 > 0)
245
return size(GigaBytes);
246
else if (deviceSize/1048576 > 0)
247
return size(MegaBytes);
248
else if (deviceSize/1024 > 0)
249
return size(KiloBytes);
256
QString& DiskDevice::product() {
257
return deviceProduct;
260
const QString& DiskDevice::product() const {
261
return deviceProduct;
264
QString& DiskDevice::model() {
268
const QString& DiskDevice::model() const {
272
QString& DiskDevice::bus() {
276
const QString& DiskDevice::bus() const {
280
QString& DiskDevice::vendor() {
284
const QString& DiskDevice::vendor() const {
288
qulonglong DiskDevice::size() const {
292
int DiskDevice::blocks() const {
296
int DiskDevice::majorNumber() const {
300
int DiskDevice::minorNumber() const {
304
int DiskDevice::fsck() const {
305
return fstabItem ? fstabItem->itemFsck : 0;
308
DiskDevice *DiskDevice::parent() {
312
bool DiskDevice::isMounted() const {
313
if (cdOrFloppyChildDevice)
314
return cdOrFloppyChildDevice->isMounted();
315
// To define mounting status of swap we have to parse file /proc/swaps
316
if (deviceFileSystem == "swap") {
317
QFile swapsFile("/proc/swaps");
318
swapsFile.open(QIODevice::ReadOnly);
319
QTextStream swapsStream(&swapsFile);
320
QString shortBlockName = deviceBlockName;
321
shortBlockName.remove("/dev/");
322
if (swapsStream.readAll().contains(shortBlockName))
326
QDBusReply<bool> mountedReply = informationCenter->call("GetPropertyBoolean","volume.is_mounted");
327
return mountedReply.isValid() ? mountedReply.value() : false;
330
bool DiskDevice::isRemovable() const {
334
bool DiskDevice::isDisk() const {
338
bool DiskDevice::isCdOrFloppy() const {
342
bool DiskDevice::isSupportDvd() const {
346
bool DiskDevice::isUsb() const {
350
bool DiskDevice::isCdOrFloppyInserted() const {
351
return cdOrFloppyChildDevice != 0;
354
bool DiskDevice::dump() const {
355
return fstabItem ? fstabItem->itemDump : false;
358
//===================== SLOTS =================================//
360
void DiskDevice::propertyModified(int, const QList<ChangeStruct> &changes) {
361
foreach (const ChangeStruct changeStruct, changes) {
362
qDebug() << "[I] Device" << deviceUdi << "changed property:" << changeStruct.propertyName;
363
if (changeStruct.propertyName == "volume.mount_point")
364
emit (currentMountPointChanged(currentMountPoint()));
365
else if (changeStruct.propertyName == "volume.is_mounted")
366
emit (mountedStatusChanged(isMounted()));
371
//===================== Functions of some variables settings =========================//
373
void DiskDevice::setFstabItem(DiskFstabItem *item) {
375
if (fstabItem == 0x0) fstabItem = 0;
376
if (fstabItem != 0) {
377
deviceFstabMountPoint = fstabItem->itemMountPoint;
378
deviceFstabOptions = fstabItem->itemOptions;
379
deviceFstabFileSystem = fstabItem->itemFileSystem;
383
void DiskDevice::setParent(DiskDevice *parent) {
384
parentDevice = parent;
385
if (!removable && parentDevice)
386
removable = parentDevice->isRemovable();
387
if (deviceType.isEmpty())
388
deviceType = parent->type();
389
usb = parent->isUsb();
390
cdOrFloppy = deviceType == "cdrom" || deviceType == "floppy";
391
deviceBus = parent->bus();
392
deviceModel = parent->model();
395
void DiskDevice::setCdOrFloppyChild(DiskDevice *child) {
396
cdOrFloppyChildDevice = child;
397
connect(cdOrFloppyChildDevice,SIGNAL(currentMountPointChanged(const QString&)),this,SIGNAL(currentMountPointChanged(const QString&)));
398
connect(cdOrFloppyChildDevice,SIGNAL(mountedStatusChanged(bool)),this,SIGNAL(mountedStatusChanged(bool)));
401
void DiskDevice::removeCdOrFloppyChild() {
402
cdOrFloppyChildDevice = 0;