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

« back to all changes in this revision

Viewing changes to sources/core/diskhal.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 <QDBusConnection>
 
20
#include <QDBusInterface>
 
21
#include <QDBusReply>
 
22
#include <QStringList>
 
23
#include <QDebug>
 
24
#include "diskdevice.h"
 
25
#include "diskhal.h"
 
26
#include "const.h"
 
27
 
 
28
DiskHal::DiskHal(bool deb,QObject *parent) : QObject(parent) {
 
29
        debug = deb;
 
30
        
 
31
        // Create interface to get information from Hal
 
32
        informationCenter = new QDBusInterface(HAL_SERVICE,HAL_PATH,HAL_INTERFACE,QDBusConnection::systemBus());
 
33
        
 
34
        // Check interace and write result to log 
 
35
        if (informationCenter->isValid()) {
 
36
                if (debug) qDebug("[G] DBus interface was created");
 
37
        } else
 
38
                if (debug) qDebug("[E] DBus interface was not created");
 
39
        
 
40
        QDBusReply<QStringList> allDevicesReply = informationCenter->call("GetAllDevices");
 
41
        allDevicesReply.isValid() ? qDebug("[G] All devices were recieved") : qDebug("[E] Cannot receive all devices");
 
42
        
 
43
        foreach (QString udi,allDevicesReply.value())
 
44
                addDevice(udi);
 
45
 
 
46
        foreach (DiskDevice *device,devicesList)
 
47
                foreach (DiskDevice *parentDevice,devicesList)
 
48
                        if (parentDevice->udi() == device->parentUdi()) {
 
49
                                device->setParent(parentDevice);
 
50
                                break;
 
51
                        }
 
52
        
 
53
        
 
54
        informationCenter->connection().connect(HAL_SERVICE,HAL_PATH,HAL_INTERFACE,
 
55
                                                        "DeviceAdded",this,SLOT(addDevice(const QString&)));
 
56
        informationCenter->connection().connect(HAL_SERVICE,HAL_PATH,HAL_INTERFACE,
 
57
                                                        "DeviceRemoved",this,SLOT(removeDevice(const QString&)));
 
58
}
 
59
 
 
60
DiskHal::~DiskHal() {
 
61
        delete informationCenter;
 
62
        foreach (DiskDevice *device,devicesList)
 
63
                delete device;
 
64
}
 
65
 
 
66
void DiskHal::addDevice(const QString &udi) {
 
67
        // Skip not storage device
 
68
        QDBusInterface *storageInterface = new QDBusInterface(HAL_SERVICE,
 
69
                                        udi,HAL_DEVICE_INTERFACE,QDBusConnection::systemBus());
 
70
        QDBusReply<QString> blockReply = storageInterface->call("GetPropertyString","block.device");
 
71
        QString name = blockReply.value();
 
72
        if (name.isEmpty())  {
 
73
                delete storageInterface;
 
74
                return;
 
75
        }
 
76
 
 
77
        DiskDevice *storageDevice = new DiskDevice(storageInterface,udi);
 
78
        storageDevice->setFstabItem(fstabItem(storageDevice));
 
79
 
 
80
        foreach (DiskDevice *parentDevice,devicesList)
 
81
                if (parentDevice->udi() == storageDevice->parentUdi()) {
 
82
                        storageDevice->setParent(parentDevice);
 
83
                        break;
 
84
                }
 
85
        
 
86
        if (debug) qDebug() << "[I] Storage device was detected:" << name;
 
87
        devicesList.append(storageDevice);
 
88
 
 
89
        emit (deviceAdded(storageDevice));
 
90
}
 
91
 
 
92
void DiskHal::removeDevice(const QString &udi) {
 
93
        DiskDevice *removedDevice = deviceByUdi(udi);
 
94
        if (removedDevice != 0) {
 
95
                if (debug)  qDebug() << "[I] Storage device was removed:" << removedDevice->blockName();
 
96
                emit (deviceRemoved(removedDevice));
 
97
                devicesList.removeAll(removedDevice);
 
98
        }
 
99
}
 
100
 
 
101
int DiskHal::count() {
 
102
        return devicesList.count();
 
103
}
 
104
 
 
105
QList<DiskDevice *>&  DiskHal::devices() {
 
106
        return devicesList;
 
107
}
 
108
 
 
109
DiskDevice* DiskHal::deviceByUdi(const QString& udi) {
 
110
        foreach (DiskDevice *device,devicesList)
 
111
                if (device->udi() == udi)
 
112
                        return device;
 
113
        return 0;
 
114
}
 
115
 
 
116
DiskDevice* DiskHal::deviceByBlockName(const QString& blockName) {
 
117
        foreach (DiskDevice *device,devicesList)
 
118
                if (device->blockName() == blockName)
 
119
                        return device;
 
120
        return 0;
 
121
}
 
122
 
 
123
DiskDevice* DiskHal::deviceByUuid(const QString& uuid) {
 
124
        foreach (DiskDevice *device,devicesList)
 
125
                if (device->uuid() == uuid)
 
126
                        return device;
 
127
        return 0;       
 
128
}