add more spacing
[personal-kdebase.git] / workspace / plasma / dataengines / filebrowser / filebrowserengine.cpp
blobcfdc3c27c58a9c624d4b63fe931eec64cc14d5de
1 /*
2 * Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License either version 2, or
6 * (at your option) any later version as published by the Free Software
7 * Foundation.
9 * This program 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
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "filebrowserengine.h"
22 #include <Plasma/DataContainer>
24 #include <QDir>
25 #include <KDirWatch>
26 #include <KDebug>
27 #include <KFileMetaInfo>
29 #define InvalidIfEmpty(A) ((A.isEmpty())?(QVariant()):(QVariant(A)))
30 #define forMatchingSources for (DataEngine::SourceDict::iterator it = sources.begin(); it != sources.end(); it++) \
31 if (dir == QDir(it.key()))
33 FileBrowserEngine::FileBrowserEngine(QObject* parent, const QVariantList& args) :
34 Plasma::DataEngine(parent, args), m_dirWatch(NULL)
36 Q_UNUSED(args)
38 m_dirWatch = new KDirWatch(this);
39 connect(m_dirWatch, SIGNAL(created(
40 const QString &)), this, SLOT(dirCreated(const QString &)));
41 connect(m_dirWatch, SIGNAL(deleted(
42 const QString &)), this, SLOT(dirDeleted(const QString &)));
43 connect(m_dirWatch, SIGNAL(dirty(
44 const QString &)), this, SLOT(dirDirty(const QString &)));
47 FileBrowserEngine::~FileBrowserEngine()
49 delete m_dirWatch;
52 void FileBrowserEngine::init()
54 kDebug() << "init() called";
57 bool FileBrowserEngine::sourceRequestEvent(const QString &path)
59 kDebug() << "source requested() called: "<< path;
60 m_dirWatch->addDir(path);
61 setData(path, "type", QVariant("unknown"));
62 updateData (path, INIT);
63 return true;
66 void FileBrowserEngine::dirDirty(const QString &path)
68 updateData(path, DIRTY);
71 void FileBrowserEngine::dirCreated(const QString &path)
73 updateData(path, CREATED);
76 void FileBrowserEngine::dirDeleted(const QString &path)
78 updateData(path, DELETED);
81 void FileBrowserEngine::updateData(const QString &path, EventType event)
83 Q_UNUSED(event)
85 ObjectType type = NOTHING;
86 if (QDir(path).exists()) {
87 type = DIRECTORY;
88 } else if (QFile::exists(path)) {
89 type = FILE;
92 DataEngine::SourceDict sources = containerDict();
94 QDir dir(path);
95 clearData(path);
97 if (type == DIRECTORY) {
98 kDebug() << "directory info processing: "<< path;
99 if (dir.isReadable()) {
100 QStringList visibleFiles = dir.entryList(QDir::Files, QDir::Name);
101 QStringList allFiles = dir.entryList(QDir::Files | QDir::Hidden,
102 QDir::Name);
104 QStringList visibleDirectories = dir.entryList(QDir::Dirs
105 | QDir::NoDotAndDotDot, QDir::Name);
106 QStringList allDirectories = dir.entryList(QDir::Dirs
107 | QDir::NoDotAndDotDot | QDir::Hidden, QDir::Name);
109 forMatchingSources {
110 kDebug() << "MATCH";
111 it.value()->setData("item.type", QVariant("directory"));
113 QVariant vdTmp;
114 if (!visibleDirectories.isEmpty()) vdTmp = QVariant(visibleDirectories);
115 it.value()->setData("directories.visible", vdTmp);
117 QVariant adTmp;
118 if (!allDirectories.empty()) adTmp = QVariant(allDirectories);
119 it.value()->setData("directories.all", adTmp);
121 QVariant vfTmp;
122 if (!visibleFiles.empty()) vfTmp = QVariant(visibleFiles);
123 it.value()->setData("files.visible", vfTmp);
125 QVariant afTmp;
126 if (!allFiles.empty()) afTmp = QVariant(allFiles);
127 it.value()->setData("files.all", afTmp);
130 } else if (type == FILE) {
131 kDebug() << "file info processing: "<< path;
132 KFileMetaInfo kfmi(path, QString(), KFileMetaInfo::Everything);
133 if (kfmi.isValid()) {
134 kDebug() << "METAINFO: " << kfmi.keys();
136 forMatchingSources {
137 kDebug() << "MATCH";
138 it.value()->setData("item.type", QVariant("file"));
140 for (QHash< QString, KFileMetaInfoItem >::const_iterator i = kfmi.items().constBegin(); i != kfmi.items().constEnd(); i++) {
141 it.value()->setData(i.key(), i.value().value());
145 } else {
146 forMatchingSources {
147 it.value()->setData("item.type", QVariant("imaginary"));
151 scheduleSourcesUpdated();
155 void FileBrowserEngine::clearData(const QString &path)
157 QDir dir(path);
158 const DataEngine::SourceDict sources = containerDict();
159 for (DataEngine::SourceDict::const_iterator it = sources.begin(); it
160 != sources.end(); it++) {
161 if (dir == QDir(it.key())) {
162 kDebug() << "matched: "<< path << " "<< it.key();
163 it.value()->removeAllData();
165 } else {
166 kDebug() << "didn't match: "<< path << " "<< it.key();
171 #include "filebrowserengine.moc"