kdesktopfile.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00046
00047
00048 backEnd->changeFileName(fileName, resType, false);
00049 setReadOnly(bReadOnly);
00050 reparseConfiguration();
00051 setDesktopGroup();
00052 }
00053
00054 KDesktopFile::~KDesktopFile()
00055 {
00056
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
00068 local = KGlobal::dirs()->relativeLocation("apps", path);
00069 }
00070
00071 if (QDir::isRelativePath(local))
00072 {
00073 local = ::locateLocal("apps", local);
00074 }
00075 else
00076 {
00077
00078
00079 local = KGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
00080 if (!QDir::isRelativePath(local))
00081 {
00082
00083
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);
00094 }
00095 else
00096 {
00097
00098
00099 local = KGlobal::dirs()->relativeLocation("xdgdata-apps", path);
00100 if (!QDir::isRelativePath(local))
00101 {
00102
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;
00130
00131 if (QDir::isRelativePath(path))
00132 return true;
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
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
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
00270
00271
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
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
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
00340
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
|