32 #ifdef HAVE_SYS_STAT_H
35 #include <sys/param.h>
36 #include <sys/types.h>
42 #include <tqasciidict.h>
45 #include <tqfileinfo.h>
47 #include <tqstringlist.h>
49 #include "kstandarddirs.h"
50 #include "tdeconfig.h"
51 #include "kinstance.h"
53 #include "ksimpleconfig.h"
55 #include "kstaticdeleter.h"
58 template class TQDict<TQStringList>;
60 class TDEStandardDirs::TDEStandardDirsPrivate
63 TDEStandardDirsPrivate()
64 : restrictionsActive(false),
65 dataRestrictionActive(false),
66 checkRestrictions(true)
69 bool restrictionsActive;
70 bool dataRestrictionActive;
71 bool checkRestrictions;
72 TQAsciiDict<bool> restrictions;
73 TQStringList xdgdata_prefixes;
74 TQStringList xdgconf_prefixes;
79 class TDEStandardDirsSingleton
82 TQString defaultprefix;
83 TQString defaultbindir;
84 static TDEStandardDirsSingleton*
self();
86 static TDEStandardDirsSingleton* s_self;
89 TDEStandardDirsSingleton* TDEStandardDirsSingleton::s_self = 0;
90 TDEStandardDirsSingleton* TDEStandardDirsSingleton::self() {
92 kstds_sd.setObject( s_self,
new TDEStandardDirsSingleton );
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",
"tqtplugins",
101 "xdgdata-apps",
"xdgdata-dirs",
"xdgconf-menu",
102 "xdgdata-icon",
"xdgdata-pixmap",
"xdgconf-autostart",
103 "kcfg",
"emoticons", 0 };
105 static int tokenize( TQStringList& token,
const TQString& str,
106 const TQString& delim );
110 d =
new TDEStandardDirsPrivate;
111 dircache.setAutoDelete(
true);
112 relatives.setAutoDelete(
true);
113 absolutes.setAutoDelete(
true);
114 savelocations.setAutoDelete(
true);
125 if (!d || !d->restrictionsActive)
128 if (d->restrictions[type])
131 if (strcmp(type,
"data")==0)
133 applyDataRestrictions(relPath);
134 if (d->dataRestrictionActive)
136 d->dataRestrictionActive =
false;
143 void TDEStandardDirs::applyDataRestrictions(
const TQString &relPath)
const
146 int i = relPath.find(TQChar(
'/'));
148 key =
"data_"+relPath.left(i);
150 key =
"data_"+relPath;
152 if (d && d->restrictions[key.latin1()])
153 d->dataRestrictionActive =
true;
160 for (
int i = 0; types[i] != 0; ++i)
161 list.append(TQString::fromLatin1(types[i]));
165 static void priorityAdd(TQStringList &prefixes,
const TQString& dir,
bool priority)
167 if (priority && !prefixes.isEmpty())
170 TQStringList::iterator it = prefixes.begin();
172 prefixes.insert(it, 1, dir);
176 prefixes.append(dir);
191 if (dir.at(dir.length() - 1) != TQChar(
'/'))
194 if (!prefixes.contains(dir)) {
195 priorityAdd(prefixes, dir, priority);
211 if (dir.at(dir.length() - 1) != TQChar(
'/'))
214 if (!d->xdgconf_prefixes.contains(dir)) {
215 priorityAdd(d->xdgconf_prefixes, dir, priority);
231 if (dir.at(dir.length() - 1) != TQChar(
'/'))
234 if (!d->xdgdata_prefixes.contains(dir)) {
235 priorityAdd(d->xdgdata_prefixes, dir, priority);
240 TQString TDEStandardDirs::kfsstnd_prefixes()
242 return prefixes.join(TQChar(KPATH_SEPARATOR));
245 TQString TDEStandardDirs::kfsstnd_xdg_conf_prefixes()
247 return d->xdgconf_prefixes.join(TQChar(KPATH_SEPARATOR));
250 TQString TDEStandardDirs::kfsstnd_xdg_data_prefixes()
252 return d->xdgdata_prefixes.join(TQChar(KPATH_SEPARATOR));
256 const TQString& relativename )
261 const TQString& relativename,
264 if (relativename.isEmpty())
267 TQStringList *rels = relatives.find(type);
269 rels =
new TQStringList();
270 relatives.insert(type, rels);
272 TQString
copy = relativename;
273 if (
copy.at(
copy.length() - 1) != TQChar(
'/'))
275 if (!rels->contains(copy)) {
280 dircache.remove(type);
287 const TQString& absdir)
294 const TQString& absdir,
297 TQStringList *paths = absolutes.find(type);
299 paths =
new TQStringList();
300 absolutes.insert(type, paths);
302 TQString
copy = absdir;
303 if (
copy.at(
copy.length() - 1) != TQChar(
'/'))
306 if (!paths->contains(copy)) {
308 paths->prepend(copy);
311 dircache.remove(type);
318 const TQString& filename )
const
320 if (!TQDir::isRelativePath(filename))
324 kdDebug() <<
"Find resource: " << type <<
endl;
325 for (TQStringList::ConstIterator pit = prefixes.begin();
326 pit != prefixes.end();
329 kdDebug() <<
"Prefix: " << *pit <<
endl;
336 else return dir + filename;
339 static TQ_UINT32 updateHash(
const TQString &file, TQ_UINT32 hash)
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 )))
347 hash = hash + (TQ_UINT32) buff.st_ctime;
353 const TQString& filename,
bool deep)
const
357 if (!TQDir::isRelativePath(filename))
360 return updateHash(filename, hash);
362 if (d && d->restrictionsActive && (strcmp(type,
"data")==0))
363 applyDataRestrictions(filename);
367 for (TQStringList::ConstIterator it = candidates.begin();
368 it != candidates.end(); ++it)
370 hash = updateHash(*it + filename, hash);
379 const TQString& reldir )
const
383 if (!TQDir::isRelativePath(reldir))
385 testdir.setPath(reldir);
386 if (testdir.exists())
388 if (reldir.endsWith(
"/"))
391 list.append(reldir+TQChar(
'/'));
398 if (d && d->restrictionsActive && (strcmp(type,
"data")==0))
399 applyDataRestrictions(reldir);
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() + TQChar(
'/'));
413 const TQString& filename)
const
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;
422 if (d && d->restrictionsActive && (strcmp(type,
"data")==0))
423 applyDataRestrictions(filename);
427 for (TQStringList::ConstIterator it = candidates.begin();
428 it != candidates.end(); ++it) {
429 if (
exists(*it + filename)) {
431 if ((*it).isEmpty() && filename.right(3)==
".la") {
433 fprintf(stderr,
"TDEStandardDirs::findResourceDir() found .la in cwd: skipping. (fname=%s)\n", filename.ascii());
443 if(
false && strcmp(type,
"locale"))
444 fprintf(stderr,
"KStdDirs::findResDir(): can't find \"%s\" in type \"%s\".\n", filename.ascii(), type);
447 return TQString::null;
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) != TQChar(
'/')) {
455 if (S_ISREG( buff.st_mode ))
459 if (S_ISDIR( buff.st_mode )) {
467 static void lookupDirectory(
const TQString& path,
const TQString &relPart,
468 const TQRegExp ®exp,
470 TQStringList& relList,
471 bool recursive,
bool unique)
473 TQString pattern = regexp.pattern();
474 if (recursive || pattern.contains(
'?') || pattern.contains(
'*'))
479 DIR *dp = opendir( TQFile::encodeName(path));
484 assert(path.at(path.length() - 1) == TQChar(
'/') || path.at(path.length() - 1) == TQChar(
'\\'));
486 assert(path.at(path.length() - 1) == TQChar(
'/'));
490 KDE_struct_stat buff;
493 TQString _dotdot(
"..");
495 while( ( ep = readdir( dp ) ) != 0L )
497 TQString fn( TQFile::decodeName(ep->d_name));
498 if (fn == _dot || fn == _dotdot || TQChar(fn.at(fn.length() - 1)).latin1() == TQChar(
'~').latin1())
501 if (!recursive && !regexp.exactMatch(fn))
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);
510 if ( S_ISDIR( buff.st_mode )) {
511 lookupDirectory(pathfn + TQChar(
'/'), relPart + fn + TQChar(
'/'), regexp, list, relList, recursive, unique);
513 if (!regexp.exactMatch(fn))
516 if ( S_ISREG( buff.st_mode))
518 if (!unique || !relList.contains(relPart + fn))
520 list.append( pathfn );
521 relList.append( relPart + fn );
530 TQString fn = pattern;
531 TQString pathfn = path + fn;
532 KDE_struct_stat buff;
533 if ( KDE_stat( TQFile::encodeName(pathfn), &buff ) != 0 )
535 if ( S_ISREG( buff.st_mode))
537 if (!unique || !relList.contains(relPart + fn))
539 list.append( pathfn );
540 relList.append( relPart + fn );
546 static void lookupPrefix(
const TQString& prefix,
const TQString& relpath,
547 const TQString& relPart,
548 const TQRegExp ®exp,
550 TQStringList& relList,
551 bool recursive,
bool unique)
553 if (relpath.isEmpty()) {
554 lookupDirectory(prefix, relPart, regexp, list,
555 relList, recursive, unique);
561 if (relpath.length())
563 int slash = relpath.find(TQChar(
'/'));
565 rest = relpath.left(relpath.length() - 1);
567 path = relpath.left(slash);
568 rest = relpath.mid(slash + 1);
572 if (prefix.isEmpty())
575 assert(prefix.at(prefix.length() - 1) == TQChar(
'/') || prefix.at(prefix.length() - 1) == TQChar(
'\\'));
577 assert(prefix.at(prefix.length() - 1) == TQChar(
'/'));
579 KDE_struct_stat buff;
581 if (path.contains(
'*') || path.contains(
'?')) {
583 TQRegExp pathExp(path,
true,
true);
584 DIR *dp = opendir( TQFile::encodeName(prefix) );
592 TQString _dotdot(
"..");
594 while( ( ep = readdir( dp ) ) != 0L )
596 TQString fn( TQFile::decodeName(ep->d_name));
597 if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1) == TQChar(
'~'))
600 if ( !pathExp.exactMatch(fn) )
602 TQString rfn = relPart+fn;
604 if ( KDE_stat( TQFile::encodeName(fn), &buff ) != 0 ) {
605 fprintf(stderr,
"Error statting %s : %d\n", fn.ascii(), errno);
608 if ( S_ISDIR( buff.st_mode ))
609 lookupPrefix(fn + TQChar(
'/'), rest, rfn + TQChar(
'/'), regexp, list, relList, recursive, unique);
616 lookupPrefix(prefix + path + TQChar(
'/'), rest,
617 relPart + path + TQChar(
'/'), regexp, list,
618 relList, recursive, unique);
624 const TQString& filter,
627 TQStringList &relList)
const
635 int slash = filter.findRev(
'/');
639 filterPath = filter.left(slash + 1);
640 filterFile = filter.mid(slash + 1);
646 TQStringList candidates;
647 if (!TQDir::isRelativePath(filter))
650 candidates << filterPath.left(3);
651 filterPath = filterPath.mid(3);
654 filterPath = filterPath.mid(1);
659 if (d && d->restrictionsActive && (strcmp(type,
"data")==0))
660 applyDataRestrictions(filter);
663 if (filterFile.isEmpty())
666 TQRegExp regExp(filterFile,
true,
true);
668 for (TQStringList::ConstIterator it = candidates.begin();
669 it != candidates.end(); ++it)
671 lookupPrefix(*it, filterPath,
"", regExp, list,
672 relList, recursive, unique);
680 const TQString& filter,
684 TQStringList relList;
691 char realpath_buffer[MAXPATHLEN + 1];
692 memset(realpath_buffer, 0, MAXPATHLEN + 1);
695 if (realpath( TQFile::encodeName(dirname).data(), realpath_buffer) != 0) {
697 int len = strlen(realpath_buffer);
698 realpath_buffer[len] = TQChar(
'/');
699 realpath_buffer[len+1] = 0;
700 return TQFile::decodeName(realpath_buffer);
709 char realpath_buffer[MAXPATHLEN + 1];
710 memset(realpath_buffer, 0, MAXPATHLEN + 1);
713 if (realpath( TQFile::encodeName(filename).data(), realpath_buffer) != 0) {
715 return TQFile::decodeName(realpath_buffer);
721 void TDEStandardDirs::createSpecialResource(
const char *type)
725 if( getenv(
"XAUTHLOCALHOSTNAME"))
726 strlcpy(hostname, getenv(
"XAUTHLOCALHOSTNAME"), 255 );
728 gethostname(hostname, 255);
729 TQString dir = TQString(
"%1%2-%3").arg(
localtdedir()).arg(type).arg(hostname);
732 int result = readlink(TQFile::encodeName(dir).data(), link, 1023);
733 bool relink = (result == -1) && (errno == ENOENT);
737 if (!TQDir::isRelativePath(link))
739 KDE_struct_stat stat_buf;
740 int res = KDE_lstat(link, &stat_buf);
741 if ((res == -1) && (errno == ENOENT))
745 else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
747 fprintf(stderr,
"Error: \"%s\" is not a directory.\n", link);
750 else if (stat_buf.st_uid != getuid())
752 fprintf(stderr,
"Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
761 fprintf(stderr,
"failed to create \"%s\"", dir.latin1());
763 result = readlink(TQFile::encodeName(dir).data(), link, 1023);
768 TQString srv =
findExe(TQString::fromLatin1(
"lnusertemp"), kfsstnd_defaultbindir());
770 srv =
findExe(TQString::fromLatin1(
"lnusertemp"));
773 if (system(TQFile::encodeName(srv)+
" "+type) < 0 ) {
774 result = readlink(TQFile::encodeName(dir).data(), link, 1023);
784 if (link[0] == TQChar(
'/').latin1()) {
785 dir = TQFile::decodeName(link);
788 dir = TQDir::cleanDirPath(dir+TQFile::decodeName(link));
797 TQStringList *candidates = dircache.find(type);
800 if (strcmp(type,
"socket") == 0)
802 else if (strcmp(type,
"tmp") == 0)
804 else if (strcmp(type,
"cache") == 0)
809 candidates =
new TQStringList();
812 bool restrictionActive =
false;
813 if (d && d->restrictionsActive)
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;
824 dirs = relatives.find(type);
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);
834 prefixList = &prefixes;
836 for (TQStringList::ConstIterator pit = prefixList->begin();
837 pit != prefixList->end();
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)
846 if ((local || testdir.exists()) && !candidates->contains(path))
847 candidates->append(path);
850 if (local && (!strcmp(
"config", type)))
851 candidates->append(
"/etc/trinity/");
856 dirs = absolutes.find(type);
858 for (TQStringList::ConstIterator it = dirs->begin();
859 it != dirs->end(); ++it)
861 testdir.setPath(*it);
862 if (testdir.exists())
865 if (!candidates->contains(filename))
866 candidates->append(filename);
869 dircache.insert(type, candidates);
873 kdDebug() <<
"found dirs for resource " << type <<
":" <<
endl;
874 for (TQStringList::ConstIterator pit = candidates->begin();
875 pit != candidates->end();
878 fprintf(stderr,
"%s\n", (*pit).latin1());
893 p = getenv(
"PATH" );
896 TQString delimiters(TQChar(KPATH_SEPARATOR));
898 tokenize( tokens, p, delimiters );
900 TQStringList exePaths;
903 for(
unsigned i = 0; i < tokens.count(); i++ )
907 if ( p[ 0 ] == TQChar(
'~') )
909 int len = p.find( TQChar(
'/') );
914 p.replace( 0, 1, TQDir::homeDirPath() );
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 ) );
933 const TQString& pstr,
bool ignore)
936 TQString real_appname = appname +
".exe";
938 TQString real_appname = appname;
943 if (real_appname.find(TQDir::separator()) >= 0)
945 info.setFile( real_appname );
946 if( info.exists() && ( ignore || info.isExecutable() )
948 return info.absFilePath();
950 return TQString::null;
953 TQString p = TQString(
"%1/%2").arg(kfsstnd_defaultbindir()).arg(real_appname);
955 if( info.exists() && ( ignore || info.isExecutable() )
956 && ( info.isFile() || info.isSymLink() ) ) {
961 for (TQStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
969 if( info.exists() && ( ignore || info.isExecutable() )
970 && ( info.isFile() || info.isSymLink() ) ) {
978 return TQString::null;
982 const TQString& pstr,
bool ignore )
985 TQString real_appname = appname +
".exe";
987 TQString real_appname = appname;
994 for (TQStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
1001 if( info.exists() && (ignore || info.isExecutable())
1002 && info.isFile() ) {
1007 return list.count();
1010 static int tokenize( TQStringList& tokens,
const TQString& str,
1011 const TQString& delim )
1013 int len = str.length();
1014 TQString token =
"";
1016 for(
int index = 0; index < len; index++)
1018 if ( delim.find( str[ index ] ) >= 0 )
1020 tokens.append( token );
1025 token += str[ index ];
1028 if ( token.length() > 0 )
1030 tokens.append( token );
1033 return tokens.count();
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"))
1071 if (!strcmp(type,
"lib"))
1072 return "lib" KDELIBSUFF
"/";
1073 if (!strcmp(type,
"module"))
1074 return "lib" KDELIBSUFF
"/trinity/";
1075 if (!strcmp(type,
"tqtplugins"))
1076 return "lib" KDELIBSUFF
"/trinity/plugins";
1077 if (!strcmp(type,
"xdgdata-apps"))
1078 return "applications/";
1079 if (!strcmp(type,
"xdgdata-icon"))
1081 if (!strcmp(type,
"xdgdata-pixmap"))
1083 if (!strcmp(type,
"xdgdata-dirs"))
1084 return "desktop-directories/";
1085 if (!strcmp(type,
"xdgconf-menu"))
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";
1095 tqFatal(
"unknown resource type %s", type);
1096 return TQString::null;
1100 const TQString& suffix,
1105 TQString *pPath = savelocations.find(type);
1108 TQStringList *dirs = relatives.find(type);
1110 (strcmp(type,
"socket") == 0) ||
1111 (strcmp(type,
"tmp") == 0) ||
1112 (strcmp(type,
"cache") == 0) ))
1115 dirs = relatives.find(type);
1120 if (strncmp(type,
"xdgdata-", 8) == 0)
1122 else if (strncmp(type,
"xdgconf-", 8) == 0)
1128 dirs = absolutes.find(type);
1130 tqFatal(
"TDEStandardDirs: The resource type %s is not registered", type);
1131 pPath =
new TQString(
realPath(dirs->last()));
1134 savelocations.insert(type, pPath);
1136 TQString fullPath = *pPath + (pPath->endsWith(
"/") ?
"" :
"/") + suffix;
1139 if (KDE_stat(TQFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
1143 if(!
makeDir(fullPath, 0700)) {
1146 dircache.remove(type);
1148 if (!fullPath.endsWith(
"/")) {
1156 TQString fullPath = absPath;
1157 int i = absPath.findRev(
'/');
1160 fullPath =
realPath(absPath.left(i+1))+absPath.mid(i+1);
1165 for (TQStringList::ConstIterator it = candidates.begin();
1166 it != candidates.end(); ++it)
1167 if (fullPath.startsWith(*it))
1169 return fullPath.mid((*it).length());
1179 if (TQDir::isRelativePath(dir))
1182 TQString target = dir;
1183 uint len = target.length();
1186 if (dir.at(len - 1) != TQChar(
'/'))
1187 target += TQChar(
'/');
1195 int pos = target.find(TQChar(
'/'), i);
1196 base += target.mid(i - 1, pos - i + 1);
1197 TQCString baseEncoded = TQFile::encodeName(base);
1199 if (KDE_stat(baseEncoded, &st) != 0)
1203 if (KDE_lstat(baseEncoded, &st) == 0)
1204 (void)unlink(baseEncoded);
1206 if ( KDE_mkdir(baseEncoded, (mode_t) mode) != 0) {
1207 baseEncoded.prepend(
"trying to create local folder " );
1208 perror(baseEncoded.data());
1217 static TQString readEnvPath(
const char *env)
1219 TQCString c_path = getenv(env);
1220 if (c_path.isEmpty())
1221 return TQString::null;
1224 return TQFile::decodeName(c_path).lower();
1226 return TQFile::decodeName(c_path);
1231 static TQString executablePrefix()
1233 char path_buffer[MAXPATHLEN + 1];
1234 path_buffer[MAXPATHLEN] = 0;
1235 int length = readlink (
"/proc/self/exe", path_buffer, MAXPATHLEN);
1237 return TQString::null;
1239 path_buffer[length] = TQChar(
'\0');
1241 TQString path = TQFile::decodeName(path_buffer);
1244 return TQString::null;
1246 int pos = path.findRev(
'/');
1248 return TQString::null;
1249 pos = path.findRev(TQChar(
'/'), pos - 1);
1251 return TQString::null;
1253 return path.left(pos);
1257 TQString TDEStandardDirs::kfsstnd_defaultprefix()
1259 TDEStandardDirsSingleton* s = TDEStandardDirsSingleton::self();
1260 if (!s->defaultprefix.isEmpty())
1261 return s->defaultprefix;
1263 s->defaultprefix = readEnvPath(
"TDEDIR");
1264 if (s->defaultprefix.isEmpty()) {
1265 s->defaultprefix = TQFile::decodeName(
"c:\\kde");
1269 s->defaultprefix = TDEDIR;
1271 if (s->defaultprefix.isEmpty()) {
1272 fprintf(stderr,
"TDEStandardDirs::kfsstnd_defaultprefix(): default TDE prefix not found!\n");
1274 return s->defaultprefix;
1277 TQString TDEStandardDirs::kfsstnd_defaultbindir()
1279 TDEStandardDirsSingleton* s = TDEStandardDirsSingleton::self();
1280 if (!s->defaultbindir.isEmpty())
1281 return s->defaultbindir;
1283 s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1(
"/bin");
1285 s->defaultbindir = __TDE_BINDIR;
1286 if (s->defaultbindir.isEmpty())
1287 s->defaultbindir = kfsstnd_defaultprefix() + TQString::fromLatin1(
"/bin");
1289 if (s->defaultbindir.isEmpty()) {
1290 fprintf(stderr,
"TDEStandardDirs::kfsstnd_defaultbindir(): default binary TDE dir not found!\n");
1292 return s->defaultbindir;
1297 TQStringList tdedirList;
1300 TQString tdedirs = readEnvPath(
"TDEDIRS");
1301 if (!tdedirs.isEmpty())
1303 tokenize(tdedirList, tdedirs, TQChar(KPATH_SEPARATOR));
1307 TQString tdedir = readEnvPath(
"TDEDIR");
1308 if (!tdedir.isEmpty())
1311 tdedirList.append(tdedir);
1316 tdedirList.append(TDEDIR);
1319 #ifdef __KDE_EXECPREFIX
1320 TQString execPrefix(__KDE_EXECPREFIX);
1321 if (execPrefix!=
"NONE")
1322 tdedirList.append(execPrefix);
1325 const TQString linuxExecPrefix = executablePrefix();
1326 if ( !linuxExecPrefix.isEmpty() )
1327 tdedirList.append( linuxExecPrefix );
1332 TQString localKdeDir;
1333 if (getuid() == 0) {
1334 localKdeDir = readEnvPath(
"TDEROOTHOME");
1335 if (localKdeDir.isEmpty() ==
true)
1336 localKdeDir = readEnvPath(
"TDEHOME");
1339 localKdeDir = readEnvPath(
"TDEHOME");
1341 if (!localKdeDir.isEmpty())
1343 if (localKdeDir[localKdeDir.length()-1] != TQChar(
'/'))
1344 localKdeDir += TQChar(
'/');
1348 localKdeDir = TQDir::homeDirPath() +
"/.trinity/";
1351 if (localKdeDir != TQString(
"-/"))
1357 TQStringList::ConstIterator end(tdedirList.end());
1358 for (TQStringList::ConstIterator it = tdedirList.begin();
1367 TQStringList xdgdirList;
1368 TQString xdgdirs = readEnvPath(
"XDG_CONFIG_DIRS");
1369 if (!xdgdirs.isEmpty())
1371 tokenize(xdgdirList, xdgdirs, TQChar(KPATH_SEPARATOR));
1376 xdgdirList.append(
"/etc/xdg");
1378 xdgdirList.append(kfsstnd_defaultprefix() +
"/etc/xdg");
1380 xdgdirList.append(KDESYSCONFDIR
"/xdg");
1384 TQString localXdgDir = readEnvPath(
"XDG_CONFIG_HOME");
1385 if (!localXdgDir.isEmpty())
1387 if (localXdgDir[localXdgDir.length()-1] != TQChar(
'/'))
1388 localXdgDir += TQChar(
'/');
1392 localXdgDir = TQDir::homeDirPath() +
"/.config/";
1398 for (TQStringList::ConstIterator it = xdgdirList.begin();
1399 it != xdgdirList.end(); ++it)
1407 xdgdirs = readEnvPath(
"XDG_DATA_DIRS");
1408 if (!xdgdirs.isEmpty())
1410 tokenize(xdgdirList, xdgdirs, TQChar(KPATH_SEPARATOR));
1415 for (TQStringList::ConstIterator it = tdedirList.begin();
1416 it != tdedirList.end(); ++it)
1419 if (dir[dir.length()-1] != TQChar(
'/'))
1421 xdgdirList.append(dir+
"share/");
1424 xdgdirList.append(
"/usr/local/share/");
1425 xdgdirList.append(
"/usr/share/");
1428 localXdgDir = readEnvPath(
"XDG_DATA_HOME");
1429 if (!localXdgDir.isEmpty())
1431 if (localXdgDir[localXdgDir.length()-1] != TQChar(
'/'))
1432 localXdgDir += TQChar(
'/');
1436 localXdgDir = TQDir::homeDirPath() +
"/.local/share/";
1442 for (TQStringList::ConstIterator it = xdgdirList.begin();
1443 it != xdgdirList.end(); ++it)
1452 while (types[index] != 0) {
1462 void TDEStandardDirs::checkConfig()
const
1464 if (!addedCustoms && TDEGlobal::_instance && TDEGlobal::_instance->_config)
1468 static TQStringList lookupProfiles(
const TQString &mapFile)
1470 TQStringList profiles;
1472 if (mapFile.isEmpty() || !TQFile::exists(mapFile))
1474 profiles <<
"default";
1478 struct passwd *pw = getpwuid(geteuid());
1481 profiles <<
"default";
1485 TQCString user = pw->pw_name;
1487 gid_t sup_gids[512];
1488 int sup_gids_nr = getgroups(512, sup_gids);
1491 mapCfg.setGroup(
"Users");
1492 if (mapCfg.hasKey(user.data()))
1494 profiles = mapCfg.readListEntry(user.data());
1498 mapCfg.setGroup(
"General");
1499 TQStringList groups = mapCfg.readListEntry(
"groups");
1501 mapCfg.setGroup(
"Groups");
1503 for( TQStringList::ConstIterator it = groups.begin();
1504 it != groups.end(); ++it )
1506 TQCString grp = (*it).utf8();
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)
1514 profiles += mapCfg.readListEntry(*it);
1518 for(
int i = 0; i < sup_gids_nr; i++)
1520 if (sup_gids[i] == gid)
1523 profiles += mapCfg.readListEntry(*it);
1530 if (profiles.isEmpty())
1531 profiles <<
"default";
1535 extern bool kde_kiosk_admin;
1539 if (addedCustoms && !d->checkRestrictions)
1547 TQString oldGroup = config->
group();
1552 addedCustoms =
true;
1555 TQString group = TQString::fromLatin1(
"Directories");
1558 TQString kioskAdmin = config->
readEntry(
"kioskAdmin");
1559 if (!kioskAdmin.isEmpty() && !kde_kiosk_admin)
1561 int i = kioskAdmin.find(
':');
1562 TQString user = kioskAdmin.left(i);
1563 TQString host = kioskAdmin.mid(i+1);
1566 char hostname[ 256 ];
1567 hostname[ 0 ] = TQChar(
'\0');
1568 if (!gethostname( hostname, 255 ))
1569 hostname[
sizeof(hostname)-1] = TQChar(
'\0');
1572 (host.isEmpty() || (host == hostname)))
1574 kde_kiosk_admin =
true;
1578 bool readProfiles =
true;
1580 if (kde_kiosk_admin && !TQCString(getenv(
"TDE_KIOSK_NO_PROFILES")).isEmpty())
1581 readProfiles =
false;
1583 TQString userMapFile = config->
readEntry(
"userProfileMapFile");
1584 TQString profileDirsPrefix = config->
readEntry(
"profileDirsPrefix");
1585 if (!profileDirsPrefix.isEmpty() && !profileDirsPrefix.endsWith(
"/"))
1586 profileDirsPrefix.append(
'/');
1588 TQStringList profiles;
1590 profiles = lookupProfiles(userMapFile);
1593 bool priority =
false;
1598 for (TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
1606 if (list.isEmpty() && !profile.isEmpty() && !profileDirsPrefix.isEmpty())
1608 TQString dir = profileDirsPrefix + profile;
1616 TQMap<TQString, TQString> entries = config->
entryMap(group);
1617 for (TQMap<TQString, TQString>::ConstIterator it2 = entries.begin();
1618 it2 != entries.end(); it2++)
1620 TQString key = it2.key();
1621 if (key.startsWith(
"dir_")) {
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)
1632 if (profiles.isEmpty())
1634 profile = profiles.back();
1635 group = TQString::fromLatin1(
"Directories-%1").arg(profile);
1636 profiles.pop_back();
1642 if (!kde_kiosk_admin || TQCString(getenv(
"TDE_KIOSK_NO_RESTRICTIONS")).isEmpty())
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++)
1649 TQString key = it2.key();
1652 d->restrictionsActive =
true;
1653 d->restrictions.insert(key.latin1(), &d->restrictionsActive);
1654 dircache.remove(key.latin1());
1662 bool configDirsChanged = (
resourceDirs(
"config").count() != configdirs);
1664 d->checkRestrictions = configDirsChanged;
1666 return configDirsChanged;
1672 return prefixes.first();
1678 return d->xdgdata_prefixes.first();
1684 return d->xdgconf_prefixes.first();
1690 const TQString& filename,
const TDEInstance* inst )
1696 const TQString& filename,
const TDEInstance* inst )
1702 const TQString& filename,
bool createDir,
const TDEInstance* inst )
1706 int slash = filename.findRev(
'/')+1;
1708 return inst->
dirs()->
saveLocation(type, TQString::null, createDir) + filename;
1711 TQString dir = filename.left(slash);
1712 TQString file = filename.mid(slash);
KDE Configuration entries.
Little helper class to clean up static objects that are held as pointer.
Represents a user on your system.
TQString loginName() const
The login name of the user.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
Reads the value of an entry specified by pKey in the current group.
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
Reads a boolean entry.
TQString group() const
Returns the name of the group in which we are searching for keys and from which we are retrieving ent...
int readListEntry(const TQString &pKey, TQStrList &list, char sep=',') const
Reads a list of strings.
void setGroup(const TQString &group)
Specifies the group in which keys will be read and written.
Access KDE Configuration entries.
virtual TQMap< TQString, TQString > entryMap(const TQString &pGroup) const
Returns a map (tree) of entries for all entries in a particular group.
Access to KDE global objects for use in shared libraries.
TDEStandardDirs * dirs() const
Returns the application standard dirs object.
Site-independent access to standard KDE directories.
void addKDEDefaults()
This function adds the defaults that are used by the current KDE version.
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.
TQString localxdgdatadir() const
TQ_UINT32 calcResourceHash(const char *type, const TQString &filename, bool deep) const
TQString localxdgconfdir() const
void addXdgConfigPrefix(const TQString &dir)
Adds another search dir to front of the XDG_CONFIG_XXX list of prefixes.
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.
void addXdgDataPrefix(const TQString &dir)
Adds another search dir to front of the XDG_DATA_XXX list of prefixes.
TQString findResourceDir(const char *type, const TQString &filename) const
Tries to find the directory the file is in.
TQString relativeLocation(const char *type, const TQString &absPath)
Converts an absolute path to a path relative to a certain resource.
TQStringList allTypes() const
This function will return a list of all the types that TDEStandardDirs supports.
TQString localtdedir() const
Returns the toplevel directory in which TDEStandardDirs will store things.
TQStringList resourceDirs(const char *type) const
This function is used internally by almost all other function as it serves and fills the directories ...
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.
virtual ~TDEStandardDirs()
TDEStandardDirs' destructor.
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...
static TQString findExe(const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
Finds the executable in the system path.
static bool makeDir(const TQString &dir, int mode=0755)
Recursively creates still-missing directories in the given path.
static TQString realPath(const TQString &dirname)
Expands all symbolic links and resolves references to '/.
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.
static TQString realFilePath(const TQString &filename)
Expands all symbolic links and resolves references to '/.
bool isRestrictedResource(const char *type, const TQString &relPath=TQString::null) const
Checks whether a resource is restricted as part of the KIOSK framework.
bool addCustomized(TDEConfig *config)
Reads customized entries out of the given config object and add them via addResourceDirs().
bool addResourceType(const char *type, const TQString &relativename)
Adds suffixes for types.
TDEStandardDirs()
TDEStandardDirs' constructor.
TQString findResource(const char *type, const TQString &filename) const
Tries to find a resource in the following order:
static TQString kde_default(const char *type)
This returns a default relative path for the standard KDE resource types.
void addPrefix(const TQString &dir)
Adds another search dir to front of the fsstnd list.
static TQStringList systemPaths(const TQString &pstr=TQString::null)
Returns a TQStringList list of pathnames in the system path.
static bool exists(const TQString &fullPath)
Checks for existence and accessability of a file or directory.
kndbgstream & endl(kndbgstream &s)
Does nothing.
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
TQString tildeExpand(const TQString &path)
Performs tilde expansion on path.
TDEAction * copy(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)