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
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>
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
)
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()
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
);
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
)
85 ObjectType type
= NOTHING
;
86 if (QDir(path
).exists()) {
88 } else if (QFile::exists(path
)) {
92 DataEngine::SourceDict sources
= containerDict();
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
,
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
);
111 it
.value()->setData("item.type", QVariant("directory"));
114 if (!visibleDirectories
.isEmpty()) vdTmp
= QVariant(visibleDirectories
);
115 it
.value()->setData("directories.visible", vdTmp
);
118 if (!allDirectories
.empty()) adTmp
= QVariant(allDirectories
);
119 it
.value()->setData("directories.all", adTmp
);
122 if (!visibleFiles
.empty()) vfTmp
= QVariant(visibleFiles
);
123 it
.value()->setData("files.visible", vfTmp
);
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();
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());
147 it
.value()->setData("item.type", QVariant("imaginary"));
151 scheduleSourcesUpdated();
155 void FileBrowserEngine::clearData(const QString
&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();
166 kDebug() << "didn't match: "<< path
<< " "<< it
.key();
171 #include "filebrowserengine.moc"