Plasma5Support

filebrowserengine.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filebrowserengine.h"
8
9#include <Plasma5Support/DataContainer>
10
11#include <KDirWatch>
12#include <QDebug>
13#include <QDir>
14
15#define InvalidIfEmpty(A) ((A.isEmpty()) ? (QVariant()) : (QVariant(A)))
16#define forMatchingSources \
17 for (DataEngine::SourceDict::iterator it = sources.begin(); it != sources.end(); ++it) \
18 if (dir == QDir(it.key()))
19
20FileBrowserEngine::FileBrowserEngine(QObject *parent)
21 : Plasma5Support::DataEngine(parent)
22 , m_dirWatch(new KDirWatch(this))
23{
24 connect(m_dirWatch, &KDirWatch::created, this, &FileBrowserEngine::dirCreated);
25 connect(m_dirWatch, &KDirWatch::deleted, this, &FileBrowserEngine::dirDeleted);
26 connect(m_dirWatch, &KDirWatch::dirty, this, &FileBrowserEngine::dirDirty);
27}
28
29FileBrowserEngine::~FileBrowserEngine()
30{
31 delete m_dirWatch;
32}
33
34void FileBrowserEngine::init()
35{
36 qDebug() << "init() called";
37}
38
40{
41 qDebug() << "source requested() called: " << path;
42 m_dirWatch->addDir(path);
43 setData(path, QStringLiteral("type"), QVariant("unknown"));
44 updateData(path, INIT);
45 return true;
46}
47
48void FileBrowserEngine::dirDirty(const QString &path)
49{
50 updateData(path, DIRTY);
51}
52
53void FileBrowserEngine::dirCreated(const QString &path)
54{
55 updateData(path, CREATED);
56}
57
58void FileBrowserEngine::dirDeleted(const QString &path)
59{
60 updateData(path, DELETED);
61}
62
63void FileBrowserEngine::updateData(const QString &path, EventType event)
64{
65 Q_UNUSED(event)
66
67 ObjectType type = NOTHING;
68 if (QDir(path).exists()) {
69 type = DIRECTORY;
70 } else if (QFile::exists(path)) {
71 type = FILE;
72 }
73
74 DataEngine::SourceDict sources = containerDict();
75
76 QDir dir(path);
77 clearData(path);
78
79 if (type == DIRECTORY) {
80 qDebug() << "directory info processing: " << path;
81 if (dir.isReadable()) {
82 const QStringList visibleFiles = dir.entryList(QDir::Files, QDir::Name);
83 const QStringList allFiles = dir.entryList(QDir::Files | QDir::Hidden, QDir::Name);
84
85 const QStringList visibleDirectories = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
86 const QStringList allDirectories = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden, QDir::Name);
87
88 forMatchingSources
89 {
90 qDebug() << "MATCH";
91 it.value()->setData(QStringLiteral("item.type"), QVariant("directory"));
92
93 QVariant vdTmp;
94 if (!visibleDirectories.isEmpty())
95 vdTmp = QVariant(visibleDirectories);
96 it.value()->setData(QStringLiteral("directories.visible"), vdTmp);
97
98 QVariant adTmp;
99 if (!allDirectories.empty())
100 adTmp = QVariant(allDirectories);
101 it.value()->setData(QStringLiteral("directories.all"), adTmp);
102
103 QVariant vfTmp;
104 if (!visibleFiles.empty())
105 vfTmp = QVariant(visibleFiles);
106 it.value()->setData(QStringLiteral("files.visible"), vfTmp);
107
108 QVariant afTmp;
109 if (!allFiles.empty())
110 afTmp = QVariant(allFiles);
111 it.value()->setData(QStringLiteral("files.all"), afTmp);
112 }
113 }
114 } else if (type == FILE) {
115 qDebug() << "file info processing: " << path;
116 forMatchingSources
117 {
118 it.value()->setData(QStringLiteral("item.type"), QVariant("file"));
119 }
120 } else {
121 forMatchingSources
122 {
123 it.value()->setData(QStringLiteral("item.type"), QVariant("imaginary"));
124 }
125 };
126}
127
128void FileBrowserEngine::clearData(const QString &path)
129{
130 QDir dir(path);
131 const DataEngine::SourceDict sources = containerDict();
132 for (DataEngine::SourceDict::const_iterator it = sources.begin(); it != sources.end(); ++it) {
133 if (dir == QDir(it.key())) {
134 qDebug() << "matched: " << path << " " << it.key();
135 it.value()->removeAllData();
136
137 } else {
138 qDebug() << "didn't match: " << path << " " << it.key();
139 }
140 }
141}
142
143K_PLUGIN_CLASS_WITH_JSON(FileBrowserEngine, "plasma-dataengine-filebrowser.json")
144
145#include "filebrowserengine.moc"
This class evaluates the basic expressions given in the interface.
bool sourceRequestEvent(const QString &path) override
When a source that does not currently exist is requested by the consumer, this method is called to gi...
void deleted(const QString &path)
void addDir(const QString &path, WatchModes watchModes=WatchDirOnly)
void dirty(const QString &path)
void created(const QString &path)
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
virtual QStringList sources() const
QHash< QString, DataContainer * > containerDict() const
void setData(const QString &source, const QVariant &value)
Sets a value for a data source.
Type type(const QSqlDatabase &db)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
Namespace for everything in libplasma.
Definition datamodel.cpp:15
bool exists() const const
iterator begin()
bool empty() const const
iterator end()
bool isEmpty() const const
virtual bool event(QEvent *e)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.