# kdecore Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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.2.4 2003/08/27 13:53:04 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
    00081   if (dirs->relativeLocation("apps", path)[0] != '/')
    00082      return true;
    00083   if (dirs->relativeLocation("services", path)[0] != '/')
    00084      return true;
    00085   if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
    00086      return true;
    00087
    00088   return false;
    00089 }
    00090
    00091 QString KDesktopFile::readType() const
    00092 {
    00093   return readEntry("Type");
    00094 }
    00095
    00096 QString KDesktopFile::readIcon() const
    00097 {
    00098   return readEntry("Icon");
    00099 }
    00100
    00101 QString KDesktopFile::readName() const
    00102 {
    00103   return readEntry("Name");
    00104 }
    00105
    00106 QString KDesktopFile::readComment() const
    00107 {
    00108   return readEntry("Comment");
    00109 }
    00110
    00111 QString KDesktopFile::readGenericName() const
    00112 {
    00113   return readEntry("GenericName");
    00114 }
    00115
    00116 QString KDesktopFile::readPath() const
    00117 {
    00118   return readPathEntry("Path");
    00119 }
    00120
    00121 QString KDesktopFile::readDevice() const
    00122 {
    00123   return readEntry("Dev");
    00124 }
    00125
    00126 QString KDesktopFile::readURL() const
    00127 {
    00128     if (hasDeviceType()) {
    00129         QString devURL;
    00130
    00131         // in this case, we do some magic. :)
    00132         QCString fstabFile;
    00133         int indexDevice = 0;  // device on first column
    00134         int indexMountPoint = 1; // mount point on second column
    00135         int indexFSType = 2; // fstype on third column
    00136         if (QFile::exists(QString::fromLatin1("/etc/fstab"))) { // Linux, ...
    00137             fstabFile = "/etc/fstab";
    00138         } else if (QFile::exists(QString::fromLatin1("/etc/vfstab"))) { // Solaris
    00139             fstabFile = "/etc/vfstab";
    00140             indexMountPoint++;
    00141             indexFSType++;
    00142         }
    00143         // insert your favorite location for fstab here
    00144
    00145         if ( !fstabFile.isEmpty() ) {
    00146             QFile f( fstabFile );
    00147             f.open( IO_ReadOnly );
    00148             QTextStream stream( &f );
    00149             stream.setEncoding( QTextStream::Latin1 );
    00150             while ( !stream.eof() ) {
    00151                 QString line = stream.readLine();
    00152                 line = line.simplifyWhiteSpace();
    00153                 if (!line.isEmpty() && line[0] == '/') { // skip comments but also
    00154                     QStringList lst = QStringList::split( ' ', line );
    00155                     if ( lst[indexDevice] == readDevice() )
    00156                         devURL = lst[indexMountPoint];
    00157                 }
    00158             }
    00159             f.close();
    00160         }
    00161         return devURL;
    00162
    00163     } else {
    00164         QString url = readPathEntry("URL");
    00165         if ( !url.isEmpty() && url[0] == '/' )
    00166         {
    00167             // Handle absolute paths as such (i.e. we need to escape them)
    00168             KURL u;
    00169             u.setPath( url );
    00170             return u.url();
    00171         }
    00172         return url;
    00173     }
    00174 }
    00175
    00176 QStringList KDesktopFile::readActions() const
    00177 {
    00178     return readListEntry("Actions", ';');
    00179 }
    00180
    00181 void KDesktopFile::setActionGroup(const QString &group)
    00182 {
    00183     setGroup(QString::fromLatin1("Desktop Action ") + group);
    00184 }
    00185
    00186 bool KDesktopFile::hasActionGroup(const QString &group) const
    00187 {
    00188   return hasGroup(QString::fromLatin1("Desktop Action ") + group);
    00189 }
    00190
    00191 bool KDesktopFile::hasLinkType() const
    00192 {
    00193   return readEntry("Type") == QString::fromLatin1("Link");
    00194 }
    00195
    00196 bool KDesktopFile::hasApplicationType() const
    00197 {
    00198   return readEntry("Type") == QString::fromLatin1("Application");
    00199 }
    00200
    00201 bool KDesktopFile::hasMimeTypeType() const
    00202 {
    00203   return readEntry("Type") == QString::fromLatin1("MimeType");
    00204 }
    00205
    00206 bool KDesktopFile::hasDeviceType() const
    00207 {
    00208   return readEntry("Type") == QString::fromLatin1("FSDev") ||
    00209          readEntry("Type") == QString::fromLatin1("FSDevice");
    00210 }
    00211
    00212 bool KDesktopFile::tryExec() const
    00213 {
    00214   // Test for TryExec and "X-KDE-AuthorizeAction"
    00215   QString te = readPathEntry("TryExec");
    00216
    00217   if (!te.isEmpty()) {
    00218     if (te[0] == '/') {
    00219       if (::access(QFile::encodeName(te), R_OK | X_OK))
    00220         return false;
    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       bool match = false;
    00228       for (; it != dirs.end(); ++it) {
    00229         QString fName = *it + "/" + te;
    00230         if (::access(QFile::encodeName(fName), R_OK | X_OK) == 0)
    00231         {
    00232           match = true;
    00233           break;
    00234         }
    00235       }
    00236       // didn't match at all
    00237       if (!match)
    00238         return false;
    00239     }
    00240   }
    00241   QStringList list = readListEntry("X-KDE-AuthorizeAction");
    00242   if (kapp && !list.isEmpty())
    00243   {
    00244      for(QStringList::ConstIterator it = list.begin();
    00245          it != list.end();
    00246          ++it)
    00247      {
    00248         if (!kapp->authorize((*it).stripWhiteSpace()))
    00249            return false;
    00250      }
    00251   }
    00252
    00253   // See also KService::username()
    00254   bool su = readBoolEntry("X-KDE-SubstituteUID");
    00255   if (su)
    00256   {
    00257       QString user = readEntry("X-KDE-Username");
    00258       if (user.isEmpty())
    00259         user = ::getenv("ADMIN_ACCOUNT");
    00260       if (user.isEmpty())
    00261         user = "root";
    00262       if (!kapp->authorize("user/"+user))
    00263         return false;
    00264   }
    00265
    00266   return true;
    00267 }
    00268
    00272 QString
    00273 KDesktopFile::fileName() const { return backEnd->fileName(); }
    00274
    00278 QString
    00279 KDesktopFile::resource() const { return backEnd->resource(); }
    00280
    00281 QStringList
    00282 KDesktopFile::sortOrder() const
    00283 {
    00284   return readListEntry("SortOrder");
    00285 }
    00286
    00287 void KDesktopFile::virtual_hook( int id, void* data )
    00288 { KConfig::virtual_hook( id, data ); }
    00289
    00290 QString KDesktopFile::readDocPath() const
    00291 {
    00292         return readPathEntry( "DocPath" );
    00293 }
    00294

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:26 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001