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"
23 #include <kglobalsettings.h>
24 #include <kstandarddirs.h>
25 #include <kdesktopfile.h>
28 #include <kconfiggroup.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");
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
)
64 if (!dir
.exists()) continue;
67 = dir
.entryList( QDir::Files
| QDir::Readable
);
72 QStringList::ConstIterator name
= filenames
.constBegin();
73 QStringList::ConstIterator endf
= filenames
.constEnd();
75 for(; name
!=endf
; ++name
)
77 if (!names_found
.contains(*name
))
80 createEntry(entry
, *dirpath
, *name
);
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
)
99 if (!dir
.exists()) continue;
101 QStringList filenames
102 = dir
.entryList( QDir::Files
| QDir::Readable
);
107 QStringList::ConstIterator name
= filenames
.constBegin();
108 QStringList::ConstIterator endf
= filenames
.constEnd();
110 for(; name
!=endf
; ++name
)
114 directory
= *dirpath
;
123 QString
RemoteImpl::findDesktopFile(const QString
&filename
) const
125 kDebug(1220) << "RemoteImpl::findDesktopFile";
128 if (findDirectory(filename
+".desktop", directory
))
130 return directory
+filename
+".desktop";
136 KUrl
RemoteImpl::findBaseURL(const QString
&filename
) const
138 kDebug(1220) << "RemoteImpl::findBaseURL";
140 QString file
= findDesktopFile(filename
);
143 KDesktopFile
desktop( file
);
144 return desktop
.readUrl();
151 void RemoteImpl::createTopLevelEntry(KIO::UDSEntry
&entry
) const
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()
166 KService::Ptr service
= KService::serviceByDesktopName(WIZARD_SERVICE
);
168 if (service
&& service
->isValid())
170 url
.setPath( KStandardDirs::locate("apps",
171 service
->entryPath())
178 bool RemoteImpl::createWizardEntry(KIO::UDSEntry
&entry
) const
182 KUrl url
= findWizardRealURL();
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"));
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
;
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
;
238 if (findDirectory(filename
+".desktop", directory
))
240 createEntry(entry
, directory
, filename
+".desktop");
247 bool RemoteImpl::deleteNetworkFolder(const QString
&filename
) const
249 kDebug(1220) << "RemoteImpl::deleteNetworkFolder: " << filename
;
252 if (findDirectory(filename
+".desktop", directory
))
254 kDebug(1220) << "Removing " << directory
<< filename
<< ".desktop";
255 return QFile::remove(directory
+filename
+".desktop");
261 bool RemoteImpl::renameFolders(const QString
&src
, const QString
&dest
,
262 bool overwrite
) const
264 kDebug(1220) << "RemoteImpl::renameFolders: "
265 << src
<< ", " << dest
<< endl
;
268 if (findDirectory(src
+".desktop", directory
))
270 if (!overwrite
&& QFile::exists(directory
+dest
+".desktop"))
275 kDebug(1220) << "Renaming " << directory
<< src
<< ".desktop";
277 bool res
= dir
.rename(src
+".desktop", dest
+".desktop");
280 KDesktopFile
desktop(directory
+dest
+".desktop");
281 desktop
.desktopGroup().writeEntry("Name", dest
);