~ben-kietzman/ubuntu/quantal/mountmanager/fix-for-598070

« back to all changes in this revision

Viewing changes to sources/core/diskdevice.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2008-08-20 10:22:14 UTC
  • Revision ID: james.westby@ubuntu.com-20080820102214-fv93myu0ncb1503r
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//MountManager - the program for easy mounting of storage devices in Linux
 
2
//Copyright (C) 2007-2008 Tikhonov Sergey
 
3
//
 
4
//This file is part of MountManager Core
 
5
//
 
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.
 
10
//
 
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.
 
15
//
 
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>
 
20
#include <QDBusReply>
 
21
#include <QDBusMetaType>
 
22
#include <QDBusArgument>
 
23
#include <math.h>
 
24
#include <QDebug>
 
25
#include <QFile>
 
26
#include <QTextStream>
 
27
#include "diskdevice.h"
 
28
#include "diskfstab.h"
 
29
#include "../gui/const.h"
 
30
 
 
31
Q_DECLARE_METATYPE(ChangeStruct)
 
32
Q_DECLARE_METATYPE(QList<ChangeStruct>)
 
33
 
 
34
const QDBusArgument &operator<<( QDBusArgument &arg, const ChangeStruct &change ) {
 
35
        arg.beginStructure();
 
36
        arg << change.propertyName << change.added << change.removed;
 
37
        arg.endStructure();
 
38
        return arg;
 
39
}
 
40
 
 
41
const QDBusArgument &operator>>( const QDBusArgument &arg, ChangeStruct &change ) {
 
42
        arg.beginStructure();
 
43
        arg >> change.propertyName >> change.added >> change.removed;
 
44
        arg.endStructure();
 
45
        return arg;
 
46
}
 
47
 
 
48
DiskDevice::DiskDevice(QDBusInterface *interface, const QString& udi) {
 
49
 
 
50
        fstabItem = 0;
 
51
        parentDevice = 0;
 
52
        cdOrFloppyChildDevice = 0;
 
53
        
 
54
        qDBusRegisterMetaType<ChangeStruct>();
 
55
        qDBusRegisterMetaType< QList<ChangeStruct> >();
 
56
 
 
57
        informationCenter = interface;
 
58
        deviceUdi = udi;
 
59
 
 
60
        QDBusReply<QString> categoryReply = informationCenter->call("GetPropertyString","info.category");
 
61
        deviceCategory = categoryReply.value();
 
62
        disk = categoryReply.value() == "volume" ? false : true;
 
63
        
 
64
        QDBusReply<QString> parentReply = informationCenter->call("GetPropertyString","info.parent");
 
65
        deviceParentUdi = parentReply.value();
 
66
 
 
67
        QDBusReply<QString> blockReply = informationCenter->call("GetPropertyString","block.device");
 
68
        deviceBlockName = blockReply.value();
 
69
        
 
70
        QDBusReply<QString> uuidReply = informationCenter->call("GetPropertyString","volume.uuid");
 
71
        deviceUuid = uuidReply.value();
 
72
 
 
73
        QDBusReply<QString> labelReply = informationCenter->call("GetPropertyString","volume.label");
 
74
        deviceLabel = labelReply.value();
 
75
 
 
76
        QDBusReply<QString> typeReply = informationCenter->call("GetPropertyString","storage.drive_type");
 
77
        deviceType = typeReply.value();
 
78
        cdOrFloppy = deviceType == "cdrom" || deviceType == "floppy";
 
79
 
 
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";
 
85
 
 
86
        QDBusReply<bool> removableReply = informationCenter->call("GetPropertyBoolean","storage.removable");
 
87
        removable = removableReply.isValid() ? removableReply.value() : false;
 
88
 
 
89
        QDBusReply<int> blocksReply = informationCenter->call("GetPropertyInteger","volume.num_blocks");
 
90
        deviceBlocks = blocksReply.isValid() ?  blocksReply.value() : 0;
 
91
 
 
92
        QDBusReply<QString> productReply = informationCenter->call("GetPropertyString","info.product");
 
93
        if (productReply.isValid()) deviceProduct = productReply.value();
 
94
        
 
95
        QDBusReply<QString> vendorReply = informationCenter->call("GetPropertyString","info.vendor");
 
96
        if (vendorReply.isValid()) deviceVendor = vendorReply.value();
 
97
        
 
98
        QDBusReply<QString> storageBusReply = informationCenter->call("GetPropertyString","storage.bus");
 
99
        if (storageBusReply.isValid()) deviceBus = storageBusReply.value();
 
100
        usb = deviceBus == "usb";
 
101
        
 
102
        QDBusReply<QString> modelReply = informationCenter->call("GetPropertyString","storage.model");
 
103
        if (modelReply.isValid()) deviceModel = modelReply.value();
 
104
        
 
105
        QDBusReply<bool> supportDvdReply = informationCenter->call("GetPropertyBoolean","storage.cdrom.dvd");
 
106
        supportDvd = supportDvdReply.isValid() ? supportDvdReply.value() : false;
 
107
        
 
108
        QDBusReply<qulonglong> sizeReply;
 
109
        sizeReply = isDisk() ? informationCenter->call("GetProperty","storage.size") : informationCenter->call("GetProperty","volume.size");
 
110
        deviceSize = sizeReply.isValid() ? sizeReply.value() : 0;
 
111
 
 
112
        QDBusReply<int> majorReply = informationCenter->call("GetPropertyInteger","block.major");
 
113
        major = majorReply.isValid() ? majorReply.value() : 0;
 
114
        
 
115
        QDBusReply<int> minorReply = informationCenter->call("GetPropertyInteger","block.minor");
 
116
        minor = minorReply.isValid() ? minorReply.value() : 0;
 
117
        
 
118
        
 
119
        informationCenter->connection().connect(HAL_SERVICE,deviceUdi,HAL_DEVICE_INTERFACE,"PropertyModified",
 
120
                                                        this,SLOT(propertyModified(int, const QList<ChangeStruct>&)));
 
121
}
 
122
 
 
123
DiskDevice::~DiskDevice() {
 
124
        delete informationCenter;
 
125
}
 
126
 
 
127
QString& DiskDevice::blockName() {
 
128
        return deviceBlockName;
 
129
}
 
130
 
 
131
const QString& DiskDevice::blockName() const {
 
132
        return deviceBlockName;
 
133
}
 
134
 
 
135
QString& DiskDevice::udi() {
 
136
        return deviceUdi;
 
137
}
 
138
 
 
139
const QString& DiskDevice::udi() const {
 
140
        return deviceUdi;
 
141
}
 
142
 
 
143
QString& DiskDevice::parentUdi() {
 
144
        return deviceParentUdi;
 
145
}
 
146
 
 
147
const QString& DiskDevice::parentUdi() const {
 
148
        return deviceParentUdi;
 
149
}
 
150
 
 
151
QString& DiskDevice::uuid() {
 
152
        if (cdOrFloppyChildDevice)
 
153
                return cdOrFloppyChildDevice->uuid();
 
154
        return deviceUuid;
 
155
}
 
156
 
 
157
const QString& DiskDevice::uuid() const {
 
158
        return uuid();
 
159
}
 
160
                
 
161
QString& DiskDevice::label() {
 
162
        return deviceLabel;
 
163
}
 
164
 
 
165
const QString& DiskDevice::label() const {
 
166
        return deviceLabel;
 
167
}
 
168
 
 
169
QString& DiskDevice::fileSystem() {
 
170
        if (cdOrFloppyChildDevice)
 
171
                return cdOrFloppyChildDevice->fileSystem();
 
172
        return deviceFileSystem;
 
173
}
 
174
 
 
175
const QString& DiskDevice::fileSystem() const {
 
176
        return fileSystem();
 
177
}
 
178
 
 
179
QString& DiskDevice::fstabFileSystem() {
 
180
        return deviceFstabFileSystem;
 
181
}
 
182
 
 
183
const QString DiskDevice::fstabFileSystem() const {
 
184
        return deviceFstabFileSystem;
 
185
}
 
186
 
 
187
QString& DiskDevice::type() {
 
188
        return deviceType;
 
189
}
 
190
 
 
191
const QString& DiskDevice::type() const {
 
192
        return deviceType;
 
193
}
 
194
 
 
195
QString& DiskDevice::category() {
 
196
        return deviceCategory;
 
197
}
 
198
 
 
199
const QString& DiskDevice::category() const {
 
200
        return deviceCategory;
 
201
}
 
202
 
 
203
QString& DiskDevice::fstabOptions() {
 
204
        return deviceFstabOptions;
 
205
}
 
206
 
 
207
const QString& DiskDevice::fstabOptions() const {
 
208
        return deviceFstabOptions;
 
209
}
 
210
 
 
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;
 
217
}
 
218
 
 
219
const QString& DiskDevice::currentMountPoint() const {
 
220
        return currentMountPoint();
 
221
}
 
222
 
 
223
QString& DiskDevice::fstabMountPoint() {
 
224
        return deviceFstabMountPoint;
 
225
}
 
226
 
 
227
const QString& DiskDevice::fstabMountPoint() const {
 
228
        return deviceFstabMountPoint;
 
229
}
 
230
 
 
231
QString DiskDevice::size(int format) {
 
232
        if (cdOrFloppyChildDevice)
 
233
                return cdOrFloppyChildDevice->size(format);
 
234
        switch (format) {
 
235
                case Bytes:
 
236
                        return QString::number(deviceSize) + " Bytes";
 
237
                case KiloBytes:
 
238
                        return QString::number(round(deviceSize/1024.0*10)/10.0) + "Kb";
 
239
                case MegaBytes:
 
240
                        return QString::number(round(deviceSize/1048576.0*10)/10.0) + "Mb";
 
241
                case GigaBytes:
 
242
                        return QString::number(round(deviceSize/1073741824.0*10)/10.0) + "Gb";
 
243
                case Truncation:
 
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);
 
250
                        return size(Bytes);
 
251
                        break;
 
252
        }
 
253
        return QString();
 
254
}
 
255
 
 
256
QString& DiskDevice::product() {
 
257
        return deviceProduct;
 
258
}
 
259
 
 
260
const QString& DiskDevice::product() const {
 
261
        return deviceProduct;
 
262
}
 
263
 
 
264
QString& DiskDevice::model() {
 
265
        return deviceModel;
 
266
}
 
267
 
 
268
const QString& DiskDevice::model() const {
 
269
        return deviceModel;
 
270
}
 
271
 
 
272
QString& DiskDevice::bus() {
 
273
        return deviceBus;
 
274
}
 
275
 
 
276
const QString& DiskDevice::bus() const {
 
277
        return deviceBus;
 
278
}
 
279
                
 
280
QString& DiskDevice::vendor() {
 
281
        return deviceVendor;
 
282
}
 
283
 
 
284
const QString& DiskDevice::vendor() const {
 
285
        return deviceVendor;
 
286
}               
 
287
 
 
288
qulonglong DiskDevice::size() const {
 
289
        return deviceSize;
 
290
}
 
291
 
 
292
int DiskDevice::blocks() const {
 
293
        return deviceBlocks;
 
294
}
 
295
                
 
296
int DiskDevice::majorNumber() const {
 
297
        return major;
 
298
}
 
299
 
 
300
int DiskDevice::minorNumber() const {
 
301
        return minor;
 
302
}
 
303
 
 
304
int DiskDevice::fsck() const {
 
305
        return fstabItem ? fstabItem->itemFsck : 0;
 
306
}
 
307
                
 
308
DiskDevice *DiskDevice::parent() {
 
309
        return parentDevice;
 
310
}
 
311
 
 
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))
 
323
                        return true;
 
324
                return false;
 
325
        }
 
326
        QDBusReply<bool> mountedReply = informationCenter->call("GetPropertyBoolean","volume.is_mounted");
 
327
        return mountedReply.isValid() ? mountedReply.value() : false;
 
328
}
 
329
 
 
330
bool DiskDevice::isRemovable() const {
 
331
        return removable;
 
332
}
 
333
 
 
334
bool DiskDevice::isDisk() const {
 
335
        return disk;
 
336
}
 
337
 
 
338
bool DiskDevice::isCdOrFloppy() const {
 
339
        return cdOrFloppy;
 
340
}
 
341
 
 
342
bool DiskDevice::isSupportDvd() const {
 
343
        return supportDvd;
 
344
}
 
345
 
 
346
bool DiskDevice::isUsb() const {
 
347
        return usb;
 
348
}
 
349
 
 
350
bool DiskDevice::isCdOrFloppyInserted() const {
 
351
        return cdOrFloppyChildDevice != 0;
 
352
}
 
353
 
 
354
bool DiskDevice::dump() const {
 
355
        return fstabItem ? fstabItem->itemDump : false;
 
356
}
 
357
                
 
358
//===================== SLOTS =================================//
 
359
 
 
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()));
 
367
        }
 
368
}
 
369
 
 
370
 
 
371
//===================== Functions of some variables settings =========================//
 
372
 
 
373
void DiskDevice::setFstabItem(DiskFstabItem *item) {
 
374
        fstabItem = item;
 
375
        if (fstabItem == 0x0) fstabItem = 0;
 
376
        if (fstabItem != 0) {
 
377
                deviceFstabMountPoint = fstabItem->itemMountPoint;
 
378
                deviceFstabOptions = fstabItem->itemOptions;
 
379
                deviceFstabFileSystem = fstabItem->itemFileSystem;
 
380
        }
 
381
}
 
382
 
 
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();
 
393
}
 
394
 
 
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)));
 
399
}
 
400
 
 
401
void DiskDevice::removeCdOrFloppyChild() {
 
402
        cdOrFloppyChildDevice = 0;
 
403
}