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

kdecore

  • kdecore
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 "kconfigbackend.h"
34 #include "kapplication.h"
35 #include "kstandarddirs.h"
36 #include "kmountpoint.h"
37 #include "kcatalogue.h"
38 #include "klocale.h"
39 
40 #include "kdesktopfile.h"
41 #include "kdesktopfile.moc"
42 
43 KDesktopFile::KDesktopFile(const TQString &fileName, bool bReadOnly,
44  const char * resType)
45  : KConfig(TQString::fromLatin1(""), bReadOnly, false)
46 {
47  // KConfigBackEnd 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 
56 KDesktopFile::~KDesktopFile()
57 {
58  // no need to do anything
59 }
60 
61 TQString 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 = KGlobal::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 = KGlobal::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 = KGlobal::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 
113 bool 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 
125 bool KDesktopFile::isAuthorizedDesktopFile(const TQString& path)
126 {
127  if (!kapp || kapp->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  KStandardDirs *dirs = KGlobal::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 
150 TQString 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 = KGlobal::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 
171 TQString KDesktopFile::readType() const
172 {
173  return readEntry("Type");
174 }
175 
176 TQString KDesktopFile::readIcon() const
177 {
178  return readEntry("Icon");
179 }
180 
181 TQString KDesktopFile::readName() const
182 {
183  return translatedEntry("Name");
184 }
185 
186 TQString KDesktopFile::readComment() const
187 {
188  return translatedEntry("Comment");
189 }
190 
191 TQString KDesktopFile::readGenericName() const
192 {
193  return translatedEntry("GenericName");
194 }
195 
196 TQString KDesktopFile::readPath() const
197 {
198  return readPathEntry("Path");
199 }
200 
201 TQString KDesktopFile::readDevice() const
202 {
203  return readEntry("Dev");
204 }
205 
206 TQString 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 
237 TQStringList KDesktopFile::readActions() const
238 {
239  return readListEntry("Actions", ';');
240 }
241 
242 void KDesktopFile::setActionGroup(const TQString &group)
243 {
244  setGroup(TQString::fromLatin1("Desktop Action ") + group);
245 }
246 
247 bool KDesktopFile::hasActionGroup(const TQString &group) const
248 {
249  return hasGroup(TQString::fromLatin1("Desktop Action ") + group);
250 }
251 
252 bool KDesktopFile::hasLinkType() const
253 {
254  return readEntry("Type") == TQString::fromLatin1("Link");
255 }
256 
257 bool KDesktopFile::hasApplicationType() const
258 {
259  return readEntry("Type") == TQString::fromLatin1("Application");
260 }
261 
262 bool KDesktopFile::hasMimeTypeType() const
263 {
264  return readEntry("Type") == TQString::fromLatin1("MimeType");
265 }
266 
267 bool KDesktopFile::hasDeviceType() const
268 {
269  return readEntry("Type") == TQString::fromLatin1("FSDev") ||
270  readEntry("Type") == TQString::fromLatin1("FSDevice");
271 }
272 
273 bool KDesktopFile::tryExec() const
274 {
275  // Test for TryExec and "X-KDE-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-KDE-AuthorizeAction");
303  if (kapp && !list.isEmpty())
304  {
305  for(TQStringList::ConstIterator it = list.begin();
306  it != list.end();
307  ++it)
308  {
309  if (!kapp->authorize((*it).stripWhiteSpace()))
310  return false;
311  }
312  }
313 
314  // See also KService::username()
315  bool su = readBoolEntry("X-KDE-SubstituteUID");
316  if (su)
317  {
318  TQString user = readEntry("X-KDE-Username");
319  if (user.isEmpty())
320  user = ::getenv("ADMIN_ACCOUNT");
321  if (user.isEmpty())
322  user = "root";
323  if (!kapp->authorize("user/"+user))
324  return false;
325  }
326 
327  return true;
328 }
329 
333 TQString
334 KDesktopFile::fileName() const { return backEnd->fileName(); }
335 
339 TQString
340 KDesktopFile::resource() const { return backEnd->resource(); }
341 
342 TQStringList
343 KDesktopFile::sortOrder() const
344 {
345  return readListEntry("SortOrder");
346 }
347 
348 void KDesktopFile::virtual_hook( int id, void* data )
349 { KConfig::virtual_hook( id, data ); }
350 
351 TQString KDesktopFile::readDocPath() const
352 {
353  // Depreciated, remove in KDE4 or 5?
354  // See: http://www.freedesktop.org/Standards/desktop-entry-spec
355  if(hasKey( "DocPath" ))
356  return readPathEntry( "DocPath" );
357 
358  return readPathEntry( "X-DocPath" );
359 }
360 
361 KDesktopFile* KDesktopFile::copyTo(const TQString &file) const
362 {
363  KDesktopFile *config = new KDesktopFile(TQString::null, false);
364  KConfig::copyTo(file, config);
365  config->setDesktopGroup();
366  return config;
367 }

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.1.2
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |