Changeset View
Changeset View
Standalone View
Standalone View
kioslave/remote/remoteimpl.cpp
- This file was added.
1 | /* This file is part of the KDE project | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2004 Kevin 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 as published by the Free Software Foundation; either | ||||
7 | version 2 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Library General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "remoteimpl.h" | ||||
21 | | ||||
22 | #include "debug.h" | ||||
23 | #include <kdesktopfile.h> | ||||
24 | #include <kservice.h> | ||||
25 | #include <kconfiggroup.h> | ||||
26 | #include <KLocalizedString> | ||||
27 | | ||||
28 | #include <QDir> | ||||
29 | #include <QFile> | ||||
30 | | ||||
31 | #include <sys/stat.h> | ||||
32 | | ||||
33 | #define WIZARD_URL "remote:/x-wizard_service.desktop" | ||||
34 | #define WIZARD_SERVICE "org.kde.knetattach" | ||||
35 | | ||||
36 | RemoteImpl::RemoteImpl() | ||||
37 | { | ||||
38 | const QString path = QStringLiteral("%1/remoteview").arg(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)); | ||||
39 | | ||||
40 | QDir dir = path; | ||||
41 | if (!dir.exists()) | ||||
42 | { | ||||
43 | dir.cdUp(); | ||||
44 | dir.mkdir(QStringLiteral("remoteview")); | ||||
45 | } | ||||
46 | } | ||||
47 | | ||||
48 | void RemoteImpl::listRoot(KIO::UDSEntryList &list) const | ||||
49 | { | ||||
50 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::listRoot"; | ||||
51 | | ||||
52 | QStringList names_found; | ||||
53 | const QStringList dirList = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("remoteview"), QStandardPaths::LocateDirectory); | ||||
54 | | ||||
55 | QStringList::ConstIterator dirpath = dirList.constBegin(); | ||||
56 | const QStringList::ConstIterator end = dirList.constEnd(); | ||||
57 | for(; dirpath!=end; ++dirpath) | ||||
58 | { | ||||
59 | QDir dir = *dirpath; | ||||
60 | if (!dir.exists()) continue; | ||||
61 | | ||||
62 | const QStringList filenames | ||||
63 | = dir.entryList( QDir::Files | QDir::Readable ); | ||||
64 | | ||||
65 | | ||||
66 | KIO::UDSEntry entry; | ||||
67 | | ||||
68 | QStringList::ConstIterator name = filenames.constBegin(); | ||||
69 | QStringList::ConstIterator endf = filenames.constEnd(); | ||||
70 | | ||||
71 | for(; name!=endf; ++name) | ||||
72 | { | ||||
73 | if (!names_found.contains(*name)) | ||||
74 | { | ||||
75 | entry.clear(); | ||||
76 | createEntry(entry, *dirpath, *name); | ||||
77 | list.append(entry); | ||||
78 | names_found.append(*name); | ||||
79 | } | ||||
80 | } | ||||
81 | } | ||||
82 | } | ||||
83 | | ||||
84 | bool RemoteImpl::findDirectory(const QString &filename, QString &directory) const | ||||
85 | { | ||||
86 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDirectory"; | ||||
87 | | ||||
88 | const QStringList dirList = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("remoteview"), QStandardPaths::LocateDirectory); | ||||
89 | | ||||
90 | QStringList::ConstIterator dirpath = dirList.constBegin(); | ||||
91 | const QStringList::ConstIterator end = dirList.constEnd(); | ||||
92 | for(; dirpath!=end; ++dirpath) | ||||
93 | { | ||||
94 | QDir dir = *dirpath; | ||||
95 | if (!dir.exists()) continue; | ||||
96 | | ||||
97 | QStringList filenames | ||||
98 | = dir.entryList( QDir::Files | QDir::Readable ); | ||||
99 | | ||||
100 | | ||||
101 | KIO::UDSEntry entry; | ||||
102 | | ||||
103 | QStringList::ConstIterator name = filenames.constBegin(); | ||||
104 | QStringList::ConstIterator endf = filenames.constEnd(); | ||||
105 | | ||||
106 | for(; name!=endf; ++name) | ||||
107 | { | ||||
108 | if (*name==filename) | ||||
109 | { | ||||
110 | directory = *dirpath; | ||||
111 | return true; | ||||
112 | } | ||||
113 | } | ||||
114 | } | ||||
115 | | ||||
116 | return false; | ||||
117 | } | ||||
118 | | ||||
119 | QString RemoteImpl::findDesktopFile(const QString &filename) const | ||||
120 | { | ||||
121 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findDesktopFile"; | ||||
122 | | ||||
123 | QString directory; | ||||
124 | if (findDirectory(filename+".desktop", directory)) | ||||
125 | { | ||||
126 | return directory+filename+".desktop"; | ||||
127 | } | ||||
128 | | ||||
129 | return QString(); | ||||
130 | } | ||||
131 | | ||||
132 | QUrl RemoteImpl::findBaseURL(const QString &filename) const | ||||
133 | { | ||||
134 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::findBaseURL"; | ||||
135 | | ||||
136 | const QString file = findDesktopFile(filename); | ||||
137 | if (!file.isEmpty()) | ||||
138 | { | ||||
139 | KDesktopFile desktop( file ); | ||||
140 | return QUrl::fromLocalFile(desktop.readUrl()); | ||||
141 | } | ||||
142 | | ||||
143 | return QUrl(); | ||||
144 | } | ||||
145 | | ||||
146 | | ||||
147 | void RemoteImpl::createTopLevelEntry(KIO::UDSEntry &entry) const | ||||
148 | { | ||||
149 | entry.clear(); | ||||
150 | entry.insert( KIO::UDSEntry::UDS_NAME, QString::fromLatin1(".")); | ||||
151 | entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); | ||||
152 | entry.insert( KIO::UDSEntry::UDS_ACCESS, 0777); | ||||
153 | entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("inode/directory")); | ||||
154 | entry.insert( KIO::UDSEntry::UDS_ICON_NAME, QString::fromLatin1("folder-remote")); | ||||
155 | entry.insert( KIO::UDSEntry::UDS_USER, QString::fromLatin1("root")); | ||||
156 | entry.insert( KIO::UDSEntry::UDS_GROUP, QString::fromLatin1("root")); | ||||
157 | } | ||||
158 | | ||||
159 | static QUrl findWizardRealURL() | ||||
160 | { | ||||
161 | QUrl url; | ||||
162 | KService::Ptr service = KService::serviceByDesktopName(WIZARD_SERVICE); | ||||
163 | | ||||
164 | if (service && service->isValid()) | ||||
165 | { | ||||
166 | url.setPath(QStandardPaths::locate(QStandardPaths::ApplicationsLocation, | ||||
167 | QStringLiteral("%1.desktop").arg(WIZARD_SERVICE))); | ||||
168 | } | ||||
169 | | ||||
170 | return url; | ||||
171 | } | ||||
172 | | ||||
173 | bool RemoteImpl::createWizardEntry(KIO::UDSEntry &entry) const | ||||
174 | { | ||||
175 | entry.clear(); | ||||
176 | | ||||
177 | QUrl url = findWizardRealURL(); | ||||
178 | | ||||
179 | if (!url.isValid()) | ||||
180 | { | ||||
181 | return false; | ||||
182 | } | ||||
183 | | ||||
184 | entry.insert( KIO::UDSEntry::UDS_NAME, i18n("Add Network Folder")); | ||||
185 | entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG); | ||||
186 | entry.insert( KIO::UDSEntry::UDS_URL, QString::fromLatin1(WIZARD_URL) ); | ||||
187 | entry.insert( KIO::UDSEntry::UDS_LOCAL_PATH, url.path()); | ||||
188 | entry.insert( KIO::UDSEntry::UDS_ACCESS, 0500); | ||||
189 | entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("application/x-desktop")); | ||||
190 | entry.insert( KIO::UDSEntry::UDS_ICON_NAME, QString::fromLatin1("folder-new")); | ||||
191 | | ||||
192 | return true; | ||||
193 | } | ||||
194 | | ||||
195 | bool RemoteImpl::isWizardURL(const QUrl &url) const | ||||
196 | { | ||||
197 | return url==QUrl(WIZARD_URL); | ||||
198 | } | ||||
199 | | ||||
200 | | ||||
201 | void RemoteImpl::createEntry(KIO::UDSEntry &entry, | ||||
202 | const QString &directory, | ||||
203 | const QString &file) const | ||||
204 | { | ||||
205 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::createEntry"; | ||||
206 | | ||||
207 | QString dir = directory; | ||||
208 | if (!dir.endsWith(QLatin1Char('/'))) { | ||||
209 | dir += QLatin1Char('/'); | ||||
210 | } | ||||
211 | KDesktopFile desktop(dir + file); | ||||
212 | | ||||
213 | qCDebug(KIOREMOTE_LOG) << "path = " << directory << file; | ||||
214 | | ||||
215 | entry.clear(); | ||||
216 | | ||||
217 | QString new_filename = file; | ||||
218 | new_filename.truncate( file.length()-8); | ||||
219 | | ||||
220 | entry.insert( KIO::UDSEntry::UDS_NAME, desktop.readName()); | ||||
221 | entry.insert( KIO::UDSEntry::UDS_URL, "remote:/"+new_filename); | ||||
222 | | ||||
223 | entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); | ||||
224 | entry.insert( KIO::UDSEntry::UDS_ACCESS, 0500); | ||||
225 | entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("inode/directory")); | ||||
226 | | ||||
227 | const QString icon = desktop.readIcon(); | ||||
228 | entry.insert( KIO::UDSEntry::UDS_ICON_NAME, icon); | ||||
229 | entry.insert( KIO::UDSEntry::UDS_LINK_DEST, desktop.readUrl()); | ||||
230 | entry.insert( KIO::UDSEntry::UDS_TARGET_URL, desktop.readUrl()); | ||||
231 | } | ||||
232 | | ||||
233 | bool RemoteImpl::statNetworkFolder(KIO::UDSEntry &entry, const QString &filename) const | ||||
234 | { | ||||
235 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::statNetworkFolder: " << filename; | ||||
236 | | ||||
237 | QString directory; | ||||
238 | if (findDirectory(filename+".desktop", directory)) | ||||
239 | { | ||||
240 | createEntry(entry, directory, filename+".desktop"); | ||||
241 | return true; | ||||
242 | } | ||||
243 | | ||||
244 | return false; | ||||
245 | } | ||||
246 | | ||||
247 | bool RemoteImpl::deleteNetworkFolder(const QString &filename) const | ||||
248 | { | ||||
249 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::deleteNetworkFolder: " << filename; | ||||
250 | | ||||
251 | QString directory; | ||||
252 | if (findDirectory(filename+".desktop", directory)) | ||||
253 | { | ||||
254 | qCDebug(KIOREMOTE_LOG) << "Removing " << directory << filename << ".desktop"; | ||||
255 | return QFile::remove(directory+filename+".desktop"); | ||||
256 | } | ||||
257 | | ||||
258 | return false; | ||||
259 | } | ||||
260 | | ||||
261 | bool RemoteImpl::renameFolders(const QString &src, const QString &dest, | ||||
262 | bool overwrite) const | ||||
263 | { | ||||
264 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::renameFolders: " | ||||
265 | << src << ", " << dest << endl; | ||||
266 | | ||||
267 | QString directory; | ||||
268 | if (findDirectory(src+".desktop", directory)) | ||||
269 | { | ||||
270 | if (!overwrite && QFile::exists(directory+dest+".desktop")) | ||||
271 | { | ||||
272 | return false; | ||||
273 | } | ||||
274 | | ||||
275 | qCDebug(KIOREMOTE_LOG) << "Renaming " << directory << src << ".desktop"; | ||||
276 | QDir dir(directory); | ||||
277 | bool res = dir.rename(src+".desktop", dest+".desktop"); | ||||
278 | if (res) | ||||
279 | { | ||||
280 | KDesktopFile desktop(directory+dest+".desktop"); | ||||
281 | desktop.desktopGroup().writeEntry("Name", dest); | ||||
282 | } | ||||
283 | return res; | ||||
284 | } | ||||
285 | | ||||
286 | return false; | ||||
287 | } | ||||
288 | | ||||
289 | bool RemoteImpl::changeFolderTarget(const QString &src, const QString &target, | ||||
290 | bool overwrite) const | ||||
291 | { | ||||
292 | qCDebug(KIOREMOTE_LOG) << "RemoteImpl::changeFolderTarget: " | ||||
293 | << src << ", " << target << endl; | ||||
294 | | ||||
295 | QString directory; | ||||
296 | if (findDirectory(src+".desktop", directory)) | ||||
297 | { | ||||
298 | if (!overwrite || !QFile::exists(directory+src+".desktop")) | ||||
299 | { | ||||
300 | return false; | ||||
301 | } | ||||
302 | | ||||
303 | qCDebug(KIOREMOTE_LOG) << "Changing target " << directory << src << ".desktop"; | ||||
304 | KDesktopFile desktop(directory+src+".desktop"); | ||||
305 | desktop.desktopGroup().writeEntry("URL", target); | ||||
306 | return true; | ||||
307 | } | ||||
308 | | ||||
309 | return false; | ||||
310 | } | ||||
311 | |