File indexing completed on 2024-05-19 05:37:50

0001 /*
0002     SPDX-FileCopyrightText: 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "filebrowserengine.h"
0008 
0009 #include <Plasma5Support/DataContainer>
0010 
0011 #include <KDirWatch>
0012 #include <QDebug>
0013 #include <QDir>
0014 
0015 #define InvalidIfEmpty(A) ((A.isEmpty()) ? (QVariant()) : (QVariant(A)))
0016 #define forMatchingSources                                                                                                                                     \
0017     for (DataEngine::SourceDict::iterator it = sources.begin(); it != sources.end(); ++it)                                                                     \
0018         if (dir == QDir(it.key()))
0019 
0020 FileBrowserEngine::FileBrowserEngine(QObject *parent)
0021     : Plasma5Support::DataEngine(parent)
0022     , m_dirWatch(new KDirWatch(this))
0023 {
0024     connect(m_dirWatch, &KDirWatch::created, this, &FileBrowserEngine::dirCreated);
0025     connect(m_dirWatch, &KDirWatch::deleted, this, &FileBrowserEngine::dirDeleted);
0026     connect(m_dirWatch, &KDirWatch::dirty, this, &FileBrowserEngine::dirDirty);
0027 }
0028 
0029 FileBrowserEngine::~FileBrowserEngine()
0030 {
0031     delete m_dirWatch;
0032 }
0033 
0034 void FileBrowserEngine::init()
0035 {
0036     qDebug() << "init() called";
0037 }
0038 
0039 bool FileBrowserEngine::sourceRequestEvent(const QString &path)
0040 {
0041     qDebug() << "source requested() called: " << path;
0042     m_dirWatch->addDir(path);
0043     setData(path, QStringLiteral("type"), QVariant("unknown"));
0044     updateData(path, INIT);
0045     return true;
0046 }
0047 
0048 void FileBrowserEngine::dirDirty(const QString &path)
0049 {
0050     updateData(path, DIRTY);
0051 }
0052 
0053 void FileBrowserEngine::dirCreated(const QString &path)
0054 {
0055     updateData(path, CREATED);
0056 }
0057 
0058 void FileBrowserEngine::dirDeleted(const QString &path)
0059 {
0060     updateData(path, DELETED);
0061 }
0062 
0063 void FileBrowserEngine::updateData(const QString &path, EventType event)
0064 {
0065     Q_UNUSED(event)
0066 
0067     ObjectType type = NOTHING;
0068     if (QDir(path).exists()) {
0069         type = DIRECTORY;
0070     } else if (QFile::exists(path)) {
0071         type = FILE;
0072     }
0073 
0074     DataEngine::SourceDict sources = containerDict();
0075 
0076     QDir dir(path);
0077     clearData(path);
0078 
0079     if (type == DIRECTORY) {
0080         qDebug() << "directory info processing: " << path;
0081         if (dir.isReadable()) {
0082             const QStringList visibleFiles = dir.entryList(QDir::Files, QDir::Name);
0083             const QStringList allFiles = dir.entryList(QDir::Files | QDir::Hidden, QDir::Name);
0084 
0085             const QStringList visibleDirectories = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
0086             const QStringList allDirectories = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden, QDir::Name);
0087 
0088             forMatchingSources
0089             {
0090                 qDebug() << "MATCH";
0091                 it.value()->setData(QStringLiteral("item.type"), QVariant("directory"));
0092 
0093                 QVariant vdTmp;
0094                 if (!visibleDirectories.isEmpty())
0095                     vdTmp = QVariant(visibleDirectories);
0096                 it.value()->setData(QStringLiteral("directories.visible"), vdTmp);
0097 
0098                 QVariant adTmp;
0099                 if (!allDirectories.empty())
0100                     adTmp = QVariant(allDirectories);
0101                 it.value()->setData(QStringLiteral("directories.all"), adTmp);
0102 
0103                 QVariant vfTmp;
0104                 if (!visibleFiles.empty())
0105                     vfTmp = QVariant(visibleFiles);
0106                 it.value()->setData(QStringLiteral("files.visible"), vfTmp);
0107 
0108                 QVariant afTmp;
0109                 if (!allFiles.empty())
0110                     afTmp = QVariant(allFiles);
0111                 it.value()->setData(QStringLiteral("files.all"), afTmp);
0112             }
0113         }
0114     } else if (type == FILE) {
0115         qDebug() << "file info processing: " << path;
0116         forMatchingSources
0117         {
0118             it.value()->setData(QStringLiteral("item.type"), QVariant("file"));
0119         }
0120     } else {
0121         forMatchingSources
0122         {
0123             it.value()->setData(QStringLiteral("item.type"), QVariant("imaginary"));
0124         }
0125     };
0126 }
0127 
0128 void FileBrowserEngine::clearData(const QString &path)
0129 {
0130     QDir dir(path);
0131     const DataEngine::SourceDict sources = containerDict();
0132     for (DataEngine::SourceDict::const_iterator it = sources.begin(); it != sources.end(); ++it) {
0133         if (dir == QDir(it.key())) {
0134             qDebug() << "matched: " << path << " " << it.key();
0135             it.value()->removeAllData();
0136 
0137         } else {
0138             qDebug() << "didn't match: " << path << " " << it.key();
0139         }
0140     }
0141 }
0142 
0143 K_PLUGIN_CLASS_WITH_JSON(FileBrowserEngine, "plasma-dataengine-filebrowser.json")
0144 
0145 #include "filebrowserengine.moc"