kdecore Library API Documentation

kdesktopfile.cpp

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