kdesktopfile.cpp
/*
This file is part of the KDE libraries
Copyright (c) 1999 Pietro Iglio <iglio@kde.org>
Copyright (c) 1999 Preston Brown <pbrown@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// $Id: kdesktopfile.cpp 465272 2005-09-29 09:47:40Z mueller $
#include <stdlib.h>
#include <unistd.h>
#include <qfile.h>
#include <qdir.h>
#include <qtextstream.h>
#include <kdebug.h>
#include "kurl.h"
#include "kconfigbackend.h"
#include "kapplication.h"
#include "kstandarddirs.h"
#include "kmountpoint.h"
#include "kdesktopfile.h"
#include "kdesktopfile.moc"
KDesktopFile::KDesktopFile(const QString &fileName, bool bReadOnly,
const char * resType)
: KConfig(QString::fromLatin1(""), bReadOnly, false)
{
// KConfigBackEnd will try to locate the filename that is provided
// based on the resource type specified, _only_ if the filename
// is not an absolute path.
backEnd->changeFileName(fileName, resType, false);
setReadOnly(bReadOnly);
reparseConfiguration();
setDesktopGroup();
}
KDesktopFile::~KDesktopFile()
{
// no need to do anything
}
QString KDesktopFile::locateLocal(const QString &path)
{
QString local;
if (path.endsWith(".directory"))
{
local = path;
if (!QDir::isRelativePath(local))
{
// Relative wrt apps?
local = KGlobal::dirs()->relativeLocation("apps", path);
}
if (QDir::isRelativePath(local))
{
local = ::locateLocal("apps", local); // Relative to apps
}
else
{
// XDG Desktop menu items come with absolute paths, we need to
// extract their relative path and then build a local path.
local = KGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
if (!QDir::isRelativePath(local))
{
// Hm, that didn't work...
// What now? Use filename only and hope for the best.
local = path.mid(path.findRev('/')+1);
}
local = ::locateLocal("xdgdata-dirs", local);
}
}
else
{
if (QDir::isRelativePath(path))
{
local = ::locateLocal("apps", path); // Relative to apps
}
else
{
// XDG Desktop menu items come with absolute paths, we need to
// extract their relative path and then build a local path.
local = KGlobal::dirs()->relativeLocation("xdgdata-apps", path);
if (!QDir::isRelativePath(local))
{
// What now? Use filename only and hope for the best.
local = path.mid(path.findRev('/')+1);
}
local = ::locateLocal("xdgdata-apps", local);
}
}
return local;
}
bool KDesktopFile::isDesktopFile(const QString& path)
{
int len = path.length();
if(len > 8 && path.right(8) == QString::fromLatin1(".desktop"))
return true;
else if(len > 7 && path.right(7) == QString::fromLatin1(".kdelnk"))
return true;
else
return false;
}
bool KDesktopFile::isAuthorizedDesktopFile(const QString& path)
{
if (!kapp || kapp->authorize("run_desktop_files"))
return true;
if (path.isEmpty())
return false; // Empty paths are not ok.
if (QDir::isRelativePath(path))
return true; // Relative paths are ok.
KStandardDirs *dirs = KGlobal::dirs();
if (QDir::isRelativePath( dirs->relativeLocation("apps", path) ))
return true;
if (QDir::isRelativePath( dirs->relativeLocation("xdgdata-apps", path) ))
return true;
if (QDir::isRelativePath( dirs->relativeLocation("services", path) ))
return true;
if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
return true;
kdWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
return false;
}
QString KDesktopFile::readType() const
{
return readEntry("Type");
}
QString KDesktopFile::readIcon() const
{
return readEntry("Icon");
}
QString KDesktopFile::readName() const
{
return readEntry("Name");
}
QString KDesktopFile::readComment() const
{
return readEntry("Comment");
}
QString KDesktopFile::readGenericName() const
{
return readEntry("GenericName");
}
QString KDesktopFile::readPath() const
{
return readPathEntry("Path");
}
QString KDesktopFile::readDevice() const
{
return readEntry("Dev");
}
QString KDesktopFile::readURL() const
{
if (hasDeviceType()) {
QString device = readDevice();
KMountPoint::List mountPoints = KMountPoint::possibleMountPoints();
for(KMountPoint::List::ConstIterator it = mountPoints.begin();
it != mountPoints.end(); ++it)
{
KMountPoint *mp = *it;
if (mp->mountedFrom() == device)
{
KURL u;
u.setPath( mp->mountPoint() );
return u.url();
}
}
return QString::null;
} else {
QString url = readPathEntry("URL");
if ( !url.isEmpty() && !QDir::isRelativePath(url) )
{
// Handle absolute paths as such (i.e. we need to escape them)
KURL u;
u.setPath( url );
return u.url();
}
return url;
}
}
QStringList KDesktopFile::readActions() const
{
return readListEntry("Actions", ';');
}
void KDesktopFile::setActionGroup(const QString &group)
{
setGroup(QString::fromLatin1("Desktop Action ") + group);
}
bool KDesktopFile::hasActionGroup(const QString &group) const
{
return hasGroup(QString::fromLatin1("Desktop Action ") + group);
}
bool KDesktopFile::hasLinkType() const
{
return readEntry("Type") == QString::fromLatin1("Link");
}
bool KDesktopFile::hasApplicationType() const
{
return readEntry("Type") == QString::fromLatin1("Application");
}
bool KDesktopFile::hasMimeTypeType() const
{
return readEntry("Type") == QString::fromLatin1("MimeType");
}
bool KDesktopFile::hasDeviceType() const
{
return readEntry("Type") == QString::fromLatin1("FSDev") ||
readEntry("Type") == QString::fromLatin1("FSDevice");
}
bool KDesktopFile::tryExec() const
{
// Test for TryExec and "X-KDE-AuthorizeAction"
QString te = readPathEntry("TryExec");
if (!te.isEmpty()) {
if (!QDir::isRelativePath(te)) {
if (::access(QFile::encodeName(te), X_OK))
return false;
} else {
// !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
// Environment PATH may contain filenames in 8bit locale cpecified
// encoding (Like a filenames).
QStringList dirs = QStringList::split(':', QFile::decodeName(::getenv("PATH")));
QStringList::Iterator it(dirs.begin());
bool match = false;
for (; it != dirs.end(); ++it) {
QString fName = *it + "/" + te;
if (::access(QFile::encodeName(fName), X_OK) == 0)
{
match = true;
break;
}
}
// didn't match at all
if (!match)
return false;
}
}
QStringList list = readListEntry("X-KDE-AuthorizeAction");
if (kapp && !list.isEmpty())
{
for(QStringList::ConstIterator it = list.begin();
it != list.end();
++it)
{
if (!kapp->authorize((*it).stripWhiteSpace()))
return false;
}
}
// See also KService::username()
bool su = readBoolEntry("X-KDE-SubstituteUID");
if (su)
{
QString user = readEntry("X-KDE-Username");
if (user.isEmpty())
user = ::getenv("ADMIN_ACCOUNT");
if (user.isEmpty())
user = "root";
if (!kapp->authorize("user/"+user))
return false;
}
return true;
}
QString
KDesktopFile::fileName() const { return backEnd->fileName(); }
QString
KDesktopFile::resource() const { return backEnd->resource(); }
QStringList
KDesktopFile::sortOrder() const
{
return readListEntry("SortOrder");
}
void KDesktopFile::virtual_hook( int id, void* data )
{ KConfig::virtual_hook( id, data ); }
QString KDesktopFile::readDocPath() const
{
// Depreciated, remove in KDE4 or 5?
// See: http://www.freedesktop.org/Standards/desktop-entry-spec
if(hasKey( "DocPath" ))
return readPathEntry( "DocPath" );
return readPathEntry( "X-DocPath" );
}
KDesktopFile* KDesktopFile::copyTo(const QString &file) const
{
KDesktopFile *config = new KDesktopFile(QString::null, false);
KConfig::copyTo(file, config);
config->setDesktopGroup();
return config;
}
KURL
Represents and parses a URL.
Definition: kurl.h:127
KDesktopFile::setActionGroup
void setActionGroup(const QString &group)
Sets the desktop action group.
Definition: kdesktopfile.cpp:219
KDesktopFile::tryExec
bool tryExec() const
Checks whether the TryExec field contains a binary which is found on the local system.
Definition: kdesktopfile.cpp:250
KConfigBase::readListEntry
int readListEntry(const QString &pKey, QStrList &list, char sep= ',') const
Reads a list of strings.
Definition: kconfigbase.cpp:435
KMountPoint
The KMountPoint class provides information about mounted and unmounted disks.
Definition: kmountpoint.h:35
KConfigBackEnd::changeFileName
void changeFileName(const QString &_fileName, const char *_resType, bool _useKDEGlobals)
Changes the filenames associated with this back end.
Definition: kconfigbackend.cpp:243
KDesktopFile::readComment
QString readComment() const
Returns the value of the "Comment=" entry.
Definition: kdesktopfile.cpp:163
KMountPoint::mountedFrom
QString mountedFrom() const
Where this filesystem gets mounted from.
Definition: kmountpoint.h:62
KConfigBase::hasKey
bool hasKey(const QString &key) const
Checks whether the key has an entry in the currently active group.
Definition: kconfigbase.cpp:109
KURL::url
QString url(int _trailing=0, int encoding_hint=0) const
Returns the URL as string, with all escape sequences intact, encoded in a given charset.
Definition: kurl.cpp:1464
KConfigBase::hasGroup
bool hasGroup(const QString &group) const
Returns true if the specified group is known about.
Definition: kconfigbase.cpp:134
KDesktopFile::hasApplicationType
bool hasApplicationType() const
Checks whether there is an entry "Type=Application".
Definition: kdesktopfile.cpp:234
KDesktopFile::sortOrder
QStringList sortOrder() const
Returns the entry of the "SortOrder=" entry.
Definition: kdesktopfile.cpp:320
KConfigBase::readPathEntry
QString readPathEntry(const QString &pKey, const QString &aDefault=QString::null) const
Reads a path.
Definition: kconfigbase.cpp:553
KDesktopFile::isAuthorizedDesktopFile
static bool isAuthorizedDesktopFile(const QString &path)
Checks whether the user is authorized to run this desktop file.
Definition: kdesktopfile.cpp:123
KDesktopFile::readType
QString readType() const
Returns the value of the "Type=" entry.
Definition: kdesktopfile.cpp:148
KDesktopFile::resource
QString resource() const
Returns the resource.
Definition: kdesktopfile.cpp:317
KConfig::copyTo
KConfig * copyTo(const QString &file, KConfig *config=0) const
Copies all entries from this config object to a new config object that will save itself to file...
Definition: kconfig.cpp:303
KDesktopFile::hasMimeTypeType
bool hasMimeTypeType() const
Checks whether there is an entry "Type=MimeType".
Definition: kdesktopfile.cpp:239
KGlobal::dirs
static KStandardDirs * dirs()
Returns the application standard dirs object.
Definition: kglobal.cpp:54
KDesktopFile::readDevice
QString readDevice() const
Returns the value of the "Dev=" entry.
Definition: kdesktopfile.cpp:178
KDesktopFile::hasDeviceType
bool hasDeviceType() const
Checks whether there is an entry "Type=FSDev".
Definition: kdesktopfile.cpp:244
KDesktopFile::copyTo
KDesktopFile * copyTo(const QString &file) const
Copies all entries from this config object to a new KDesktopFile object that will save itself to file...
Definition: kdesktopfile.cpp:338
KStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:125
KStandardDirs::relativeLocation
QString relativeLocation(const char *type, const QString &absPath)
Converts an absolute path to a path relative to a certain resource.
Definition: kstandarddirs.cpp:1134
KConfigBase::backEnd
KConfigBackEnd * backEnd
A back end for loading/saving to disk in a particular format.
Definition: kconfigbase.h:1975
KConfigBase::setDesktopGroup
void setDesktopGroup()
Sets the group to the "Desktop Entry" group used for desktop configuration files for applications...
Definition: kconfigbase.cpp:104
KDesktopFile::hasLinkType
bool hasLinkType() const
Checks whether there is a "Type=Link" entry.
Definition: kdesktopfile.cpp:229
KURL::setPath
void setPath(const QString &path)
Sets the decoded path of the URL.
Definition: kurl.cpp:1990
KDesktopFile::readActions
QStringList readActions() const
Returns a list of the "Actions=" entries.
Definition: kdesktopfile.cpp:214
KDesktopFile
KDE Desktop File Management.
Definition: kdesktopfile.h:34
KDesktopFile::readURL
QString readURL() const
Returns the value of the "URL=" entry.
Definition: kdesktopfile.cpp:183
KDesktopFile::KDesktopFile
KDesktopFile(const QString &fileName, bool readOnly=false, const char *resType="apps")
Constructs a KDesktopFile object and make it either read-write or read-only.
Definition: kdesktopfile.cpp:41
KDesktopFile::readGenericName
QString readGenericName() const
Returns the value of the "GenericName=" entry.
Definition: kdesktopfile.cpp:168
KConfig
Access KDE Configuration entries.
Definition: kconfig.h:43
KDesktopFile::~KDesktopFile
virtual ~KDesktopFile()
Destructs the KDesktopFile object.
Definition: kdesktopfile.cpp:54
KMountPoint::mountPoint
QString mountPoint() const
Path where the filesystem is mounted or can be mounted.
Definition: kmountpoint.h:74
KDesktopFile::isDesktopFile
static bool isDesktopFile(const QString &path)
Checks whether this is really a desktop file.
Definition: kdesktopfile.cpp:111
KConfigBase::readEntry
QString readEntry(const QString &pKey, const QString &aDefault=QString::null) const
Reads the value of an entry specified by pKey in the current group.
Definition: kconfigbase.cpp:203
KDesktopFile::locateLocal
static QString locateLocal(const QString &path)
Returns the location where changes for the .desktop file path should be written to.
Definition: kdesktopfile.cpp:59
KDesktopFile::fileName
QString fileName() const
Returns the file name.
Definition: kdesktopfile.cpp:311
KConfig::reparseConfiguration
virtual void reparseConfiguration()
Clears all internal data structures and then reread configuration information from disk...
Definition: kconfig.cpp:161
KConfigBackEnd::resource
const char * resource() const
Returns the resource type as passed to the constructor.
Definition: kconfigbackend.h:125
KConfigBase::readBoolEntry
bool readBoolEntry(const QString &pKey, bool bDefault=false) const
Reads a boolean entry.
Definition: kconfigbase.cpp:716
KDesktopFile::hasActionGroup
bool hasActionGroup(const QString &group) const
Returns true if the action group exists, false otherwise.
Definition: kdesktopfile.cpp:224
KDesktopFile::readIcon
QString readIcon() const
Returns the value of the "Icon=" entry.
Definition: kdesktopfile.cpp:153
KConfigBase::setReadOnly
virtual void setReadOnly(bool _ro)
Sets the config object's read-only status.
Definition: kconfigbase.h:1731
KDesktopFile::readPath
QString readPath() const
Returns the value of the "Path=" entry.
Definition: kdesktopfile.cpp:173
KDesktopFile::readDocPath
QString readDocPath() const
Returns the value of the "X-DocPath=" Or "DocPath=" entry.
Definition: kdesktopfile.cpp:328
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
KConfigBase::setGroup
void setGroup(const QString &group)
Specifies the group in which keys will be read and written.
Definition: kconfigbase.cpp:79
KMountPoint::possibleMountPoints
static KMountPoint::List possibleMountPoints(int infoNeeded=0)
This function gives a list of all possible mountpoints.
Definition: kmountpoint.cpp:129
KConfigBackEnd::fileName
QString fileName() const
Returns the filename as passed to the constructor.
Definition: kconfigbackend.h:119
KDesktopFile::readName
QString readName() const
Returns the value of the "Name=" entry.
Definition: kdesktopfile.cpp:158

KDE Home | KDE Accessibility Home | Description of Access Keys