kdesktopfile.cpp
00001
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 <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
00043
00044
00045 backEnd->changeFileName(fileName, resType, false);
00046 setReadOnly(bReadOnly);
00047 reparseConfiguration();
00048 setDesktopGroup();
00049 }
00050
00051 KDesktopFile::~KDesktopFile()
00052 {
00053
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;
00075
00076 if (path[0] != '/')
00077 return true;
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
00130 QCString fstabFile;
00131 int indexDevice = 0;
00132 int indexMountPoint = 1;
00133 int indexFSType = 2;
00134 if (QFile::exists(QString::fromLatin1("/etc/fstab"))) {
00135 fstabFile = "/etc/fstab";
00136 } else if (QFile::exists(QString::fromLatin1("/etc/vfstab"))) {
00137 fstabFile = "/etc/vfstab";
00138 indexMountPoint++;
00139 indexFSType++;
00140 }
00141
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] == '/') {
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
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
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
00223
00224
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
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
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
This file is part of the documentation for kdelibs Version 3.1.0.