2 * Copyright (C) 2007 Alex Merry <huntedhacker@tiscali.co.uk>
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
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
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.
20 #include "placesengine.h"
23 #include <QtCore/QString>
24 #include <QtCore/QVariantList>
28 #include <KDiskFreeSpace>
30 PlacesEngine::PlacesEngine(QObject
*parent
, const QVariantList
&args
)
31 : Plasma::DataEngine(parent
, args
)
33 // dataChanged(), rowsRemoved() and setupDone() signals from
34 // KFilePlacesModel are not propagated between applications.
35 // layoutChanged() is not emitted at all.
36 connect(&m_placesModel
, SIGNAL(modelReset()),
37 this, SLOT(modelReset()));
38 connect(&m_placesModel
, SIGNAL(rowsInserted(QModelIndex
,int,int)),
39 this, SLOT(placesAdded(QModelIndex
,int,int)));
44 PlacesEngine::~PlacesEngine()
48 void PlacesEngine::modelReset()
50 kDebug() << "Model reset";
55 void PlacesEngine::placesAdded(const QModelIndex
&parent
, int start
, int end
)
57 kDebug() << "Places added:" << parent
<< "from" << start
<< "to" << end
;
61 void PlacesEngine::diskFreeSpaceFound(const QString
&mountPoint
,
66 kDebug() << "Sir! We got one!" << mountPoint
+ ": "
69 << "avail =" << kBAvailable
;
72 foreach (QString testsource
, sources()) {
73 kDebug() << "Testing" << query(testsource
)["url"];
74 KUrl
url(query(testsource
)["url"].toString());
75 if (url
.isLocalFile() && url
.path() == mountPoint
) {
81 kDebug() << "Source:" << source
;
82 if (!source
.isEmpty()) {
83 setData(source
, "kBSize", kBSize
);
84 setData(source
, "kBUsed", kBUsed
);
85 setData(source
, "kBAvailable", kBAvailable
);
89 void PlacesEngine::tryGetFreeSpace(const KUrl
&url
)
91 if (!url
.isLocalFile()) {
95 kDebug() << "Requesting free space on" << url
;
97 // suicidal object: don't need to worry about cleanup
98 KDiskFreeSpace
*diskFreeSpace
= new KDiskFreeSpace(this);
99 connect(diskFreeSpace
,
100 SIGNAL(foundMountPoint(QString
,quint64
,quint64
,quint64
)),
102 SLOT(diskFreeSpaceFound(QString
,quint64
,quint64
,quint64
)));
103 diskFreeSpace
->readDF(url
.path());
106 void PlacesEngine::sendData()
108 int rowCount
= m_placesModel
.rowCount();
109 for (int i
= 0; i
< rowCount
; ++i
) {
110 QModelIndex index
= m_placesModel
.index(i
,0);
114 QString source
= QString::number(i
);
116 setData(source
, "name", m_placesModel
.text(index
));
117 setData(source
, "url", m_placesModel
.url(index
).url());
118 setData(source
, "icon", m_placesModel
.icon(index
));
119 setData(source
, "hidden",
120 m_placesModel
.data(index
, KFilePlacesModel::HiddenRole
));
121 setData(source
, "setupNeeded",
122 m_placesModel
.data(index
, KFilePlacesModel::SetupNeededRole
));
124 if (m_placesModel
.deviceForIndex(index
).isValid()) {
125 setData(source
, "isDevice", true);
126 tryGetFreeSpace(m_placesModel
.url(index
));
128 setData(source
, "isDevice", false);
133 K_EXPORT_PLASMA_DATAENGINE(places
, PlacesEngine
)