~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to plasma/kickoff/src/core/systemmodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-31 19:16:54 UTC
  • Revision ID: james.westby@ubuntu.com-20071031191654-xuof6e1jg6uxqaze
Tags: 3.95.0-0ubuntu1~gutsy1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  
2
 
    Copyright 2007 Robert Knight <robertknight@gmail.com>
3
 
 
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.
8
 
 
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.
13
 
 
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.
18
 
*/
19
 
 
20
 
// Own
21
 
#include "core/systemmodel.h"
22
 
 
23
 
// Qt
24
 
#include <QFile>
25
 
#include <QHash>
26
 
#include <QtDebug>
27
 
 
28
 
// KDE
29
 
#include <KDiskFreeSpace>
30
 
#include <KLocalizedString>
31
 
#include <KIcon>
32
 
#include <KGlobal>
33
 
#include <KUrl>
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>
41
 
 
42
 
// Local
43
 
#include "core/models.h"
44
 
 
45
 
 
46
 
using namespace Kickoff;
47
 
 
48
 
class SystemModel::Private
49
 
{
50
 
public:
51
 
    Private(SystemModel *parent)
52
 
        :q(parent)
53
 
        ,removableStorageItem(0)
54
 
        ,fixedStorageItem(0)
55
 
    {
56
 
        
57
 
    }
58
 
    QStandardItem *lookupDeviceByMountPoint(const QString& mountPoint)
59
 
    {
60
 
        QString mountUrl = KUrl(mountPoint).url();
61
 
        foreach(QStandardItem *item,deviceItemById) {
62
 
            if (item->data(UrlRole).value<QString>() == mountUrl) {
63
 
                return item;
64
 
            }
65
 
        }
66
 
        return 0;
67
 
    }
68
 
    void addDevice(const Solid::Device& device) 
69
 
    {
70
 
            const Solid::StorageAccess* access = device.as<Solid::StorageAccess>();
71
 
            if (!access)
72
 
                return;
73
 
 
74
 
            QStandardItem *deviceItem = new QStandardItem;
75
 
            deviceItem->setText(device.product());
76
 
            deviceItem->setIcon(KIcon(device.icon()));
77
 
 
78
 
            deviceItem->setData(access->filePath(),SubTitleRole);
79
 
            deviceItem->setData(KUrl(access->filePath()).url(),UrlRole);
80
 
 
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
83
 
            // for each drive
84
 
            queryFreeSpace(access->filePath());
85
 
 
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();
91
 
            }
92
 
 
93
 
            if (drive && (drive->isHotpluggable() || drive->isRemovable())) { 
94
 
                removableStorageItem->appendRow(deviceItem);
95
 
            } else {
96
 
                fixedStorageItem->appendRow(deviceItem);
97
 
            }
98
 
            deviceItemById.insert(device.udi(),deviceItem);
99
 
    }
100
 
    void loadStorageItems()
101
 
    {
102
 
        // get device list
103
 
        QList<Solid::Device> deviceList = Solid::Device::listFromType(Solid::DeviceInterface::StorageAccess,QString());
104
 
 
105
 
        // add items
106
 
        removableStorageItem = new QStandardItem(i18n("Removable Storage"));
107
 
        fixedStorageItem = new QStandardItem(i18n("Storage"));
108
 
        
109
 
        foreach(const Solid::Device& device,deviceList) {
110
 
            addDevice(device);        
111
 
        }
112
 
        
113
 
        q->appendRow(removableStorageItem);
114
 
        q->appendRow(fixedStorageItem);
115
 
    }
116
 
    void loadSettings()
117
 
    {
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);
122
 
 
123
 
        if (settingsService) {
124
 
            q->appendRow(StandardItemFactory::createItemForService(settingsService));
125
 
        }
126
 
    }
127
 
    void loadPlaces()
128
 
    {
129
 
        QStandardItem *placesItem = new QStandardItem(i18n("Places"));
130
 
 
131
 
        QStandardItem *homeItem = StandardItemFactory::createItemForUrl(getenv("HOME"));
132
 
        placesItem->appendRow(homeItem);
133
 
        
134
 
        QStandardItem *networkItem = StandardItemFactory::createItemForUrl("remote:/");
135
 
        networkItem->setData(QVariant(),SubTitleRole);
136
 
        placesItem->appendRow(networkItem); 
137
 
        
138
 
        q->appendRow(placesItem);
139
 
    }
140
 
    void queryFreeSpace(const QString& mountPoint)
141
 
    {
142
 
        KDiskFreeSpace *freeSpace = KDiskFreeSpace::findUsageInfo(mountPoint);
143
 
        connect(freeSpace,SIGNAL(foundMountPoint(QString,quint64,quint64,quint64)),q,
144
 
            SLOT(freeSpaceInfoAvailable(QString,quint64,quint64,quint64)));
145
 
    }
146
 
 
147
 
    SystemModel * const q;
148
 
    QStandardItem *removableStorageItem;
149
 
    QStandardItem *fixedStorageItem;
150
 
    QHash<QString,QStandardItem*> deviceItemById;
151
 
};
152
 
 
153
 
SystemModel::SystemModel(QObject *parent)
154
 
    : QStandardItemModel(parent)
155
 
    , d(new Private(this))
156
 
{
157
 
    d->loadSettings();
158
 
    d->loadPlaces();
159
 
    d->loadStorageItems();
160
 
 
161
 
    connect(Solid::DeviceNotifier::instance(),SIGNAL(deviceAdded(QString)),this,SLOT(deviceAdded(QString)));
162
 
    connect(Solid::DeviceNotifier::instance(),SIGNAL(deviceRemoved(QString)),this,SLOT(deviceRemoved(QString)));
163
 
 
164
 
    }
165
 
SystemModel::~SystemModel()
166
 
{
167
 
    delete d;
168
 
}
169
 
void SystemModel::deviceAdded(const QString& udi)
170
 
{
171
 
    qDebug() << "SystemModel adding device" << udi;
172
 
    d->addDevice(Solid::Device(udi));
173
 
}
174
 
void SystemModel::deviceRemoved(const QString& udi)
175
 
{
176
 
    QStandardItem *deviceItem = d->deviceItemById[udi];
177
 
    if(deviceItem) {
178
 
        Q_ASSERT(deviceItem->parent());
179
 
        deviceItem->parent()->removeRow(deviceItem->row());
180
 
        d->deviceItemById.remove(udi);
181
 
    }
182
 
}
183
 
 
184
 
void SystemModel::freeSpaceInfoAvailable(const QString& mountPoint,quint64,quint64 kbUsed,quint64 kbAvailable)
185
 
{
186
 
    QStandardItem *deviceItem = d->lookupDeviceByMountPoint(mountPoint);
187
 
    if (deviceItem) {
188
 
        deviceItem->setData(kbUsed,DiskUsedSpaceRole);
189
 
        deviceItem->setData(kbAvailable,DiskFreeSpaceRole);
190
 
    }
191
 
}
192
 
 
193
 
 
194
 
#include "systemmodel.moc"