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