• Skip to content
  • Skip to link menu
KDE 4.1 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 
00028 #include <config.h>
00029 #include "kconfig_p.h"
00030 #include "kdebug.h"
00031 #include "kurl.h"
00032 #include "kconfiggroup.h"
00033 #include "kauthorized.h"
00034 #include "kstandarddirs.h"
00035 #include "kconfigini_p.h"
00036 #include "kde_file.h"
00037 
00038 class KDesktopFilePrivate : public KConfigPrivate
00039 {
00040  public:
00041     KDesktopFilePrivate(const char * resourceType, const QString &fileName);
00042     KConfigGroup desktopGroup;
00043 };
00044 
00045 KDesktopFilePrivate::KDesktopFilePrivate(const char * resourceType, const QString &fileName)
00046     : KConfigPrivate(KGlobal::mainComponent(), KConfig::NoGlobals, resourceType)
00047 {
00048     mBackend = new KConfigIniBackend();
00049     bDynamicBackend = false;
00050     changeFileName(fileName, resourceType);
00051 }
00052 
00053 KDesktopFile::KDesktopFile(const char * resourceType, const QString &fileName)
00054     : KConfig(*new KDesktopFilePrivate(resourceType, fileName))
00055 {
00056     Q_D(KDesktopFile);
00057     reparseConfiguration();
00058     d->desktopGroup = KConfigGroup(this, "Desktop Entry");
00059 //    setGroup("Desktop Entry"); // for backwards compat
00060 }
00061 
00062 KDesktopFile::KDesktopFile(const QString &fileName)
00063     : KConfig(*new KDesktopFilePrivate("apps", fileName))
00064 {
00065     Q_D(KDesktopFile);
00066     reparseConfiguration();
00067 
00068     d->desktopGroup = KConfigGroup(this, "Desktop Entry");
00069 //    setGroup("Desktop Entry"); // for backwards compat
00070 }
00071 
00072 KDesktopFile::~KDesktopFile()
00073 {
00074 }
00075 
00076 KConfigGroup KDesktopFile::desktopGroup() const
00077 {
00078     Q_D(const KDesktopFile);
00079     return d->desktopGroup;
00080 }
00081 
00082 QString KDesktopFile::locateLocal(const QString &path)
00083 {
00084   QString local;
00085   if (path.endsWith(".directory"))
00086   {
00087     local = path;
00088     if (!QDir::isRelativePath(local))
00089     {
00090       // Relative wrt apps?
00091       local = KGlobal::dirs()->relativeLocation("apps", path);
00092     }
00093 
00094     if (QDir::isRelativePath(local))
00095     {
00096       local = KStandardDirs::locateLocal("apps", local); // Relative to apps
00097     }
00098     else
00099     {
00100       // XDG Desktop menu items come with absolute paths, we need to
00101       // extract their relative path and then build a local path.
00102       local = KGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
00103       if (!QDir::isRelativePath(local))
00104       {
00105         // Hm, that didn't work...
00106         // What now? Use filename only and hope for the best.
00107         local = path.mid(path.lastIndexOf('/')+1);
00108       }
00109       local = KStandardDirs::locateLocal("xdgdata-dirs", local);
00110     }
00111   }
00112   else
00113   {
00114     if (QDir::isRelativePath(path))
00115     {
00116       local = KStandardDirs::locateLocal("apps", path); // Relative to apps
00117     }
00118     else
00119     {
00120       // XDG Desktop menu items come with absolute paths, we need to
00121       // extract their relative path and then build a local path.
00122       local = KGlobal::dirs()->relativeLocation("xdgdata-apps", path);
00123       if (!QDir::isRelativePath(local))
00124       {
00125         // What now? Use filename only and hope for the best.
00126         local = path.mid(path.lastIndexOf('/')+1);
00127       }
00128       local = KStandardDirs::locateLocal("xdgdata-apps", local);
00129     }
00130   }
00131   return local;
00132 }
00133 
00134 bool KDesktopFile::isDesktopFile(const QString& path)
00135 {
00136   return (path.length() > 8
00137           && path.endsWith(QLatin1String(".desktop")));
00138 }
00139 
00140 bool KDesktopFile::isAuthorizedDesktopFile(const QString& path)
00141 {
00142   if (KAuthorized::authorize("run_desktop_files"))
00143      return true;
00144 
00145   if (path.isEmpty())
00146      return false; // Empty paths are not ok.
00147 
00148   if (QDir::isRelativePath(path))
00149      return true; // Relative paths are ok.
00150 
00151   KStandardDirs *dirs = KGlobal::dirs();
00152   if (QDir::isRelativePath( dirs->relativeLocation("apps", path) ))
00153      return true;
00154   if (QDir::isRelativePath( dirs->relativeLocation("xdgdata-apps", path) ))
00155      return true;
00156   if (QDir::isRelativePath( dirs->relativeLocation("services", path) ))
00157      return true;
00158   if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
00159      return true;
00160 
00161   kWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
00162   return false;
00163 }
00164 
00165 QString KDesktopFile::readType() const
00166 {
00167   Q_D(const KDesktopFile);
00168   return d->desktopGroup.readEntry("Type", QString());
00169 }
00170 
00171 QString KDesktopFile::readIcon() const
00172 {
00173   Q_D(const KDesktopFile);
00174   return d->desktopGroup.readEntry("Icon", QString());
00175 }
00176 
00177 QString KDesktopFile::readName() const
00178 {
00179   Q_D(const KDesktopFile);
00180   return d->desktopGroup.readEntry("Name", QString());
00181 }
00182 
00183 QString KDesktopFile::readComment() const
00184 {
00185   Q_D(const KDesktopFile);
00186   return d->desktopGroup.readEntry("Comment", QString());
00187 }
00188 
00189 QString KDesktopFile::readGenericName() const
00190 {
00191   Q_D(const KDesktopFile);
00192   return d->desktopGroup.readEntry("GenericName", QString());
00193 }
00194 
00195 QString KDesktopFile::readPath() const
00196 {
00197   Q_D(const KDesktopFile);
00198   // NOT readPathEntry, it is not XDG-compliant. Path entries written by
00199   // KDE4 will be still treated as such, though.
00200   return d->desktopGroup.readEntry("Path", QString());
00201 }
00202 
00203 QString KDesktopFile::readDevice() const
00204 {
00205   Q_D(const KDesktopFile);
00206   return d->desktopGroup.readEntry("Dev", QString());
00207 }
00208 
00209 QString KDesktopFile::readUrl() const
00210 {
00211     Q_D(const KDesktopFile);
00212     if (hasDeviceType()) {
00213         return d->desktopGroup.readEntry("MountPoint", QString());
00214     } else {
00215         // NOT readPathEntry (see readPath())
00216         QString url = d->desktopGroup.readEntry("URL", QString());
00217         if ( !url.isEmpty() && !QDir::isRelativePath(url) )
00218         {
00219             // Handle absolute paths as such (i.e. we need to escape them)
00220             return KUrl(url).url();
00221         }
00222         return url;
00223     }
00224 }
00225 
00226 QStringList KDesktopFile::readActions() const
00227 {
00228     Q_D(const KDesktopFile);
00229     return d->desktopGroup.readXdgListEntry("Actions");
00230 }
00231 
00232 KConfigGroup KDesktopFile::actionGroup(const QString &group)
00233 {
00234     return KConfigGroup(this, QLatin1String("Desktop Action ") + group);
00235 }
00236 
00237 const KConfigGroup KDesktopFile::actionGroup(const QString& group) const
00238 {
00239     return const_cast<KDesktopFile*>(this)->actionGroup(group);
00240 }
00241 
00242 bool KDesktopFile::hasActionGroup(const QString &group) const
00243 {
00244   return hasGroup((QLatin1String("Desktop Action ") + group).toUtf8().constData());
00245 }
00246 
00247 bool KDesktopFile::hasLinkType() const
00248 {
00249   return readType() == QLatin1String("Link");
00250 }
00251 
00252 bool KDesktopFile::hasApplicationType() const
00253 {
00254   return readType() == QLatin1String("Application");
00255 }
00256 
00257 bool KDesktopFile::hasMimeTypeType() const
00258 {
00259   return readType() == QLatin1String("MimeType");
00260 }
00261 
00262 bool KDesktopFile::hasDeviceType() const
00263 {
00264   return readType() == QLatin1String("FSDevice");
00265 }
00266 
00267 bool KDesktopFile::tryExec() const
00268 {
00269   Q_D(const KDesktopFile);
00270   // Test for TryExec and "X-KDE-AuthorizeAction"
00271   // NOT readPathEntry (see readPath())
00272   QString te = d->desktopGroup.readEntry("TryExec", QString());
00273 
00274   if (!te.isEmpty()) {
00275     if (!QDir::isRelativePath(te)) {
00276 #ifdef Q_WS_WIN /* FIXME read below (js) */
00277         struct stat st;
00278         if (KDE_stat(QFile::encodeName(te), &st) == 0
00279           && (st.st_mode & S_IXUSR))
00280 #else
00281       if (::access(QFile::encodeName(te), X_OK))
00282 #endif
00283         return false;
00284     } else {
00285       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00286       // Environment PATH may contain filenames in 8bit locale specified
00287       // encoding (Like a filenames).
00288       const QStringList dirs = QFile::decodeName(qgetenv("PATH"))
00289         .split(KPATH_SEPARATOR,QString::SkipEmptyParts);
00290       QStringList::ConstIterator it(dirs.begin());
00291       bool match = false;
00292       for (; it != dirs.end(); ++it) {
00293         QString fName = *it + KDIR_SEPARATOR + te;
00294 /* FIXME (js) todo: use ACL winapi because access(..,X_OK) is not available - asserts for msvc>=2k5;
00295         in the meantime more costly KDE_stat is used...
00296 */
00297 #ifdef Q_WS_WIN
00298         struct stat st;
00299         if (KDE_stat(QFile::encodeName(fName), &st) == 0
00300           && (st.st_mode & S_IXUSR))
00301 #else
00302         if (::access(QFile::encodeName(fName).constData(), X_OK) == 0)
00303 #endif
00304         {
00305           match = true;
00306           break;
00307         }
00308       }
00309       // didn't match at all
00310       if (!match)
00311         return false;
00312     }
00313   }
00314   const QStringList list = d->desktopGroup.readEntry("X-KDE-AuthorizeAction", QStringList());
00315   if (!list.isEmpty())
00316   {
00317      for(QStringList::ConstIterator it = list.begin();
00318          it != list.end();
00319          ++it)
00320      {
00321         if (!KAuthorized::authorize((*it).trimmed()))
00322            return false;
00323      }
00324   }
00325 
00326   // See also KService::username()
00327   bool su = d->desktopGroup.readEntry("X-KDE-SubstituteUID", false);
00328   if (su)
00329   {
00330       QString user = d->desktopGroup.readEntry("X-KDE-Username", QString());
00331       if (user.isEmpty())
00332         user = QString::fromLocal8Bit(qgetenv("ADMIN_ACCOUNT"));
00333       if (user.isEmpty())
00334         user = "root";
00335       if (!KAuthorized::authorize("user/"+user))
00336         return false;
00337   }
00338 
00339   return true;
00340 }
00341 
00345 //QString KDesktopFile::fileName() const { return backEnd->fileName(); }
00346 
00350 //QString
00351 //KDesktopFile::resource() const { return backEnd->resource(); }
00352 
00353 QStringList
00354 KDesktopFile::sortOrder() const
00355 {
00356   Q_D(const KDesktopFile);
00357   return d->desktopGroup.readEntry("SortOrder", QStringList());
00358 }
00359 
00360 //void KDesktopFile::virtual_hook( int id, void* data )
00361 //{ KConfig::virtual_hook( id, data ); }
00362 
00363 QString KDesktopFile::readDocPath() const
00364 {
00365   Q_D(const KDesktopFile);
00366   //legacy entry in kde3 apps
00367   if(d->desktopGroup.hasKey( "DocPath" ))
00368     return d->desktopGroup.readPathEntry( "DocPath", QString() );
00369   return d->desktopGroup.readPathEntry( "X-DocPath", QString() );
00370 }
00371 
00372 KDesktopFile* KDesktopFile::copyTo(const QString &file) const
00373 {
00374   KDesktopFile *config = new KDesktopFile(QString());
00375   this->KConfig::copyTo(file, config);
00376 //  config->setDesktopGroup();
00377   return config;
00378 }
00379 
00380 const char *KDesktopFile::resource() const
00381 {
00382     Q_D(const KDesktopFile);
00383     return d->resourceType;
00384 }
00385 
00386 QString KDesktopFile::fileName() const
00387 {
00388     return name();
00389 }
00390 
00391 bool KDesktopFile::noDisplay() const
00392 {
00393     Q_D(const KDesktopFile);
00394     if (d->desktopGroup.readEntry("NoDisplay", false)) {
00395         return true;
00396     }
00397     if (d->desktopGroup.hasKey("OnlyShowIn")) {
00398         if (!d->desktopGroup.readXdgListEntry("OnlyShowIn").contains("KDE"))
00399             return true;
00400     }
00401     if (d->desktopGroup.hasKey("NotShowIn")) {
00402         if (d->desktopGroup.readXdgListEntry("NotShowIn").contains("KDE"))
00403             return true;
00404     }
00405     return false;
00406 }

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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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