• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kdesktopfile.cpp
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 1999 Pietro Iglio <iglio@kde.org>
4 Copyright (c) 1999 Preston Brown <pbrown@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22// $Id$
23
24#include <stdlib.h>
25#include <unistd.h>
26
27#include <tqfile.h>
28#include <tqdir.h>
29#include <tqtextstream.h>
30
31#include <kdebug.h>
32#include "kurl.h"
33#include "tdeconfigbackend.h"
34#include "tdeapplication.h"
35#include "tdestandarddirs.h"
36#include "kmountpoint.h"
37#include "kcatalogue.h"
38#include "tdelocale.h"
39
40#include "kdesktopfile.h"
41#include "kdesktopfile.moc"
42
43KDesktopFile::KDesktopFile(const TQString &fileName, bool bReadOnly,
44 const char * resType)
45 : TDEConfig(TQString::fromLatin1(""), bReadOnly, false)
46{
47 // TDEConfigBackEnd will try to locate the filename that is provided
48 // based on the resource type specified, _only_ if the filename
49 // is not an absolute path.
50 backEnd->changeFileName(fileName, resType, false);
51 setReadOnly(bReadOnly);
52 reparseConfiguration();
53 setDesktopGroup();
54}
55
56KDesktopFile::~KDesktopFile()
57{
58 // no need to do anything
59}
60
61TQString KDesktopFile::locateLocal(const TQString &path)
62{
63 TQString local;
64 if (path.endsWith(".directory"))
65 {
66 local = path;
67 if (!TQDir::isRelativePath(local))
68 {
69 // Relative wrt apps?
70 local = TDEGlobal::dirs()->relativeLocation("apps", path);
71 }
72
73 if (TQDir::isRelativePath(local))
74 {
75 local = ::locateLocal("apps", local); // Relative to apps
76 }
77 else
78 {
79 // XDG Desktop menu items come with absolute paths, we need to
80 // extract their relative path and then build a local path.
81 local = TDEGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
82 if (!TQDir::isRelativePath(local))
83 {
84 // Hm, that didn't work...
85 // What now? Use filename only and hope for the best.
86 local = path.mid(path.findRev('/')+1);
87 }
88 local = ::locateLocal("xdgdata-dirs", local);
89 }
90 }
91 else
92 {
93 if (TQDir::isRelativePath(path))
94 {
95 local = ::locateLocal("apps", path); // Relative to apps
96 }
97 else
98 {
99 // XDG Desktop menu items come with absolute paths, we need to
100 // extract their relative path and then build a local path.
101 local = TDEGlobal::dirs()->relativeLocation("xdgdata-apps", path);
102 if (!TQDir::isRelativePath(local))
103 {
104 // What now? Use filename only and hope for the best.
105 local = path.mid(path.findRev('/')+1);
106 }
107 local = ::locateLocal("xdgdata-apps", local);
108 }
109 }
110 return local;
111}
112
113bool KDesktopFile::isDesktopFile(const TQString& path)
114{
115 int len = path.length();
116
117 if(len > 8 && path.right(8) == TQString::fromLatin1(".desktop"))
118 return true;
119 else if(len > 7 && path.right(7) == TQString::fromLatin1(".kdelnk"))
120 return true;
121 else
122 return false;
123}
124
125bool KDesktopFile::isAuthorizedDesktopFile(const TQString& path)
126{
127 if (!tdeApp || tdeApp->authorize("run_desktop_files"))
128 return true;
129
130 if (path.isEmpty())
131 return false; // Empty paths are not ok.
132
133 if (TQDir::isRelativePath(path))
134 return true; // Relative paths are ok.
135
136 TDEStandardDirs *dirs = TDEGlobal::dirs();
137 if (TQDir::isRelativePath( dirs->relativeLocation("apps", path) ))
138 return true;
139 if (TQDir::isRelativePath( dirs->relativeLocation("xdgdata-apps", path) ))
140 return true;
141 if (TQDir::isRelativePath( dirs->relativeLocation("services", path) ))
142 return true;
143 if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
144 return true;
145
146 kdWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
147 return false;
148}
149
150TQString KDesktopFile::translatedEntry(const char* key) const
151{
152 if (hasTranslatedKey(key))
153 return readEntry(key);
154
155 if (hasKey(key)) {
156 TQString value = readEntryUntranslated(key);
157 TQString fName = fileName();
158 fName = fName.mid(fName.findRev('/')+1);
159 TQString po_lookup_key = TQString::fromLatin1(key) + "(" + fName + "): " + value;
160 TQString po_value = TDEGlobal::locale()->translate(po_lookup_key.utf8().data());
161
162 if (po_value == po_lookup_key)
163 return value;
164
165 return po_value;
166 }
167
168 return TQString::null;
169}
170
171TQString KDesktopFile::readType() const
172{
173 return readEntry("Type");
174}
175
176TQString KDesktopFile::readIcon() const
177{
178 return readEntry("Icon");
179}
180
181TQString KDesktopFile::readName() const
182{
183 return translatedEntry("Name");
184}
185
186TQString KDesktopFile::readComment() const
187{
188 return translatedEntry("Comment");
189}
190
191TQString KDesktopFile::readGenericName() const
192{
193 return translatedEntry("GenericName");
194}
195
196TQString KDesktopFile::readPath() const
197{
198 return readPathEntry("Path");
199}
200
201TQString KDesktopFile::readDevice() const
202{
203 return readEntry("Dev");
204}
205
206TQString KDesktopFile::readURL() const
207{
208 if (hasDeviceType()) {
209 TQString device = readDevice();
210 KMountPoint::List mountPoints = KMountPoint::possibleMountPoints();
211
212 for(KMountPoint::List::ConstIterator it = mountPoints.begin();
213 it != mountPoints.end(); ++it)
214 {
215 KMountPoint *mp = *it;
216 if (mp->mountedFrom() == device)
217 {
218 KURL u;
219 u.setPath( mp->mountPoint() );
220 return u.url();
221 }
222 }
223 return TQString::null;
224 } else {
225 TQString url = readPathEntry("URL");
226 if ( !url.isEmpty() && !TQDir::isRelativePath(url) )
227 {
228 // Handle absolute paths as such (i.e. we need to escape them)
229 KURL u;
230 u.setPath( url );
231 return u.url();
232 }
233 return url;
234 }
235}
236
237TQStringList KDesktopFile::readActions() const
238{
239 return readListEntry("Actions", ';');
240}
241
242void KDesktopFile::setActionGroup(const TQString &group)
243{
244 setGroup(TQString::fromLatin1("Desktop Action ") + group);
245}
246
247bool KDesktopFile::hasActionGroup(const TQString &group) const
248{
249 return hasGroup(TQString::fromLatin1("Desktop Action ") + group);
250}
251
252bool KDesktopFile::hasLinkType() const
253{
254 return readEntry("Type") == TQString::fromLatin1("Link");
255}
256
257bool KDesktopFile::hasApplicationType() const
258{
259 return readEntry("Type") == TQString::fromLatin1("Application");
260}
261
262bool KDesktopFile::hasMimeTypeType() const
263{
264 return readEntry("Type") == TQString::fromLatin1("MimeType");
265}
266
267bool KDesktopFile::hasDeviceType() const
268{
269 return readEntry("Type") == TQString::fromLatin1("FSDev") ||
270 readEntry("Type") == TQString::fromLatin1("FSDevice");
271}
272
273bool KDesktopFile::tryExec() const
274{
275 // Test for TryExec and "X-TDE-AuthorizeAction"
276 TQString te = readPathEntry("TryExec");
277
278 if (!te.isEmpty()) {
279 if (!TQDir::isRelativePath(te)) {
280 if (::access(TQFile::encodeName(te), X_OK))
281 return false;
282 } else {
283 // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
284 // Environment PATH may contain filenames in 8bit locale cpecified
285 // encoding (Like a filenames).
286 TQStringList dirs = TQStringList::split(':', TQFile::decodeName(::getenv("PATH")));
287 TQStringList::Iterator it(dirs.begin());
288 bool match = false;
289 for (; it != dirs.end(); ++it) {
290 TQString fName = *it + "/" + te;
291 if (::access(TQFile::encodeName(fName), X_OK) == 0)
292 {
293 match = true;
294 break;
295 }
296 }
297 // didn't match at all
298 if (!match)
299 return false;
300 }
301 }
302 TQStringList list = readListEntry("X-TDE-AuthorizeAction");
303 if (tdeApp && !list.isEmpty())
304 {
305 for(TQStringList::ConstIterator it = list.begin();
306 it != list.end();
307 ++it)
308 {
309 if (!tdeApp->authorize((*it).stripWhiteSpace()))
310 return false;
311 }
312 }
313
314 // See also KService::username()
315 bool su = readBoolEntry("X-TDE-SubstituteUID");
316 if (su)
317 {
318 TQString user = readEntry("X-TDE-Username");
319 if (user.isEmpty())
320 user = ::getenv("ADMIN_ACCOUNT");
321 if (user.isEmpty())
322 user = "root";
323 if (!tdeApp->authorize("user/"+user))
324 return false;
325 }
326 else {
327 // Respect KDE su request if present
328 su = readBoolEntry("X-KDE-SubstituteUID");
329 if (su)
330 {
331 TQString user = readEntry("X-KDE-Username");
332 if (user.isEmpty())
333 user = ::getenv("ADMIN_ACCOUNT");
334 if (user.isEmpty())
335 user = "root";
336 if (!tdeApp->authorize("user/"+user))
337 return false;
338 }
339 }
340
341 return true;
342}
343
347TQString
348KDesktopFile::fileName() const { return backEnd->fileName(); }
349
353TQString
354KDesktopFile::resource() const { return backEnd->resource(); }
355
356TQStringList
357KDesktopFile::sortOrder() const
358{
359 return readListEntry("SortOrder");
360}
361
362void KDesktopFile::virtual_hook( int id, void* data )
363{ TDEConfig::virtual_hook( id, data ); }
364
365TQString KDesktopFile::readDocPath() const
366{
367 return readPathEntry( "X-DocPath" );
368}
369
370KDesktopFile* KDesktopFile::copyTo(const TQString &file) const
371{
372 KDesktopFile *config = new KDesktopFile(TQString::null, false);
373 TDEConfig::copyTo(file, config);
374 config->setDesktopGroup();
375 return config;
376}
KDesktopFile
KDE Desktop File Management.
Definition: kdesktopfile.h:35
KDesktopFile::hasApplicationType
bool hasApplicationType() const
Checks whether there is an entry "Type=Application".
Definition: kdesktopfile.cpp:257
KDesktopFile::copyTo
KDesktopFile * copyTo(const TQString &file) const
Copies all entries from this config object to a new KDesktopFile object that will save itself to file...
Definition: kdesktopfile.cpp:370
KDesktopFile::isDesktopFile
static bool isDesktopFile(const TQString &path)
Checks whether this is really a desktop file.
Definition: kdesktopfile.cpp:113
KDesktopFile::readGenericName
TQString readGenericName() const
Returns the value of the "GenericName=" entry.
Definition: kdesktopfile.cpp:191
KDesktopFile::readDevice
TQString readDevice() const
Returns the value of the "Dev=" entry.
Definition: kdesktopfile.cpp:201
KDesktopFile::hasDeviceType
bool hasDeviceType() const
Checks whether there is an entry "Type=FSDev".
Definition: kdesktopfile.cpp:267
KDesktopFile::KDesktopFile
KDesktopFile(const TQString &fileName, bool readOnly=false, const char *resType="apps")
Constructs a KDesktopFile object and make it either read-write or read-only.
Definition: kdesktopfile.cpp:43
KDesktopFile::readType
TQString readType() const
Returns the value of the "Type=" entry.
Definition: kdesktopfile.cpp:171
KDesktopFile::hasActionGroup
bool hasActionGroup(const TQString &group) const
Returns true if the action group exists, false otherwise.
Definition: kdesktopfile.cpp:247
KDesktopFile::readPath
TQString readPath() const
Returns the value of the "Path=" entry.
Definition: kdesktopfile.cpp:196
KDesktopFile::readComment
TQString readComment() const
Returns the value of the "Comment=" entry.
Definition: kdesktopfile.cpp:186
KDesktopFile::readDocPath
TQString readDocPath() const
Returns the value of the "X-DocPath=" entry.
Definition: kdesktopfile.cpp:365
KDesktopFile::readActions
TQStringList readActions() const
Returns a list of the "Actions=" entries.
Definition: kdesktopfile.cpp:237
KDesktopFile::~KDesktopFile
virtual ~KDesktopFile()
Destructs the KDesktopFile object.
Definition: kdesktopfile.cpp:56
KDesktopFile::hasMimeTypeType
bool hasMimeTypeType() const
Checks whether there is an entry "Type=MimeType".
Definition: kdesktopfile.cpp:262
KDesktopFile::readName
TQString readName() const
Returns the value of the "Name=" entry.
Definition: kdesktopfile.cpp:181
KDesktopFile::hasLinkType
bool hasLinkType() const
Checks whether there is a "Type=Link" entry.
Definition: kdesktopfile.cpp:252
KDesktopFile::tryExec
bool tryExec() const
Checks whether the TryExec field contains a binary which is found on the local system.
Definition: kdesktopfile.cpp:273
KDesktopFile::sortOrder
TQStringList sortOrder() const
Returns the entry of the "SortOrder=" entry.
Definition: kdesktopfile.cpp:357
KDesktopFile::resource
TQString resource() const
Returns the resource.
Definition: kdesktopfile.cpp:354
KDesktopFile::fileName
TQString fileName() const
Returns the file name.
Definition: kdesktopfile.cpp:348
KDesktopFile::readURL
TQString readURL() const
Returns the value of the "URL=" entry.
Definition: kdesktopfile.cpp:206
KDesktopFile::locateLocal
static TQString locateLocal(const TQString &path)
Returns the location where changes for the .desktop file path should be written to.
Definition: kdesktopfile.cpp:61
KDesktopFile::isAuthorizedDesktopFile
static bool isAuthorizedDesktopFile(const TQString &path)
Checks whether the user is authorized to run this desktop file.
Definition: kdesktopfile.cpp:125
KDesktopFile::readIcon
TQString readIcon() const
Returns the value of the "Icon=" entry.
Definition: kdesktopfile.cpp:176
KDesktopFile::setActionGroup
void setActionGroup(const TQString &group)
Sets the desktop action group.
Definition: kdesktopfile.cpp:242
KMountPoint
The KMountPoint class provides information about mounted and unmounted disks.
Definition: kmountpoint.h:36
KMountPoint::mountPoint
TQString mountPoint() const
Path where the filesystem is mounted or can be mounted.
Definition: kmountpoint.h:74
KMountPoint::possibleMountPoints
static KMountPoint::List possibleMountPoints(int infoNeeded=0)
This function gives a list of all possible mountpoints.
Definition: kmountpoint.cpp:130
KMountPoint::mountedFrom
TQString mountedFrom() const
Where this filesystem gets mounted from.
Definition: kmountpoint.h:62
KURL
Represents and parses a URL.
Definition: kurl.h:128
KURL::setPath
void setPath(const TQString &path)
Sets the decoded path of the URL.
Definition: kurl.cpp:2025
KURL::url
TQString 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:1499
TDEConfigBackEnd::resource
const char * resource() const
Returns the resource type as passed to the constructor.
Definition: tdeconfigbackend.h:126
TDEConfigBackEnd::fileName
TQString fileName() const
Returns the filename as passed to the constructor.
Definition: tdeconfigbackend.h:120
TDEConfigBackEnd::changeFileName
void changeFileName(const TQString &_fileName, const char *_resType, bool _useKDEGlobals)
Changes the filenames associated with this back end.
Definition: tdeconfigbackend.cpp:243
TDEConfigBase::readEntry
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads the value of an entry specified by pKey in the current group.
Definition: tdeconfigbase.cpp:221
TDEConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
Reads a boolean entry.
Definition: tdeconfigbase.cpp:748
TDEConfigBase::backEnd
TDEConfigBackEnd * backEnd
A back end for loading/saving to disk in a particular format.
Definition: tdeconfigbase.h:1999
TDEConfigBase::setDesktopGroup
void setDesktopGroup()
Sets the group to the "Desktop Entry" group used for desktop configuration files for applications,...
Definition: tdeconfigbase.cpp:104
TDEConfigBase::hasGroup
bool hasGroup(const TQString &group) const
Returns true if the specified group is known about.
Definition: tdeconfigbase.cpp:152
TDEConfigBase::readEntryUntranslated
TQString readEntryUntranslated(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads the value of an entry specified by pKey in the current group.
Definition: tdeconfigbase.cpp:204
TDEConfigBase::group
TQString group() const
Returns the name of the group in which we are searching for keys and from which we are retrieving ent...
Definition: tdeconfigbase.cpp:100
TDEConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
Reads a list of strings.
Definition: tdeconfigbase.cpp:467
TDEConfigBase::setReadOnly
virtual void setReadOnly(bool _ro)
Sets the config object's read-only status.
Definition: tdeconfigbase.h:1755
TDEConfigBase::hasKey
bool hasKey(const TQString &key) const
Checks whether the key has an entry in the currently active group.
Definition: tdeconfigbase.cpp:109
TDEConfigBase::readPathEntry
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads a path.
Definition: tdeconfigbase.cpp:585
TDEConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: tdeconfigbase.cpp:79
TDEConfig
Access KDE Configuration entries.
Definition: tdeconfig.h:44
TDEConfig::reparseConfiguration
virtual void reparseConfiguration()
Clears all internal data structures and then reread configuration information from disk.
Definition: tdeconfig.cpp:161
TDEConfig::copyTo
TDEConfig * copyTo(const TQString &file, TDEConfig *config=0) const
Copies all entries from this config object to a new config object that will save itself to file.
Definition: tdeconfig.cpp:303
TDEGlobal::dirs
static TDEStandardDirs * dirs()
Returns the application standard dirs object.
Definition: tdeglobal.cpp:58
TDEGlobal::locale
static TDELocale * locale()
Returns the global locale object.
Definition: tdeglobal.cpp:108
TDELocale::translate
TQString translate(const char *index) const
Translates the string into the corresponding string in the national language, if available.
Definition: tdelocale.cpp:768
TDEStandardDirs
Site-independent access to standard TDE directories.
Definition: tdestandarddirs.h:126
TDEStandardDirs::relativeLocation
TQString relativeLocation(const char *type, const TQString &absPath)
Converts an absolute path to a path relative to a certain resource.
Definition: tdestandarddirs.cpp:1154
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
tdelocale.h

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.