2
Copyright 2007 Robert Knight <robertknight@gmail.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
21
#include "core/systemmodel.h"
29
#include <KDiskFreeSpace>
30
#include <KLocalizedString>
34
#include <KServiceTypeTrader>
35
#include <KStandardDirs>
36
#include <solid/device.h>
37
#include <solid/deviceinterface.h>
38
#include <solid/devicenotifier.h>
39
#include <solid/storageaccess.h>
40
#include <solid/storagedrive.h>
43
#include "core/models.h"
46
using namespace Kickoff;
48
class SystemModel::Private
51
Private(SystemModel *parent)
53
,removableStorageItem(0)
58
QStandardItem *lookupDeviceByMountPoint(const QString& mountPoint)
60
QString mountUrl = KUrl(mountPoint).url();
61
foreach(QStandardItem *item,deviceItemById) {
62
if (item->data(UrlRole).value<QString>() == mountUrl) {
68
void addDevice(const Solid::Device& device)
70
const Solid::StorageAccess* access = device.as<Solid::StorageAccess>();
74
QStandardItem *deviceItem = new QStandardItem;
75
deviceItem->setText(device.product());
76
deviceItem->setIcon(KIcon(device.icon()));
78
deviceItem->setData(access->filePath(),SubTitleRole);
79
deviceItem->setData(KUrl(access->filePath()).url(),UrlRole);
81
// start a request to find the available free disk space
82
// FIXME: On Unix this is not very efficient as KDiskFreeSpace starts a 'df' process
84
queryFreeSpace(access->filePath());
86
Solid::StorageDrive *drive = 0;
87
Solid::Device parentDevice = device;
88
while (parentDevice.isValid() && !drive) {
89
drive = parentDevice.as<Solid::StorageDrive>();
90
parentDevice = parentDevice.parent();
93
if (drive && (drive->isHotpluggable() || drive->isRemovable())) {
94
removableStorageItem->appendRow(deviceItem);
96
fixedStorageItem->appendRow(deviceItem);
98
deviceItemById.insert(device.udi(),deviceItem);
100
void loadStorageItems()
103
QList<Solid::Device> deviceList = Solid::Device::listFromType(Solid::DeviceInterface::StorageAccess,QString());
106
removableStorageItem = new QStandardItem(i18n("Removable Storage"));
107
fixedStorageItem = new QStandardItem(i18n("Storage"));
109
foreach(const Solid::Device& device,deviceList) {
113
q->appendRow(removableStorageItem);
114
q->appendRow(fixedStorageItem);
118
// FIXME Implement the search for systemsettings correctly, I had problems locally
119
// using KService::serviceByDesktopName() or similar.
120
QString path = KStandardDirs::installPath("xdgdata-apps") + "systemsettings.desktop";
121
KService::Ptr settingsService = KService::serviceByStorageId(path);
123
if (settingsService) {
124
q->appendRow(StandardItemFactory::createItemForService(settingsService));
129
QStandardItem *placesItem = new QStandardItem(i18n("Places"));
131
QStandardItem *homeItem = StandardItemFactory::createItemForUrl(getenv("HOME"));
132
placesItem->appendRow(homeItem);
134
QStandardItem *networkItem = StandardItemFactory::createItemForUrl("remote:/");
135
networkItem->setData(QVariant(),SubTitleRole);
136
placesItem->appendRow(networkItem);
138
q->appendRow(placesItem);
140
void queryFreeSpace(const QString& mountPoint)
142
KDiskFreeSpace *freeSpace = KDiskFreeSpace::findUsageInfo(mountPoint);
143
connect(freeSpace,SIGNAL(foundMountPoint(QString,quint64,quint64,quint64)),q,
144
SLOT(freeSpaceInfoAvailable(QString,quint64,quint64,quint64)));
147
SystemModel * const q;
148
QStandardItem *removableStorageItem;
149
QStandardItem *fixedStorageItem;
150
QHash<QString,QStandardItem*> deviceItemById;
153
SystemModel::SystemModel(QObject *parent)
154
: QStandardItemModel(parent)
155
, d(new Private(this))
159
d->loadStorageItems();
161
connect(Solid::DeviceNotifier::instance(),SIGNAL(deviceAdded(QString)),this,SLOT(deviceAdded(QString)));
162
connect(Solid::DeviceNotifier::instance(),SIGNAL(deviceRemoved(QString)),this,SLOT(deviceRemoved(QString)));
165
SystemModel::~SystemModel()
169
void SystemModel::deviceAdded(const QString& udi)
171
qDebug() << "SystemModel adding device" << udi;
172
d->addDevice(Solid::Device(udi));
174
void SystemModel::deviceRemoved(const QString& udi)
176
QStandardItem *deviceItem = d->deviceItemById[udi];
178
Q_ASSERT(deviceItem->parent());
179
deviceItem->parent()->removeRow(deviceItem->row());
180
d->deviceItemById.remove(udi);
184
void SystemModel::freeSpaceInfoAvailable(const QString& mountPoint,quint64,quint64 kbUsed,quint64 kbAvailable)
186
QStandardItem *deviceItem = d->lookupDeviceByMountPoint(mountPoint);
188
deviceItem->setData(kbUsed,DiskUsedSpaceRole);
189
deviceItem->setData(kbAvailable,DiskFreeSpaceRole);
194
#include "systemmodel.moc"