• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

filebrowserengine.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License either version 2, or
00006  *   (at your option) any later version as published by the Free Software
00007  *   Foundation.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "filebrowserengine.h"
00021 
00022 #include "plasma/datacontainer.h"
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     DataEngine::SourceDict sources = containerDict();
00159     for (DataEngine::SourceDict::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"

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal