Changeset View
Changeset View
Standalone View
Standalone View
kioslave/remote/kdedmodule/remotedirnotify.cpp
Show All 13 Lines | 1 | /* This file is part of the KDE Project | |||
---|---|---|---|---|---|
14 | along with this library; see the file COPYING.LIB. If not, write to | 14 | along with this library; see the file COPYING.LIB. If not, write to | ||
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
16 | Boston, MA 02110-1301, USA. | 16 | Boston, MA 02110-1301, USA. | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | #include "remotedirnotify.h" | 19 | #include "remotedirnotify.h" | ||
20 | 20 | | |||
21 | #include "../debug.h" | 21 | #include "../debug.h" | ||
22 | #include <kdesktopfile.h> | | |||
23 | #include <kdirnotify.h> | 22 | #include <kdirnotify.h> | ||
24 | #include <KIO/Global> | 23 | #include <KDirWatch> | ||
25 | 24 | | |||
26 | #include <QtDBus/QtDBus> | 25 | #include <QtDBus/QtDBus> | ||
27 | 26 | | |||
28 | RemoteDirNotify::RemoteDirNotify() | 27 | RemoteDirNotify::RemoteDirNotify() | ||
29 | { | 28 | { | ||
30 | const QString path = QStringLiteral("%1/remoteview").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)); | 29 | const QString path = QStringLiteral("%1/remoteview").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)); | ||
31 | m_baseURL.setPath(path); | | |||
32 | 30 | | |||
33 | QDBusConnection::sessionBus().connect(QString(), QString(), QStringLiteral("org.kde.KDirNotify"), | 31 | m_dirWatch = new KDirWatch(this); | ||
34 | QStringLiteral("FilesAdded"), this, SLOT(FilesAdded(QString))); | 32 | m_dirWatch->addDir(path, KDirWatch::WatchFiles); | ||
35 | QDBusConnection::sessionBus().connect(QString(), QString(), QStringLiteral("org.kde.KDirNotify"), | | |||
36 | QStringLiteral("FilesRemoved"), this, SLOT(FilesRemoved(QStringList))); | | |||
37 | QDBusConnection::sessionBus().connect(QString(), QString(), QStringLiteral("org.kde.KDirNotify"), | | |||
38 | QStringLiteral("FilesChanged"), this, SLOT(FilesChanged(QStringList))); | | |||
39 | } | | |||
40 | | ||||
41 | QUrl RemoteDirNotify::toRemoteURL(const QUrl &url) | | |||
42 | { | | |||
43 | qCDebug(KIOREMOTE_LOG) << "RemoteDirNotify::toRemoteURL(" << url << ")"; | | |||
44 | if ( m_baseURL.isParentOf(url) ) | | |||
45 | { | | |||
46 | QString path = QDir(m_baseURL.path()).relativeFilePath(url.path()); | | |||
47 | QUrl result; | | |||
48 | result.setScheme(QStringLiteral("remote")); | | |||
49 | result.setPath(path); | | |||
50 | result.setPath(QDir::cleanPath(result.path())); | | |||
51 | qCDebug(KIOREMOTE_LOG) << "result => " << result; | | |||
52 | return result; | | |||
53 | } | | |||
54 | | ||||
55 | qCDebug(KIOREMOTE_LOG) << "result => QUrl()"; | | |||
56 | return QUrl(); | | |||
57 | } | | |||
58 | | ||||
59 | QList<QUrl> RemoteDirNotify::toRemoteURLList(const QStringList &list) | | |||
60 | { | | |||
61 | QList<QUrl> urls; | | |||
62 | for (const QString &file : list) { | | |||
63 | QUrl url = toRemoteURL(QUrl::fromLocalFile(file)); | | |||
64 | if (url.isValid()) { | | |||
65 | urls.append(url); | | |||
66 | } | | |||
67 | } | | |||
68 | | ||||
69 | return urls; | | |||
70 | } | | |||
71 | | ||||
72 | void RemoteDirNotify::FilesAdded(const QString &directory) | | |||
73 | { | | |||
74 | qCDebug(KIOREMOTE_LOG) << "RemoteDirNotify::FilesAdded"; | | |||
75 | | ||||
76 | QUrl new_dir = toRemoteURL(QUrl::fromLocalFile(directory)); | | |||
77 | | ||||
78 | if (new_dir.isValid()) | | |||
79 | { | | |||
80 | org::kde::KDirNotify::emitFilesAdded(new_dir); | | |||
81 | } | | |||
82 | } | | |||
83 | 33 | | |||
84 | // This hack is required because of the way we manage .desktop files with | 34 | connect(m_dirWatch, &KDirWatch::created, this, &RemoteDirNotify::slotRemoteChanged); | ||
85 | // Forwarding Slaves, their URL is out of the ioslave (most remote:/ files | 35 | connect(m_dirWatch, &KDirWatch::deleted, this, &RemoteDirNotify::slotRemoteChanged); | ||
86 | // have a file:/ based UDS_URL so that they are executed correctly. | 36 | connect(m_dirWatch, &KDirWatch::dirty, this, &RemoteDirNotify::slotRemoteChanged); | ||
87 | // Hence, FilesRemoved and FilesChanged does nothing... We're forced to use | | |||
88 | // FilesAdded to re-list the modified directory. | | |||
89 | inline void evil_hack(const QList<QUrl> &list) | | |||
90 | { | | |||
91 | QList<QUrl> notified; | | |||
92 | | ||||
93 | QList<QUrl>::const_iterator it = list.begin(); | | |||
94 | QList<QUrl>::const_iterator end = list.end(); | | |||
95 | | ||||
96 | for (; it!=end; ++it) | | |||
97 | { | | |||
98 | QUrl url = KIO::upUrl(*it); | | |||
99 | | ||||
100 | if (!notified.contains(url)) | | |||
101 | { | | |||
102 | org::kde::KDirNotify::emitFilesAdded(url); | | |||
103 | notified.append(url); | | |||
104 | } | | |||
105 | } | | |||
106 | } | | |||
107 | | ||||
108 | | ||||
109 | void RemoteDirNotify::FilesRemoved(const QStringList &fileList) | | |||
110 | { | | |||
111 | qCDebug(KIOREMOTE_LOG) << "RemoteDirNotify::FilesRemoved"; | | |||
112 | | ||||
113 | QList<QUrl> new_list = toRemoteURLList(fileList); | | |||
114 | | ||||
115 | if (!new_list.isEmpty()) | | |||
116 | { | | |||
117 | //KDirNotify_stub notifier("*", "*"); | | |||
118 | //notifier.FilesRemoved( new_list ); | | |||
119 | evil_hack(new_list); | | |||
120 | } | | |||
121 | } | 37 | } | ||
122 | 38 | | |||
123 | void RemoteDirNotify::FilesChanged(const QStringList &fileList) | 39 | void RemoteDirNotify::slotRemoteChanged() | ||
124 | { | 40 | { | ||
125 | qCDebug(KIOREMOTE_LOG) << "RemoteDirNotify::FilesChanged"; | 41 | org::kde::KDirNotify::emitFilesAdded(QUrl("remote:/")); | ||
126 | | ||||
127 | QList<QUrl> new_list = toRemoteURLList(fileList); | | |||
128 | | ||||
129 | if (!new_list.isEmpty()) | | |||
130 | { | | |||
131 | //KDirNotify_stub notifier("*", "*"); | | |||
132 | //notifier.FilesChanged( new_list ); | | |||
133 | evil_hack(new_list); | | |||
134 | } | | |||
135 | } | 42 | } | ||
136 | 43 | |