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.33 2002/08/16 22:42:13 waba 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 
00035 #include "kdesktopfile.h"
00036 #include "kdesktopfile.moc"
00037 
00038 KDesktopFile::KDesktopFile(const QString &fileName, bool bReadOnly,
00039                const char * resType)
00040   : KConfig(QString::fromLatin1(""), bReadOnly, false)
00041 {
00042   // KConfigBackEnd will try to locate the filename that is provided
00043   // based on the resource type specified, _only_ if the filename
00044   // is not an absolute path.
00045   backEnd->changeFileName(fileName, resType, false);
00046   setReadOnly(bReadOnly);
00047   reparseConfiguration();
00048   setDesktopGroup();
00049 }
00050 
00051 KDesktopFile::~KDesktopFile()
00052 {
00053   // no need to do anything
00054 }
00055 
00056 bool KDesktopFile::isDesktopFile(const QString& path)
00057 {
00058   int len = path.length();
00059 
00060   if(len > 8 && path.right(8) == QString::fromLatin1(".desktop"))
00061     return true;
00062   else if(len > 7 && path.right(7) == QString::fromLatin1(".kdelnk"))
00063     return true;
00064   else
00065     return false;
00066 }
00067 
00068 bool KDesktopFile::isAuthorizedDesktopFile(const QString& path)
00069 {
00070   if (!kapp || kapp->authorize("run_desktop_files"))
00071      return true;
00072 
00073   if (path.isEmpty())
00074      return false; // Empty paths are not ok.
00075   
00076   if (path[0] != '/')
00077      return true; // Relative paths are ok.
00078      
00079   KStandardDirs *dirs = KGlobal::dirs();
00080   if (dirs->relativeLocation("applnk", path)[0] != '/')
00081      return true;
00082   if (dirs->relativeLocation("services", path)[0] != '/')
00083      return true;
00084   if (dirs->relativeLocation("appdata", path).startsWith("kdesktop/Desktop"))
00085      return true;
00086   return false;
00087 }
00088 
00089 QString KDesktopFile::readType() const
00090 {
00091   return readEntry("Type");
00092 }
00093 
00094 QString KDesktopFile::readIcon() const
00095 {
00096   return readEntry("Icon");
00097 }
00098 
00099 QString KDesktopFile::readName() const
00100 {
00101   return readEntry("Name");
00102 }
00103 
00104 QString KDesktopFile::readComment() const
00105 {
00106   return readEntry("Comment");
00107 }
00108 
00109 QString KDesktopFile::readGenericName() const
00110 {
00111   return readEntry("GenericName");
00112 }
00113 
00114 QString KDesktopFile::readPath() const
00115 {
00116   return readEntry("Path");
00117 }
00118 
00119 QString KDesktopFile::readDevice() const
00120 {
00121   return readEntry("Dev");
00122 }
00123 
00124 QString KDesktopFile::readURL() const
00125 {
00126     if (hasDeviceType()) {
00127     QString devURL;
00128     
00129     // in this case, we do some magic. :)
00130     QCString fstabFile;
00131     int indexDevice = 0;  // device on first column
00132     int indexMountPoint = 1; // mount point on second column
00133     int indexFSType = 2; // fstype on third column
00134     if (QFile::exists(QString::fromLatin1("/etc/fstab"))) { // Linux, ...
00135         fstabFile = "/etc/fstab";
00136     } else if (QFile::exists(QString::fromLatin1("/etc/vfstab"))) { // Solaris
00137         fstabFile = "/etc/vfstab";
00138         indexMountPoint++;
00139         indexFSType++;
00140     }
00141     // insert your favorite location for fstab here
00142     
00143     if ( !fstabFile.isEmpty() ) {
00144         QFile f( fstabFile );
00145         f.open( IO_ReadOnly );
00146         QTextStream stream( &f );
00147         stream.setEncoding( QTextStream::Latin1 );
00148         while ( !stream.eof() ) {
00149         QString line = stream.readLine();
00150         line = line.simplifyWhiteSpace();
00151         if (!line.isEmpty() && line[0] == '/') { // skip comments but also
00152             QStringList lst = QStringList::split( ' ', line );
00153             if ( lst[indexDevice] == readDevice() )
00154             devURL = lst[indexMountPoint];
00155         }
00156         }
00157         f.close();
00158     }
00159     return devURL;
00160 
00161     } else {
00162     QString url = readEntry("URL");
00163         if ( !url.isEmpty() && url[0] == '/' )
00164         {
00165             // Handle absolute paths as such (i.e. we need to escape them)
00166             KURL u;
00167             u.setPath( url );
00168             return u.url();
00169         }
00170         return url;
00171     }
00172 }
00173 
00174 QStringList KDesktopFile::readActions() const
00175 {
00176     return readListEntry("Actions", ';');
00177 }
00178 
00179 void KDesktopFile::setActionGroup(const QString &group)
00180 {
00181     setGroup(QString::fromLatin1("Desktop Action ") + group);
00182 }
00183 
00184 bool KDesktopFile::hasActionGroup(const QString &group) const
00185 {
00186   return hasGroup(QString::fromLatin1("Desktop Action ") + group);
00187 }
00188 
00189 bool KDesktopFile::hasLinkType() const
00190 {
00191   return readEntry("Type") == QString::fromLatin1("Link");
00192 }
00193 
00194 bool KDesktopFile::hasApplicationType() const
00195 {
00196   return readEntry("Type") == QString::fromLatin1("Application");
00197 }
00198 
00199 bool KDesktopFile::hasMimeTypeType() const
00200 {
00201   return readEntry("Type") == QString::fromLatin1("MimeType");
00202 }
00203 
00204 bool KDesktopFile::hasDeviceType() const
00205 {
00206   return readEntry("Type") == QString::fromLatin1("FSDev") ||
00207          readEntry("Type") == QString::fromLatin1("FSDevice");
00208 }
00209 
00210 bool KDesktopFile::tryExec() const
00211 {
00212   // Test for TryExec and "X-KDE-AuthorizeAction" 
00213   QString te = readEntry("TryExec");
00214 
00215   if (!te.isEmpty()) {
00216     if (te[0] == '/') {
00217       if (::access(QFile::encodeName(te), R_OK & X_OK))
00218     return false;
00219       else
00220     return true;
00221     } else {
00222       // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
00223       // Environment PATH may contain filenames in 8bit locale cpecified
00224       // encoding (Like a filenames).
00225       QStringList dirs = QStringList::split(':', QFile::decodeName(::getenv("PATH")));
00226       QStringList::Iterator it(dirs.begin());
00227       for (; it != dirs.end(); ++it) {
00228     QString fName = *it + "/" + te;
00229     if (::access(QFile::encodeName(fName), R_OK & X_OK) == 0)
00230       return true;
00231       }
00232       // didn't match at all
00233       return false;
00234     }
00235   }
00236   QStringList list = readListEntry("X-KDE-AuthorizeAction");
00237   if (kapp && !list.isEmpty())
00238   {
00239      for(QStringList::ConstIterator it = list.begin();
00240          it != list.end();
00241          ++it)
00242      {
00243         if (!kapp->authorize((*it).stripWhiteSpace()))
00244            return false;
00245      }
00246   }
00247   
00248   // See also KService::username()
00249   bool su = readBoolEntry("X-KDE-SubstituteUID");
00250   if (su)
00251   {
00252       QString user = readEntry("X-KDE-Username");
00253       if (user.isEmpty())
00254         user = ::getenv("ADMIN_ACCOUNT");
00255       if (user.isEmpty())
00256         user = "root";
00257       if (!kapp->authorize("user/"+user))
00258         return false;
00259   }
00260   
00261   return true;
00262 }
00263 
00267 QString
00268 KDesktopFile::fileName() const { return backEnd->fileName(); }
00269 
00273 QString
00274 KDesktopFile::resource() const { return backEnd->resource(); }
00275 
00276 QStringList
00277 KDesktopFile::sortOrder() const
00278 {
00279   return readListEntry("SortOrder");
00280 }
00281 
00282 void KDesktopFile::virtual_hook( int id, void* data )
00283 { KConfig::virtual_hook( id, data ); }
00284 
00285 QString KDesktopFile::readDocPath() const
00286 {
00287     return readEntry( "DocPath" );
00288 }
00289 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:40 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001