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.2.1 2004/07/21 13:32:54 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 #include "kmountpoint.h" 00035 00036 #include "kdesktopfile.h" 00037 #include "kdesktopfile.moc" 00038 00039 #include <kdebug.h> 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 (local.startsWith("/")) 00066 { 00067 // Relative wrt apps? 00068 local = KGlobal::dirs()->relativeLocation("apps", path); 00069 } 00070 00071 if (!local.startsWith("/")) 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 (local.startsWith("/")) 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 (!path.startsWith("/")) 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 (local.startsWith("/")) 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 (path[0] != '/') 00132 return true; // Relative paths are ok. 00133 00134 KStandardDirs *dirs = KGlobal::dirs(); 00135 if (dirs->relativeLocation("apps", path)[0] != '/') 00136 return true; 00137 if (dirs->relativeLocation("xdgdata-apps", path)[0] != '/') 00138 return true; 00139 if (dirs->relativeLocation("services", path)[0] != '/') 00140 return true; 00141 if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop")) 00142 return true; 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.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:54:55 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003