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

tdecore

  • tdecore
kstandarddirs.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Sirtaj Singh Kang <taj@kde.org>
3  Copyright (C) 1999 Stephan Kulow <coolo@kde.org>
4  Copyright (C) 1999 Waldo Bastian <bastian@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 version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 /*
22  * Author: Stephan Kulow <coolo@kde.org> and Sirtaj Singh Kang <taj@kde.org>
23  * Version: $Id$
24  * Generated: Thu Mar 5 16:05:28 EST 1998
25  */
26 
27 #include "config.h"
28 
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <errno.h>
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <dirent.h>
38 #include <pwd.h>
39 #include <grp.h>
40 
41 #include <tqregexp.h>
42 #include <tqasciidict.h>
43 #include <tqdict.h>
44 #include <tqdir.h>
45 #include <tqfileinfo.h>
46 #include <tqstring.h>
47 #include <tqstringlist.h>
48 
49 #include "kstandarddirs.h"
50 #include "tdeconfig.h"
51 #include "kinstance.h"
52 #include "kshell.h"
53 #include "ksimpleconfig.h"
54 #include "kuser.h"
55 #include "kstaticdeleter.h"
56 #include <kde_file.h>
57 
58 template class TQDict<TQStringList>;
59 
60 class TDEStandardDirs::TDEStandardDirsPrivate
61 {
62 public:
63  TDEStandardDirsPrivate()
64  : restrictionsActive(false),
65  dataRestrictionActive(false),
66  checkRestrictions(true)
67  { }
68 
69  bool restrictionsActive;
70  bool dataRestrictionActive;
71  bool checkRestrictions;
72  TQAsciiDict<bool> restrictions;
73  TQStringList xdgdata_prefixes;
74  TQStringList xdgconf_prefixes;
75 };
76 
77 // Singleton, with data shared by all kstandarddirs instances.
78 // Used in static methods like findExe()
79 class TDEStandardDirsSingleton
80 {
81 public:
82  TQString defaultprefix;
83  TQString defaultbindir;
84  static TDEStandardDirsSingleton* self();
85 private:
86  static TDEStandardDirsSingleton* s_self;
87 };
88 static KStaticDeleter<TDEStandardDirsSingleton> kstds_sd;
89 TDEStandardDirsSingleton* TDEStandardDirsSingleton::s_self = 0;
90 TDEStandardDirsSingleton* TDEStandardDirsSingleton::self() {
91  if ( !s_self )
92  kstds_sd.setObject( s_self, new TDEStandardDirsSingleton );
93  return s_self;
94 }
95 
96 static const char* const types[] = {"html", "html-bundle", "icon", "apps", "sound",
97  "data", "locale", "locale-bundle", "services", "mime",
98  "servicetypes", "config", "exe",
99  "wallpaper", "lib", "pixmap", "templates",
100  "module", "qtplugins",
101  "xdgdata-apps", "xdgdata-dirs", "xdgconf-menu",
102  "xdgdata-icon", "xdgdata-pixmap", "xdgconf-autostart",
103  "kcfg", "emoticons", 0 };
104 
105 static int tokenize( TQStringList& token, const TQString& str,
106  const TQString& delim );
107 
108 TDEStandardDirs::TDEStandardDirs( ) : addedCustoms(false)
109 {
110  d = new TDEStandardDirsPrivate;
111  dircache.setAutoDelete(true);
112  relatives.setAutoDelete(true);
113  absolutes.setAutoDelete(true);
114  savelocations.setAutoDelete(true);
115  addKDEDefaults();
116 }
117 
118 TDEStandardDirs::~TDEStandardDirs()
119 {
120  delete d;
121 }
122 
123 bool TDEStandardDirs::isRestrictedResource(const char *type, const TQString& relPath) const
124 {
125  if (!d || !d->restrictionsActive)
126  return false;
127 
128  if (d->restrictions[type])
129  return true;
130 
131  if (strcmp(type, "data")==0)
132  {
133  applyDataRestrictions(relPath);
134  if (d->dataRestrictionActive)
135  {
136  d->dataRestrictionActive = false;
137  return true;
138  }
139  }
140  return false;
141 }
142 
143 void TDEStandardDirs::applyDataRestrictions(const TQString &relPath) const
144 {
145  TQString key;
146  int i = relPath.find(QChar('/'));
147  if (i != -1)
148  key = "data_"+relPath.left(i);
149  else
150  key = "data_"+relPath;
151 
152  if (d && d->restrictions[key.latin1()])
153  d->dataRestrictionActive = true;
154 }
155 
156 
157 TQStringList TDEStandardDirs::allTypes() const
158 {
159  TQStringList list;
160  for (int i = 0; types[i] != 0; ++i)
161  list.append(TQString::fromLatin1(types[i]));
162  return list;
163 }
164 
165 static void priorityAdd(TQStringList &prefixes, const TQString& dir, bool priority)
166 {
167  if (priority && !prefixes.isEmpty())
168  {
169  // Add in front but behind $TDEHOME
170  TQStringList::iterator it = prefixes.begin();
171  it++;
172  prefixes.insert(it, 1, dir);
173  }
174  else
175  {
176  prefixes.append(dir);
177  }
178 }
179 
180 void TDEStandardDirs::addPrefix( const TQString& _dir )
181 {
182  addPrefix(_dir, false);
183 }
184 
185 void TDEStandardDirs::addPrefix( const TQString& _dir, bool priority )
186 {
187  if (_dir.isEmpty())
188  return;
189 
190  TQString dir = _dir;
191  if (dir.at(dir.length() - 1) != QChar('/'))
192  dir += QChar('/');
193 
194  if (!prefixes.contains(dir)) {
195  priorityAdd(prefixes, dir, priority);
196  dircache.clear();
197  }
198 }
199 
200 void TDEStandardDirs::addXdgConfigPrefix( const TQString& _dir )
201 {
202  addXdgConfigPrefix(_dir, false);
203 }
204 
205 void TDEStandardDirs::addXdgConfigPrefix( const TQString& _dir, bool priority )
206 {
207  if (_dir.isEmpty())
208  return;
209 
210  TQString dir = _dir;
211  if (dir.at(dir.length() - 1) != QChar('/'))
212  dir += QChar('/');
213 
214  if (!d->xdgconf_prefixes.contains(dir)) {
215  priorityAdd(d->xdgconf_prefixes, dir, priority);
216  dircache.clear();
217  }
218 }
219 
220 void TDEStandardDirs::addXdgDataPrefix( const TQString& _dir )
221 {
222  addXdgDataPrefix(_dir, false);
223 }
224 
225 void TDEStandardDirs::addXdgDataPrefix( const TQString& _dir, bool priority )
226 {
227  if (_dir.isEmpty())
228  return;
229 
230  TQString dir = _dir;
231  if (dir.at(dir.length() - 1) != QChar('/'))
232  dir += QChar('/');
233 
234  if (!d->xdgdata_prefixes.contains(dir)) {
235  priorityAdd(d->xdgdata_prefixes, dir, priority);
236  dircache.clear();
237  }
238 }
239 
240 TQString TDEStandardDirs::kfsstnd_prefixes()
241 {
242  return prefixes.join(TQChar(KPATH_SEPARATOR));
243 }
244 
245 TQString TDEStandardDirs::kfsstnd_xdg_conf_prefixes()
246 {
247  return d->xdgconf_prefixes.join(TQChar(KPATH_SEPARATOR));
248 }
249 
250 TQString TDEStandardDirs::kfsstnd_xdg_data_prefixes()
251 {
252  return d->xdgdata_prefixes.join(TQChar(KPATH_SEPARATOR));
253 }
254 
255 bool TDEStandardDirs::addResourceType( const char *type,
256  const TQString& relativename )
257 {
258  return addResourceType(type, relativename, true);
259 }
260 bool TDEStandardDirs::addResourceType( const char *type,
261  const TQString& relativename,
262  bool priority )
263 {
264  if (relativename.isEmpty())
265  return false;
266 
267  TQStringList *rels = relatives.find(type);
268  if (!rels) {
269  rels = new TQStringList();
270  relatives.insert(type, rels);
271  }
272  TQString copy = relativename;
273  if (copy.at(copy.length() - 1) != QChar('/'))
274  copy += QChar('/');
275  if (!rels->contains(copy)) {
276  if (priority)
277  rels->prepend(copy);
278  else
279  rels->append(copy);
280  dircache.remove(type); // clean the cache
281  return true;
282  }
283  return false;
284 }
285 
286 bool TDEStandardDirs::addResourceDir( const char *type,
287  const TQString& absdir)
288 {
289  // KDE4: change priority to bring in line with addResourceType
290  return addResourceDir(type, absdir, false);
291 }
292 
293 bool TDEStandardDirs::addResourceDir( const char *type,
294  const TQString& absdir,
295  bool priority)
296 {
297  TQStringList *paths = absolutes.find(type);
298  if (!paths) {
299  paths = new TQStringList();
300  absolutes.insert(type, paths);
301  }
302  TQString copy = absdir;
303  if (copy.at(copy.length() - 1) != QChar('/'))
304  copy += QChar('/');
305 
306  if (!paths->contains(copy)) {
307  if (priority)
308  paths->prepend(copy);
309  else
310  paths->append(copy);
311  dircache.remove(type); // clean the cache
312  return true;
313  }
314  return false;
315 }
316 
317 TQString TDEStandardDirs::findResource( const char *type,
318  const TQString& filename ) const
319 {
320  if (!TQDir::isRelativePath(filename))
321  return filename; // absolute dirs are absolute dirs, right? :-/
322 
323 #if 0
324 kdDebug() << "Find resource: " << type << endl;
325 for (TQStringList::ConstIterator pit = prefixes.begin();
326  pit != prefixes.end();
327  pit++)
328 {
329  kdDebug() << "Prefix: " << *pit << endl;
330 }
331 #endif
332 
333  TQString dir = findResourceDir(type, filename);
334  if (dir.isEmpty())
335  return dir;
336  else return dir + filename;
337 }
338 
339 static TQ_UINT32 updateHash(const TQString &file, TQ_UINT32 hash)
340 {
341  TQCString cFile = TQFile::encodeName(file);
342  KDE_struct_stat buff;
343  if ((access(cFile, R_OK) == 0) &&
344  (KDE_stat( cFile, &buff ) == 0) &&
345  (S_ISREG( buff.st_mode )))
346  {
347  hash = hash + (TQ_UINT32) buff.st_ctime;
348  }
349  return hash;
350 }
351 
352 TQ_UINT32 TDEStandardDirs::calcResourceHash( const char *type,
353  const TQString& filename, bool deep) const
354 {
355  TQ_UINT32 hash = 0;
356 
357  if (!TQDir::isRelativePath(filename))
358  {
359  // absolute dirs are absolute dirs, right? :-/
360  return updateHash(filename, hash);
361  }
362  if (d && d->restrictionsActive && (strcmp(type, "data")==0))
363  applyDataRestrictions(filename);
364  TQStringList candidates = resourceDirs(type);
365  TQString fullPath;
366 
367  for (TQStringList::ConstIterator it = candidates.begin();
368  it != candidates.end(); ++it)
369  {
370  hash = updateHash(*it + filename, hash);
371  if (!deep && hash)
372  return hash;
373  }
374  return hash;
375 }
376 
377 
378 TQStringList TDEStandardDirs::findDirs( const char *type,
379  const TQString& reldir ) const
380 {
381  TQDir testdir;
382  TQStringList list;
383  if (!TQDir::isRelativePath(reldir))
384  {
385  testdir.setPath(reldir);
386  if (testdir.exists())
387  {
388  if (reldir.endsWith("/"))
389  list.append(reldir);
390  else
391  list.append(reldir+QChar('/'));
392  }
393  return list;
394  }
395 
396  checkConfig();
397 
398  if (d && d->restrictionsActive && (strcmp(type, "data")==0))
399  applyDataRestrictions(reldir);
400  TQStringList candidates = resourceDirs(type);
401 
402  for (TQStringList::ConstIterator it = candidates.begin();
403  it != candidates.end(); ++it) {
404  testdir.setPath(*it + reldir);
405  if (testdir.exists())
406  list.append(testdir.absPath() + QChar('/'));
407  }
408 
409  return list;
410 }
411 
412 TQString TDEStandardDirs::findResourceDir( const char *type,
413  const TQString& filename) const
414 {
415 #ifndef NDEBUG
416  if (filename.isEmpty()) {
417  fprintf(stderr, "filename for type %s in TDEStandardDirs::findResourceDir is not supposed to be empty!!", type);
418  return TQString::null;
419  }
420 #endif
421 
422  if (d && d->restrictionsActive && (strcmp(type, "data")==0))
423  applyDataRestrictions(filename);
424  TQStringList candidates = resourceDirs(type);
425  TQString fullPath;
426 
427  for (TQStringList::ConstIterator it = candidates.begin();
428  it != candidates.end(); ++it) {
429  if (exists(*it + filename)) {
430 #ifdef Q_WS_WIN //this ensures we're using installed .la files
431  if ((*it).isEmpty() && filename.right(3)==".la") {
432 #ifndef NDEBUG
433  fprintf(stderr, "TDEStandardDirs::findResourceDir() found .la in cwd: skipping. (fname=%s)\n", filename.ascii());
434 #endif
435  continue;
436  }
437 #endif //Q_WS_WIN
438  return *it;
439  }
440  }
441 
442 #ifndef NDEBUG
443  if(false && strcmp(type, "locale"))
444  fprintf(stderr, "KStdDirs::findResDir(): can't find \"%s\" in type \"%s\".\n", filename.ascii(), type);
445 #endif
446 
447  return TQString::null;
448 }
449 
450 bool TDEStandardDirs::exists(const TQString &fullPath)
451 {
452  KDE_struct_stat buff;
453  if ((access(TQFile::encodeName(fullPath), R_OK) == 0) && (KDE_stat( TQFile::encodeName(fullPath), &buff ) == 0)) {
454  if (fullPath.at(fullPath.length() - 1) != QChar('/')) {
455  if (S_ISREG( buff.st_mode ))
456  return true;
457  }
458  else {
459  if (S_ISDIR( buff.st_mode )) {
460  return true;
461  }
462  }
463  }
464  return false;
465 }
466 
467 static void lookupDirectory(const TQString& path, const TQString &relPart,
468  const TQRegExp &regexp,
469  TQStringList& list,
470  TQStringList& relList,
471  bool recursive, bool unique)
472 {
473  TQString pattern = regexp.pattern();
474  if (recursive || pattern.contains('?') || pattern.contains('*'))
475  {
476  if (path.isEmpty()) //for sanity
477  return;
478  // We look for a set of files.
479  DIR *dp = opendir( TQFile::encodeName(path));
480  if (!dp)
481  return;
482 
483 #ifdef Q_WS_WIN
484  assert(path.at(path.length() - 1) == QChar('/') || path.at(path.length() - 1) == QChar('\\'));
485 #else
486  assert(path.at(path.length() - 1) == QChar('/'));
487 #endif
488 
489  struct dirent *ep;
490  KDE_struct_stat buff;
491 
492  TQString _dot(".");
493  TQString _dotdot("..");
494 
495  while( ( ep = readdir( dp ) ) != 0L )
496  {
497  TQString fn( TQFile::decodeName(ep->d_name));
498  if (fn == _dot || fn == _dotdot || TQChar(fn.at(fn.length() - 1)).latin1() == TQChar('~').latin1())
499  continue;
500 
501  if (!recursive && !regexp.exactMatch(fn))
502  continue; // No match
503 
504  TQString pathfn = path + fn;
505  if ( KDE_stat( TQFile::encodeName(pathfn), &buff ) != 0 ) {
506  fprintf(stderr, "Error stat'ing %s : %d\n", pathfn.ascii(), errno);
507  continue; // Couldn't stat (e.g. no read permissions)
508  }
509  if ( recursive ) {
510  if ( S_ISDIR( buff.st_mode )) {
511  lookupDirectory(pathfn + QChar('/'), relPart + fn + QChar('/'), regexp, list, relList, recursive, unique);
512  }
513  if (!regexp.exactMatch(fn))
514  continue; // No match
515  }
516  if ( S_ISREG( buff.st_mode))
517  {
518  if (!unique || !relList.contains(relPart + fn))
519  {
520  list.append( pathfn );
521  relList.append( relPart + fn );
522  }
523  }
524  }
525  closedir( dp );
526  }
527  else
528  {
529  // We look for a single file.
530  TQString fn = pattern;
531  TQString pathfn = path + fn;
532  KDE_struct_stat buff;
533  if ( KDE_stat( TQFile::encodeName(pathfn), &buff ) != 0 )
534  return; // File not found
535  if ( S_ISREG( buff.st_mode))
536  {
537  if (!unique || !relList.contains(relPart + fn))
538  {
539  list.append( pathfn );
540  relList.append( relPart + fn );
541  }
542  }
543  }
544 }
545 
546 static void lookupPrefix(const TQString& prefix, const TQString& relpath,
547  const TQString& relPart,
548  const TQRegExp &regexp,
549  TQStringList& list,
550  TQStringList& relList,
551  bool recursive, bool unique)
552 {
553  if (relpath.isEmpty()) {
554  lookupDirectory(prefix, relPart, regexp, list,
555  relList, recursive, unique);
556  return;
557  }
558  TQString path;
559  TQString rest;
560 
561  if (relpath.length())
562  {
563  int slash = relpath.find(QChar('/'));
564  if (slash < 0)
565  rest = relpath.left(relpath.length() - 1);
566  else {
567  path = relpath.left(slash);
568  rest = relpath.mid(slash + 1);
569  }
570  }
571 
572  if (prefix.isEmpty()) //for sanity
573  return;
574 #ifdef Q_WS_WIN
575  assert(prefix.at(prefix.length() - 1) == QChar('/') || prefix.at(prefix.length() - 1) == QChar('\\'));
576 #else
577  assert(prefix.at(prefix.length() - 1) == QChar('/'));
578 #endif
579  KDE_struct_stat buff;
580 
581  if (path.contains('*') || path.contains('?')) {
582 
583  TQRegExp pathExp(path, true, true);
584  DIR *dp = opendir( TQFile::encodeName(prefix) );
585  if (!dp) {
586  return;
587  }
588 
589  struct dirent *ep;
590 
591  TQString _dot(".");
592  TQString _dotdot("..");
593 
594  while( ( ep = readdir( dp ) ) != 0L )
595  {
596  TQString fn( TQFile::decodeName(ep->d_name));
597  if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1) == QChar('~'))
598  continue;
599 
600  if ( !pathExp.exactMatch(fn) )
601  continue; // No match
602  TQString rfn = relPart+fn;
603  fn = prefix + fn;
604  if ( KDE_stat( TQFile::encodeName(fn), &buff ) != 0 ) {
605  fprintf(stderr, "Error statting %s : %d\n", fn.ascii(), errno);
606  continue; // Couldn't stat (e.g. no permissions)
607  }
608  if ( S_ISDIR( buff.st_mode ))
609  lookupPrefix(fn + QChar('/'), rest, rfn + QChar('/'), regexp, list, relList, recursive, unique);
610  }
611 
612  closedir( dp );
613  } else {
614  // Don't stat, if the dir doesn't exist we will find out
615  // when we try to open it.
616  lookupPrefix(prefix + path + QChar('/'), rest,
617  relPart + path + QChar('/'), regexp, list,
618  relList, recursive, unique);
619  }
620 }
621 
622 TQStringList
623 TDEStandardDirs::findAllResources( const char *type,
624  const TQString& filter,
625  bool recursive,
626  bool unique,
627  TQStringList &relList) const
628 {
629  TQStringList list;
630  TQString filterPath;
631  TQString filterFile;
632 
633  if (filter.length())
634  {
635  int slash = filter.findRev('/');
636  if (slash < 0)
637  filterFile = filter;
638  else {
639  filterPath = filter.left(slash + 1);
640  filterFile = filter.mid(slash + 1);
641  }
642  }
643 
644  checkConfig();
645 
646  TQStringList candidates;
647  if (!TQDir::isRelativePath(filter)) // absolute path
648  {
649 #ifdef Q_OS_WIN
650  candidates << filterPath.left(3); //e.g. "C:\"
651  filterPath = filterPath.mid(3);
652 #else
653  candidates << "/";
654  filterPath = filterPath.mid(1);
655 #endif
656  }
657  else
658  {
659  if (d && d->restrictionsActive && (strcmp(type, "data")==0))
660  applyDataRestrictions(filter);
661  candidates = resourceDirs(type);
662  }
663  if (filterFile.isEmpty())
664  filterFile = "*";
665 
666  TQRegExp regExp(filterFile, true, true);
667 
668  for (TQStringList::ConstIterator it = candidates.begin();
669  it != candidates.end(); ++it)
670  {
671  lookupPrefix(*it, filterPath, "", regExp, list,
672  relList, recursive, unique);
673  }
674 
675  return list;
676 }
677 
678 TQStringList
679 TDEStandardDirs::findAllResources( const char *type,
680  const TQString& filter,
681  bool recursive,
682  bool unique) const
683 {
684  TQStringList relList;
685  return findAllResources(type, filter, recursive, unique, relList);
686 }
687 
688 TQString
689 TDEStandardDirs::realPath(const TQString &dirname)
690 {
691  char realpath_buffer[MAXPATHLEN + 1];
692  memset(realpath_buffer, 0, MAXPATHLEN + 1);
693 
694  /* If the path contains symlinks, get the real name */
695  if (realpath( TQFile::encodeName(dirname).data(), realpath_buffer) != 0) {
696  // success, use result from realpath
697  int len = strlen(realpath_buffer);
698  realpath_buffer[len] = TQChar('/');
699  realpath_buffer[len+1] = 0;
700  return TQFile::decodeName(realpath_buffer);
701  }
702 
703  return dirname;
704 }
705 
706 TQString
707 TDEStandardDirs::realFilePath(const TQString &filename)
708 {
709  char realpath_buffer[MAXPATHLEN + 1];
710  memset(realpath_buffer, 0, MAXPATHLEN + 1);
711 
712  /* If the path contains symlinks, get the real name */
713  if (realpath( TQFile::encodeName(filename).data(), realpath_buffer) != 0) {
714  // success, use result from realpath
715  return TQFile::decodeName(realpath_buffer);
716  }
717 
718  return filename;
719 }
720 
721 void TDEStandardDirs::createSpecialResource(const char *type)
722 {
723  char hostname[256];
724  hostname[0] = 0;
725  if( getenv("XAUTHLOCALHOSTNAME"))
726  strlcpy(hostname, getenv("XAUTHLOCALHOSTNAME"), 255 );
727  else
728  gethostname(hostname, 255);
729  TQString dir = TQString("%1%2-%3").arg(localtdedir()).arg(type).arg(hostname);
730  char link[1024];
731  link[1023] = 0;
732  int result = readlink(TQFile::encodeName(dir).data(), link, 1023);
733  bool relink = (result == -1) && (errno == ENOENT);
734  if (result > 0)
735  {
736  link[result] = 0;
737  if (!TQDir::isRelativePath(link))
738  {
739  KDE_struct_stat stat_buf;
740  int res = KDE_lstat(link, &stat_buf);
741  if ((res == -1) && (errno == ENOENT))
742  {
743  relink = true;
744  }
745  else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
746  {
747  fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
748  relink = true;
749  }
750  else if (stat_buf.st_uid != getuid())
751  {
752  fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
753  relink = true;
754  }
755  }
756  }
757 #ifdef Q_WS_WIN
758  if (relink)
759  {
760  if (!makeDir(dir, 0700))
761  fprintf(stderr, "failed to create \"%s\"", dir.latin1());
762  else
763  result = readlink(TQFile::encodeName(dir).data(), link, 1023);
764  }
765 #else //UNIX
766  if (relink)
767  {
768  TQString srv = findExe(TQString::fromLatin1("lnusertemp"), kfsstnd_defaultbindir());
769  if (srv.isEmpty())
770  srv = findExe(TQString::fromLatin1("lnusertemp"));
771  if (!srv.isEmpty())
772  {
773  if (system(TQFile::encodeName(srv)+" "+type) < 0 ) {
774  result = readlink(TQFile::encodeName(dir).data(), link, 1023);
775  }
776  else {
777  result = -1;
778  }
779  }
780  }
781  if (result > 0)
782  {
783  link[result] = 0;
784  if (link[0] == TQChar('/').latin1()) {
785  dir = TQFile::decodeName(link);
786  }
787  else {
788  dir = TQDir::cleanDirPath(dir+TQFile::decodeName(link));
789  }
790  }
791 #endif
792  addResourceDir(type, dir+QChar('/'));
793 }
794 
795 TQStringList TDEStandardDirs::resourceDirs(const char *type) const
796 {
797  TQStringList *candidates = dircache.find(type);
798 
799  if (!candidates) { // filling cache
800  if (strcmp(type, "socket") == 0)
801  const_cast<TDEStandardDirs *>(this)->createSpecialResource(type);
802  else if (strcmp(type, "tmp") == 0)
803  const_cast<TDEStandardDirs *>(this)->createSpecialResource(type);
804  else if (strcmp(type, "cache") == 0)
805  const_cast<TDEStandardDirs *>(this)->createSpecialResource(type);
806 
807  TQDir testdir;
808 
809  candidates = new TQStringList();
810  TQStringList *dirs;
811 
812  bool restrictionActive = false;
813  if (d && d->restrictionsActive)
814  {
815  if (d->dataRestrictionActive)
816  restrictionActive = true;
817  else if (d->restrictions["all"])
818  restrictionActive = true;
819  else if (d->restrictions[type])
820  restrictionActive = true;
821  d->dataRestrictionActive = false; // Reset
822  }
823 
824  dirs = relatives.find(type);
825  if (dirs)
826  {
827  bool local = true;
828  const TQStringList *prefixList = 0;
829  if (strncmp(type, "xdgdata-", 8) == 0)
830  prefixList = &(d->xdgdata_prefixes);
831  else if (strncmp(type, "xdgconf-", 8) == 0)
832  prefixList = &(d->xdgconf_prefixes);
833  else
834  prefixList = &prefixes;
835 
836  for (TQStringList::ConstIterator pit = prefixList->begin();
837  pit != prefixList->end();
838  ++pit)
839  {
840  for (TQStringList::ConstIterator it = dirs->begin();
841  it != dirs->end(); ++it) {
842  TQString path = realPath(*pit + *it);
843  testdir.setPath(path);
844  if (local && restrictionActive)
845  continue;
846  if ((local || testdir.exists()) && !candidates->contains(path))
847  candidates->append(path);
848  }
849  // UGLY HACK - Chris CHeney
850  if (local && (!strcmp("config", type)))
851  candidates->append("/etc/trinity/");
852  //
853  local = false;
854  }
855  }
856  dirs = absolutes.find(type);
857  if (dirs)
858  for (TQStringList::ConstIterator it = dirs->begin();
859  it != dirs->end(); ++it)
860  {
861  testdir.setPath(*it);
862  if (testdir.exists())
863  {
864  TQString filename = realPath(*it);
865  if (!candidates->contains(filename))
866  candidates->append(filename);
867  }
868  }
869  dircache.insert(type, candidates);
870  }
871 
872 #if 0
873  kdDebug() << "found dirs for resource " << type << ":" << endl;
874  for (TQStringList::ConstIterator pit = candidates->begin();
875  pit != candidates->end();
876  pit++)
877  {
878  fprintf(stderr, "%s\n", (*pit).latin1());
879  }
880 #endif
881 
882 
883  return *candidates;
884 }
885 
886 TQStringList TDEStandardDirs::systemPaths( const TQString& pstr )
887 {
888  TQStringList tokens;
889  TQString p = pstr;
890 
891  if( p.isNull() )
892  {
893  p = getenv( "PATH" );
894  }
895 
896  TQString delimiters(TQChar(KPATH_SEPARATOR));
897  delimiters += "\b";
898  tokenize( tokens, p, delimiters );
899 
900  TQStringList exePaths;
901 
902  // split path using : or \b as delimiters
903  for( unsigned i = 0; i < tokens.count(); i++ )
904  {
905  p = tokens[ i ];
906 
907  if ( p[ 0 ] == QChar('~') )
908  {
909  int len = p.find( QChar('/') );
910  if ( len == -1 )
911  len = p.length();
912  if ( len == 1 )
913  {
914  p.replace( 0, 1, TQDir::homeDirPath() );
915  }
916  else
917  {
918  TQString user = p.mid( 1, len - 1 );
919  struct passwd *dir = getpwnam( user.local8Bit().data() );
920  if ( dir && strlen( dir->pw_dir ) )
921  p.replace( 0, len, TQString::fromLocal8Bit( dir->pw_dir ) );
922  }
923  }
924 
925  exePaths << p;
926  }
927 
928  return exePaths;
929 }
930 
931 
932 TQString TDEStandardDirs::findExe( const TQString& appname,
933  const TQString& pstr, bool ignore)
934 {
935 #ifdef Q_WS_WIN
936  TQString real_appname = appname + ".exe";
937 #else
938  TQString real_appname = appname;
939 #endif
940  TQFileInfo info;
941 
942  // absolute or relative path given
943  if (real_appname.find(TQDir::separator()) >= 0)
944  {
945  info.setFile( real_appname );
946  if( info.exists() && ( ignore || info.isExecutable() )
947  && info.isFile() ) {
948  return info.absFilePath();
949  }
950  return TQString::null;
951  }
952 
953  TQString p = TQString("%1/%2").arg(kfsstnd_defaultbindir()).arg(real_appname);
954  info.setFile( p );
955  if( info.exists() && ( ignore || info.isExecutable() )
956  && ( info.isFile() || info.isSymLink() ) ) {
957  return p;
958  }
959 
960  TQStringList exePaths = systemPaths( pstr );
961  for (TQStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
962  {
963  p = (*it) + "/";
964  p += real_appname;
965 
966  // Check for executable in this tokenized path
967  info.setFile( p );
968 
969  if( info.exists() && ( ignore || info.isExecutable() )
970  && ( info.isFile() || info.isSymLink() ) ) {
971  return p;
972  }
973  }
974 
975  // If we reach here, the executable wasn't found.
976  // So return empty string.
977 
978  return TQString::null;
979 }
980 
981 int TDEStandardDirs::findAllExe( TQStringList& list, const TQString& appname,
982  const TQString& pstr, bool ignore )
983 {
984 #ifdef Q_WS_WIN
985  TQString real_appname = appname + ".exe";
986 #else
987  TQString real_appname = appname;
988 #endif
989  TQFileInfo info;
990  TQString p;
991  list.clear();
992 
993  TQStringList exePaths = systemPaths( pstr );
994  for (TQStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
995  {
996  p = (*it) + "/";
997  p += real_appname;
998 
999  info.setFile( p );
1000 
1001  if( info.exists() && (ignore || info.isExecutable())
1002  && info.isFile() ) {
1003  list.append( p );
1004  }
1005  }
1006 
1007  return list.count();
1008 }
1009 
1010 static int tokenize( TQStringList& tokens, const TQString& str,
1011  const TQString& delim )
1012 {
1013  int len = str.length();
1014  TQString token = "";
1015 
1016  for( int index = 0; index < len; index++)
1017  {
1018  if ( delim.find( str[ index ] ) >= 0 )
1019  {
1020  tokens.append( token );
1021  token = "";
1022  }
1023  else
1024  {
1025  token += str[ index ];
1026  }
1027  }
1028  if ( token.length() > 0 )
1029  {
1030  tokens.append( token );
1031  }
1032 
1033  return tokens.count();
1034 }
1035 
1036 TQString TDEStandardDirs::kde_default(const char *type) {
1037  if (!strcmp(type, "data"))
1038  return "share/apps/";
1039  if (!strcmp(type, "html-bundle"))
1040  return "share/doc-bundle/HTML/";
1041  if (!strcmp(type, "html"))
1042  return "share/doc/tde/HTML/";
1043  if (!strcmp(type, "icon"))
1044  return "share/icons/";
1045  if (!strcmp(type, "config"))
1046  return "share/config/";
1047  if (!strcmp(type, "pixmap"))
1048  return "share/pixmaps/";
1049  if (!strcmp(type, "apps"))
1050  return "share/applnk/";
1051  if (!strcmp(type, "sound"))
1052  return "share/sounds/";
1053  if (!strcmp(type, "locale-bundle"))
1054  return "share/locale-bundle/";
1055  if (!strcmp(type, "locale"))
1056  return "share/locale/";
1057  if (!strcmp(type, "services"))
1058  return "share/services/";
1059  if (!strcmp(type, "servicetypes"))
1060  return "share/servicetypes/";
1061  if (!strcmp(type, "mime"))
1062  return "share/mimelnk/";
1063  if (!strcmp(type, "cgi"))
1064  return "lib/cgi-bin/";
1065  if (!strcmp(type, "wallpaper"))
1066  return "share/wallpapers/";
1067  if (!strcmp(type, "templates"))
1068  return "share/templates/";
1069  if (!strcmp(type, "exe"))
1070  return "bin/";
1071  if (!strcmp(type, "lib"))
1072  return "lib" KDELIBSUFF "/";
1073  if (!strcmp(type, "module"))
1074  return "lib" KDELIBSUFF "/trinity/";
1075  if (!strcmp(type, "qtplugins"))
1076  return "lib" KDELIBSUFF "/trinity/plugins";
1077  if (!strcmp(type, "xdgdata-apps"))
1078  return "applications/";
1079  if (!strcmp(type, "xdgdata-icon"))
1080  return "icons/";
1081  if (!strcmp(type, "xdgdata-pixmap"))
1082  return "pixmaps/";
1083  if (!strcmp(type, "xdgdata-dirs"))
1084  return "desktop-directories/";
1085  if (!strcmp(type, "xdgconf-menu"))
1086  return "menus/";
1087  if (!strcmp(type, "xdgconf-autostart"))
1088  return "autostart/";
1089  if (!strcmp(type, "kcfg"))
1090  return "share/config.kcfg";
1091  if (!strcmp(type, "emoticons"))
1092  return "share/emoticons";
1093 
1094 
1095  tqFatal("unknown resource type %s", type);
1096  return TQString::null;
1097 }
1098 
1099 TQString TDEStandardDirs::saveLocation(const char *type,
1100  const TQString& suffix,
1101  bool create) const
1102 {
1103  checkConfig();
1104 
1105  TQString *pPath = savelocations.find(type);
1106  if (!pPath)
1107  {
1108  TQStringList *dirs = relatives.find(type);
1109  if (!dirs && (
1110  (strcmp(type, "socket") == 0) ||
1111  (strcmp(type, "tmp") == 0) ||
1112  (strcmp(type, "cache") == 0) ))
1113  {
1114  (void) resourceDirs(type); // Generate socket|tmp|cache resource.
1115  dirs = relatives.find(type); // Search again.
1116  }
1117  if (dirs)
1118  {
1119  // Check for existence of typed directory + suffix
1120  if (strncmp(type, "xdgdata-", 8) == 0)
1121  pPath = new TQString(realPath(localxdgdatadir() + dirs->last()));
1122  else if (strncmp(type, "xdgconf-", 8) == 0)
1123  pPath = new TQString(realPath(localxdgconfdir() + dirs->last()));
1124  else
1125  pPath = new TQString(realPath(localtdedir() + dirs->last()));
1126  }
1127  else {
1128  dirs = absolutes.find(type);
1129  if (!dirs)
1130  tqFatal("TDEStandardDirs: The resource type %s is not registered", type);
1131  pPath = new TQString(realPath(dirs->last()));
1132  }
1133 
1134  savelocations.insert(type, pPath);
1135  }
1136  TQString fullPath = *pPath + (pPath->endsWith("/") ? "" : "/") + suffix;
1137 
1138  KDE_struct_stat st;
1139  if (KDE_stat(TQFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
1140  if(!create) {
1141  return fullPath;
1142  }
1143  if(!makeDir(fullPath, 0700)) {
1144  return fullPath;
1145  }
1146  dircache.remove(type);
1147  }
1148  if (!fullPath.endsWith("/")) {
1149  fullPath += "/";
1150  }
1151  return fullPath;
1152 }
1153 
1154 TQString TDEStandardDirs::relativeLocation(const char *type, const TQString &absPath)
1155 {
1156  TQString fullPath = absPath;
1157  int i = absPath.findRev('/');
1158  if (i != -1)
1159  {
1160  fullPath = realPath(absPath.left(i+1))+absPath.mid(i+1); // Normalize
1161  }
1162 
1163  TQStringList candidates = resourceDirs(type);
1164 
1165  for (TQStringList::ConstIterator it = candidates.begin();
1166  it != candidates.end(); ++it)
1167  if (fullPath.startsWith(*it))
1168  {
1169  return fullPath.mid((*it).length());
1170  }
1171 
1172  return absPath;
1173 }
1174 
1175 
1176 bool TDEStandardDirs::makeDir(const TQString& dir, int mode)
1177 {
1178  // we want an absolute path
1179  if (TQDir::isRelativePath(dir))
1180  return false;
1181 
1182  TQString target = dir;
1183  uint len = target.length();
1184 
1185  // append trailing slash if missing
1186  if (dir.at(len - 1) != QChar('/'))
1187  target += QChar('/');
1188 
1189  TQString base("");
1190  uint i = 1;
1191 
1192  while( i < len )
1193  {
1194  KDE_struct_stat st;
1195  int pos = target.find(QChar('/'), i);
1196  base += target.mid(i - 1, pos - i + 1);
1197  TQCString baseEncoded = TQFile::encodeName(base);
1198  // bail out if we encountered a problem
1199  if (KDE_stat(baseEncoded, &st) != 0)
1200  {
1201  // Directory does not exist....
1202  // Or maybe a dangling symlink ?
1203  if (KDE_lstat(baseEncoded, &st) == 0)
1204  (void)unlink(baseEncoded); // try removing
1205 
1206  if ( KDE_mkdir(baseEncoded, (mode_t) mode) != 0) {
1207  baseEncoded.prepend( "trying to create local folder " );
1208  perror(baseEncoded.data());
1209  return false; // Couldn't create it :-(
1210  }
1211  }
1212  i = pos + 1;
1213  }
1214  return true;
1215 }
1216 
1217 static TQString readEnvPath(const char *env)
1218 {
1219  TQCString c_path = getenv(env);
1220  if (c_path.isEmpty())
1221  return TQString::null;
1222 #ifdef Q_OS_WIN
1223  //win32 paths are case-insensitive: avoid duplicates on various dir lists
1224  return TQFile::decodeName(c_path).lower();
1225 #else
1226  return TQFile::decodeName(c_path);
1227 #endif
1228 }
1229 
1230 #ifdef __linux__
1231 static TQString executablePrefix()
1232 {
1233  char path_buffer[MAXPATHLEN + 1];
1234  path_buffer[MAXPATHLEN] = 0;
1235  int length = readlink ("/proc/self/exe", path_buffer, MAXPATHLEN);
1236  if (length == -1)
1237  return TQString::null;
1238 
1239  path_buffer[length] = TQChar('\0');
1240 
1241  TQString path = TQFile::decodeName(path_buffer);
1242 
1243  if(path.isEmpty())
1244  return TQString::null;
1245 
1246  int pos = path.findRev('/'); // Skip filename
1247  if(pos <= 0)
1248  return TQString::null;
1249  pos = path.findRev(TQChar('/'), pos - 1); // Skip last directory
1250  if(pos <= 0)
1251  return TQString::null;
1252 
1253  return path.left(pos);
1254 }
1255 #endif
1256 
1257 TQString TDEStandardDirs::kfsstnd_defaultprefix()
1258 {
1259  TDEStandardDirsSingleton* s = TDEStandardDirsSingleton::self();
1260  if (!s->defaultprefix.isEmpty())
1261  return s->defaultprefix;
1262 #ifdef Q_WS_WIN
1263  s->defaultprefix = readEnvPath("TDEDIR");
1264  if (s->defaultprefix.isEmpty()) {
1265  s->defaultprefix = TQFile::decodeName("c:\\kde");
1266  //TODO: find other location (the Registry?)
1267  }
1268 #else //UNIX
1269  s->defaultprefix = TDEDIR;
1270 #endif
1271  if (s->defaultprefix.isEmpty()) {
1272  fprintf(stderr, "TDEStandardDirs::kfsstnd_defaultprefix(): default TDE prefix not found!\n");
1273  }
1274  return s->defaultprefix;
1275 }
1276 
1277 TQString TDEStandardDirs::kfsstnd_defaultbindir()
1278 {
1279  TDEStandardDirsSingleton* s = TDEStandardDirsSingleton::self();
1280  if (!s->defaultbindir.isEmpty())
1281  return s->defaultbindir;
1282 #ifdef Q_WS_WIN
1283  s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1("/bin");
1284 #else //UNIX
1285  s->defaultbindir = __TDE_BINDIR;
1286  if (s->defaultbindir.isEmpty())
1287  s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1("/bin");
1288 #endif
1289  if (s->defaultbindir.isEmpty()) {
1290  fprintf(stderr, "TDEStandardDirs::kfsstnd_defaultbindir(): default binary TDE dir not found!\n");
1291  }
1292  return s->defaultbindir;
1293 }
1294 
1295 void TDEStandardDirs::addKDEDefaults()
1296 {
1297  TQStringList tdedirList;
1298 
1299  // begin TDEDIRS
1300  TQString tdedirs = readEnvPath("TDEDIRS");
1301  if (!tdedirs.isEmpty())
1302  {
1303  tokenize(tdedirList, tdedirs, TQChar(KPATH_SEPARATOR));
1304  }
1305  else
1306  {
1307  TQString tdedir = readEnvPath("TDEDIR");
1308  if (!tdedir.isEmpty())
1309  {
1310  tdedir = KShell::tildeExpand(tdedir);
1311  tdedirList.append(tdedir);
1312  }
1313  }
1314 
1315 #ifndef Q_OS_WIN //no default TDEDIR on win32 defined
1316  tdedirList.append(TDEDIR);
1317 #endif
1318 
1319 #ifdef __KDE_EXECPREFIX
1320  TQString execPrefix(__KDE_EXECPREFIX);
1321  if (execPrefix!="NONE")
1322  tdedirList.append(execPrefix);
1323 #endif
1324 #ifdef __linux__
1325  const TQString linuxExecPrefix = executablePrefix();
1326  if ( !linuxExecPrefix.isEmpty() )
1327  tdedirList.append( linuxExecPrefix );
1328 #endif
1329 
1330  // We treat root differently to prevent a "su" shell messing up the
1331  // file permissions in the user's home directory.
1332  TQString localKdeDir;
1333  if (getuid() == 0) {
1334  localKdeDir = readEnvPath("TDEROOTHOME");
1335  if (localKdeDir.isEmpty() == true)
1336  localKdeDir = readEnvPath("TDEHOME");
1337  }
1338  else {
1339  localKdeDir = readEnvPath("TDEHOME");
1340  }
1341  if (!localKdeDir.isEmpty())
1342  {
1343  if (localKdeDir[localKdeDir.length()-1] != QChar('/'))
1344  localKdeDir += QChar('/');
1345  }
1346  else
1347  {
1348  localKdeDir = TQDir::homeDirPath() + "/.trinity/";
1349  }
1350 
1351  if (localKdeDir != QString("-/"))
1352  {
1353  localKdeDir = KShell::tildeExpand(localKdeDir);
1354  addPrefix(localKdeDir);
1355  }
1356 
1357  TQStringList::ConstIterator end(tdedirList.end());
1358  for (TQStringList::ConstIterator it = tdedirList.begin();
1359  it != end; ++it)
1360  {
1361  TQString dir = KShell::tildeExpand(*it);
1362  addPrefix(dir);
1363  }
1364  // end TDEDIRS
1365 
1366  // begin XDG_CONFIG_XXX
1367  TQStringList xdgdirList;
1368  TQString xdgdirs = readEnvPath("XDG_CONFIG_DIRS");
1369  if (!xdgdirs.isEmpty())
1370  {
1371  tokenize(xdgdirList, xdgdirs, TQChar(KPATH_SEPARATOR));
1372  }
1373  else
1374  {
1375  xdgdirList.clear();
1376  xdgdirList.append("/etc/xdg");
1377 #ifdef Q_WS_WIN
1378  xdgdirList.append(kfsstnd_defaultprefix() + "/etc/xdg");
1379 #else
1380  xdgdirList.append(KDESYSCONFDIR "/xdg");
1381 #endif
1382  }
1383 
1384  TQString localXdgDir = readEnvPath("XDG_CONFIG_HOME");
1385  if (!localXdgDir.isEmpty())
1386  {
1387  if (localXdgDir[localXdgDir.length()-1] != QChar('/'))
1388  localXdgDir += QChar('/');
1389  }
1390  else
1391  {
1392  localXdgDir = TQDir::homeDirPath() + "/.config/";
1393  }
1394 
1395  localXdgDir = KShell::tildeExpand(localXdgDir);
1396  addXdgConfigPrefix(localXdgDir);
1397 
1398  for (TQStringList::ConstIterator it = xdgdirList.begin();
1399  it != xdgdirList.end(); ++it)
1400  {
1401  TQString dir = KShell::tildeExpand(*it);
1402  addXdgConfigPrefix(dir);
1403  }
1404  // end XDG_CONFIG_XXX
1405 
1406  // begin XDG_DATA_XXX
1407  xdgdirs = readEnvPath("XDG_DATA_DIRS");
1408  if (!xdgdirs.isEmpty())
1409  {
1410  tokenize(xdgdirList, xdgdirs, TQChar(KPATH_SEPARATOR));
1411  }
1412  else
1413  {
1414  xdgdirList.clear();
1415  for (TQStringList::ConstIterator it = tdedirList.begin();
1416  it != tdedirList.end(); ++it)
1417  {
1418  TQString dir = *it;
1419  if (dir[dir.length()-1] != QChar('/'))
1420  dir += QChar('/');
1421  xdgdirList.append(dir+"share/");
1422  }
1423 
1424  xdgdirList.append("/usr/local/share/");
1425  xdgdirList.append("/usr/share/");
1426  }
1427 
1428  localXdgDir = readEnvPath("XDG_DATA_HOME");
1429  if (!localXdgDir.isEmpty())
1430  {
1431  if (localXdgDir[localXdgDir.length()-1] != QChar('/'))
1432  localXdgDir += QChar('/');
1433  }
1434  else
1435  {
1436  localXdgDir = TQDir::homeDirPath() + "/.local/share/";
1437  }
1438 
1439  localXdgDir = KShell::tildeExpand(localXdgDir);
1440  addXdgDataPrefix(localXdgDir);
1441 
1442  for (TQStringList::ConstIterator it = xdgdirList.begin();
1443  it != xdgdirList.end(); ++it)
1444  {
1445  TQString dir = KShell::tildeExpand(*it);
1446  addXdgDataPrefix(dir);
1447  }
1448  // end XDG_DATA_XXX
1449 
1450 
1451  uint index = 0;
1452  while (types[index] != 0) {
1453  addResourceType(types[index], kde_default(types[index]));
1454  index++;
1455  }
1456 
1457  addResourceDir("home", TQDir::homeDirPath());
1458 
1459  addResourceDir("locale", "/usr/share/locale-langpack/", true);
1460 }
1461 
1462 void TDEStandardDirs::checkConfig() const
1463 {
1464  if (!addedCustoms && TDEGlobal::_instance && TDEGlobal::_instance->_config)
1465  const_cast<TDEStandardDirs*>(this)->addCustomized(TDEGlobal::_instance->_config);
1466 }
1467 
1468 static TQStringList lookupProfiles(const TQString &mapFile)
1469 {
1470  TQStringList profiles;
1471 
1472  if (mapFile.isEmpty() || !TQFile::exists(mapFile))
1473  {
1474  profiles << "default";
1475  return profiles;
1476  }
1477 
1478  struct passwd *pw = getpwuid(geteuid());
1479  if (!pw)
1480  {
1481  profiles << "default";
1482  return profiles; // Not good
1483  }
1484 
1485  TQCString user = pw->pw_name;
1486 
1487  gid_t sup_gids[512];
1488  int sup_gids_nr = getgroups(512, sup_gids);
1489 
1490  KSimpleConfig mapCfg(mapFile, true);
1491  mapCfg.setGroup("Users");
1492  if (mapCfg.hasKey(user.data()))
1493  {
1494  profiles = mapCfg.readListEntry(user.data());
1495  return profiles;
1496  }
1497 
1498  mapCfg.setGroup("General");
1499  TQStringList groups = mapCfg.readListEntry("groups");
1500 
1501  mapCfg.setGroup("Groups");
1502 
1503  for( TQStringList::ConstIterator it = groups.begin();
1504  it != groups.end(); ++it )
1505  {
1506  TQCString grp = (*it).utf8();
1507  // Check if user is in this group
1508  struct group *grp_ent = getgrnam(grp);
1509  if (!grp_ent) continue;
1510  gid_t gid = grp_ent->gr_gid;
1511  if (pw->pw_gid == gid)
1512  {
1513  // User is in this group --> add profiles
1514  profiles += mapCfg.readListEntry(*it);
1515  }
1516  else
1517  {
1518  for(int i = 0; i < sup_gids_nr; i++)
1519  {
1520  if (sup_gids[i] == gid)
1521  {
1522  // User is in this group --> add profiles
1523  profiles += mapCfg.readListEntry(*it);
1524  break;
1525  }
1526  }
1527  }
1528  }
1529 
1530  if (profiles.isEmpty())
1531  profiles << "default";
1532  return profiles;
1533 }
1534 
1535 extern bool kde_kiosk_admin;
1536 
1537 bool TDEStandardDirs::addCustomized(TDEConfig *config)
1538 {
1539  if (addedCustoms && !d->checkRestrictions) // there are already customized entries
1540  return false; // we just quit and hope they are the right ones
1541 
1542  // save the numbers of config directories. If this changes,
1543  // we will return true to give TDEConfig a chance to reparse
1544  uint configdirs = resourceDirs("config").count();
1545 
1546  // Remember original group
1547  TQString oldGroup = config->group();
1548 
1549  if (!addedCustoms)
1550  {
1551  // We only add custom entries once
1552  addedCustoms = true;
1553 
1554  // reading the prefixes in
1555  TQString group = TQString::fromLatin1("Directories");
1556  config->setGroup(group);
1557 
1558  TQString kioskAdmin = config->readEntry("kioskAdmin");
1559  if (!kioskAdmin.isEmpty() && !kde_kiosk_admin)
1560  {
1561  int i = kioskAdmin.find(':');
1562  TQString user = kioskAdmin.left(i);
1563  TQString host = kioskAdmin.mid(i+1);
1564 
1565  KUser thisUser;
1566  char hostname[ 256 ];
1567  hostname[ 0 ] = TQChar('\0');
1568  if (!gethostname( hostname, 255 ))
1569  hostname[sizeof(hostname)-1] = TQChar('\0');
1570 
1571  if ((user == thisUser.loginName()) &&
1572  (host.isEmpty() || (host == hostname)))
1573  {
1574  kde_kiosk_admin = true;
1575  }
1576  }
1577 
1578  bool readProfiles = true;
1579 
1580  if (kde_kiosk_admin && !TQCString(getenv("TDE_KIOSK_NO_PROFILES")).isEmpty())
1581  readProfiles = false;
1582 
1583  TQString userMapFile = config->readEntry("userProfileMapFile");
1584  TQString profileDirsPrefix = config->readEntry("profileDirsPrefix");
1585  if (!profileDirsPrefix.isEmpty() && !profileDirsPrefix.endsWith("/"))
1586  profileDirsPrefix.append('/');
1587 
1588  TQStringList profiles;
1589  if (readProfiles)
1590  profiles = lookupProfiles(userMapFile);
1591  TQString profile;
1592 
1593  bool priority = false;
1594  while(true)
1595  {
1596  config->setGroup(group);
1597  TQStringList list = config->readListEntry("prefixes");
1598  for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
1599  {
1600  addPrefix(*it, priority);
1601  addXdgConfigPrefix(*it+"/etc/xdg", priority);
1602  addXdgDataPrefix(*it+"/share", priority);
1603  }
1604  // If there are no prefixes defined, check if there is a directory
1605  // for this profile under <profileDirsPrefix>
1606  if (list.isEmpty() && !profile.isEmpty() && !profileDirsPrefix.isEmpty())
1607  {
1608  TQString dir = profileDirsPrefix + profile;
1609  addPrefix(dir, priority);
1610  addXdgConfigPrefix(dir+"/etc/xdg", priority);
1611  addXdgDataPrefix(dir+"/share", priority);
1612  }
1613 
1614  // iterating over all entries in the group Directories
1615  // to find entries that start with dir_$type
1616  TQMap<TQString, TQString> entries = config->entryMap(group);
1617  for (TQMap<TQString, TQString>::ConstIterator it2 = entries.begin();
1618  it2 != entries.end(); it2++)
1619  {
1620  TQString key = it2.key();
1621  if (key.startsWith("dir_")) {
1622  // generate directory list, there may be more than 1.
1623  TQStringList dirs = TQStringList::split(',', *it2);
1624  TQStringList::Iterator sIt(dirs.begin());
1625  TQString resType = key.mid(4, key.length());
1626  for (; sIt != dirs.end(); ++sIt)
1627  {
1628  addResourceDir(resType.latin1(), *sIt, priority);
1629  }
1630  }
1631  }
1632  if (profiles.isEmpty())
1633  break;
1634  profile = profiles.back();
1635  group = TQString::fromLatin1("Directories-%1").arg(profile);
1636  profiles.pop_back();
1637  priority = true;
1638  }
1639  }
1640 
1641  // Process KIOSK restrictions.
1642  if (!kde_kiosk_admin || TQCString(getenv("TDE_KIOSK_NO_RESTRICTIONS")).isEmpty())
1643  {
1644  config->setGroup("KDE Resource Restrictions");
1645  TQMap<TQString, TQString> entries = config->entryMap("KDE Resource Restrictions");
1646  for (TQMap<TQString, TQString>::ConstIterator it2 = entries.begin();
1647  it2 != entries.end(); it2++)
1648  {
1649  TQString key = it2.key();
1650  if (!config->readBoolEntry(key, true))
1651  {
1652  d->restrictionsActive = true;
1653  d->restrictions.insert(key.latin1(), &d->restrictionsActive); // Anything will do
1654  dircache.remove(key.latin1());
1655  }
1656  }
1657  }
1658 
1659  config->setGroup(oldGroup);
1660 
1661  // check if the number of config dirs changed
1662  bool configDirsChanged = (resourceDirs("config").count() != configdirs);
1663  // If the config dirs changed, we check kiosk restrictions again.
1664  d->checkRestrictions = configDirsChanged;
1665  // return true if the number of config dirs changed: reparse config file
1666  return configDirsChanged;
1667 }
1668 
1669 TQString TDEStandardDirs::localtdedir() const
1670 {
1671  // Return the prefix to use for saving
1672  return prefixes.first();
1673 }
1674 
1675 TQString TDEStandardDirs::localxdgdatadir() const
1676 {
1677  // Return the prefix to use for saving
1678  return d->xdgdata_prefixes.first();
1679 }
1680 
1681 TQString TDEStandardDirs::localxdgconfdir() const
1682 {
1683  // Return the prefix to use for saving
1684  return d->xdgconf_prefixes.first();
1685 }
1686 
1687 
1688 // just to make code more readable without macros
1689 TQString locate( const char *type,
1690  const TQString& filename, const TDEInstance* inst )
1691 {
1692  return inst->dirs()->findResource(type, filename);
1693 }
1694 
1695 TQString locateLocal( const char *type,
1696  const TQString& filename, const TDEInstance* inst )
1697 {
1698  return locateLocal(type, filename, true, inst);
1699 }
1700 
1701 TQString locateLocal( const char *type,
1702  const TQString& filename, bool createDir, const TDEInstance* inst )
1703 {
1704  // try to find slashes. If there are some, we have to
1705  // create the subdir first
1706  int slash = filename.findRev('/')+1;
1707  if (!slash) // only one filename
1708  return inst->dirs()->saveLocation(type, TQString::null, createDir) + filename;
1709 
1710  // split path from filename
1711  TQString dir = filename.left(slash);
1712  TQString file = filename.mid(slash);
1713  return inst->dirs()->saveLocation(type, dir, createDir) + file;
1714 }
TDEConfig
Access KDE Configuration entries.
Definition: tdeconfig.h:43
TDEStandardDirs::addXdgConfigPrefix
void addXdgConfigPrefix(const TQString &dir)
Adds another search dir to front of the XDG_CONFIG_XXX list of prefixes.
Definition: kstandarddirs.cpp:200
TDEConfig::entryMap
virtual TQMap< TQString, TQString > entryMap(const TQString &pGroup) const
Returns a map (tree) of entries for all entries in a particular group.
Definition: tdeconfig.cpp:141
TDEStandardDirs::kde_default
static TQString kde_default(const char *type)
This returns a default relative path for the standard KDE resource types.
Definition: kstandarddirs.cpp:1036
TDEStandardDirs::addKDEDefaults
void addKDEDefaults()
This function adds the defaults that are used by the current KDE version.
Definition: kstandarddirs.cpp:1295
TDEStandardDirs::calcResourceHash
TQ_UINT32 calcResourceHash(const char *type, const TQString &filename, bool deep) const
Returns a number that identifies this version of the resource.
Definition: kstandarddirs.cpp:352
TDEStandardDirs::findResourceDir
TQString findResourceDir(const char *type, const TQString &filename) const
Tries to find the directory the file is in.
Definition: kstandarddirs.cpp:412
KStdAction::copy
TDEAction * copy(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEStandardDirs::locateLocal
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
Definition: kstandarddirs.cpp:1695
TDEStandardDirs::findResource
TQString findResource(const char *type, const TQString &filename) const
Tries to find a resource in the following order:
Definition: kstandarddirs.cpp:317
TDEInstance::dirs
TDEStandardDirs * dirs() const
Returns the application standard dirs object.
Definition: kinstance.cpp:187
TDEConfigBase::setGroup
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Definition: tdeconfigbase.cpp:79
KStaticDeleter
Little helper class to clean up static objects that are held as pointer.
Definition: kstaticdeleter.h:74
TDEStandardDirs::TDEStandardDirs
TDEStandardDirs()
TDEStandardDirs' constructor.
Definition: kstandarddirs.cpp:108
TDEStandardDirs::resourceDirs
TQStringList resourceDirs(const char *type) const
This function is used internally by almost all other function as it serves and fills the directories ...
Definition: kstandarddirs.cpp:795
KUser::loginName
TQString loginName() const
The login name of the user.
Definition: kuser.cpp:167
TDEStandardDirs::relativeLocation
TQString relativeLocation(const char *type, const TQString &absPath)
Converts an absolute path to a path relative to a certain resource.
Definition: kstandarddirs.cpp:1154
TDEStandardDirs::addXdgDataPrefix
void addXdgDataPrefix(const TQString &dir)
Adds another search dir to front of the XDG_DATA_XXX list of prefixes.
Definition: kstandarddirs.cpp:220
TDEConfigBase::readListEntry
int readListEntry(const TQString &pKey, TQStrList &list, char sep= ',') const
Reads a list of strings.
Definition: tdeconfigbase.cpp:491
TDEStandardDirs::addResourceType
bool addResourceType(const char *type, const TQString &relativename)
Adds suffixes for types.
Definition: kstandarddirs.cpp:255
KStaticDeleter::setObject
KDE_DEPRECATED type * setObject(type *obj, bool isArray=false)
Sets the object to delete and registers the object to be deleted to TDEGlobal.
Definition: kstaticdeleter.h:85
KUser
Represents a user on your system.
Definition: kuser.h:45
TDEStandardDirs
Site-independent access to standard KDE directories.
Definition: kstandarddirs.h:125
KShell::tildeExpand
TQString tildeExpand(const TQString &path)
Performs tilde expansion on path.
Definition: kshell.cpp:355
TDEStandardDirs::~TDEStandardDirs
virtual ~TDEStandardDirs()
TDEStandardDirs' destructor.
Definition: kstandarddirs.cpp:118
TDEStandardDirs::realFilePath
static TQString realFilePath(const TQString &filename)
Expands all symbolic links and resolves references to '/.
Definition: kstandarddirs.cpp:707
TDEStandardDirs::findAllResources
TQStringList findAllResources(const char *type, const TQString &filter=TQString::null, bool recursive=false, bool unique=false) const
Tries to find all resources with the specified type.
Definition: kstandarddirs.cpp:679
TDEStandardDirs::findAllExe
static int findAllExe(TQStringList &list, const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
Finds all occurrences of an executable in the system path.
Definition: kstandarddirs.cpp:981
TDEStandardDirs::addResourceDir
bool addResourceDir(const char *type, const TQString &absdir)
Adds absolute path at the end of the search path for particular types (for example in case of icons w...
Definition: kstandarddirs.cpp:286
TDEStandardDirs::findExe
static TQString findExe(const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
Finds the executable in the system path.
Definition: kstandarddirs.cpp:932
TDEConfigBase::readBoolEntry
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
Reads a boolean entry.
Definition: tdeconfigbase.cpp:772
TDEStandardDirs::systemPaths
static TQStringList systemPaths(const TQString &pstr=TQString::null)
Returns a TQStringList list of pathnames in the system path.
Definition: kstandarddirs.cpp:886
TDEStandardDirs::localtdedir
TQString localtdedir() const
Returns the toplevel directory in which TDEStandardDirs will store things.
Definition: kstandarddirs.cpp:1669
TDEStandardDirs::localxdgconfdir
TQString localxdgconfdir() const
Definition: kstandarddirs.cpp:1681
TDEStandardDirs::addCustomized
bool addCustomized(TDEConfig *config)
Reads customized entries out of the given config object and add them via addResourceDirs().
Definition: kstandarddirs.cpp:1537
TDEStandardDirs::saveLocation
TQString saveLocation(const char *type, const TQString &suffix=TQString::null, bool create=true) const
Finds a location to save files into for the given type in the user's home directory.
Definition: kstandarddirs.cpp:1099
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
TDEStandardDirs::findDirs
TQStringList findDirs(const char *type, const TQString &reldir) const
Tries to find all directories whose names consist of the specified type and a relative path...
Definition: kstandarddirs.cpp:378
TDEStandardDirs::allTypes
TQStringList allTypes() const
This function will return a list of all the types that TDEStandardDirs supports.
Definition: kstandarddirs.cpp:157
TDEStandardDirs::isRestrictedResource
bool isRestrictedResource(const char *type, const TQString &relPath=TQString::null) const
Checks whether a resource is restricted as part of the KIOSK framework.
Definition: kstandarddirs.cpp:123
TDEStandardDirs::localxdgdatadir
TQString localxdgdatadir() const
Definition: kstandarddirs.cpp:1675
endl
kndbgstream & endl(kndbgstream &s)
Does nothing.
Definition: kdebug.h:583
KSimpleConfig
KDE Configuration entries.
Definition: ksimpleconfig.h:41
TDEStandardDirs::locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
Definition: kstandarddirs.cpp:1689
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
TDEStandardDirs::realPath
static TQString realPath(const TQString &dirname)
Expands all symbolic links and resolves references to '/.
Definition: kstandarddirs.cpp:689
TDEInstance
Access to KDE global objects for use in shared libraries.
Definition: kinstance.h:47
TDEStandardDirs::makeDir
static bool makeDir(const TQString &dir, int mode=0755)
Recursively creates still-missing directories in the given path.
Definition: kstandarddirs.cpp:1176
TDEStandardDirs::exists
static bool exists(const TQString &fullPath)
Checks for existence and accessability of a file or directory.
Definition: kstandarddirs.cpp:450
TDEStandardDirs::addPrefix
void addPrefix(const TQString &dir)
Adds another search dir to front of the fsstnd list.
Definition: kstandarddirs.cpp:180

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
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.