add more spacing
[personal-kdebase.git] / runtime / kioslave / remote / remoteimpl.cpp
blob671494726942e780e980cfdfc0a7714e02f49c47
1 /* This file is part of the KDE project
2 Copyright (c) 2004 Kevin Ottens <ervin ipsquad net>
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.
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.
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.
20 #include "remoteimpl.h"
22 #include <kdebug.h>
23 #include <kglobalsettings.h>
24 #include <kstandarddirs.h>
25 #include <kdesktopfile.h>
26 #include <kservice.h>
27 #include <klocale.h>
28 #include <kconfiggroup.h>
30 #include <QDir>
31 #include <QFile>
33 #include <sys/stat.h>
35 #define WIZARD_URL "remote:/x-wizard_service.desktop"
36 #define WIZARD_SERVICE "knetattach"
38 RemoteImpl::RemoteImpl()
40 KGlobal::dirs()->addResourceType("remote_entries", "data", "remoteview");
42 QString path = KGlobal::dirs()->saveLocation("remote_entries");
44 QDir dir = path;
45 if (!dir.exists())
47 dir.cdUp();
48 dir.mkdir("remoteview");
52 void RemoteImpl::listRoot(KIO::UDSEntryList &list) const
54 kDebug(1220) << "RemoteImpl::listRoot";
56 QStringList names_found;
57 const QStringList dirList = KGlobal::dirs()->resourceDirs("remote_entries");
59 QStringList::ConstIterator dirpath = dirList.constBegin();
60 const QStringList::ConstIterator end = dirList.constEnd();
61 for(; dirpath!=end; ++dirpath)
63 QDir dir = *dirpath;
64 if (!dir.exists()) continue;
66 QStringList filenames
67 = dir.entryList( QDir::Files | QDir::Readable );
70 KIO::UDSEntry entry;
72 QStringList::ConstIterator name = filenames.constBegin();
73 QStringList::ConstIterator endf = filenames.constEnd();
75 for(; name!=endf; ++name)
77 if (!names_found.contains(*name))
79 entry.clear();
80 createEntry(entry, *dirpath, *name);
81 list.append(entry);
82 names_found.append(*name);
88 bool RemoteImpl::findDirectory(const QString &filename, QString &directory) const
90 kDebug(1220) << "RemoteImpl::findDirectory";
92 const QStringList dirList = KGlobal::dirs()->resourceDirs("remote_entries");
94 QStringList::ConstIterator dirpath = dirList.constBegin();
95 QStringList::ConstIterator end = dirList.constEnd();
96 for(; dirpath!=end; ++dirpath)
98 QDir dir = *dirpath;
99 if (!dir.exists()) continue;
101 QStringList filenames
102 = dir.entryList( QDir::Files | QDir::Readable );
105 KIO::UDSEntry entry;
107 QStringList::ConstIterator name = filenames.constBegin();
108 QStringList::ConstIterator endf = filenames.constEnd();
110 for(; name!=endf; ++name)
112 if (*name==filename)
114 directory = *dirpath;
115 return true;
120 return false;
123 QString RemoteImpl::findDesktopFile(const QString &filename) const
125 kDebug(1220) << "RemoteImpl::findDesktopFile";
127 QString directory;
128 if (findDirectory(filename+".desktop", directory))
130 return directory+filename+".desktop";
133 return QString();
136 KUrl RemoteImpl::findBaseURL(const QString &filename) const
138 kDebug(1220) << "RemoteImpl::findBaseURL";
140 QString file = findDesktopFile(filename);
141 if (!file.isEmpty())
143 KDesktopFile desktop( file );
144 return desktop.readUrl();
147 return KUrl();
151 void RemoteImpl::createTopLevelEntry(KIO::UDSEntry &entry) const
153 entry.clear();
154 entry.insert( KIO::UDSEntry::UDS_NAME, QString::fromLatin1("."));
155 entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
156 entry.insert( KIO::UDSEntry::UDS_ACCESS, 0777);
157 entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("inode/directory"));
158 entry.insert( KIO::UDSEntry::UDS_ICON_NAME, QString::fromLatin1("folder-remote"));
159 entry.insert( KIO::UDSEntry::UDS_USER, QString::fromLatin1("root"));
160 entry.insert( KIO::UDSEntry::UDS_GROUP, QString::fromLatin1("root"));
163 static KUrl findWizardRealURL()
165 KUrl url;
166 KService::Ptr service = KService::serviceByDesktopName(WIZARD_SERVICE);
168 if (service && service->isValid())
170 url.setPath( KStandardDirs::locate("apps",
171 service->entryPath())
175 return url;
178 bool RemoteImpl::createWizardEntry(KIO::UDSEntry &entry) const
180 entry.clear();
182 KUrl url = findWizardRealURL();
184 if (!url.isValid())
186 return false;
189 entry.insert( KIO::UDSEntry::UDS_NAME, i18n("Add Network Folder"));
190 entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG);
191 entry.insert( KIO::UDSEntry::UDS_URL, QString::fromLatin1(WIZARD_URL) );
192 entry.insert( KIO::UDSEntry::UDS_LOCAL_PATH, url.path());
193 entry.insert( KIO::UDSEntry::UDS_ACCESS, 0500);
194 entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("application/x-desktop"));
195 entry.insert( KIO::UDSEntry::UDS_ICON_NAME, QString::fromLatin1("folder-new"));
197 return true;
200 bool RemoteImpl::isWizardURL(const KUrl &url) const
202 return url==KUrl(WIZARD_URL);
206 void RemoteImpl::createEntry(KIO::UDSEntry &entry,
207 const QString &directory,
208 const QString &file) const
210 kDebug(1220) << "RemoteImpl::createEntry";
212 KDesktopFile desktop(directory+file);
214 kDebug(1220) << "path = " << directory << file;
216 entry.clear();
218 QString new_filename = file;
219 new_filename.truncate( file.length()-8);
221 entry.insert( KIO::UDSEntry::UDS_NAME, desktop.readName());
222 entry.insert( KIO::UDSEntry::UDS_URL, "remote:/"+new_filename);
224 entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR);
225 entry.insert( KIO::UDSEntry::UDS_MIME_TYPE, QString::fromLatin1("inode/directory"));
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());
233 bool RemoteImpl::statNetworkFolder(KIO::UDSEntry &entry, const QString &filename) const
235 kDebug(1220) << "RemoteImpl::statNetworkFolder: " << filename;
237 QString directory;
238 if (findDirectory(filename+".desktop", directory))
240 createEntry(entry, directory, filename+".desktop");
241 return true;
244 return false;
247 bool RemoteImpl::deleteNetworkFolder(const QString &filename) const
249 kDebug(1220) << "RemoteImpl::deleteNetworkFolder: " << filename;
251 QString directory;
252 if (findDirectory(filename+".desktop", directory))
254 kDebug(1220) << "Removing " << directory << filename << ".desktop";
255 return QFile::remove(directory+filename+".desktop");
258 return false;
261 bool RemoteImpl::renameFolders(const QString &src, const QString &dest,
262 bool overwrite) const
264 kDebug(1220) << "RemoteImpl::renameFolders: "
265 << src << ", " << dest << endl;
267 QString directory;
268 if (findDirectory(src+".desktop", directory))
270 if (!overwrite && QFile::exists(directory+dest+".desktop"))
272 return false;
275 kDebug(1220) << "Renaming " << directory << src << ".desktop";
276 QDir dir(directory);
277 bool res = dir.rename(src+".desktop", dest+".desktop");
278 if (res)
280 KDesktopFile desktop(directory+dest+".desktop");
281 desktop.desktopGroup().writeEntry("Name", dest);
283 return res;
286 return false;