~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/places/placesengine.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2008 Alex Merry <alex.merry@kdemail.net>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU Library General Public License version 2 as
 
6
 *   published by the Free Software Foundation
 
7
 *
 
8
 *   This program is distributed in the hope that it will be useful,
 
9
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *   GNU General Public License for more details
 
12
 *
 
13
 *   You should have received a copy of the GNU Library General Public
 
14
 *   License along with this program; if not, write to the
 
15
 *   Free Software Foundation, Inc.,
 
16
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
17
 */
 
18
 
 
19
#include "placesengine.h"
 
20
 
 
21
#include <QtCore/QString>
 
22
 
 
23
#include <KDebug>
 
24
#include <KDiskFreeSpaceInfo>
 
25
 
 
26
#include "placeservice.h"
 
27
 
 
28
PlacesEngine::PlacesEngine(QObject *parent, const QVariantList &args)
 
29
    : Plasma::DataEngine(parent, args)
 
30
{
 
31
    connect(&m_placesModel, SIGNAL(modelReset()),
 
32
            this, SLOT(modelReset()));
 
33
    connect(&m_placesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
 
34
            this, SLOT(dataChanged(QModelIndex,QModelIndex)));
 
35
    connect(&m_placesModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
 
36
            this, SLOT(placesAdded(QModelIndex,int,int)));
 
37
    connect(&m_placesModel, SIGNAL(rowsRemoved(QModelIndex,int,int)),
 
38
            this, SLOT(placesRemoved(QModelIndex,int,int)));
 
39
 
 
40
    sendAllData();
 
41
}
 
42
 
 
43
PlacesEngine::~PlacesEngine()
 
44
{
 
45
}
 
46
 
 
47
void PlacesEngine::modelReset()
 
48
{
 
49
    removeAllSources();
 
50
}
 
51
 
 
52
void PlacesEngine::placesAdded(const QModelIndex&, int start, int end)
 
53
{
 
54
    sendData(start, end);
 
55
}
 
56
 
 
57
void PlacesEngine::placesRemoved(const QModelIndex&, int start, int end)
 
58
{
 
59
    kDebug() << "Places" << start << "through" << end << "removed";
 
60
    for (int index = start; index <= end; index++) {
 
61
        removeSource(QString::number(index));
 
62
    }
 
63
}
 
64
 
 
65
void PlacesEngine::dataChanged(const QModelIndex& topLeft,
 
66
                               const QModelIndex& bottomRight)
 
67
{
 
68
    sendData(topLeft.row(), bottomRight.row());
 
69
}
 
70
 
 
71
void PlacesEngine::sendAllData()
 
72
{
 
73
    sendData(0, m_placesModel.rowCount() - 1);
 
74
}
 
75
 
 
76
Plasma::Service *PlacesEngine::serviceForSource(const QString &source)
 
77
{
 
78
    const int row = source.toInt();
 
79
    const QModelIndex index = m_placesModel.index(row, 0);
 
80
    if (index.isValid()) {
 
81
        return new PlaceService(this, &m_placesModel, index);
 
82
    }
 
83
 
 
84
    return DataEngine::serviceForSource(source);
 
85
}
 
86
 
 
87
void PlacesEngine::sendData(int start, int end)
 
88
{
 
89
    for (int row = start; row <= end; ++row) {
 
90
        const QModelIndex index = m_placesModel.index(row, 0);
 
91
 
 
92
        Data map;
 
93
 
 
94
        const QString source = QString::number(row);
 
95
 
 
96
        setData(source, "name", m_placesModel.text(index));
 
97
        setData(source, "url", m_placesModel.url(index).url());
 
98
        setData(source, "icon", m_placesModel.icon(index));
 
99
        setData(source, "hidden",
 
100
                m_placesModel.data(index, KFilePlacesModel::HiddenRole));
 
101
        setData(source, "setupNeeded",
 
102
                m_placesModel.data(index, KFilePlacesModel::SetupNeededRole));
 
103
        setData(source, "isDevice",
 
104
                m_placesModel.deviceForIndex(index).isValid());
 
105
 
 
106
        const QString path = m_placesModel.url(index).path();
 
107
        if (!path.isEmpty()) {
 
108
            // We can't get free space for unmounted volumes :-(
 
109
            KDiskFreeSpaceInfo info = KDiskFreeSpaceInfo::freeSpaceInfo(path);
 
110
            setData(source, "kBSize", info.size()/1024); // deprecated
 
111
            setData(source, "kBUsed", info.used()/1024); // deprecated
 
112
            setData(source, "kBAvailable", info.available()/1024); // deprecated
 
113
            setData(source, "size (bytes)", info.size());
 
114
            setData(source, "used (bytes)", info.used());
 
115
            setData(source, "available (bytes)", info.available());
 
116
        }
 
117
    }
 
118
}
 
119
 
 
120
K_EXPORT_PLASMA_DATAENGINE(places, PlacesEngine)
 
121
 
 
122
#include "placesengine.moc"
 
123