add more spacing
[personal-kdebase.git] / runtime / kioslave / desktop / kio_desktop.cpp
blob4859709d4fd63bbc26cef37e1afbc920122ae3d1
1 /* This file is part of the KDE project
2 Copyright (C) 2008 Fredrik Höglund <fredrik@kde.org>
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 "kio_desktop.h"
22 #include <KApplication>
23 #include <KCmdLineArgs>
24 #include <KConfigGroup>
25 #include <KDesktopFile>
26 #include <KGlobalSettings>
27 #include <KStandardDirs>
29 #include <kio/udsentry.h>
31 #include <QFile>
32 #include <QDBusInterface>
33 #include <QDesktopServices>
34 #include <QDir>
36 extern "C"
38 int KDE_EXPORT kdemain(int argc, char **argv)
40 // necessary to use other kio slaves
41 QCoreApplication app(argc, argv);
42 KComponentData("kio_desktop", "kdelibs4");
43 KGlobal::locale();
45 // start the slave
46 DesktopProtocol slave(argv[1], argv[2], argv[3]);
47 slave.dispatchLoop();
48 return 0;
52 DesktopProtocol::DesktopProtocol(const QByteArray& protocol, const QByteArray &pool, const QByteArray &app)
53 : KIO::ForwardingSlaveBase(protocol, pool, app)
55 checkLocalInstall();
57 QDBusInterface kded("org.kde.kded", "/kded", "org.kde.kded");
58 kded.call("loadModule", "desktopnotifier");
61 DesktopProtocol::~DesktopProtocol()
65 void DesktopProtocol::checkLocalInstall()
67 #ifndef Q_WS_WIN
68 // We can't use KGlobalSettings::desktopPath() here, since it returns the home dir
69 // if the desktop folder doesn't exist.
70 QString desktopPath = QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
71 if (desktopPath.isEmpty())
72 desktopPath = QDir::homePath() + "/Desktop";
74 const QDir desktopDir(desktopPath);
75 bool desktopIsEmpty;
76 bool newRelease;
78 // Check if we have a new KDE release
79 KConfig config("kio_desktoprc");
80 KConfigGroup cg(&config, "General");
81 QString version = cg.readEntry("Version", "0.0.0");
82 int major = version.section('.', 0, 0).toInt();
83 int minor = version.section('.', 1, 1).toInt();
84 int release = version.section('.', 2, 2).toInt();
86 if (KDE_MAKE_VERSION(major, minor, release) < KDE::version()) {
87 const QString version = QString::number(KDE::versionMajor()) + '.' +
88 QString::number(KDE::versionMinor()) + '.' +
89 QString::number(KDE::versionRelease());
90 cg.writeEntry("Version", version);
91 newRelease = true;
92 } else
93 newRelease = false;
95 // Create the desktop folder if it doesn't exist
96 if (!desktopDir.exists()) {
97 ::mkdir(QFile::encodeName(desktopPath), S_IRWXU);
98 desktopIsEmpty = true;
99 } else
100 desktopIsEmpty = desktopDir.entryList(QDir::AllEntries | QDir::Hidden | QDir::NoDotAndDotDot).isEmpty();
102 if (desktopIsEmpty) {
103 // Copy the .directory file
104 QFile::copy(KStandardDirs::locate("data", "kio_desktop/directory.desktop"),
105 desktopPath + "/.directory");
107 // Copy the trash link
108 QFile::copy(KStandardDirs::locate("data", "kio_desktop/directory.trash"),
109 desktopPath + "/trash.desktop");
111 // Copy the desktop links
112 const QStringList links = KGlobal::dirs()->findAllResources("data", "kio_desktop/DesktopLinks/*",
113 KStandardDirs::NoDuplicates);
114 foreach (const QString &link, links) {
115 KDesktopFile file(link);
116 if (!file.desktopGroup().readEntry("Hidden", false))
117 QFile::copy(link, desktopPath + link.mid(link.lastIndexOf('/')));
119 } else if (newRelease) {
120 // Update the icon name in the .directory file to the FDO naming spec
121 const QString directoryFile = desktopPath + "/.directory";
122 if (QFile::exists(directoryFile)) {
123 KDesktopFile file(directoryFile);
124 if (file.readIcon() == "desktop")
125 file.desktopGroup().writeEntry("Icon", "user-desktop");
126 } else
127 QFile::copy(KStandardDirs::locate("data", "kio_desktop/directory.desktop"), directoryFile);
129 // Update the home icon to the FDO naming spec
130 const QString homeLink = desktopPath + "/Home.desktop";
131 if (QFile::exists(homeLink)) {
132 KDesktopFile home(homeLink);
133 const QString icon = home.readIcon();
134 if (icon == "kfm_home" || icon == "folder_home")
135 home.desktopGroup().writeEntry("Icon", "user-home");
138 // Update the trash icon to the FDO naming spec
139 const QString trashLink = desktopPath + "/trash.desktop";
140 if (QFile::exists(trashLink)) {
141 KDesktopFile trash(trashLink);
142 if (trash.readIcon() == "trashcan_full")
143 trash.desktopGroup().writeEntry("Icon", "user-trash-full");
144 if (trash.desktopGroup().readEntry("EmptyIcon") == "trashcan_empty")
145 trash.desktopGroup().writeEntry("EmptyIcon", "user-trash");
148 #endif
151 bool DesktopProtocol::rewriteUrl(const KUrl &url, KUrl &newUrl)
153 newUrl.setProtocol("file");
154 newUrl.setPath(KGlobalSettings::desktopPath());
155 newUrl.addPath(url.path());
157 return true;
160 QString DesktopProtocol::desktopFile(KIO::UDSEntry &entry) const
162 const QString name = entry.stringValue(KIO::UDSEntry::UDS_NAME);
163 if (name == "." || name == "..")
164 return QString();
166 KUrl url = processedUrl();
167 url.addPath(name);
169 if (entry.isDir()) {
170 url.addPath(".directory");
171 if (!KStandardDirs::exists(url.path()))
172 return QString();
174 return url.path();
177 if (KDesktopFile::isDesktopFile(url.path()))
178 return url.path();
180 return QString();
183 void DesktopProtocol::prepareUDSEntry(KIO::UDSEntry &entry, bool listing) const
185 ForwardingSlaveBase::prepareUDSEntry(entry, listing);
186 const QString path = desktopFile(entry);
188 if (!path.isEmpty()) {
189 KDesktopFile file(path);
191 const QString name = file.readName();
192 if (!name.isEmpty())
193 entry.insert(KIO::UDSEntry::UDS_DISPLAY_NAME, name);
195 if (file.noDisplay())
196 entry.insert(KIO::UDSEntry::UDS_HIDDEN, 1);
199 // Set the target URL to the local path
200 entry.insert(KIO::UDSEntry::UDS_TARGET_URL, entry.stringValue(KIO::UDSEntry::UDS_LOCAL_PATH));
203 void DesktopProtocol::rename(const KUrl &src, const KUrl &dest, KIO::JobFlags flags)
205 KUrl url;
206 rewriteUrl(src, url);
208 if (src.protocol() != "desktop" || dest.protocol() != "desktop" ||
209 !KDesktopFile::isDesktopFile(url.path()))
211 ForwardingSlaveBase::rename(src, dest, flags);
212 return;
215 // Instead of renaming the .desktop file, we'll change the value of the
216 // Name key in the file.
217 KDesktopFile file(url.path());
218 file.desktopGroup().writeEntry("Name", dest.fileName());
219 file.sync();
220 finished();