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