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 <QDBusConnection>
20
#include <QDBusInterface>
22
#include <QStringList>
24
#include "diskdevice.h"
28
DiskHal::DiskHal(bool deb,QObject *parent) : QObject(parent) {
31
// Create interface to get information from Hal
32
informationCenter = new QDBusInterface(HAL_SERVICE,HAL_PATH,HAL_INTERFACE,QDBusConnection::systemBus());
34
// Check interace and write result to log
35
if (informationCenter->isValid()) {
36
if (debug) qDebug("[G] DBus interface was created");
38
if (debug) qDebug("[E] DBus interface was not created");
40
QDBusReply<QStringList> allDevicesReply = informationCenter->call("GetAllDevices");
41
allDevicesReply.isValid() ? qDebug("[G] All devices were recieved") : qDebug("[E] Cannot receive all devices");
43
foreach (QString udi,allDevicesReply.value())
46
foreach (DiskDevice *device,devicesList)
47
foreach (DiskDevice *parentDevice,devicesList)
48
if (parentDevice->udi() == device->parentUdi()) {
49
device->setParent(parentDevice);
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&)));
61
delete informationCenter;
62
foreach (DiskDevice *device,devicesList)
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();
73
delete storageInterface;
77
DiskDevice *storageDevice = new DiskDevice(storageInterface,udi);
78
storageDevice->setFstabItem(fstabItem(storageDevice));
80
foreach (DiskDevice *parentDevice,devicesList)
81
if (parentDevice->udi() == storageDevice->parentUdi()) {
82
storageDevice->setParent(parentDevice);
86
if (debug) qDebug() << "[I] Storage device was detected:" << name;
87
devicesList.append(storageDevice);
89
emit (deviceAdded(storageDevice));
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);
101
int DiskHal::count() {
102
return devicesList.count();
105
QList<DiskDevice *>& DiskHal::devices() {
109
DiskDevice* DiskHal::deviceByUdi(const QString& udi) {
110
foreach (DiskDevice *device,devicesList)
111
if (device->udi() == udi)
116
DiskDevice* DiskHal::deviceByBlockName(const QString& blockName) {
117
foreach (DiskDevice *device,devicesList)
118
if (device->blockName() == blockName)
123
DiskDevice* DiskHal::deviceByUuid(const QString& uuid) {
124
foreach (DiskDevice *device,devicesList)
125
if (device->uuid() == uuid)