• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.9.1 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • kernel
kstandarddirs.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Sirtaj Singh Kang <taj@kde.org>
3  Copyright (C) 1999,2007 Stephan Kulow <coolo@kde.org>
4  Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
5  Copyright (C) 2009 David Faure <faure@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License version 2 as published by the Free Software Foundation.
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 /*
23  * Author: Stephan Kulow <coolo@kde.org> and Sirtaj Singh Kang <taj@kde.org>
24  * Generated: Thu Mar 5 16:05:28 EST 1998
25  */
26 
27 #include "kstandarddirs.h"
28 #include "kconfig.h"
29 #include "kconfiggroup.h"
30 #include "kdebug.h"
31 #include "kcomponentdata.h"
32 #include "kshell.h"
33 #include "kuser.h"
34 #include "kde_file.h"
35 #include "kkernel_win.h"
36 #include "kkernel_mac.h"
37 #include "klocale.h"
38 
39 #include <config.h>
40 #include <config-prefix.h>
41 #include <config-kstandarddirs.h>
42 
43 #include <stdlib.h>
44 #include <assert.h>
45 #include <errno.h>
46 #ifdef HAVE_SYS_STAT_H
47 #include <sys/stat.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #include <sys/param.h>
53 #include <sys/types.h>
54 #include <dirent.h>
55 #include <pwd.h>
56 #include <grp.h>
57 #ifdef Q_WS_WIN
58 #include <windows.h>
59 #ifdef _WIN32_WCE
60 #include <basetyps.h>
61 #endif
62 #ifdef Q_WS_WIN64
63 // FIXME: did not find a reliable way to fix with kdewin mingw header
64 #define interface struct
65 #endif
66 #include <shlobj.h>
67 #include <QtCore/QVarLengthArray>
68 #endif
69 
70 #include <QtCore/QMutex>
71 #include <QtCore/QRegExp>
72 #include <QtCore/QDir>
73 #include <QtCore/QFileInfo>
74 #include <QtCore/QSettings>
75 
76 class KStandardDirs::KStandardDirsPrivate
77 {
78 public:
79  KStandardDirsPrivate(KStandardDirs* qq)
80  : m_restrictionsActive(false),
81  m_checkRestrictions(true),
82  m_cacheMutex(QMutex::Recursive), // resourceDirs is recursive
83  q(qq)
84  { }
85 
86  bool hasDataRestrictions(const QString &relPath) const;
87  QStringList resourceDirs(const char* type, const QString& subdirForRestrictions);
88  void createSpecialResource(const char*);
89 
90  bool m_restrictionsActive : 1;
91  bool m_checkRestrictions : 1;
92  QMap<QByteArray, bool> m_restrictions;
93 
94  QStringList xdgdata_prefixes;
95  QStringList xdgconf_prefixes;
96  QStringList m_prefixes;
97 
98  // Directory dictionaries
99  QMap<QByteArray, QStringList> m_absolutes; // For each resource type, the list of absolute paths, from most local (most priority) to most global
100  QMap<QByteArray, QStringList> m_relatives; // Same with relative paths
101  // The search path is "all relative paths" < "all absolute paths", from most priority to least priority.
102 
103  // Caches (protected by mutex in const methods, cf ctor docu)
104  QMap<QByteArray, QStringList> m_dircache;
105  QMap<QByteArray, QString> m_savelocations;
106  QMutex m_cacheMutex;
107 
108  KStandardDirs* q;
109 };
110 
111 /* If you add a new resource type here, make sure to
112  * 1) regenerate using "kdesdk/scripts/generate_string_table.pl types < tmpfile" with the data below in tmpfile.
113  * 2) update the KStandardDirs class documentation
114  * 3) update the list in kde-config.cpp
115 
116 data
117 share/apps
118 html
119 share/doc/HTML
120 icon
121 share/icons
122 config
123 share/config
124 pixmap
125 share/pixmaps
126 apps
127 share/applnk
128 sound
129 share/sounds
130 locale
131 share/locale
132 services
133 share/kde4/services
134 servicetypes
135 share/kde4/servicetypes
136 mime
137 share/mimelnk
138 cgi
139 cgi-bin
140 wallpaper
141 share/wallpapers
142 templates
143 share/templates
144 exe
145 bin
146 module
147 %lib/kde4
148 qtplugins
149 %lib/kde4/plugins
150 kcfg
151 share/config.kcfg
152 emoticons
153 share/emoticons
154 xdgdata-apps
155 applications
156 xdgdata-icon
157 icons
158 xdgdata-pixmap
159 pixmaps
160 xdgdata-dirs
161 desktop-directories
162 xdgdata-mime
163 mime
164 xdgconf-menu
165 menus
166 xdgconf-autostart
167 autostart
168 */
169 
170 static const char types_string[] =
171  "data\0"
172  "share/apps\0"
173  "html\0"
174  "share/doc/HTML\0"
175  "icon\0"
176  "share/icons\0"
177  "config\0"
178  "share/config\0"
179  "pixmap\0"
180  "share/pixmaps\0"
181  "apps\0"
182  "share/applnk\0"
183  "sound\0"
184  "share/sounds\0"
185  "locale\0"
186  "share/locale\0"
187  "services\0"
188  "share/kde4/services\0"
189  "servicetypes\0"
190  "share/kde4/servicetypes\0"
191  "mime\0"
192  "share/mimelnk\0"
193  "cgi\0"
194  "cgi-bin\0"
195  "wallpaper\0"
196  "share/wallpapers\0"
197  "templates\0"
198  "share/templates\0"
199  "exe\0"
200  "bin\0"
201  "module\0"
202  "%lib/kde4\0"
203  "qtplugins\0"
204  "%lib/kde4/plugins\0"
205  "kcfg\0"
206  "share/config.kcfg\0"
207  "emoticons\0"
208  "share/emoticons\0"
209  "xdgdata-apps\0"
210  "applications\0"
211  "xdgdata-icon\0"
212  "icons\0"
213  "xdgdata-pixmap\0"
214  "pixmaps\0"
215  "xdgdata-dirs\0"
216  "desktop-directories\0"
217  "xdgdata-mime\0"
218  "xdgconf-menu\0"
219  "menus\0"
220  "xdgconf-autostart\0"
221  "autostart\0"
222  "\0";
223 
224 static const int types_indices[] = {
225  0, 5, 16, 21, 36, 41, 53, 60,
226  73, 80, 94, 99, 112, 118, 131, 138,
227  151, 160, 180, 193, 217, 222, 236, 240,
228  248, 258, 275, 285, 301, 305, 309, 316,
229  326, 336, 354, 359, 377, 387, 403, 416,
230  429, 442, 448, 463, 471, 484, 504, 217,
231  517, 530, 536, 554, -1
232 };
233 
234 static void tokenize(QStringList& token, const QString& str,
235  const QString& delim);
236 
237 KStandardDirs::KStandardDirs()
238  : d(new KStandardDirsPrivate(this))
239 {
240  addKDEDefaults();
241 }
242 
243 KStandardDirs::~KStandardDirs()
244 {
245  delete d;
246 }
247 
248 bool KStandardDirs::isRestrictedResource(const char *type, const QString& relPath) const
249 {
250  if (!d->m_restrictionsActive)
251  return false;
252 
253  if (d->m_restrictions.value(type, false))
254  return true;
255 
256  if (strcmp(type, "data")==0 && d->hasDataRestrictions(relPath))
257  return true;
258 
259  return false;
260 }
261 
262 bool KStandardDirs::KStandardDirsPrivate::hasDataRestrictions(const QString &relPath) const
263 {
264  QString key;
265  const int i = relPath.indexOf(QLatin1Char('/'));
266  if (i != -1)
267  key = QString::fromLatin1("data_") + relPath.left(i);
268  else
269  key = QString::fromLatin1("data_") + relPath;
270 
271  return m_restrictions.value(key.toLatin1(), false);
272 }
273 
274 
275 QStringList KStandardDirs::allTypes() const
276 {
277  QStringList list;
278  for (int i = 0; types_indices[i] != -1; i += 2)
279  list.append(QLatin1String(types_string + types_indices[i]));
280  // Those are added manually by addKDEDefaults
281  list.append(QString::fromLatin1("lib"));
282  //list.append(QString::fromLatin1("home")); // undocumented on purpose, said Waldo in r113855.
283 
284  // Those are handled by resourceDirs() itself
285  list.append(QString::fromLatin1("socket"));
286  list.append(QString::fromLatin1("tmp"));
287  list.append(QString::fromLatin1("cache"));
288  // Those are handled by installPath()
289  list.append(QString::fromLatin1("include"));
290 
291  // If you add anything here, make sure kde-config.cpp has a description for it.
292 
293  return list;
294 }
295 
296 static void priorityAdd(QStringList &prefixes, const QString& dir, bool priority)
297 {
298  if (priority && !prefixes.isEmpty())
299  {
300  // Add in front but behind $KDEHOME
301  QStringList::iterator it = prefixes.begin();
302  ++it;
303  prefixes.insert(it, dir);
304  }
305  else
306  {
307  prefixes.append(dir);
308  }
309 }
310 
311 void KStandardDirs::addPrefix( const QString& _dir )
312 {
313  addPrefix(_dir, false);
314 }
315 
316 void KStandardDirs::addPrefix( const QString& _dir, bool priority )
317 {
318  if (_dir.isEmpty())
319  return;
320 
321  QString dir = _dir;
322  if (dir.at(dir.length() - 1) != QLatin1Char('/'))
323  dir += QLatin1Char('/');
324 
325  if (!d->m_prefixes.contains(dir)) {
326  priorityAdd(d->m_prefixes, dir, priority);
327  d->m_dircache.clear();
328  }
329 }
330 
331 void KStandardDirs::addXdgConfigPrefix( const QString& _dir )
332 {
333  addXdgConfigPrefix(_dir, false);
334 }
335 
336 void KStandardDirs::addXdgConfigPrefix( const QString& _dir, bool priority )
337 {
338  if (_dir.isEmpty())
339  return;
340 
341  QString dir = _dir;
342  if (dir.at(dir.length() - 1) != QLatin1Char('/'))
343  dir += QLatin1Char('/');
344 
345  if (!d->xdgconf_prefixes.contains(dir)) {
346  priorityAdd(d->xdgconf_prefixes, dir, priority);
347  d->m_dircache.clear();
348  }
349 }
350 
351 void KStandardDirs::addXdgDataPrefix( const QString& _dir )
352 {
353  addXdgDataPrefix(_dir, false);
354 }
355 
356 void KStandardDirs::addXdgDataPrefix( const QString& _dir, bool priority )
357 {
358  if (_dir.isEmpty())
359  return;
360 
361  QString dir = _dir;
362  if (dir.at(dir.length() - 1) != QLatin1Char('/'))
363  dir += QLatin1Char('/');
364 
365  if (!d->xdgdata_prefixes.contains(dir)) {
366  priorityAdd(d->xdgdata_prefixes, dir, priority);
367  d->m_dircache.clear();
368  }
369 }
370 
371 QString KStandardDirs::kfsstnd_prefixes()
372 {
373  return d->m_prefixes.join(QString(QLatin1Char(KPATH_SEPARATOR)));
374 }
375 
376 QString KStandardDirs::kfsstnd_xdg_conf_prefixes()
377 {
378  return d->xdgconf_prefixes.join(QString(QLatin1Char(KPATH_SEPARATOR)));
379 }
380 
381 QString KStandardDirs::kfsstnd_xdg_data_prefixes()
382 {
383  return d->xdgdata_prefixes.join(QString(QLatin1Char(KPATH_SEPARATOR)));
384 }
385 
386 #ifndef KDE_NO_DEPRECATED
387 bool KStandardDirs::addResourceType( const char *type,
388  const QString& relativename,
389  bool priority )
390 {
391  return addResourceType( type, 0, relativename, priority);
392 }
393 #endif
394 
395 bool KStandardDirs::addResourceType( const char *type,
396  const char *basetype,
397  const QString& relativename,
398  bool priority )
399 {
400  if (relativename.isEmpty())
401  return false;
402 
403  QString copy = relativename;
404  if (basetype)
405  copy = QLatin1Char('%') + QString::fromLatin1(basetype) + QLatin1Char('/') + relativename;
406 
407  if (!copy.endsWith(QLatin1Char('/')))
408  copy += QLatin1Char('/');
409 
410  QByteArray typeBa = type;
411  QStringList& rels = d->m_relatives[typeBa]; // find or insert
412 
413  if (!rels.contains(copy)) {
414  if (priority)
415  rels.prepend(copy);
416  else
417  rels.append(copy);
418  // clean the caches
419  d->m_dircache.remove(typeBa);
420  d->m_savelocations.remove(typeBa);
421  return true;
422  }
423  return false;
424 }
425 
426 bool KStandardDirs::addResourceDir( const char *type,
427  const QString& absdir,
428  bool priority)
429 {
430  if (absdir.isEmpty() || !type)
431  return false;
432  // find or insert entry in the map
433  QString copy = absdir;
434  if (copy.at(copy.length() - 1) != QLatin1Char('/'))
435  copy += QLatin1Char('/');
436 
437  QByteArray typeBa = type;
438  QStringList &paths = d->m_absolutes[typeBa];
439  if (!paths.contains(copy)) {
440  if (priority)
441  paths.prepend(copy);
442  else
443  paths.append(copy);
444  // clean the caches
445  d->m_dircache.remove(typeBa);
446  d->m_savelocations.remove(typeBa);
447  return true;
448  }
449  return false;
450 }
451 
452 QString KStandardDirs::findResource( const char *type,
453  const QString& _filename ) const
454 {
455  if (!QDir::isRelativePath(_filename))
456  return !KGlobal::hasLocale() ? _filename // absolute dirs are absolute dirs, right? :-/
457  : KGlobal::locale()->localizedFilePath(_filename); // -- almost.
458 
459 #if 0
460  kDebug(180) << "Find resource: " << type;
461  for (QStringList::ConstIterator pit = m_prefixes.begin();
462  pit != m_prefixes.end();
463  ++pit)
464  {
465  kDebug(180) << "Prefix: " << *pit;
466  }
467 #endif
468 
469  QString filename(_filename);
470 #ifdef Q_OS_WIN
471  if(strcmp(type, "exe") == 0) {
472  if(!filename.endsWith(QLatin1String(".exe")))
473  filename += QLatin1String(".exe");
474  }
475 #endif
476  const QString dir = findResourceDir(type, filename);
477  if (dir.isEmpty())
478  return dir;
479  else
480  return !KGlobal::hasLocale() ? dir + filename
481  : KGlobal::locale()->localizedFilePath(dir + filename);
482 }
483 
484 static quint32 updateHash(const QString &file, quint32 hash)
485 {
486  KDE_struct_stat buff;
487  if ((KDE::access(file, R_OK) == 0) && (KDE::stat(file, &buff) == 0) && (S_ISREG(buff.st_mode))) {
488  hash = hash + static_cast<quint32>(buff.st_ctime);
489  }
490  return hash;
491 }
492 
493 quint32 KStandardDirs::calcResourceHash( const char *type,
494  const QString& filename,
495  SearchOptions options ) const
496 {
497  quint32 hash = 0;
498 
499  if (!QDir::isRelativePath(filename))
500  {
501  // absolute dirs are absolute dirs, right? :-/
502  return updateHash(filename, hash);
503  }
504  QStringList candidates = d->resourceDirs(type, filename);
505 
506  foreach ( const QString& candidate, candidates )
507  {
508  hash = updateHash(candidate + filename, hash);
509  if ( !( options & Recursive ) && hash ) {
510  return hash;
511  }
512  }
513  return hash;
514 }
515 
516 
517 QStringList KStandardDirs::findDirs( const char *type,
518  const QString& reldir ) const
519 {
520  QDir testdir;
521  QStringList list;
522  if (!QDir::isRelativePath(reldir))
523  {
524  testdir.setPath(reldir);
525  if (testdir.exists())
526  {
527  if (reldir.endsWith(QLatin1Char('/')))
528  list.append(reldir);
529  else
530  list.append(reldir+QLatin1Char('/'));
531  }
532  return list;
533  }
534 
535  const QStringList candidates = d->resourceDirs(type, reldir);
536 
537  for (QStringList::ConstIterator it = candidates.begin();
538  it != candidates.end(); ++it) {
539  testdir.setPath(*it + reldir);
540  if (testdir.exists())
541  list.append(testdir.absolutePath() + QLatin1Char('/'));
542  }
543 
544  return list;
545 }
546 
547 QString KStandardDirs::findResourceDir( const char *type,
548  const QString& _filename) const
549 {
550 #ifndef NDEBUG
551  if (_filename.isEmpty()) {
552  kWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!";
553  return QString();
554  }
555 #endif
556 
557  QString filename(_filename);
558 #ifdef Q_OS_WIN
559  if(strcmp(type, "exe") == 0) {
560  if(!filename.endsWith(QLatin1String(".exe")))
561  filename += QLatin1String(".exe");
562  }
563 #endif
564  const QStringList candidates = d->resourceDirs(type, filename);
565 
566  for (QStringList::ConstIterator it = candidates.begin();
567  it != candidates.end(); ++it) {
568  if (exists(*it + filename)) {
569  return *it;
570  }
571  }
572 
573 #ifndef NDEBUG
574  if(false && strcmp(type, "locale"))
575  kDebug(180) << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\".";
576 #endif
577 
578  return QString();
579 }
580 
581 bool KStandardDirs::exists(const QString &fullPath)
582 {
583 #ifdef Q_OS_WIN
584  // access() and stat() give a stupid error message to the user
585  // if the path is not accessible at all (e.g. no disk in A:/ and
586  // we do stat("A:/.directory")
587  if (fullPath.endsWith(QLatin1Char('/')))
588  return QDir(fullPath).exists();
589  return QFileInfo(fullPath).exists();
590 #else
591  KDE_struct_stat buff;
592  QByteArray cFullPath = QFile::encodeName(fullPath);
593  if (access(cFullPath, R_OK) == 0 && KDE_stat( cFullPath, &buff ) == 0) {
594  if (!fullPath.endsWith(QLatin1Char('/'))) {
595  if (S_ISREG( buff.st_mode ))
596  return true;
597  } else
598  if (S_ISDIR( buff.st_mode ))
599  return true;
600  }
601  return false;
602 #endif
603 }
604 
605 static void lookupDirectory(const QString& path, const QString &relPart,
606  const QRegExp &regexp,
607  QStringList& list,
608  QStringList& relList,
609  bool recursive, bool unique)
610 {
611  const QString pattern = regexp.pattern();
612  if (recursive || pattern.contains(QLatin1Char('?')) || pattern.contains(QLatin1Char('*')))
613  {
614  if (path.isEmpty()) //for sanity
615  return;
616 #ifdef Q_WS_WIN
617  QString path_ = path + QLatin1String( "*.*" );
618  WIN32_FIND_DATA findData;
619  HANDLE hFile = FindFirstFile( (LPWSTR)path_.utf16(), &findData );
620  if( hFile == INVALID_HANDLE_VALUE )
621  return;
622  do {
623  const int len = wcslen( findData.cFileName );
624  if (!( findData.cFileName[0] == '.' &&
625  findData.cFileName[1] == '\0' ) &&
626  !( findData.cFileName[0] == '.' &&
627  findData.cFileName[1] == '.' &&
628  findData.cFileName[2] == '\0' ) &&
629  ( findData.cFileName[len-1] != '~' ) ) {
630  QString fn = QString::fromUtf16( (const unsigned short*)findData.cFileName );
631  if (!recursive && !regexp.exactMatch(fn))
632  continue; // No match
633  QString pathfn = path + fn;
634  bool bIsDir = ( ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY );
635  if ( recursive ) {
636  if ( bIsDir ) {
637  lookupDirectory(pathfn + QLatin1Char('/'),
638  relPart + fn + QLatin1Char('/'),
639  regexp, list, relList, recursive, unique);
640  }
641  if (!regexp.exactMatch(fn))
642  continue; // No match
643  }
644  if ( !bIsDir )
645  {
646  if ( !unique || !relList.contains(relPart + fn) )
647  {
648  list.append( pathfn );
649  relList.append( relPart + fn );
650  }
651  }
652  }
653  } while( FindNextFile( hFile, &findData ) != 0 );
654  FindClose( hFile );
655 #else
656  // We look for a set of files.
657  DIR *dp = opendir( QFile::encodeName(path));
658  if (!dp)
659  return;
660 
661  assert(path.endsWith(QLatin1Char('/')));
662 
663  struct dirent *ep;
664 
665  while( ( ep = readdir( dp ) ) != 0L )
666  {
667  QString fn( QFile::decodeName(ep->d_name));
668  if (fn == QString::fromLatin1(".") || fn == QString::fromLatin1("..") || fn.at(fn.length() - 1) == QLatin1Char('~'))
669  continue;
670 
671  if (!recursive && !regexp.exactMatch(fn))
672  continue; // No match
673 
674  bool isDir;
675  bool isReg;
676 
677  QString pathfn = path + fn;
678 #ifdef HAVE_DIRENT_D_TYPE
679  isDir = ep->d_type == DT_DIR;
680  isReg = ep->d_type == DT_REG;
681 
682  if (ep->d_type == DT_UNKNOWN || ep->d_type == DT_LNK)
683 #endif
684  {
685  KDE_struct_stat buff;
686  if ( KDE::stat( pathfn, &buff ) != 0 ) {
687  kDebug(180) << "Error stat'ing " << pathfn << " : " << perror;
688  continue; // Couldn't stat (e.g. no read permissions)
689  }
690  isReg = S_ISREG (buff.st_mode);
691  isDir = S_ISDIR (buff.st_mode);
692  }
693 
694  if ( recursive ) {
695  if ( isDir ) {
696  lookupDirectory(pathfn + QLatin1Char('/'), relPart + fn + QLatin1Char('/'), regexp, list, relList, recursive, unique);
697  }
698  if (!regexp.exactMatch(fn))
699  continue; // No match
700  }
701  if ( isReg )
702  {
703  if (!unique || !relList.contains(relPart + fn))
704  {
705  list.append( pathfn );
706  relList.append( relPart + fn );
707  }
708  }
709  }
710  closedir( dp );
711 #endif
712  }
713  else
714  {
715  // We look for a single file.
716  QString fn = pattern;
717  QString pathfn = path + fn;
718  KDE_struct_stat buff;
719  if ( KDE::stat( pathfn, &buff ) != 0 )
720  return; // File not found
721  if ( S_ISREG( buff.st_mode))
722  {
723  if (!unique || !relList.contains(relPart + fn))
724  {
725  list.append( pathfn );
726  relList.append( relPart + fn );
727  }
728  }
729  }
730 }
731 
732 static void lookupPrefix(const QString& prefix, const QString& relpath,
733  const QString& relPart,
734  const QRegExp &regexp,
735  QStringList& list,
736  QStringList& relList,
737  bool recursive, bool unique)
738 {
739  if (relpath.isEmpty()) {
740  if (recursive)
741  Q_ASSERT(prefix != QLatin1String("/")); // we don't want to recursively list the whole disk!
742  lookupDirectory(prefix, relPart, regexp, list,
743  relList, recursive, unique);
744  return;
745  }
746  QString path;
747  QString rest;
748 
749  int slash = relpath.indexOf(QLatin1Char('/'));
750  if (slash < 0)
751  rest = relpath.left(relpath.length() - 1);
752  else {
753  path = relpath.left(slash);
754  rest = relpath.mid(slash + 1);
755  }
756 
757  if (prefix.isEmpty()) //for sanity
758  return;
759 #ifndef Q_WS_WIN
760  // what does this assert check ?
761  assert(prefix.endsWith(QLatin1Char('/')));
762 #endif
763  if (path.contains(QLatin1Char('*')) || path.contains(QLatin1Char('?'))) {
764 
765  QRegExp pathExp(path, Qt::CaseSensitive, QRegExp::Wildcard);
766 
767 #ifdef Q_WS_WIN
768  QString prefix_ = prefix + QLatin1String( "*.*" );
769  WIN32_FIND_DATA findData;
770  HANDLE hFile = FindFirstFile( (LPWSTR)prefix_.utf16(), &findData );
771  if( hFile == INVALID_HANDLE_VALUE )
772  return;
773  do {
774  const int len = wcslen( findData.cFileName );
775  if (!( findData.cFileName[0] == '.' &&
776  findData.cFileName[1] == '\0' ) &&
777  !( findData.cFileName[0] == '.' &&
778  findData.cFileName[1] == '.' &&
779  findData.cFileName[2] == '\0' ) &&
780  ( findData.cFileName[len-1] != '~' ) ) {
781  const QString fn = QString::fromUtf16( (const unsigned short*)findData.cFileName );
782  if ( !pathExp.exactMatch(fn) )
783  continue; // No match
784  if ( ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY )
785  lookupPrefix(prefix + fn + QLatin1Char('/'),
786  rest, relPart + fn + QLatin1Char('/'),
787  regexp, list, relList, recursive, unique);
788  }
789  } while( FindNextFile( hFile, &findData ) != 0 );
790  FindClose( hFile );
791 #else
792  DIR *dp = opendir( QFile::encodeName(prefix) );
793  if (!dp) {
794  return;
795  }
796 
797  struct dirent *ep;
798 
799  while( ( ep = readdir( dp ) ) != 0L )
800  {
801  QString fn( QFile::decodeName(ep->d_name));
802  if (fn == QLatin1String(".") || fn == QLatin1String("..") || fn.at(fn.length() - 1) == QLatin1Char('~'))
803  continue;
804 
805  if ( !pathExp.exactMatch(fn) )
806  continue; // No match
807  QString rfn = relPart+fn;
808  fn = prefix + fn;
809 
810  bool isDir;
811 
812 #ifdef HAVE_DIRENT_D_TYPE
813  isDir = ep->d_type == DT_DIR;
814 
815  if (ep->d_type == DT_UNKNOWN || ep->d_type == DT_LNK)
816 #endif
817  {
818  QString pathfn = path + fn;
819  KDE_struct_stat buff;
820  if ( KDE::stat( fn, &buff ) != 0 ) {
821  kDebug(180) << "Error stat'ing " << fn << " : " << perror;
822  continue; // Couldn't stat (e.g. no read permissions)
823  }
824  isDir = S_ISDIR (buff.st_mode);
825  }
826  if ( isDir )
827  lookupPrefix(fn + QLatin1Char('/'), rest, rfn + QLatin1Char('/'), regexp, list, relList, recursive, unique);
828  }
829 
830  closedir( dp );
831 #endif
832  } else {
833  // Don't stat, if the dir doesn't exist we will find out
834  // when we try to open it.
835  lookupPrefix(prefix + path + QLatin1Char('/'), rest,
836  relPart + path + QLatin1Char('/'), regexp, list,
837  relList, recursive, unique);
838  }
839 }
840 
841 QStringList
842 KStandardDirs::findAllResources( const char *type,
843  const QString& filter,
844  SearchOptions options,
845  QStringList &relList) const
846 {
847  QString filterPath;
848  QString filterFile;
849 
850  if ( !filter.isEmpty() )
851  {
852  int slash = filter.lastIndexOf(QLatin1Char('/'));
853  if (slash < 0) {
854  filterFile = filter;
855  } else {
856  filterPath = filter.left(slash + 1);
857  filterFile = filter.mid(slash + 1);
858  }
859  }
860 
861  QStringList candidates;
862  if ( !QDir::isRelativePath(filter) ) // absolute path
863  {
864 #ifdef Q_OS_WIN
865  candidates << filterPath.left(3); //e.g. "C:\"
866  filterPath = filterPath.mid(3);
867 #else
868  candidates << QString::fromLatin1("/");
869  filterPath = filterPath.mid(1);
870 #endif
871  }
872  else
873  {
874  candidates = d->resourceDirs(type, filter);
875  }
876 
877  if (filterFile.isEmpty()) {
878  filterFile = QString(QLatin1Char('*'));
879  }
880 
881  QRegExp regExp(filterFile, Qt::CaseSensitive, QRegExp::Wildcard);
882 
883  QStringList list;
884  foreach ( const QString& candidate, candidates )
885  {
886  lookupPrefix(candidate, filterPath, QString(), regExp, list,
887  relList, options & Recursive, options & NoDuplicates);
888  }
889 
890  return list;
891 }
892 
893 QStringList
894 KStandardDirs::findAllResources( const char *type,
895  const QString& filter,
896  SearchOptions options ) const
897 {
898  QStringList relList;
899  return findAllResources(type, filter, options, relList);
900 }
901 
902 // ####### KDE4: should this be removed, in favor of QDir::canonicalPath()?
903 // aseigo: QDir::canonicalPath returns QString() if the dir doesn't exist
904 // and this method is often used with the expectation for it to work
905 // even if the directory doesn't exist. so ... no, we can't drop this
906 // yet
907 QString
908 KStandardDirs::realPath(const QString &dirname)
909 {
910 #ifdef Q_WS_WIN
911  const QString strRet = realFilePath(dirname);
912  if (!strRet.endsWith(QLatin1Char('/')))
913  return strRet + QLatin1Char('/');
914  return strRet;
915 #else
916  if (dirname.isEmpty() || (dirname.size() == 1 && dirname.at(0) == QLatin1Char('/')))
917  return dirname;
918 
919  if (dirname.at(0) != QLatin1Char('/')) {
920  qWarning("realPath called with a relative path '%s', please fix", qPrintable(dirname));
921  return dirname;
922  }
923 
924  char realpath_buffer[MAXPATHLEN + 1];
925  memset(realpath_buffer, 0, MAXPATHLEN + 1);
926 
927  /* If the path contains symlinks, get the real name */
928  if (realpath( QFile::encodeName(dirname).constData(), realpath_buffer) != 0) {
929  // success, use result from realpath
930  int len = strlen(realpath_buffer);
931  realpath_buffer[len] = '/';
932  realpath_buffer[len+1] = 0;
933  return QFile::decodeName(realpath_buffer);
934  }
935 
936  // Does not exist yet; resolve symlinks in parent dirs then.
937  // This ensures that once the directory exists, it will still be resolved
938  // the same way, so that the general rule that KStandardDirs always returns
939  // canonical paths stays true, and app code can compare paths more easily.
940  QString dir = dirname;
941  if (!dir.endsWith(QLatin1Char('/')))
942  dir += QLatin1Char('/');
943  QString relative;
944  while (!KStandardDirs::exists(dir)) {
945  //qDebug() << "does not exist:" << dir;
946  const int pos = dir.lastIndexOf(QLatin1Char('/'), -2);
947  Q_ASSERT(pos >= 0); // what? even "/" doesn't exist?
948  relative.prepend(dir.mid(pos+1)); // keep "subdir/"
949  dir = dir.left(pos+1);
950  Q_ASSERT(dir.endsWith(QLatin1Char('/')));
951  }
952  Q_ASSERT(!relative.isEmpty()); // infinite recursion ahead
953  if (!relative.isEmpty()) {
954  //qDebug() << "done, resolving" << dir << "and adding" << relative;
955  dir = realPath(dir) + relative;
956  }
957  return dir;
958 #endif
959 }
960 
961 // ####### KDE4: should this be removed, in favor of QDir::canonicalPath()?
962 // aseigo: QDir::canonicalPath returns QString() if the dir doesn't exist
963 // and this method is often used with the expectation for it to work
964 // even if the directory doesn't exist. so ... no, we can't drop this
965 // yet
966 QString
967 KStandardDirs::realFilePath(const QString &filename)
968 {
969 #ifdef Q_WS_WIN
970  LPCWSTR lpIn = (LPCWSTR)filename.utf16();
971  QVarLengthArray<WCHAR, MAX_PATH> buf(MAX_PATH);
972  DWORD len = GetFullPathNameW(lpIn, buf.size(), buf.data(), NULL);
973  if (len > (DWORD)buf.size()) {
974  buf.resize(len);
975  len = GetFullPathNameW(lpIn, buf.size(), buf.data(), NULL);
976  }
977  if (len == 0)
978  return QString();
979  return QString::fromUtf16((const unsigned short*)buf.data()).replace(QLatin1Char('\\'),QLatin1Char('/')).toLower();
980 #else
981  char realpath_buffer[MAXPATHLEN + 1];
982  memset(realpath_buffer, 0, MAXPATHLEN + 1);
983 
984  /* If the path contains symlinks, get the real name */
985  if (realpath( QFile::encodeName(filename).constData(), realpath_buffer) != 0) {
986  // success, use result from realpath
987  return QFile::decodeName(realpath_buffer);
988  }
989 
990  return filename;
991 #endif
992 }
993 
994 
995 void KStandardDirs::KStandardDirsPrivate::createSpecialResource(const char *type)
996 {
997  char hostname[256];
998  hostname[0] = 0;
999  gethostname(hostname, 255);
1000  const QString localkdedir = m_prefixes.first();
1001  QString dir = localkdedir + QString::fromLatin1(type) + QLatin1Char('-') + QString::fromLocal8Bit(hostname);
1002  char link[1024];
1003  link[1023] = 0;
1004  int result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1005  bool relink = (result == -1) && (errno == ENOENT);
1006  if (result > 0)
1007  {
1008  link[result] = 0;
1009  if (!QDir::isRelativePath(QFile::decodeName(link)))
1010  {
1011  KDE_struct_stat stat_buf;
1012  int res = KDE::lstat(QFile::decodeName(link), &stat_buf);
1013  if ((res == -1) && (errno == ENOENT))
1014  {
1015  relink = true;
1016  }
1017  else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
1018  {
1019  fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
1020  relink = true;
1021  }
1022  else if (stat_buf.st_uid != getuid())
1023  {
1024  fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
1025  relink = true;
1026  }
1027  }
1028  }
1029 #ifdef Q_WS_WIN
1030  if (relink)
1031  {
1032  if (!makeDir(dir, 0700))
1033  fprintf(stderr, "failed to create \"%s\"", qPrintable(dir));
1034  else
1035  result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1036  }
1037 #else //UNIX
1038  if (relink)
1039  {
1040  QString srv = findExe(QLatin1String("lnusertemp"), installPath("libexec"));
1041  if (srv.isEmpty())
1042  srv = findExe(QLatin1String("lnusertemp"));
1043  if (!srv.isEmpty())
1044  {
1045  if (system(QByteArray(QFile::encodeName(srv) + ' ' + type)) == -1) {
1046  fprintf(stderr, "Error: unable to launch lnusertemp command" );
1047  }
1048  result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1049  }
1050  }
1051  if (result > 0)
1052  {
1053  link[result] = 0;
1054  if (link[0] == '/')
1055  dir = QFile::decodeName(link);
1056  else
1057  dir = QDir::cleanPath(dir + QFile::decodeName(link));
1058  }
1059 #endif
1060  q->addResourceDir(type, dir + QLatin1Char('/'), false);
1061 }
1062 
1063 QStringList KStandardDirs::resourceDirs(const char *type) const
1064 {
1065  return d->resourceDirs(type, QString());
1066 }
1067 
1068 QStringList KStandardDirs::KStandardDirsPrivate::resourceDirs(const char* type, const QString& subdirForRestrictions)
1069 {
1070  QMutexLocker lock(&m_cacheMutex);
1071  const bool dataRestrictionActive = m_restrictionsActive
1072  && (strcmp(type, "data") == 0)
1073  && hasDataRestrictions(subdirForRestrictions);
1074 
1075  QMap<QByteArray, QStringList>::const_iterator dirCacheIt = m_dircache.constFind(type);
1076 
1077  QStringList candidates;
1078 
1079  if (dirCacheIt != m_dircache.constEnd() && !dataRestrictionActive) {
1080  //qDebug() << this << "resourceDirs(" << type << "), in cache already";
1081  candidates = *dirCacheIt;
1082  }
1083  else // filling cache
1084  {
1085  //qDebug() << this << "resourceDirs(" << type << "), not in cache";
1086  if (strcmp(type, "socket") == 0)
1087  createSpecialResource(type);
1088  else if (strcmp(type, "tmp") == 0)
1089  createSpecialResource(type);
1090  else if (strcmp(type, "cache") == 0)
1091  createSpecialResource(type);
1092 
1093  QDir testdir;
1094 
1095  bool restrictionActive = false;
1096  if (m_restrictionsActive) {
1097  if (dataRestrictionActive)
1098  restrictionActive = true;
1099  if (m_restrictions.value("all", false))
1100  restrictionActive = true;
1101  else if (m_restrictions.value(type, false))
1102  restrictionActive = true;
1103  }
1104 
1105  QStringList dirs;
1106  dirs = m_relatives.value(type);
1107  const QString typeInstallPath = installPath(type); // could be empty
1108 // better #ifdef incasesensitive_filesystem
1109 #ifdef Q_WS_WIN
1110  const QString installdir = typeInstallPath.isEmpty() ? QString() : realPath(typeInstallPath).toLower();
1111  const QString installprefix = installPath("kdedir").toLower();
1112 #else
1113  const QString installdir = typeInstallPath.isEmpty() ? QString() : realPath(typeInstallPath);
1114  const QString installprefix = installPath("kdedir");
1115 #endif
1116  if (!dirs.isEmpty())
1117  {
1118  bool local = true;
1119 
1120  for (QStringList::ConstIterator it = dirs.constBegin();
1121  it != dirs.constEnd(); ++it)
1122  {
1123  if ((*it).startsWith(QLatin1Char('%'))) {
1124  // grab the "data" from "%data/apps"
1125  const int pos = (*it).indexOf(QLatin1Char('/'));
1126  QString rel = (*it).mid(1, pos - 1);
1127  QString rest = (*it).mid(pos + 1);
1128  const QStringList basedirs = resourceDirs(rel.toUtf8().constData(), subdirForRestrictions);
1129  for (QStringList::ConstIterator it2 = basedirs.begin();
1130  it2 != basedirs.end(); ++it2)
1131  {
1132 #ifdef Q_WS_WIN
1133  const QString path = realPath( *it2 + rest ).toLower();
1134 #else
1135  const QString path = realPath( *it2 + rest );
1136 #endif
1137  testdir.setPath(path);
1138  if ((local || testdir.exists()) && !candidates.contains(path))
1139  candidates.append(path);
1140  local = false;
1141  }
1142  }
1143  }
1144 
1145  const QStringList *prefixList = 0;
1146  if (strncmp(type, "xdgdata-", 8) == 0)
1147  prefixList = &(xdgdata_prefixes);
1148  else if (strncmp(type, "xdgconf-", 8) == 0)
1149  prefixList = &(xdgconf_prefixes);
1150  else
1151  prefixList = &m_prefixes;
1152 
1153  for (QStringList::ConstIterator pit = prefixList->begin();
1154  pit != prefixList->end();
1155  ++pit)
1156  {
1157  // "exe" never has a custom install path, and the check triggers
1158  // a false positive due to the libexecdir patch
1159  if((*pit)!=installprefix||installdir.isEmpty()||!strcmp("exe", type))
1160  {
1161  for (QStringList::ConstIterator it = dirs.constBegin();
1162  it != dirs.constEnd(); ++it)
1163  {
1164  if ((*it).startsWith(QLatin1Char('%')))
1165  continue;
1166 #ifdef Q_WS_WIN
1167  const QString path = realPath( *pit + *it ).toLower();
1168 #else
1169  const QString path = realPath( *pit + *it );
1170 #endif
1171  testdir.setPath(path);
1172  if (local && restrictionActive)
1173  continue;
1174  if ((local || testdir.exists()) && !candidates.contains(path))
1175  candidates.append(path);
1176  }
1177  // special-case "config" (forward porting Chris Cheney's
1178  // hack) - we want /etc/kde after the local config paths
1179  // and before the ones in /usr (including kde-profile)
1180  if (local && !strcmp("config", type))
1181  candidates.append(QLatin1String("/etc/kde/"));
1182  local = false;
1183  }
1184  else
1185  {
1186  // we have a custom install path, so use this instead of <installprefix>/<relative dir>
1187  testdir.setPath(installdir);
1188  if(testdir.exists() && ! candidates.contains(installdir))
1189  candidates.append(installdir);
1190  }
1191  }
1192  }
1193 
1194  // make sure we find the path where it's installed
1195  if (!installdir.isEmpty()) {
1196  bool ok = true;
1197  foreach (const QString &s, candidates) {
1198  if (installdir.startsWith(s)) {
1199  ok = false;
1200  break;
1201  }
1202  }
1203  if (ok)
1204  candidates.append(installdir);
1205  }
1206 
1207  dirs = m_absolutes.value(type);
1208  if (!dirs.isEmpty())
1209  for (QStringList::ConstIterator it = dirs.constBegin();
1210  it != dirs.constEnd(); ++it)
1211  {
1212  testdir.setPath(*it);
1213  if (testdir.exists()) {
1214 #ifdef Q_WS_WIN
1215  const QString filename = realPath( *it ).toLower();
1216 #else
1217  const QString filename = realPath( *it );
1218 #endif
1219  if (!candidates.contains(filename)) {
1220  candidates.append(filename);
1221  }
1222  }
1223  }
1224 
1225  // Insert result into the cache for next time.
1226  // Exception: data_subdir restrictions are per-subdir, so we can't store such results
1227  if (!dataRestrictionActive) {
1228  //kDebug() << this << "Inserting" << type << candidates << "into dircache";
1229  m_dircache.insert(type, candidates);
1230  }
1231  }
1232 
1233 #if 0
1234  kDebug(180) << "found dirs for resource" << type << ":" << candidates;
1235 #endif
1236 
1237  return candidates;
1238 }
1239 
1240 #ifdef Q_OS_WIN
1241 static QStringList executableExtensions()
1242 {
1243  QStringList ret = QString::fromLocal8Bit(qgetenv("PATHEXT")).split(QLatin1Char(';'));
1244  if (!ret.contains(QLatin1String(".exe"), Qt::CaseInsensitive)) {
1245  // If %PATHEXT% does not contain .exe, it is either empty, malformed, or distorted in ways that we cannot support, anyway.
1246  ret.clear();
1247  ret << QLatin1String(".exe")
1248  << QLatin1String(".com")
1249  << QLatin1String(".bat")
1250  << QLatin1String(".cmd");
1251  }
1252  return ret;
1253 }
1254 #endif
1255 
1256 QStringList KStandardDirs::systemPaths( const QString& pstr )
1257 {
1258  QStringList tokens;
1259  QString p = pstr;
1260 
1261  if( p.isEmpty() )
1262  {
1263  p = QString::fromLocal8Bit( qgetenv( "PATH" ) );
1264  }
1265 
1266  QString delimiters(QLatin1Char(KPATH_SEPARATOR));
1267  delimiters += QLatin1Char('\b');
1268  tokenize( tokens, p, delimiters );
1269 
1270  QStringList exePaths;
1271 
1272  // split path using : or \b as delimiters
1273  for( int i = 0; i < tokens.count(); i++ )
1274  {
1275  exePaths << KShell::tildeExpand( tokens[ i ] );
1276  }
1277 
1278  return exePaths;
1279 }
1280 
1281 #ifdef Q_WS_MAC
1282 static QString getBundle( const QString& path, bool ignore )
1283 {
1284  //kDebug(180) << "getBundle(" << path << ", " << ignore << ") called";
1285  QFileInfo info;
1286  QString bundle = path;
1287  bundle += QLatin1String(".app/Contents/MacOS/") + bundle.section(QLatin1Char('/'), -1);
1288  info.setFile( bundle );
1289  FILE *file;
1290  if (file = fopen(info.absoluteFilePath().toUtf8().constData(), "r")) {
1291  fclose(file);
1292  struct stat _stat;
1293  if ((stat(info.absoluteFilePath().toUtf8().constData(), &_stat)) < 0) {
1294  return QString();
1295  }
1296  if ( ignore || (_stat.st_mode & S_IXUSR) ) {
1297  if ( ((_stat.st_mode & S_IFMT) == S_IFREG) || ((_stat.st_mode & S_IFMT) == S_IFLNK) ) {
1298  //kDebug(180) << "getBundle(): returning " << bundle;
1299  return bundle;
1300  }
1301  }
1302  }
1303  return QString();
1304 }
1305 #endif
1306 
1307 static QString checkExecutable( const QString& path, bool ignoreExecBit )
1308 {
1309 #ifdef Q_WS_MAC
1310  QString bundle = getBundle( path, ignoreExecBit );
1311  if ( !bundle.isEmpty() ) {
1312  //kDebug(180) << "findExe(): returning " << bundle;
1313  return bundle;
1314  }
1315 #endif
1316  QFileInfo info( path );
1317  QFileInfo orig = info;
1318 #if defined(Q_OS_DARWIN) || defined(Q_OS_MAC)
1319  FILE *file;
1320  if (file = fopen(orig.absoluteFilePath().toUtf8().constData(), "r")) {
1321  fclose(file);
1322  struct stat _stat;
1323  if ((stat(orig.absoluteFilePath().toUtf8().constData(), &_stat)) < 0) {
1324  return QString();
1325  }
1326  if ( ignoreExecBit || (_stat.st_mode & S_IXUSR) ) {
1327  if ( ((_stat.st_mode & S_IFMT) == S_IFREG) || ((_stat.st_mode & S_IFMT) == S_IFLNK) ) {
1328  orig.makeAbsolute();
1329  return orig.filePath();
1330  }
1331  }
1332  }
1333  return QString();
1334 #else
1335  if( info.exists() && info.isSymLink() )
1336  info = QFileInfo( info.canonicalFilePath() );
1337  if( info.exists() && ( ignoreExecBit || info.isExecutable() ) && info.isFile() ) {
1338  // return absolute path, but without symlinks resolved in order to prevent
1339  // problems with executables that work differently depending on name they are
1340  // run as (for example gunzip)
1341  orig.makeAbsolute();
1342  return orig.filePath();
1343  }
1344  //kDebug(180) << "checkExecutable(): failed, returning empty string";
1345  return QString();
1346 #endif
1347 }
1348 
1349 QString KStandardDirs::findExe( const QString& appname,
1350  const QString& pstr,
1351  SearchOptions options )
1352 {
1353  //kDebug(180) << "findExe(" << appname << ", pstr, " << ignoreExecBit << ") called";
1354 
1355 #ifdef Q_OS_WIN
1356  QStringList executable_extensions = executableExtensions();
1357  if (!executable_extensions.contains(appname.section(QLatin1Char('.'), -1, -1, QString::SectionIncludeLeadingSep), Qt::CaseInsensitive)) {
1358  QString found_exe;
1359  foreach (const QString& extension, executable_extensions) {
1360  found_exe = findExe(appname + extension, pstr, options);
1361  if (!found_exe.isEmpty()) {
1362  return found_exe;
1363  }
1364  }
1365  return QString();
1366  }
1367 #endif
1368  QFileInfo info;
1369 
1370  // absolute or relative path?
1371  if (appname.contains(QDir::separator()))
1372  {
1373  //kDebug(180) << "findExe(): absolute path given";
1374  QString path = checkExecutable(appname, options & IgnoreExecBit);
1375  return path;
1376  }
1377 
1378  //kDebug(180) << "findExe(): relative path given";
1379 
1380  QString p = installPath("libexec") + appname;
1381  QString result = checkExecutable(p, options & IgnoreExecBit);
1382  if (!result.isEmpty()) {
1383  //kDebug(180) << "findExe(): returning " << result;
1384  return result;
1385  }
1386 
1387  //kDebug(180) << "findExe(): checking system paths";
1388  const QStringList exePaths = systemPaths( pstr );
1389  for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
1390  {
1391  p = (*it) + QLatin1Char('/');
1392  p += appname;
1393 
1394  // Check for executable in this tokenized path
1395  result = checkExecutable(p, options & IgnoreExecBit);
1396  if (!result.isEmpty()) {
1397  //kDebug(180) << "findExe(): returning " << result;
1398  return result;
1399  }
1400  }
1401 
1402  // Not found in PATH, look into the KDE-specific bin dir ("exe" resource)
1403  p = installPath("exe");
1404  p += appname;
1405  result = checkExecutable(p, options & IgnoreExecBit);
1406  if (!result.isEmpty()) {
1407  //kDebug(180) << "findExe(): returning " << result;
1408  return result;
1409  }
1410 
1411  // If we reach here, the executable wasn't found.
1412  // So return empty string.
1413 
1414  //kDebug(180) << "findExe(): failed, nothing matched";
1415  return QString();
1416 }
1417 
1418 int KStandardDirs::findAllExe( QStringList& list, const QString& appname,
1419  const QString& pstr, SearchOptions options )
1420 {
1421 #ifdef Q_OS_WIN
1422  QStringList executable_extensions = executableExtensions();
1423  if (!executable_extensions.contains(appname.section(QLatin1Char('.'), -1, -1, QString::SectionIncludeLeadingSep), Qt::CaseInsensitive)) {
1424  int total = 0;
1425  foreach (const QString& extension, executable_extensions) {
1426  total += findAllExe (list, appname + extension, pstr, options);
1427  }
1428  return total;
1429  }
1430 #endif
1431  QFileInfo info;
1432  QString p;
1433  list.clear();
1434 
1435  const QStringList exePaths = systemPaths( pstr );
1436  for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
1437  {
1438  p = (*it) + QLatin1Char('/');
1439  p += appname;
1440 
1441 #ifdef Q_WS_MAC
1442  QString bundle = getBundle( p, (options & IgnoreExecBit) );
1443  if ( !bundle.isEmpty() ) {
1444  //kDebug(180) << "findExe(): returning " << bundle;
1445  list.append( bundle );
1446  }
1447 #endif
1448 
1449  info.setFile( p );
1450 
1451  if( info.exists() && ( ( options & IgnoreExecBit ) || info.isExecutable())
1452  && info.isFile() ) {
1453  list.append( p );
1454  }
1455  }
1456 
1457  return list.count();
1458 }
1459 
1460 static inline QString equalizePath(QString &str)
1461 {
1462 #ifdef Q_WS_WIN
1463  // filter pathes through QFileInfo to have always
1464  // the same case for drive letters
1465  QFileInfo f(str);
1466  if (f.isAbsolute())
1467  return f.absoluteFilePath();
1468  else
1469 #endif
1470  return str;
1471 }
1472 
1473 static void tokenize(QStringList& tokens, const QString& str,
1474  const QString& delim)
1475 {
1476  const int len = str.length();
1477  QString token;
1478 
1479  for(int index = 0; index < len; index++) {
1480  if (delim.contains(str[index])) {
1481  tokens.append(equalizePath(token));
1482  token.clear();
1483  } else {
1484  token += str[index];
1485  }
1486  }
1487  if (!token.isEmpty()) {
1488  tokens.append(equalizePath(token));
1489  }
1490 }
1491 
1492 #ifndef KDE_NO_DEPRECATED
1493 QString KStandardDirs::kde_default(const char *type)
1494 {
1495  return QString(QLatin1Char('%')) + QString::fromLatin1(type) + QLatin1Char('/');
1496 }
1497 #endif
1498 
1499 QString KStandardDirs::saveLocation(const char *type,
1500  const QString& suffix,
1501  bool create) const
1502 {
1503  QMutexLocker lock(&d->m_cacheMutex);
1504  QString path = d->m_savelocations.value(type);
1505  if (path.isEmpty())
1506  {
1507  QStringList dirs = d->m_relatives.value(type);
1508  if (dirs.isEmpty() && (
1509  (strcmp(type, "socket") == 0) ||
1510  (strcmp(type, "tmp") == 0) ||
1511  (strcmp(type, "cache") == 0) ))
1512  {
1513  (void) resourceDirs(type); // Generate socket|tmp|cache resource.
1514  dirs = d->m_relatives.value(type); // Search again.
1515  }
1516  if (!dirs.isEmpty())
1517  {
1518  path = dirs.first();
1519 
1520  if (path.startsWith(QLatin1Char('%'))) {
1521  // grab the "data" from "%data/apps"
1522  const int pos = path.indexOf(QLatin1Char('/'));
1523  QString rel = path.mid(1, pos - 1);
1524  QString rest = path.mid(pos + 1);
1525  QString basepath = saveLocation(rel.toUtf8().constData());
1526  path = basepath + rest;
1527  } else
1528 
1529  // Check for existence of typed directory + suffix
1530  if (strncmp(type, "xdgdata-", 8) == 0) {
1531  path = realPath( localxdgdatadir() + path ) ;
1532  } else if (strncmp(type, "xdgconf-", 8) == 0) {
1533  path = realPath( localxdgconfdir() + path );
1534  } else {
1535  path = realPath( localkdedir() + path );
1536  }
1537  }
1538  else {
1539  dirs = d->m_absolutes.value(type);
1540  if (dirs.isEmpty()) {
1541  qFatal("KStandardDirs: The resource type %s is not registered", type);
1542  } else {
1543  path = realPath(dirs.first());
1544  }
1545  }
1546 
1547  d->m_savelocations.insert(type, path.endsWith(QLatin1Char('/')) ? path : path + QLatin1Char('/'));
1548  }
1549  QString fullPath = path + suffix;
1550 
1551  KDE_struct_stat st;
1552  if (KDE::stat(fullPath, &st) != 0 || !(S_ISDIR(st.st_mode))) {
1553  if(!create) {
1554 #ifndef NDEBUG
1555  // Too much noise from kbuildsycoca4 -- it's fine if this happens from KConfig
1556  // when parsing global files without a local equivalent.
1557  //kDebug(180) << QString("save location %1 doesn't exist").arg(fullPath);
1558 #endif
1559  return fullPath;
1560  }
1561  if(!makeDir(fullPath, 0700)) {
1562  return fullPath;
1563  }
1564  d->m_dircache.remove(type);
1565  }
1566  if (!fullPath.endsWith(QLatin1Char('/')))
1567  fullPath += QLatin1Char('/');
1568  return fullPath;
1569 }
1570 
1571 // KDE5: make the method const
1572 QString KStandardDirs::relativeLocation(const char *type, const QString &absPath)
1573 {
1574  QString fullPath = absPath;
1575  int i = absPath.lastIndexOf(QLatin1Char('/'));
1576  if (i != -1) {
1577  fullPath = realFilePath(absPath); // Normalize
1578  }
1579 
1580  const QStringList candidates = resourceDirs(type);
1581 
1582  for (QStringList::ConstIterator it = candidates.begin();
1583  it != candidates.end(); ++it) {
1584  if (fullPath.startsWith(*it)) {
1585  return fullPath.mid((*it).length());
1586  }
1587  }
1588  return absPath;
1589 }
1590 
1591 
1592 bool KStandardDirs::makeDir(const QString& dir, int mode)
1593 {
1594  // we want an absolute path
1595  if (QDir::isRelativePath(dir))
1596  return false;
1597 
1598 #ifdef Q_WS_WIN
1599  return QDir().mkpath(dir);
1600 #else
1601  QString target = dir;
1602  uint len = target.length();
1603 
1604  // append trailing slash if missing
1605  if (dir.at(len - 1) != QLatin1Char('/'))
1606  target += QLatin1Char('/');
1607 
1608  QString base;
1609  uint i = 1;
1610 
1611  while( i < len )
1612  {
1613  KDE_struct_stat st;
1614  int pos = target.indexOf(QLatin1Char('/'), i);
1615  base += target.mid(i - 1, pos - i + 1);
1616  QByteArray baseEncoded = QFile::encodeName(base);
1617  // bail out if we encountered a problem
1618  if (KDE_stat(baseEncoded, &st) != 0)
1619  {
1620  // Directory does not exist....
1621  // Or maybe a dangling symlink ?
1622  if (KDE_lstat(baseEncoded, &st) == 0)
1623  (void)unlink(baseEncoded); // try removing
1624 
1625  if (KDE_mkdir(baseEncoded, static_cast<mode_t>(mode)) != 0) {
1626  baseEncoded.prepend( "trying to create local folder " );
1627  perror(baseEncoded.constData());
1628  return false; // Couldn't create it :-(
1629  }
1630  }
1631  i = pos + 1;
1632  }
1633  return true;
1634 #endif
1635 }
1636 
1637 static QString readEnvPath(const char *env)
1638 {
1639  QByteArray c_path;
1640 #ifndef _WIN32_WCE
1641  c_path = qgetenv(env);
1642  if (c_path.isEmpty())
1643  return QString();
1644 #else
1645  bool ok;
1646  QString retval = getWin32RegistryValue(HKEY_LOCAL_MACHINE, "Software\\kde", "KDEDIRS", &ok);
1647  if (!ok){
1648  return QString();
1649  } else {
1650  c_path = retval.toAscii();
1651  }
1652 #endif
1653  return QDir::fromNativeSeparators(QFile::decodeName(c_path));
1654 }
1655 
1656 #ifdef __linux__
1657 static QString executablePrefix()
1658 {
1659  char path_buffer[MAXPATHLEN + 1];
1660  path_buffer[MAXPATHLEN] = 0;
1661  int length = readlink ("/proc/self/exe", path_buffer, MAXPATHLEN);
1662  if (length == -1)
1663  return QString();
1664 
1665  path_buffer[length] = '\0';
1666 
1667  QString path = QFile::decodeName(path_buffer);
1668 
1669  if(path.isEmpty())
1670  return QString();
1671 
1672  int pos = path.lastIndexOf(QLatin1Char('/')); // Skip filename
1673  if(pos <= 0)
1674  return QString();
1675  pos = path.lastIndexOf(QLatin1Char('/'), pos - 1); // Skip last directory
1676  if(pos <= 0)
1677  return QString();
1678 
1679  return path.left(pos);
1680 }
1681 #endif
1682 
1683 void KStandardDirs::addResourcesFrom_krcdirs()
1684 {
1685  QString localFile = QDir::currentPath() + QLatin1String("/.krcdirs");
1686  if (!QFile::exists(localFile))
1687  return;
1688 
1689  QSettings iniFile(localFile, QSettings::IniFormat);
1690  iniFile.beginGroup(QString::fromLatin1("KStandardDirs"));
1691  const QStringList resources = iniFile.allKeys();
1692  foreach(const QString &key, resources)
1693  {
1694  QDir path(iniFile.value(key).toString());
1695  if (!path.exists())
1696  continue;
1697 
1698  if(path.makeAbsolute())
1699  addResourceDir(key.toAscii(), path.path(), false);
1700  }
1701 }
1702 
1703 void KStandardDirs::addKDEDefaults()
1704 {
1705  addResourcesFrom_krcdirs();
1706 
1707  QStringList kdedirList;
1708  // begin KDEDIRS
1709  QString kdedirs = readEnvPath("KDEDIRS");
1710 
1711  if (!kdedirs.isEmpty())
1712  {
1713  tokenize(kdedirList, kdedirs, QString(QLatin1Char(KPATH_SEPARATOR)));
1714  }
1715  kdedirList.append(installPath("kdedir"));
1716 
1717  QString execPrefix(QFile::decodeName(EXEC_INSTALL_PREFIX));
1718  if (!execPrefix.isEmpty() && !kdedirList.contains(execPrefix))
1719  kdedirList.append(execPrefix);
1720 #ifdef __linux__
1721  const QString linuxExecPrefix = executablePrefix();
1722  if ( !linuxExecPrefix.isEmpty() )
1723  kdedirList.append( linuxExecPrefix );
1724 #endif
1725 
1726  // We treat root differently to prevent a "su" shell messing up the
1727  // file permissions in the user's home directory.
1728  QString localKdeDir = readEnvPath(getuid() ? "KDEHOME" : "KDEROOTHOME");
1729  if (!localKdeDir.isEmpty()) {
1730  if (!localKdeDir.endsWith(QLatin1Char('/')))
1731  localKdeDir += QLatin1Char('/');
1732  } else {
1733  // TODO KDE5: make localKdeDir equal to localXdgDir (which is determined further below and
1734  // defaults to ~/.config) + '/' + $KDECONFIG (which would default to e.g. "KDE")
1735  // This would mean ~/.config/KDE/ by default, more xdg-compliant.
1736 
1737 #if defined(Q_WS_MACX)
1738  localKdeDir = QDir::homePath() + QLatin1String("/Library/Preferences/KDE/");
1739 #elif defined(Q_WS_WIN)
1740 #ifndef _WIN32_WCE
1741  WCHAR wPath[MAX_PATH+1];
1742  if ( SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wPath) == S_OK) {
1743  localKdeDir = QDir::fromNativeSeparators(QString::fromUtf16((const ushort *) wPath)) + QLatin1Char('/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char('/');
1744  } else {
1745 #endif
1746  localKdeDir = QDir::homePath() + QLatin1Char('/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char('/');
1747 #ifndef _WIN32_WCE
1748  }
1749 #endif
1750 #else
1751  localKdeDir = QDir::homePath() + QLatin1Char('/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char('/');
1752 #endif
1753  }
1754 
1755  if (localKdeDir != QLatin1String("-/"))
1756  {
1757  localKdeDir = KShell::tildeExpand(localKdeDir);
1758  addPrefix(localKdeDir);
1759  }
1760 
1761 #ifdef Q_WS_MACX
1762  // Adds the "Contents" directory of the current application bundle to
1763  // the search path. This way bundled resources can be found.
1764  QDir bundleDir(mac_app_filename());
1765  if (bundleDir.dirName() == QLatin1String("MacOS")) { // just to be sure we're in a bundle
1766  bundleDir.cdUp();
1767  // now dirName should be "Contents". In there we can find our normal
1768  // dir-structure, beginning with "share"
1769  addPrefix(bundleDir.absolutePath());
1770  }
1771 #endif
1772 
1773  QStringList::ConstIterator end(kdedirList.end());
1774  for (QStringList::ConstIterator it = kdedirList.constBegin();
1775  it != kdedirList.constEnd(); ++it)
1776  {
1777  const QString dir = KShell::tildeExpand(*it);
1778  addPrefix(dir);
1779  }
1780  // end KDEDIRS
1781 
1782  // begin XDG_CONFIG_XXX
1783  QStringList xdgdirList;
1784  QString xdgdirs = readEnvPath("XDG_CONFIG_DIRS");
1785  if (!xdgdirs.isEmpty())
1786  {
1787  tokenize(xdgdirList, xdgdirs, QString(QLatin1Char(KPATH_SEPARATOR)));
1788  }
1789  else
1790  {
1791  xdgdirList.clear();
1792  xdgdirList.append(QString::fromLatin1("/etc/xdg"));
1793 #ifdef Q_WS_WIN
1794  xdgdirList.append(installPath("kdedir") + QString::fromLatin1("etc/xdg"));
1795 #else
1796  xdgdirList.append(QFile::decodeName(KDESYSCONFDIR "/xdg"));
1797 #endif
1798  }
1799 
1800  QString localXdgDir = readEnvPath("XDG_CONFIG_HOME");
1801  if (!localXdgDir.isEmpty()) {
1802  if (!localXdgDir.endsWith(QLatin1Char('/')))
1803  localXdgDir += QLatin1Char('/');
1804  } else {
1805 #ifdef Q_WS_MACX
1806  localXdgDir = QDir::homePath() + QString::fromLatin1("/Library/Preferences/XDG/");
1807 #else
1808  localXdgDir = QDir::homePath() + QString::fromLatin1("/.config/");
1809 #endif
1810  }
1811 
1812  localXdgDir = KShell::tildeExpand(localXdgDir);
1813  addXdgConfigPrefix(localXdgDir);
1814 
1815  for (QStringList::ConstIterator it = xdgdirList.constBegin();
1816  it != xdgdirList.constEnd(); ++it)
1817  {
1818  QString dir = KShell::tildeExpand(*it);
1819  addXdgConfigPrefix(dir);
1820  }
1821  // end XDG_CONFIG_XXX
1822 
1823  // begin XDG_DATA_XXX
1824  QStringList kdedirDataDirs;
1825  for (QStringList::ConstIterator it = kdedirList.constBegin();
1826  it != kdedirList.constEnd(); ++it) {
1827  QString dir = *it;
1828  if (!dir.endsWith(QLatin1Char('/')))
1829  dir += QLatin1Char('/');
1830  kdedirDataDirs.append(dir + QLatin1String("share/"));
1831  }
1832 
1833  xdgdirs = readEnvPath("XDG_DATA_DIRS");
1834  if (!xdgdirs.isEmpty()) {
1835  tokenize(xdgdirList, xdgdirs, QString(QLatin1Char(KPATH_SEPARATOR)));
1836  // Ensure the kdedirDataDirs are in there too,
1837  // otherwise resourceDirs() will add kdedir/share/applications/kde4
1838  // as returned by installPath(), and that's incorrect.
1839  Q_FOREACH(const QString& dir, kdedirDataDirs) {
1840  if (!xdgdirList.contains(dir))
1841  xdgdirList.append(dir);
1842  }
1843  } else {
1844  xdgdirList = kdedirDataDirs;
1845 #ifndef Q_WS_WIN
1846  xdgdirList.append(QString::fromLatin1("/usr/local/share/"));
1847  xdgdirList.append(QString::fromLatin1("/usr/share/"));
1848 #endif
1849  }
1850 
1851  localXdgDir = readEnvPath("XDG_DATA_HOME");
1852  if (!localXdgDir.isEmpty())
1853  {
1854  if (localXdgDir[localXdgDir.length()-1] != QLatin1Char('/'))
1855  localXdgDir += QLatin1Char('/');
1856  }
1857  else
1858  {
1859  localXdgDir = QDir::homePath() + QLatin1String("/.local/share/");
1860  }
1861 
1862  localXdgDir = KShell::tildeExpand(localXdgDir);
1863  addXdgDataPrefix(localXdgDir);
1864 
1865  for (QStringList::ConstIterator it = xdgdirList.constBegin();
1866  it != xdgdirList.constEnd(); ++it)
1867  {
1868  QString dir = KShell::tildeExpand(*it);
1869  addXdgDataPrefix(dir);
1870  }
1871  // end XDG_DATA_XXX
1872 
1873 
1874  addResourceType("lib", 0, "lib" KDELIBSUFF "/");
1875 
1876  uint index = 0;
1877  while (types_indices[index] != -1) {
1878  addResourceType(types_string + types_indices[index], 0, types_string + types_indices[index+1], true);
1879  index+=2;
1880  }
1881  addResourceType("exe", 0, "libexec/kde4", true );
1882 
1883  addResourceDir("home", QDir::homePath(), false);
1884 
1885  addResourceType("autostart", "xdgconf-autostart", "/"); // merge them, start with xdg autostart
1886  addResourceType("autostart", NULL, "share/autostart"); // KDE ones are higher priority
1887 }
1888 
1889 static QStringList lookupProfiles(const QString &mapFile)
1890 {
1891  QStringList profiles;
1892 
1893  if (mapFile.isEmpty() || !QFile::exists(mapFile))
1894  {
1895  profiles << QString::fromLatin1("default");
1896  return profiles;
1897  }
1898 
1899  struct passwd *pw = getpwuid(geteuid());
1900  if (!pw)
1901  {
1902  profiles << QString::fromLatin1("default");
1903  return profiles; // Not good
1904  }
1905 
1906  QByteArray user = pw->pw_name;
1907 
1908  gid_t sup_gids[512];
1909  int sup_gids_nr = getgroups(512, sup_gids);
1910 
1911  KConfig mapCfgFile(mapFile);
1912  KConfigGroup mapCfg(&mapCfgFile, "Users");
1913  if (mapCfg.hasKey(user.constData()))
1914  {
1915  profiles = mapCfg.readEntry(user.constData(), QStringList());
1916  return profiles;
1917  }
1918 
1919  const KConfigGroup generalGrp(&mapCfgFile, "General");
1920  const QStringList groups = generalGrp.readEntry("groups", QStringList());
1921 
1922  const KConfigGroup groupsGrp(&mapCfgFile, "Groups");
1923 
1924  for( QStringList::ConstIterator it = groups.begin();
1925  it != groups.end(); ++it )
1926  {
1927  QByteArray grp = (*it).toUtf8();
1928  // Check if user is in this group
1929  struct group *grp_ent = getgrnam(grp);
1930  if (!grp_ent) continue;
1931  gid_t gid = grp_ent->gr_gid;
1932  if (pw->pw_gid == gid)
1933  {
1934  // User is in this group --> add profiles
1935  profiles += groupsGrp.readEntry(*it, QStringList());
1936  }
1937  else
1938  {
1939  for(int i = 0; i < sup_gids_nr; i++)
1940  {
1941  if (sup_gids[i] == gid)
1942  {
1943  // User is in this group --> add profiles
1944  profiles += groupsGrp.readEntry(*it, QStringList());
1945  break;
1946  }
1947  }
1948  }
1949  }
1950 
1951  if (profiles.isEmpty())
1952  profiles << QString::fromLatin1("default");
1953  return profiles;
1954 }
1955 
1956 extern bool kde_kiosk_admin;
1957 
1958 bool KStandardDirs::addCustomized(KConfig *config)
1959 {
1960  if (!d->m_checkRestrictions) // there are already customized entries
1961  return false; // we just quit and hope they are the right ones
1962 
1963  // save the numbers of config directories. If this changes,
1964  // we will return true to give KConfig a chance to reparse
1965  int configdirs = resourceDirs("config").count();
1966 
1967  if (true)
1968  {
1969  // reading the prefixes in
1970  QString group = QLatin1String("Directories");
1971  KConfigGroup cg(config, group);
1972 
1973  QString kioskAdmin = cg.readEntry("kioskAdmin");
1974  if (!kioskAdmin.isEmpty() && !kde_kiosk_admin)
1975  {
1976  int i = kioskAdmin.indexOf(QLatin1Char(':'));
1977  QString user = kioskAdmin.left(i);
1978  QString host = kioskAdmin.mid(i+1);
1979 
1980  KUser thisUser;
1981  char hostname[ 256 ];
1982  hostname[ 0 ] = '\0';
1983  if (!gethostname( hostname, 255 ))
1984  hostname[sizeof(hostname)-1] = '\0';
1985 
1986  if ((user == thisUser.loginName()) &&
1987  (host.isEmpty() || (host == QLatin1String(hostname))))
1988  {
1989  kde_kiosk_admin = true;
1990  }
1991  }
1992 
1993  bool readProfiles = true;
1994 
1995  if (kde_kiosk_admin && !qgetenv("KDE_KIOSK_NO_PROFILES").isEmpty())
1996  readProfiles = false;
1997 
1998  QString userMapFile = cg.readEntry("userProfileMapFile");
1999  QString profileDirsPrefix = cg.readEntry("profileDirsPrefix");
2000  if (!profileDirsPrefix.isEmpty() && !profileDirsPrefix.endsWith(QLatin1Char('/')))
2001  profileDirsPrefix.append(QLatin1Char('/'));
2002 
2003  QStringList profiles;
2004  if (readProfiles)
2005  profiles = lookupProfiles(userMapFile);
2006  QString profile;
2007 
2008  bool priority = false;
2009  while(true)
2010  {
2011  KConfigGroup cg(config, group);
2012  const QStringList list = cg.readEntry("prefixes", QStringList());
2013  for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
2014  {
2015  addPrefix(*it, priority);
2016  addXdgConfigPrefix(*it + QLatin1String("/etc/xdg"), priority);
2017  addXdgDataPrefix(*it + QLatin1String("/share"), priority);
2018  }
2019  // If there are no prefixes defined, check if there is a directory
2020  // for this profile under <profileDirsPrefix>
2021  if (list.isEmpty() && !profile.isEmpty() && !profileDirsPrefix.isEmpty())
2022  {
2023  QString dir = profileDirsPrefix + profile;
2024  addPrefix(dir, priority);
2025  addXdgConfigPrefix(dir + QLatin1String("/etc/xdg"), priority);
2026  addXdgDataPrefix(dir + QLatin1String("/share"), priority);
2027  }
2028 
2029  // iterating over all entries in the group Directories
2030  // to find entries that start with dir_$type
2031  const QMap<QString, QString> entries = config->entryMap(group);
2032  for (QMap<QString, QString>::ConstIterator it2 = entries.begin();
2033  it2 != entries.end(); ++it2)
2034  {
2035  const QString key = it2.key();
2036  if (key.startsWith(QLatin1String("dir_"))) {
2037  // generate directory list, there may be more than 1.
2038  const QStringList dirs = (*it2).split(QString(QLatin1Char(',')));
2039  QStringList::ConstIterator sIt(dirs.begin());
2040  QString resType = key.mid(4);
2041  for (; sIt != dirs.end(); ++sIt)
2042  {
2043  addResourceDir(resType.toLatin1(), *sIt, priority);
2044  }
2045  }
2046  }
2047  if (profiles.isEmpty())
2048  break;
2049  profile = profiles.back();
2050  group = QString::fromLatin1("Directories-%1").arg(profile);
2051  profiles.pop_back();
2052  priority = true;
2053  }
2054  }
2055 
2056  // Process KIOSK restrictions.
2057  if (!kde_kiosk_admin || qgetenv("KDE_KIOSK_NO_RESTRICTIONS").isEmpty())
2058  {
2059  KConfigGroup cg(config, "KDE Resource Restrictions");
2060  const QMap<QString, QString> entries = cg.entryMap();
2061  for (QMap<QString, QString>::ConstIterator it2 = entries.begin();
2062  it2 != entries.end(); ++it2)
2063  {
2064  const QString key = it2.key();
2065  if (!cg.readEntry(key, true))
2066  {
2067  d->m_restrictionsActive = true;
2068  const QByteArray cKey = key.toLatin1();
2069  d->m_restrictions.insert(cKey, true);
2070  d->m_dircache.remove(cKey);
2071  d->m_savelocations.remove(cKey);
2072  }
2073  }
2074  }
2075 
2076  // check if the number of config dirs changed
2077  bool configDirsChanged = (resourceDirs("config").count() != configdirs);
2078  // If the config dirs changed, we check kiosk restrictions again.
2079  d->m_checkRestrictions = configDirsChanged;
2080  // return true if the number of config dirs changed: reparse config file
2081  return configDirsChanged;
2082 }
2083 
2084 QString KStandardDirs::localkdedir() const
2085 {
2086  // Return the prefix to use for saving
2087  return d->m_prefixes.first();
2088 }
2089 
2090 QString KStandardDirs::localxdgdatadir() const
2091 {
2092  // Return the prefix to use for saving
2093  return d->xdgdata_prefixes.first();
2094 }
2095 
2096 QString KStandardDirs::localxdgconfdir() const
2097 {
2098  // Return the prefix to use for saving
2099  return d->xdgconf_prefixes.first();
2100 }
2101 
2102 
2103 // just to make code more readable without macros
2104 QString KStandardDirs::locate( const char *type,
2105  const QString& filename, const KComponentData &cData)
2106 {
2107  return cData.dirs()->findResource(type, filename);
2108 }
2109 
2110 QString KStandardDirs::locateLocal( const char *type,
2111  const QString& filename, const KComponentData &cData)
2112 {
2113  return locateLocal(type, filename, true, cData);
2114 }
2115 
2116 QString KStandardDirs::locateLocal( const char *type,
2117  const QString& filename, bool createDir,
2118  const KComponentData &cData)
2119 {
2120  // try to find slashes. If there are some, we have to
2121  // create the subdir first
2122  int slash = filename.lastIndexOf(QLatin1Char('/')) + 1;
2123  if (!slash) { // only one filename
2124  return cData.dirs()->saveLocation(type, QString(), createDir) + filename;
2125  }
2126 
2127  // split path from filename
2128  QString dir = filename.left(slash);
2129  QString file = filename.mid(slash);
2130  return cData.dirs()->saveLocation(type, dir, createDir) + file;
2131 }
2132 
2133 bool KStandardDirs::checkAccess(const QString& pathname, int mode)
2134 {
2135  int accessOK = KDE::access( pathname, mode );
2136  if ( accessOK == 0 )
2137  return true; // OK, I can really access the file
2138 
2139  // else
2140  // if we want to write the file would be created. Check, if the
2141  // user may write to the directory to create the file.
2142  if ( (mode & W_OK) == 0 )
2143  return false; // Check for write access is not part of mode => bail out
2144 
2145 
2146  if (!KDE::access( pathname, F_OK)) // if it already exists
2147  return false;
2148 
2149  //strip the filename (everything until '/' from the end
2150  QString dirName(pathname);
2151  int pos = dirName.lastIndexOf(QLatin1Char('/'));
2152  if ( pos == -1 )
2153  return false; // No path in argument. This is evil, we won't allow this
2154  else if ( pos == 0 ) // don't turn e.g. /root into an empty string
2155  pos = 1;
2156 
2157  dirName.truncate(pos); // strip everything starting from the last '/'
2158 
2159  accessOK = KDE::access( dirName, W_OK );
2160  // -?- Can I write to the accessed diretory
2161  if ( accessOK == 0 )
2162  return true; // Yes
2163  else
2164  return false; // No
2165 }
2166 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Sep 24 2012 11:20:34 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.9.1 API Reference

Skip menu "kdelibs-4.9.1 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal