Engines
filebrowserengine.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "filebrowserengine.h"
00021
00022 #include <Plasma/DataContainer>
00023
00024 #include <QDir>
00025 #include <KDirWatch>
00026 #include <KDebug>
00027 #include <KFileMetaInfo>
00028
00029 #define InvalidIfEmpty(A) ((A.isEmpty())?(QVariant()):(QVariant(A)))
00030 #define forMatchingSources for (DataEngine::SourceDict::iterator it = sources.begin(); it != sources.end(); it++) \
00031 if (dir == QDir(it.key()))
00032
00033 FileBrowserEngine::FileBrowserEngine(QObject* parent, const QVariantList& args) :
00034 Plasma::DataEngine(parent, args), m_dirWatch(NULL)
00035 {
00036 Q_UNUSED(args)
00037
00038 m_dirWatch = new KDirWatch(this);
00039 connect(m_dirWatch, SIGNAL(created(
00040 const QString &)), this, SLOT(dirCreated(const QString &)));
00041 connect(m_dirWatch, SIGNAL(deleted(
00042 const QString &)), this, SLOT(dirDeleted(const QString &)));
00043 connect(m_dirWatch, SIGNAL(dirty(
00044 const QString &)), this, SLOT(dirDirty(const QString &)));
00045 }
00046
00047 FileBrowserEngine::~FileBrowserEngine()
00048 {
00049 delete m_dirWatch;
00050 }
00051
00052 void FileBrowserEngine::init()
00053 {
00054 kDebug() << "init() called";
00055 }
00056
00057 bool FileBrowserEngine::sourceRequestEvent(const QString &path)
00058 {
00059 kDebug() << "source requested() called: "<< path;
00060 m_dirWatch->addDir(path);
00061 setData(path, "type", QVariant("unknown"));
00062 updateData (path, INIT);
00063 return true;
00064 }
00065
00066 void FileBrowserEngine::dirDirty(const QString &path)
00067 {
00068 updateData(path, DIRTY);
00069 }
00070
00071 void FileBrowserEngine::dirCreated(const QString &path)
00072 {
00073 updateData(path, CREATED);
00074 }
00075
00076 void FileBrowserEngine::dirDeleted(const QString &path)
00077 {
00078 updateData(path, DELETED);
00079 }
00080
00081 void FileBrowserEngine::updateData(const QString &path, EventType event)
00082 {
00083 Q_UNUSED(event)
00084
00085 ObjectType type = NOTHING;
00086 if (QDir(path).exists()) {
00087 type = DIRECTORY;
00088 } else if (QFile::exists(path)) {
00089 type = FILE;
00090 }
00091
00092 DataEngine::SourceDict sources = containerDict();
00093
00094 QDir dir(path);
00095 clearData(path);
00096
00097 if (type == DIRECTORY) {
00098 kDebug() << "directory info processing: "<< path;
00099 if (dir.isReadable()) {
00100 QStringList visibleFiles = dir.entryList(QDir::Files, QDir::Name);
00101 QStringList allFiles = dir.entryList(QDir::Files | QDir::Hidden,
00102 QDir::Name);
00103
00104 QStringList visibleDirectories = dir.entryList(QDir::Dirs
00105 | QDir::NoDotAndDotDot, QDir::Name);
00106 QStringList allDirectories = dir.entryList(QDir::Dirs
00107 | QDir::NoDotAndDotDot | QDir::Hidden, QDir::Name);
00108
00109 forMatchingSources {
00110 kDebug() << "MATCH";
00111 it.value()->setData("item.type", QVariant("directory"));
00112
00113 QVariant vdTmp;
00114 if (!visibleDirectories.isEmpty()) vdTmp = QVariant(visibleDirectories);
00115 it.value()->setData("directories.visible", vdTmp);
00116
00117 QVariant adTmp;
00118 if (!allDirectories.empty()) adTmp = QVariant(allDirectories);
00119 it.value()->setData("directories.all", adTmp);
00120
00121 QVariant vfTmp;
00122 if (!visibleFiles.empty()) vfTmp = QVariant(visibleFiles);
00123 it.value()->setData("files.visible", vfTmp);
00124
00125 QVariant afTmp;
00126 if (!allFiles.empty()) afTmp = QVariant(allFiles);
00127 it.value()->setData("files.all", afTmp);
00128 }
00129 }
00130 } else if (type == FILE) {
00131 kDebug() << "file info processing: "<< path;
00132 KFileMetaInfo kfmi(path, QString(), KFileMetaInfo::Everything);
00133 if (kfmi.isValid()) {
00134 kDebug() << "METAINFO: " << kfmi.keys();
00135
00136 forMatchingSources {
00137 kDebug() << "MATCH";
00138 it.value()->setData("item.type", QVariant("file"));
00139
00140 for (QHash< QString, KFileMetaInfoItem >::const_iterator i = kfmi.items().constBegin(); i != kfmi.items().constEnd(); i++) {
00141 it.value()->setData(i.key(), i.value().value());
00142 }
00143 }
00144 }
00145 } else {
00146 forMatchingSources {
00147 it.value()->setData("item.type", QVariant("imaginary"));
00148 }
00149 };
00150
00151 scheduleSourcesUpdated();
00152
00153 }
00154
00155 void FileBrowserEngine::clearData(const QString &path)
00156 {
00157 QDir dir(path);
00158 const DataEngine::SourceDict sources = containerDict();
00159 for (DataEngine::SourceDict::const_iterator it = sources.begin(); it
00160 != sources.end(); it++) {
00161 if (dir == QDir(it.key())) {
00162 kDebug() << "matched: "<< path << " "<< it.key();
00163 it.value()->removeAllData();
00164
00165 } else {
00166 kDebug() << "didn't match: "<< path << " "<< it.key();
00167 }
00168 }
00169 }
00170
00171 #include "filebrowserengine.moc"