• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kdesktopfile.cpp

Go to the documentation of this file.
00001 /*
00002   This file is part of the KDE libraries
00003   Copyright (c) 1999 Pietro Iglio <iglio@kde.org>
00004   Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00005 
00006   This library is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This library is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015 
00016   You should have received a copy of the GNU Library General Public License
00017   along with this library; see the file COPYING.LIB.  If not, write to
00018   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019   Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kdesktopfile.h"
00023 
00024 #include <unistd.h>
00025 
00026 #include <QtCore/QDir>
00027 #include <QtCore/QFileInfo>
00028 
00029 #include <config.h>
00030 #include "kconfig_p.h"
00031 #include "kdebug.h"
00032 #include "kurl.h"
00033 #include "kconfiggroup.h"
00034 #include "kauthorized.h"
00035 #include "kstandarddirs.h"
00036 #include "kconfigini_p.h"
00037 #include "kde_file.h"
00038 
00039 class KDesktopFilePrivate : public KConfigPrivate
00040 {
00041  public:
00042     KDesktopFilePrivate(const char * resourceType, const QString &fileName);
00043     KConfigGroup desktopGroup;
00044 };
00045 
00046 KDesktopFilePrivate::KDesktopFilePrivate(const char * resourceType, const QString &fileName)
00047     : KConfigPrivate(KGlobal::mainComponent(), KConfig::NoGlobals, resourceType)
00048 {
00049     mBackend = new KConfigIniBackend();
00050     bDynamicBackend = false;
00051     changeFileName(fileName, resourceType);
00052 }
00053 
00054 KDesktopFile::KDesktopFile(const char * resourceType, const QString &fileName)
00055     : KConfig(*new KDesktopFilePrivate(resourceType, fileName))
00056 {
00057     Q_D(KDesktopFile);
00058     reparseConfiguration();
00059     d->desktopGroup = KConfigGroup(this, "Desktop Entry");
00060 //    setGroup("Desktop Entry"); // for backwards compat
00061 }
00062 
00063 KDesktopFile::KDesktopFile(const QString &fileName)
00064     : KConfig(*new KDesktopFilePrivate("apps", fileName))
00065 {
00066     Q_D(KDesktopFile);
00067     reparseConfiguration();
00068 
00069     d->desktopGroup = KConfigGroup(this, "Desktop Entry");
00070 //    setGroup("Desktop Entry"); // for backwards compat
00071 }
00072 
00073 KDesktopFile::~KDesktopFile()
00074 {
00075 }
00076 
00077 KConfigGroup KDesktopFile::desktopGroup() const
00078 {
00079     Q_D(const KDesktopFile);
00080     return d->desktopGroup;
00081 }
00082 
00083 QString KDesktopFile::locateLocal(const QString &path)
00084 {
00085   QString local;
00086   if (path.endsWith(".directory"))
00087   {
00088     local = path;
00089     if (!QDir::isRelativePath(local))
00090     {
00091       // Relative wrt apps?
00092       local = KGlobal::dirs()->relativeLocation("apps", path);
00093     }
00094 
00095     if (QDir::isRelativePath(local))
00096     {
00097       local = KStandardDirs::locateLocal("apps", local); // Relative to apps
00098     }
00099     else
00100     {
00101       // XDG Desktop menu items come with absolute paths, we need to
00102       // extract their relative path and then build a local path.
00103       local = KGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
00104       if (!QDir::isRelativePath(local))
00105       {
00106         // Hm, that didn't work...
00107         // What now? Use filename only and hope for the best.
00108         local = path.mid(path.lastIndexOf('/')+1);
00109       }
00110       local = KStandardDirs::locateLocal("xdgdata-dirs", local);
00111     }
00112   }
00113   else
00114   {
00115     if (QDir::isRelativePath(path))
00116     {
00117       local = KStandardDirs::locateLocal("apps", path); // Relative to apps
00118     }
00119     else
00120     {
00121       // XDG Desktop menu items come with absolute paths, we need to
00122       // extract their relative path and then build a local path.
00123       local = KGlobal::dirs()->relativeLocation("xdgdata-apps", path);
00124       if (!QDir::isRelativePath(local))
00125       {
00126         // What now? Use filename only and hope for the best.
00127         local = path.mid(path.lastIndexOf('/')+1);
00128       }
00129       local = KStandardDirs::locateLocal("xdgdata-apps", local);
00130     }
00131   }
00132   return local;
00133 }
00134 
00135 bool KDesktopFile::isDesktopFile(const QString& path)
00136 {
00137   return (path.length() > 8
00138           && path.endsWith(QLatin1String(".desktop")));
00139 }
00140 
00141 bool KDesktopFile::isAuthorizedDesktopFile(const QString& path)
00142 {
00143   // Explicitly forbid desktop files if Kiosk does not allow.  There
00144   // may be other reasons that desktop files are forbidden so keep
00145   // checking otherwise.
00146   if (!KAuthorized::authorize("run_desktop_files")) {
00147      kWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
00148      return false;
00149   }
00150 
00151   if (path.isEmpty())
00152      return false; // Empty paths are not ok.
00153 
00154   if (QDir::isRelativePath(path))
00155      return true; // Relative paths are ok.
00156 
00157   KStandardDirs *dirs = KGlobal::dirs();
00158   QStringList kdePrefixes;
00159   kdePrefixes += dirs->resourceDirs("apps");
00160   kdePrefixes += dirs->resourceDirs("services");
00161   kdePrefixes += dirs->resourceDirs("xdgdata-apps");
00162   kdePrefixes += dirs->resourceDirs("autostart");
00163 
00164   const QString realPath = KStandardDirs::realPath(path);
00165 
00166   // Check if the .desktop file is installed as part of KDE or XDG.
00167   foreach (const QString &prefix, kdePrefixes) {
00168     if (realPath.startsWith(prefix))
00169       return true;
00170   }
00171 
00172   // Not otherwise permitted, so only allow if the file is executable, or if
00173   // owned by root (uid == 0)
00174   QFileInfo entryInfo( path );
00175   if (entryInfo.isExecutable() || entryInfo.ownerId() == 0)
00176       return true;
00177 
00178   kWarning() << "Access to '" << path << "' denied because of 'non_executable_desktop_file' restriction." << endl;
00179   return false;
00180 }
00181 
00182 QString KDesktopFile::readType() const
00183 {
00184   Q_D(const KDesktopFile);
00185   return d->desktopGroup.readEntry("Type", QString());
00186 }
00187 
00188 QString KDesktopFile::readIcon() const
00189 {
00190   Q_D(const KDesktopFile);
00191   return d->desktopGroup.readEntry("Icon", QString());
00192 }
00193 
00194 QString KDesktopFile::readName() const
00195 {
00196   Q_D(const KDesktopFile);
00197   return d->desktopGroup.readEntry("Name", QString());
00198 }
00199 
00200 QString KDesktopFile::readComment() const
00201 {
00202   Q_D(const KDesktopFile);
00203   return d->desktopGroup.readEntry("Comment", QString());
00204 }
00205 
00206 QString KDesktopFile::readGenericName() const
00207 {
00208   Q_D(const KDesktopFile);
00209   return d->desktopGroup.readEntry("GenericName", QString());
00210 }
00211 
00212 QString KDesktopFile::readPath() const
00213 {
00214   Q_D(const KDesktopFile);
00215   // NOT readPathEntry, it is not XDG-compliant. Path entries written by
00216   // KDE4 will be still treated as such, though.
00217   return d->desktopGroup.readEntry("Path", QString());
00218 }
00219 
00220 QString KDesktopFile::readDevice() const
00221 {
00222   Q_D(const KDesktopFile);
00223   return d->desktopGroup.readEntry("Dev", QString());
00224 }
00225 
00226 QString KDesktopFile::readUrl() const
00227 {
00228     Q_D(const KDesktopFile);
00229     if (hasDeviceType()) {
00230         return d->desktopGroup.readEntry("MountPoint", QString());
00231     } else {
00232         // NOT readPathEntry (see readPath())
00233         QString url = d->desktopGroup.readEntry("URL", QString());
00234         if ( !url.isEmpty() && !QDir::isRelativePath(url) )
00235         {
00236             // Handle absolute paths as such (i.e. we need to escape them)
00237             return KUrl(url).url();
00238         }
00239         return url;
00240     }
00241 }
00242 
00243 QStringList KDesktopFile::readActions() const
00244 {
00245     Q_D(const KDesktopFile);
00246     return d->desktopGroup.readXdgListEntry("Actions");
00247 }
00248 
00249 KConfigGroup KDesktopFile::actionGroup(const QString &group)
00250 {
00251     return KConfigGroup(this, QLatin1String("Desktop Action ") + group);
00252 }
00253 
00254 const KConfigGroup KDesktopFile::actionGroup(const QString& group) const
00255 {
00256     return const_cast<KDesktopFile*>(this)->actionGroup(group);
00257 }
00258 
00259 bool KDesktopFile::hasActionGroup(const QString &group) const
00260 {
00261   return hasGroup((QLatin1String("Desktop Action ") + group).toUtf8().constData());
00262 }
00263 
00264 bool KDesktopFile::hasLinkType() const
00265 {
00266   return readType() == QLatin1String("Link");
00267 }
00268 
00269 bool KDesktopFile::hasApplicationType() const
00270 {
00271   return readType() == QLatin1String("Application");
00272 }
00273 
00274 bool KDesktopFile::hasMimeTypeType() const
00275 {
00276   return readType() == QLatin1String("MimeType");
00277 }
00278 
00279 bool KDesktopFile::hasDeviceType() const
00280 {
00281   return readType() == QLatin1String("FSDevice");
00282 }
00283 
00284 bool KDesktopFile::tryExec() const
00285 {
00286   Q_D(const KDesktopFile);
00287   // Test for TryExec and "X-KDE-AuthorizeAction"
00288   // NOT readPathEntry (see readPath())
00289   QString te = d->desktopGroup.readEntry("TryExec", QString());
00290 
00291   if (!te.isEmpty()) {
00292     if (!QDir::isRelativePath(te)) {
00293       if (KDE::access(te, X_OK))
00294         return false;
00295     } else {
00296       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00297       // Environment PATH may contain filenames in 8bit locale specified
00298       // encoding (Like a filenames).
00299       const QStringList dirs = QFile::decodeName(qgetenv("PATH"))
00300         .split(KPATH_SEPARATOR,QString::SkipEmptyParts);
00301       QStringList::ConstIterator it(dirs.begin());
00302       bool match = false;
00303       for (; it != dirs.end(); ++it) {
00304         QString fName = *it + KDIR_SEPARATOR + te;
00305         if (KDE::access(fName, X_OK) == 0)
00306         {
00307           match = true;
00308           break;
00309         }
00310       }
00311       // didn't match at all
00312       if (!match)
00313         return false;
00314     }
00315   }
00316   const QStringList list = d->desktopGroup.readEntry("X-KDE-AuthorizeAction", QStringList());
00317   if (!list.isEmpty())
00318   {
00319      for(QStringList::ConstIterator it = list.begin();
00320          it != list.end();
00321          ++it)
00322      {
00323         if (!KAuthorized::authorize((*it).trimmed()))
00324            return false;
00325      }
00326   }
00327 
00328   // See also KService::username()
00329   bool su = d->desktopGroup.readEntry("X-KDE-SubstituteUID", false);
00330   if (su)
00331   {
00332       QString user = d->desktopGroup.readEntry("X-KDE-Username", QString());
00333       if (user.isEmpty())
00334         user = QString::fromLocal8Bit(qgetenv("ADMIN_ACCOUNT"));
00335       if (user.isEmpty())
00336         user = "root";
00337       if (!KAuthorized::authorize("user/"+user))
00338         return false;
00339   }
00340 
00341   return true;
00342 }
00343 
00347 //QString KDesktopFile::fileName() const { return backEnd->fileName(); }
00348 
00352 //QString
00353 //KDesktopFile::resource() const { return backEnd->resource(); }
00354 
00355 QStringList
00356 KDesktopFile::sortOrder() const
00357 {
00358   Q_D(const KDesktopFile);
00359   return d->desktopGroup.readEntry("SortOrder", QStringList());
00360 }
00361 
00362 //void KDesktopFile::virtual_hook( int id, void* data )
00363 //{ KConfig::virtual_hook( id, data ); }
00364 
00365 QString KDesktopFile::readDocPath() const
00366 {
00367   Q_D(const KDesktopFile);
00368   //legacy entry in kde3 apps
00369   if(d->desktopGroup.hasKey( "DocPath" ))
00370     return d->desktopGroup.readPathEntry( "DocPath", QString() );
00371   return d->desktopGroup.readPathEntry( "X-DocPath", QString() );
00372 }
00373 
00374 KDesktopFile* KDesktopFile::copyTo(const QString &file) const
00375 {
00376   KDesktopFile *config = new KDesktopFile(QString());
00377   this->KConfig::copyTo(file, config);
00378 //  config->setDesktopGroup();
00379   return config;
00380 }
00381 
00382 const char *KDesktopFile::resource() const
00383 {
00384     Q_D(const KDesktopFile);
00385     return d->resourceType;
00386 }
00387 
00388 QString KDesktopFile::fileName() const
00389 {
00390     return name();
00391 }
00392 
00393 bool KDesktopFile::noDisplay() const
00394 {
00395     Q_D(const KDesktopFile);
00396     if (d->desktopGroup.readEntry("NoDisplay", false)) {
00397         return true;
00398     }
00399     if (d->desktopGroup.hasKey("OnlyShowIn")) {
00400         if (!d->desktopGroup.readXdgListEntry("OnlyShowIn").contains("KDE"))
00401             return true;
00402     }
00403     if (d->desktopGroup.hasKey("NotShowIn")) {
00404         if (d->desktopGroup.readXdgListEntry("NotShowIn").contains("KDE"))
00405             return true;
00406     }
00407     return false;
00408 }

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal