• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • 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 // $Id: kdesktopfile.cpp 465272 2005-09-29 09:47:40Z mueller $
00023 
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 
00027 #include <qfile.h>
00028 #include <qdir.h>
00029 #include <qtextstream.h>
00030 
00031 #include <kdebug.h>
00032 #include "kurl.h"
00033 #include "kconfigbackend.h"
00034 #include "kapplication.h"
00035 #include "kstandarddirs.h"
00036 #include "kmountpoint.h"
00037 
00038 #include "kdesktopfile.h"
00039 #include "kdesktopfile.moc"
00040 
00041 KDesktopFile::KDesktopFile(const QString &fileName, bool bReadOnly,
00042                const char * resType)
00043   : KConfig(QString::fromLatin1(""), bReadOnly, false)
00044 {
00045   // KConfigBackEnd will try to locate the filename that is provided
00046   // based on the resource type specified, _only_ if the filename
00047   // is not an absolute path.
00048   backEnd->changeFileName(fileName, resType, false);
00049   setReadOnly(bReadOnly);
00050   reparseConfiguration();
00051   setDesktopGroup();
00052 }
00053 
00054 KDesktopFile::~KDesktopFile()
00055 {
00056   // no need to do anything
00057 }
00058 
00059 QString KDesktopFile::locateLocal(const QString &path)
00060 {
00061   QString local;
00062   if (path.endsWith(".directory"))
00063   {
00064     local = path;
00065     if (!QDir::isRelativePath(local))
00066     {
00067       // Relative wrt apps?
00068       local = KGlobal::dirs()->relativeLocation("apps", path);
00069     }
00070 
00071     if (QDir::isRelativePath(local))
00072     {
00073       local = ::locateLocal("apps", local); // Relative to apps
00074     }
00075     else
00076     {
00077       // XDG Desktop menu items come with absolute paths, we need to 
00078       // extract their relative path and then build a local path.
00079       local = KGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
00080       if (!QDir::isRelativePath(local))
00081       {
00082         // Hm, that didn't work...
00083         // What now? Use filename only and hope for the best.
00084         local = path.mid(path.findRev('/')+1);
00085       }
00086       local = ::locateLocal("xdgdata-dirs", local);
00087     }
00088   }
00089   else
00090   {
00091     if (QDir::isRelativePath(path))
00092     {
00093       local = ::locateLocal("apps", path); // Relative to apps
00094     }
00095     else
00096     {
00097       // XDG Desktop menu items come with absolute paths, we need to 
00098       // extract their relative path and then build a local path.
00099       local = KGlobal::dirs()->relativeLocation("xdgdata-apps", path);
00100       if (!QDir::isRelativePath(local))
00101       {
00102         // What now? Use filename only and hope for the best.
00103         local = path.mid(path.findRev('/')+1);
00104       }
00105       local = ::locateLocal("xdgdata-apps", local);
00106     }
00107   }
00108   return local;
00109 }
00110 
00111 bool KDesktopFile::isDesktopFile(const QString& path)
00112 {
00113   int len = path.length();
00114 
00115   if(len > 8 && path.right(8) == QString::fromLatin1(".desktop"))
00116     return true;
00117   else if(len > 7 && path.right(7) == QString::fromLatin1(".kdelnk"))
00118     return true;
00119   else
00120     return false;
00121 }
00122 
00123 bool KDesktopFile::isAuthorizedDesktopFile(const QString& path)
00124 {
00125   if (!kapp || kapp->authorize("run_desktop_files"))
00126      return true;
00127 
00128   if (path.isEmpty())
00129      return false; // Empty paths are not ok.
00130   
00131   if (QDir::isRelativePath(path))
00132      return true; // Relative paths are ok.
00133      
00134   KStandardDirs *dirs = KGlobal::dirs();
00135   if (QDir::isRelativePath( dirs->relativeLocation("apps", path) ))
00136      return true;
00137   if (QDir::isRelativePath( dirs->relativeLocation("xdgdata-apps", path) ))
00138      return true;
00139   if (QDir::isRelativePath( dirs->relativeLocation("services", path) ))
00140      return true;
00141   if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
00142      return true;
00143      
00144   kdWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
00145   return false;
00146 }
00147 
00148 QString KDesktopFile::readType() const
00149 {
00150   return readEntry("Type");
00151 }
00152 
00153 QString KDesktopFile::readIcon() const
00154 {
00155   return readEntry("Icon");
00156 }
00157 
00158 QString KDesktopFile::readName() const
00159 {
00160   return readEntry("Name");
00161 }
00162 
00163 QString KDesktopFile::readComment() const
00164 {
00165   return readEntry("Comment");
00166 }
00167 
00168 QString KDesktopFile::readGenericName() const
00169 {
00170   return readEntry("GenericName");
00171 }
00172 
00173 QString KDesktopFile::readPath() const
00174 {
00175   return readPathEntry("Path");
00176 }
00177 
00178 QString KDesktopFile::readDevice() const
00179 {
00180   return readEntry("Dev");
00181 }
00182 
00183 QString KDesktopFile::readURL() const
00184 {
00185     if (hasDeviceType()) {
00186         QString device = readDevice();
00187         KMountPoint::List mountPoints = KMountPoint::possibleMountPoints();
00188     
00189         for(KMountPoint::List::ConstIterator it = mountPoints.begin();
00190             it != mountPoints.end(); ++it)
00191         {
00192             KMountPoint *mp = *it;
00193             if (mp->mountedFrom() == device)
00194             {
00195                 KURL u;
00196                 u.setPath( mp->mountPoint() );
00197                 return u.url();
00198             }
00199         }
00200         return QString::null;
00201     } else {
00202     QString url = readPathEntry("URL");
00203         if ( !url.isEmpty() && !QDir::isRelativePath(url) )
00204         {
00205             // Handle absolute paths as such (i.e. we need to escape them)
00206             KURL u;
00207             u.setPath( url );
00208             return u.url();
00209         }
00210         return url;
00211     }
00212 }
00213 
00214 QStringList KDesktopFile::readActions() const
00215 {
00216     return readListEntry("Actions", ';');
00217 }
00218 
00219 void KDesktopFile::setActionGroup(const QString &group)
00220 {
00221     setGroup(QString::fromLatin1("Desktop Action ") + group);
00222 }
00223 
00224 bool KDesktopFile::hasActionGroup(const QString &group) const
00225 {
00226   return hasGroup(QString::fromLatin1("Desktop Action ") + group);
00227 }
00228 
00229 bool KDesktopFile::hasLinkType() const
00230 {
00231   return readEntry("Type") == QString::fromLatin1("Link");
00232 }
00233 
00234 bool KDesktopFile::hasApplicationType() const
00235 {
00236   return readEntry("Type") == QString::fromLatin1("Application");
00237 }
00238 
00239 bool KDesktopFile::hasMimeTypeType() const
00240 {
00241   return readEntry("Type") == QString::fromLatin1("MimeType");
00242 }
00243 
00244 bool KDesktopFile::hasDeviceType() const
00245 {
00246   return readEntry("Type") == QString::fromLatin1("FSDev") ||
00247          readEntry("Type") == QString::fromLatin1("FSDevice");
00248 }
00249 
00250 bool KDesktopFile::tryExec() const
00251 {
00252   // Test for TryExec and "X-KDE-AuthorizeAction" 
00253   QString te = readPathEntry("TryExec");
00254 
00255   if (!te.isEmpty()) {
00256     if (!QDir::isRelativePath(te)) {
00257       if (::access(QFile::encodeName(te), X_OK))
00258     return false;
00259     } else {
00260       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00261       // Environment PATH may contain filenames in 8bit locale cpecified
00262       // encoding (Like a filenames).
00263       QStringList dirs = QStringList::split(':', QFile::decodeName(::getenv("PATH")));
00264       QStringList::Iterator it(dirs.begin());
00265       bool match = false;
00266       for (; it != dirs.end(); ++it) {
00267     QString fName = *it + "/" + te;
00268     if (::access(QFile::encodeName(fName), X_OK) == 0)
00269     {
00270       match = true;
00271       break;
00272     }
00273       }
00274       // didn't match at all
00275       if (!match)
00276         return false;
00277     }
00278   }
00279   QStringList list = readListEntry("X-KDE-AuthorizeAction");
00280   if (kapp && !list.isEmpty())
00281   {
00282      for(QStringList::ConstIterator it = list.begin();
00283          it != list.end();
00284          ++it)
00285      {
00286         if (!kapp->authorize((*it).stripWhiteSpace()))
00287            return false;
00288      }
00289   }
00290   
00291   // See also KService::username()
00292   bool su = readBoolEntry("X-KDE-SubstituteUID");
00293   if (su)
00294   {
00295       QString user = readEntry("X-KDE-Username");
00296       if (user.isEmpty())
00297         user = ::getenv("ADMIN_ACCOUNT");
00298       if (user.isEmpty())
00299         user = "root";
00300       if (!kapp->authorize("user/"+user))
00301         return false;
00302   }
00303   
00304   return true;
00305 }
00306 
00310 QString
00311 KDesktopFile::fileName() const { return backEnd->fileName(); }
00312 
00316 QString
00317 KDesktopFile::resource() const { return backEnd->resource(); }
00318 
00319 QStringList
00320 KDesktopFile::sortOrder() const
00321 {
00322   return readListEntry("SortOrder");
00323 }
00324 
00325 void KDesktopFile::virtual_hook( int id, void* data )
00326 { KConfig::virtual_hook( id, data ); }
00327 
00328 QString KDesktopFile::readDocPath() const
00329 {
00330   // Depreciated, remove in KDE4 or 5?
00331   // See: http://www.freedesktop.org/Standards/desktop-entry-spec
00332   if(hasKey( "DocPath" ))
00333     return readPathEntry( "DocPath" );
00334 
00335   return readPathEntry( "X-DocPath" );
00336 }
00337 
00338 KDesktopFile* KDesktopFile::copyTo(const QString &file) const
00339 {
00340   KDesktopFile *config = new KDesktopFile(QString::null, false);
00341   KConfig::copyTo(file, config);
00342   config->setDesktopGroup();
00343   return config;
00344 }
00345 
00346 

KDECore

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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
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