Changeset View
Changeset View
Standalone View
Standalone View
kioslave/remote/kdedmodule/remotedirnotify.cpp
- This file was added.
1 | /* This file is part of the KDE Project | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2004 Kévin Ottens <ervin ipsquad net> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Library General Public | ||||
6 | License version 2 as published by the Free Software Foundation. | ||||
7 | | ||||
8 | This library is distributed in the hope that it will be useful, | ||||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
11 | Library General Public License for more details. | ||||
12 | | ||||
13 | You should have received a copy of the GNU Library General Public License | ||||
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, | ||||
16 | Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "remotedirnotify.h" | ||||
20 | | ||||
21 | #include "../debug.h" | ||||
22 | #include <kdesktopfile.h> | ||||
23 | #include <kdirnotify.h> | ||||
24 | #include <KIO/Global> | ||||
25 | | ||||
26 | #include <QtDBus/QtDBus> | ||||
27 | | ||||
28 | RemoteDirNotify::RemoteDirNotify() | ||||
29 | { | ||||
30 | const QString path = QStringLiteral("%1/remoteview").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)); | ||||
31 | m_baseURL.setPath(path); | ||||
32 | | ||||
33 | QDBusConnection::sessionBus().connect(QString(), QString(), QStringLiteral("org.kde.KDirNotify"), | ||||
34 | QStringLiteral("FilesAdded"), this, SLOT(FilesAdded(QString))); | ||||
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 | | ||||
84 | // This hack is required because of the way we manage .desktop files with | ||||
85 | // Forwarding Slaves, their URL is out of the ioslave (most remote:/ files | ||||
86 | // have a file:/ based UDS_URL so that they are executed correctly. | ||||
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 | } | ||||
122 | | ||||
123 | void RemoteDirNotify::FilesChanged(const QStringList &fileList) | ||||
124 | { | ||||
125 | qCDebug(KIOREMOTE_LOG) << "RemoteDirNotify::FilesChanged"; | ||||
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 | } | ||||
136 | |