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., 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   QString englishName = readEntryUntranslated("Name");
00161   QString translateName = readEntry("Name");
00162   if(englishName == translateName)
00163   {
00164     KGlobal::locale()->insertCatalogue("menu-messages");
00165     QString newName = i18n(englishName.latin1());
00166     return newName;
00167   }
00168   else
00169     return translateName;
00170 }
00171 
00172 QString KDesktopFile::readComment() const
00173 {
00174   return readEntry("Comment");
00175 }
00176 
00177 QString KDesktopFile::readGenericName() const
00178 {
00179   return readEntry("GenericName");
00180 }
00181 
00182 QString KDesktopFile::readPath() const
00183 {
00184   return readPathEntry("Path");
00185 }
00186 
00187 QString KDesktopFile::readDevice() const
00188 {
00189   return readEntry("Dev");
00190 }
00191 
00192 QString KDesktopFile::readURL() const
00193 {
00194     if (hasDeviceType()) {
00195         QString device = readDevice();
00196         KMountPoint::List mountPoints = KMountPoint::possibleMountPoints();
00197     
00198         for(KMountPoint::List::ConstIterator it = mountPoints.begin();
00199             it != mountPoints.end(); ++it)
00200         {
00201             KMountPoint *mp = *it;
00202             if (mp->mountedFrom() == device)
00203             {
00204                 KURL u;
00205                 u.setPath( mp->mountPoint() );
00206                 return u.url();
00207             }
00208         }
00209         return QString::null;
00210     } else {
00211     QString url = readPathEntry("URL");
00212         if ( !url.isEmpty() && !QDir::isRelativePath(url) )
00213         {
00214             // Handle absolute paths as such (i.e. we need to escape them)
00215             KURL u;
00216             u.setPath( url );
00217             return u.url();
00218         }
00219         return url;
00220     }
00221 }
00222 
00223 QStringList KDesktopFile::readActions() const
00224 {
00225     return readListEntry("Actions", ';');
00226 }
00227 
00228 void KDesktopFile::setActionGroup(const QString &group)
00229 {
00230     setGroup(QString::fromLatin1("Desktop Action ") + group);
00231 }
00232 
00233 bool KDesktopFile::hasActionGroup(const QString &group) const
00234 {
00235   return hasGroup(QString::fromLatin1("Desktop Action ") + group);
00236 }
00237 
00238 bool KDesktopFile::hasLinkType() const
00239 {
00240   return readEntry("Type") == QString::fromLatin1("Link");
00241 }
00242 
00243 bool KDesktopFile::hasApplicationType() const
00244 {
00245   return readEntry("Type") == QString::fromLatin1("Application");
00246 }
00247 
00248 bool KDesktopFile::hasMimeTypeType() const
00249 {
00250   return readEntry("Type") == QString::fromLatin1("MimeType");
00251 }
00252 
00253 bool KDesktopFile::hasDeviceType() const
00254 {
00255   return readEntry("Type") == QString::fromLatin1("FSDev") ||
00256          readEntry("Type") == QString::fromLatin1("FSDevice");
00257 }
00258 
00259 bool KDesktopFile::tryExec() const
00260 {
00261   // Test for TryExec and "X-KDE-AuthorizeAction" 
00262   QString te = readPathEntry("TryExec");
00263 
00264   if (!te.isEmpty()) {
00265     if (!QDir::isRelativePath(te)) {
00266       if (::access(QFile::encodeName(te), X_OK))
00267     return false;
00268     } else {
00269       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00270       // Environment PATH may contain filenames in 8bit locale cpecified
00271       // encoding (Like a filenames).
00272       QStringList dirs = QStringList::split(':', QFile::decodeName(::getenv("PATH")));
00273       QStringList::Iterator it(dirs.begin());
00274       bool match = false;
00275       for (; it != dirs.end(); ++it) {
00276     QString fName = *it + "/" + te;
00277     if (::access(QFile::encodeName(fName), X_OK) == 0)
00278     {
00279       match = true;
00280       break;
00281     }
00282       }
00283       // didn't match at all
00284       if (!match)
00285         return false;
00286     }
00287   }
00288   QStringList list = readListEntry("X-KDE-AuthorizeAction");
00289   if (kapp && !list.isEmpty())
00290   {
00291      for(QStringList::ConstIterator it = list.begin();
00292          it != list.end();
00293          ++it)
00294      {
00295         if (!kapp->authorize((*it).stripWhiteSpace()))
00296            return false;
00297      }
00298   }
00299   
00300   // See also KService::username()
00301   bool su = readBoolEntry("X-KDE-SubstituteUID");
00302   if (su)
00303   {
00304       QString user = readEntry("X-KDE-Username");
00305       if (user.isEmpty())
00306         user = ::getenv("ADMIN_ACCOUNT");
00307       if (user.isEmpty())
00308         user = "root";
00309       if (!kapp->authorize("user/"+user))
00310         return false;
00311   }
00312   
00313   return true;
00314 }
00315 
00319 QString
00320 KDesktopFile::fileName() const { return backEnd->fileName(); }
00321 
00325 QString
00326 KDesktopFile::resource() const { return backEnd->resource(); }
00327 
00328 QStringList
00329 KDesktopFile::sortOrder() const
00330 {
00331   return readListEntry("SortOrder");
00332 }
00333 
00334 void KDesktopFile::virtual_hook( int id, void* data )
00335 { KConfig::virtual_hook( id, data ); }
00336 
00337 QString KDesktopFile::readDocPath() const
00338 {
00339   // Depreciated, remove in KDE4 or 5?
00340   // See: http://www.freedesktop.org/Standards/desktop-entry-spec
00341   if(hasKey( "DocPath" ))
00342     return readPathEntry( "DocPath" );
00343 
00344   return readPathEntry( "X-DocPath" );
00345 }
00346 
00347 KDesktopFile* KDesktopFile::copyTo(const QString &file) const
00348 {
00349   KDesktopFile *config = new KDesktopFile(QString::null, false);
00350   KConfig::copyTo(file, config);
00351   config->setDesktopGroup();
00352   return config;
00353 }
00354 
00355 
KDE Home | KDE Accessibility Home | Description of Access Keys