00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "config.h"
00028
00029 #include <stdlib.h>
00030 #include <assert.h>
00031 #include <errno.h>
00032 #ifdef HAVE_SYS_STAT_H
00033 #include <sys/stat.h>
00034 #endif
00035 #include <sys/types.h>
00036 #include <dirent.h>
00037 #include <pwd.h>
00038
00039 #include <qregexp.h>
00040 #include <qasciidict.h>
00041 #include <qdict.h>
00042 #include <qdir.h>
00043 #include <qfileinfo.h>
00044 #include <qstring.h>
00045 #include <qstringlist.h>
00046
00047 #include "kstandarddirs.h"
00048 #include "kconfig.h"
00049 #include "kdebug.h"
00050 #include "kinstance.h"
00051 #include "kshell.h"
00052 #include <sys/param.h>
00053 #include <unistd.h>
00054
00055 template class QDict<QStringList>;
00056
00057 class KStandardDirs::KStandardDirsPrivate
00058 {
00059 public:
00060 KStandardDirsPrivate()
00061 : restrictionsActive(false),
00062 dataRestrictionActive(false)
00063 { }
00064
00065 bool restrictionsActive;
00066 bool dataRestrictionActive;
00067 QAsciiDict<bool> restrictions;
00068 QStringList xdgdata_prefixes;
00069 QStringList xdgconf_prefixes;
00070 };
00071
00072 static const char* const types[] = {"html", "icon", "apps", "sound",
00073 "data", "locale", "services", "mime",
00074 "servicetypes", "config", "exe",
00075 "wallpaper", "lib", "pixmap", "templates",
00076 "module", "qtplugins",
00077 "xdgdata-apps", "xdgdata-dirs", "xdgconf-menu",
00078 "kcfg", 0 };
00079
00080 #if defined(__x86_64__) || defined(__s390x__) || defined(__powerpc64__) || defined(__sparc64__)
00081 # define LIBDIR_NAME "lib64"
00082 #else
00083 # define LIBDIR_NAME "lib"
00084 #endif
00085
00086 static int tokenize( QStringList& token, const QString& str,
00087 const QString& delim );
00088
00089 KStandardDirs::KStandardDirs( ) : addedCustoms(false)
00090 {
00091 d = new KStandardDirsPrivate;
00092 dircache.setAutoDelete(true);
00093 relatives.setAutoDelete(true);
00094 absolutes.setAutoDelete(true);
00095 savelocations.setAutoDelete(true);
00096 addKDEDefaults();
00097 }
00098
00099 KStandardDirs::~KStandardDirs()
00100 {
00101 delete d;
00102 }
00103
00104 bool KStandardDirs::isRestrictedResource(const char *type, const QString& relPath) const
00105 {
00106 if (!d || !d->restrictionsActive)
00107 return false;
00108
00109 if (d->restrictions[type])
00110 return true;
00111
00112 if (strcmp(type, "data")==0)
00113 {
00114 applyDataRestrictions(relPath);
00115 if (d->dataRestrictionActive)
00116 {
00117 d->dataRestrictionActive = false;
00118 return true;
00119 }
00120 }
00121 return false;
00122 }
00123
00124 void KStandardDirs::applyDataRestrictions(const QString &relPath) const
00125 {
00126 QString key;
00127 int i = relPath.find('/');
00128 if (i != -1)
00129 key = "data_"+relPath.left(i);
00130 else
00131 key = "data_"+relPath;
00132
00133 if (d && d->restrictions[key.latin1()])
00134 d->dataRestrictionActive = true;
00135 }
00136
00137
00138 QStringList KStandardDirs::allTypes() const
00139 {
00140 QStringList list;
00141 for (int i = 0; types[i] != 0; ++i)
00142 list.append(QString::fromLatin1(types[i]));
00143 return list;
00144 }
00145
00146 void KStandardDirs::addPrefix( const QString& _dir )
00147 {
00148 if (_dir.isNull())
00149 return;
00150
00151 QString dir = _dir;
00152 if (dir.at(dir.length() - 1) != '/')
00153 dir += '/';
00154
00155 if (!prefixes.contains(dir)) {
00156 prefixes.append(dir);
00157 dircache.clear();
00158 }
00159 }
00160
00161 void KStandardDirs::addXdgConfigPrefix( const QString& _dir )
00162 {
00163 if (_dir.isNull())
00164 return;
00165
00166 QString dir = _dir;
00167 if (dir.at(dir.length() - 1) != '/')
00168 dir += '/';
00169
00170 if (!d->xdgconf_prefixes.contains(dir)) {
00171 d->xdgconf_prefixes.append(dir);
00172 dircache.clear();
00173 }
00174 }
00175
00176 void KStandardDirs::addXdgDataPrefix( const QString& _dir )
00177 {
00178 if (_dir.isNull())
00179 return;
00180
00181 QString dir = _dir;
00182 if (dir.at(dir.length() - 1) != '/')
00183 dir += '/';
00184
00185 if (!d->xdgdata_prefixes.contains(dir)) {
00186 d->xdgdata_prefixes.append(dir);
00187 dircache.clear();
00188 }
00189 }
00190
00191
00192 QString KStandardDirs::kfsstnd_prefixes()
00193 {
00194 return prefixes.join(":");
00195 }
00196
00197 bool KStandardDirs::addResourceType( const char *type,
00198 const QString& relativename )
00199 {
00200 if (relativename.isNull())
00201 return false;
00202
00203 QStringList *rels = relatives.find(type);
00204 if (!rels) {
00205 rels = new QStringList();
00206 relatives.insert(type, rels);
00207 }
00208 QString copy = relativename;
00209 if (copy.at(copy.length() - 1) != '/')
00210 copy += '/';
00211 if (!rels->contains(copy)) {
00212 rels->prepend(copy);
00213 dircache.remove(type);
00214 return true;
00215 }
00216 return false;
00217 }
00218
00219 bool KStandardDirs::addResourceDir( const char *type,
00220 const QString& absdir)
00221 {
00222 QStringList *paths = absolutes.find(type);
00223 if (!paths) {
00224 paths = new QStringList();
00225 absolutes.insert(type, paths);
00226 }
00227 QString copy = absdir;
00228 if (copy.at(copy.length() - 1) != '/')
00229 copy += '/';
00230
00231 if (!paths->contains(copy)) {
00232 paths->append(copy);
00233 dircache.remove(type);
00234 return true;
00235 }
00236 return false;
00237 }
00238
00239 QString KStandardDirs::findResource( const char *type,
00240 const QString& filename ) const
00241 {
00242 if (filename.at(0) == '/')
00243 return filename;
00244
00245 #if 0
00246 kdDebug() << "Find resource: " << type << endl;
00247 for (QStringList::ConstIterator pit = prefixes.begin();
00248 pit != prefixes.end();
00249 pit++)
00250 {
00251 kdDebug() << "Prefix: " << *pit << endl;
00252 }
00253 #endif
00254
00255 QString dir = findResourceDir(type, filename);
00256 if (dir.isNull())
00257 return dir;
00258 else return dir + filename;
00259 }
00260
00261 static Q_UINT32 updateHash(const QString &file, Q_UINT32 hash)
00262 {
00263 QCString cFile = QFile::encodeName(file);
00264 struct stat buff;
00265 if ((access(cFile, R_OK) == 0) &&
00266 (stat( cFile, &buff ) == 0) &&
00267 (S_ISREG( buff.st_mode )))
00268 {
00269 hash = hash + (Q_UINT32) buff.st_ctime;
00270 }
00271 return hash;
00272 }
00273
00274 Q_UINT32 KStandardDirs::calcResourceHash( const char *type,
00275 const QString& filename, bool deep) const
00276 {
00277 Q_UINT32 hash = 0;
00278
00279 if (filename.at(0) == '/')
00280 {
00281
00282 return updateHash(filename, hash);
00283 }
00284 if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00285 applyDataRestrictions(filename);
00286 QStringList candidates = resourceDirs(type);
00287 QString fullPath;
00288
00289 for (QStringList::ConstIterator it = candidates.begin();
00290 it != candidates.end(); it++)
00291 {
00292 hash = updateHash(*it + filename, hash);
00293 if (!deep && hash)
00294 return hash;
00295 }
00296 return hash;
00297 }
00298
00299
00300 QStringList KStandardDirs::findDirs( const char *type,
00301 const QString& reldir ) const
00302 {
00303 QDir testdir;
00304 QStringList list;
00305 if (reldir.startsWith("/"))
00306 {
00307 testdir.setPath(reldir);
00308 if (testdir.exists())
00309 {
00310 if (reldir.endsWith("/"))
00311 list.append(reldir);
00312 else
00313 list.append(reldir+'/');
00314 }
00315 return list;
00316 }
00317
00318 checkConfig();
00319
00320 if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00321 applyDataRestrictions(reldir);
00322 QStringList candidates = resourceDirs(type);
00323
00324 for (QStringList::ConstIterator it = candidates.begin();
00325 it != candidates.end(); it++) {
00326 testdir.setPath(*it + reldir);
00327 if (testdir.exists())
00328 list.append(testdir.absPath() + '/');
00329 }
00330
00331 return list;
00332 }
00333
00334 QString KStandardDirs::findResourceDir( const char *type,
00335 const QString& filename) const
00336 {
00337 #ifndef NDEBUG
00338 if (filename.isEmpty()) {
00339 kdWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!" << endl;
00340 return QString::null;
00341 }
00342 #endif
00343
00344 if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00345 applyDataRestrictions(filename);
00346 QStringList candidates = resourceDirs(type);
00347 QString fullPath;
00348
00349 for (QStringList::ConstIterator it = candidates.begin();
00350 it != candidates.end(); it++)
00351 if (exists(*it + filename))
00352 return *it;
00353
00354 #ifndef NDEBUG
00355 if(false && type != "locale")
00356 kdDebug() << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\"." << endl;
00357 #endif
00358
00359 return QString::null;
00360 }
00361
00362 bool KStandardDirs::exists(const QString &fullPath)
00363 {
00364 struct stat buff;
00365 if (access(QFile::encodeName(fullPath), R_OK) == 0 && stat( QFile::encodeName(fullPath), &buff ) == 0)
00366 if (fullPath.at(fullPath.length() - 1) != '/') {
00367 if (S_ISREG( buff.st_mode ))
00368 return true;
00369 } else
00370 if (S_ISDIR( buff.st_mode ))
00371 return true;
00372 return false;
00373 }
00374
00375 static void lookupDirectory(const QString& path, const QString &relPart,
00376 const QRegExp ®exp,
00377 QStringList& list,
00378 QStringList& relList,
00379 bool recursive, bool unique)
00380 {
00381 QString pattern = regexp.pattern();
00382 if (recursive || pattern.contains('?') || pattern.contains('*'))
00383 {
00384
00385 DIR *dp = opendir( QFile::encodeName(path));
00386 if (!dp)
00387 return;
00388
00389 assert(path.at(path.length() - 1) == '/');
00390
00391 struct dirent *ep;
00392 struct stat buff;
00393
00394 QString _dot(".");
00395 QString _dotdot("..");
00396
00397 while( ( ep = readdir( dp ) ) != 0L )
00398 {
00399 QString fn( QFile::decodeName(ep->d_name));
00400 if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1).latin1() == '~')
00401 continue;
00402
00403 if (!recursive && !regexp.exactMatch(fn))
00404 continue;
00405
00406 QString pathfn = path + fn;
00407 if ( stat( QFile::encodeName(pathfn), &buff ) != 0 ) {
00408 kdDebug() << "Error stat'ing " << pathfn << " : " << perror << endl;
00409 continue;
00410 }
00411 if ( recursive ) {
00412 if ( S_ISDIR( buff.st_mode )) {
00413 lookupDirectory(pathfn + '/', relPart + fn + '/', regexp, list, relList, recursive, unique);
00414 }
00415 if (!regexp.exactMatch(fn))
00416 continue;
00417 }
00418 if ( S_ISREG( buff.st_mode))
00419 {
00420 if (!unique || !relList.contains(relPart + fn))
00421 {
00422 list.append( pathfn );
00423 relList.append( relPart + fn );
00424 }
00425 }
00426 }
00427 closedir( dp );
00428 }
00429 else
00430 {
00431
00432 QString fn = pattern;
00433 QString pathfn = path + fn;
00434 struct stat buff;
00435 if ( stat( QFile::encodeName(pathfn), &buff ) != 0 )
00436 return;
00437 if ( S_ISREG( buff.st_mode))
00438 {
00439 if (!unique || !relList.contains(relPart + fn))
00440 {
00441 list.append( pathfn );
00442 relList.append( relPart + fn );
00443 }
00444 }
00445 }
00446 }
00447
00448 static void lookupPrefix(const QString& prefix, const QString& relpath,
00449 const QString& relPart,
00450 const QRegExp ®exp,
00451 QStringList& list,
00452 QStringList& relList,
00453 bool recursive, bool unique)
00454 {
00455 if (relpath.isNull()) {
00456 lookupDirectory(prefix, relPart, regexp, list,
00457 relList, recursive, unique);
00458 return;
00459 }
00460 QString path;
00461 QString rest;
00462
00463 if (relpath.length())
00464 {
00465 int slash = relpath.find('/');
00466 if (slash < 0)
00467 rest = relpath.left(relpath.length() - 1);
00468 else {
00469 path = relpath.left(slash);
00470 rest = relpath.mid(slash + 1);
00471 }
00472 }
00473
00474 assert(prefix.at(prefix.length() - 1) == '/');
00475
00476 struct stat buff;
00477
00478 if (path.contains('*') || path.contains('?')) {
00479
00480 QRegExp pathExp(path, true, true);
00481 DIR *dp = opendir( QFile::encodeName(prefix) );
00482 if (!dp) {
00483 return;
00484 }
00485
00486 struct dirent *ep;
00487
00488 QString _dot(".");
00489 QString _dotdot("..");
00490
00491 while( ( ep = readdir( dp ) ) != 0L )
00492 {
00493 QString fn( QFile::decodeName(ep->d_name));
00494 if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1) == '~')
00495 continue;
00496
00497 if (pathExp.search(fn) == -1)
00498 continue;
00499 QString rfn = relPart+fn;
00500 fn = prefix + fn;
00501 if ( stat( QFile::encodeName(fn), &buff ) != 0 ) {
00502 kdDebug() << "Error statting " << fn << " : " << perror << endl;
00503 continue;
00504 }
00505 if ( S_ISDIR( buff.st_mode ))
00506 lookupPrefix(fn + '/', rest, rfn + '/', regexp, list, relList, recursive, unique);
00507 }
00508
00509 closedir( dp );
00510 } else {
00511
00512
00513 lookupPrefix(prefix + path + '/', rest,
00514 relPart + path + '/', regexp, list,
00515 relList, recursive, unique);
00516 }
00517 }
00518
00519 QStringList
00520 KStandardDirs::findAllResources( const char *type,
00521 const QString& filter,
00522 bool recursive,
00523 bool unique,
00524 QStringList &relList) const
00525 {
00526 QStringList list;
00527 QString filterPath;
00528 QString filterFile;
00529
00530 if (filter.length())
00531 {
00532 int slash = filter.findRev('/');
00533 if (slash < 0)
00534 filterFile = filter;
00535 else {
00536 filterPath = filter.left(slash + 1);
00537 filterFile = filter.mid(slash + 1);
00538 }
00539 }
00540
00541 checkConfig();
00542
00543 QStringList candidates;
00544 if (filterPath.startsWith("/"))
00545 {
00546 filterPath = filterPath.mid(1);
00547 candidates << "/";
00548 }
00549 else
00550 {
00551 if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00552 applyDataRestrictions(filter);
00553 candidates = resourceDirs(type);
00554 }
00555 if (filterFile.isEmpty())
00556 filterFile = "*";
00557
00558 QRegExp regExp(filterFile, true, true);
00559
00560 for (QStringList::ConstIterator it = candidates.begin();
00561 it != candidates.end(); it++)
00562 {
00563 lookupPrefix(*it, filterPath, "", regExp, list,
00564 relList, recursive, unique);
00565 }
00566
00567 return list;
00568 }
00569
00570 QStringList
00571 KStandardDirs::findAllResources( const char *type,
00572 const QString& filter,
00573 bool recursive,
00574 bool unique) const
00575 {
00576 QStringList relList;
00577 return findAllResources(type, filter, recursive, unique, relList);
00578 }
00579
00580 QString
00581 KStandardDirs::realPath(const QString &dirname)
00582 {
00583 char realpath_buffer[MAXPATHLEN + 1];
00584 memset(realpath_buffer, 0, MAXPATHLEN + 1);
00585
00586
00587 if (realpath( QFile::encodeName(dirname).data(), realpath_buffer) != 0) {
00588
00589 int len = strlen(realpath_buffer);
00590 realpath_buffer[len] = '/';
00591 realpath_buffer[len+1] = 0;
00592 return QFile::decodeName(realpath_buffer);
00593 }
00594
00595 return dirname;
00596 }
00597
00598 void KStandardDirs::createSpecialResource(const char *type)
00599 {
00600 char hostname[256];
00601 hostname[0] = 0;
00602 gethostname(hostname, 255);
00603 QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
00604 char link[1024];
00605 link[1023] = 0;
00606 int result = readlink(QFile::encodeName(dir).data(), link, 1023);
00607 bool relink = (result == -1) && (errno == ENOENT);
00608 if ((result > 0) && (link[0] == '/'))
00609 {
00610 link[result] = 0;
00611 struct stat stat_buf;
00612 int res = lstat(link, &stat_buf);
00613 if ((res == -1) && (errno == ENOENT))
00614 {
00615 relink = true;
00616 }
00617 else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
00618 {
00619 fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
00620 relink = true;
00621 }
00622 else if (stat_buf.st_uid != getuid())
00623 {
00624 fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
00625 relink = true;
00626 }
00627 }
00628 if (relink)
00629 {
00630 QString srv = findExe(QString::fromLatin1("lnusertemp"), KDEDIR+QString::fromLatin1("/bin"));
00631 if (srv.isEmpty())
00632 srv = findExe(QString::fromLatin1("lnusertemp"));
00633 if (!srv.isEmpty())
00634 {
00635 system(QFile::encodeName(srv)+" "+type);
00636 result = readlink(QFile::encodeName(dir).data(), link, 1023);
00637 }
00638 }
00639 if (result > 0)
00640 {
00641 link[result] = 0;
00642 if (link[0] == '/')
00643 dir = QFile::decodeName(link);
00644 else
00645 dir = QDir::cleanDirPath(dir+QFile::decodeName(link));
00646 }
00647 addResourceDir(type, dir+'/');
00648 }
00649
00650 QStringList KStandardDirs::resourceDirs(const char *type) const
00651 {
00652 QStringList *candidates = dircache.find(type);
00653
00654 if (!candidates) {
00655 if (strcmp(type, "socket") == 0)
00656 const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00657 else if (strcmp(type, "tmp") == 0)
00658 const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00659 else if (strcmp(type, "cache") == 0)
00660 const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00661
00662 QDir testdir;
00663
00664 candidates = new QStringList();
00665 QStringList *dirs;
00666
00667 bool restrictionActive = false;
00668 if (d && d->restrictionsActive)
00669 {
00670 if (d->dataRestrictionActive)
00671 restrictionActive = true;
00672 else if (d->restrictions["all"])
00673 restrictionActive = true;
00674 else if (d->restrictions[type])
00675 restrictionActive = true;
00676 d->dataRestrictionActive = false;
00677 }
00678
00679 dirs = relatives.find(type);
00680 if (dirs)
00681 {
00682 bool local = true;
00683 const QStringList *prefixList = 0;
00684 if (strncmp(type, "xdgdata-", 8) == 0)
00685 prefixList = &(d->xdgdata_prefixes);
00686 else if (strncmp(type, "xdgconf-", 8) == 0)
00687 prefixList = &(d->xdgconf_prefixes);
00688 else
00689 prefixList = &prefixes;
00690
00691 for (QStringList::ConstIterator pit = prefixList->begin();
00692 pit != prefixList->end();
00693 pit++)
00694 {
00695 for (QStringList::ConstIterator it = dirs->begin();
00696 it != dirs->end(); ++it) {
00697 QString path = realPath(*pit + *it);
00698 testdir.setPath(path);
00699 if (local && restrictionActive)
00700 continue;
00701 if ((local || testdir.exists()) && !candidates->contains(path))
00702 candidates->append(path);
00703 }
00704 local = false;
00705 }
00706 }
00707 dirs = absolutes.find(type);
00708 if (dirs)
00709 for (QStringList::ConstIterator it = dirs->begin();
00710 it != dirs->end(); ++it)
00711 {
00712 testdir.setPath(*it);
00713 if (testdir.exists())
00714 {
00715 QString filename = realPath(*it);
00716 if (!candidates->contains(filename))
00717 candidates->append(filename);
00718 }
00719 }
00720 dircache.insert(type, candidates);
00721 }
00722
00723 #if 0
00724 kdDebug() << "found dirs for resource " << type << ":" << endl;
00725 for (QStringList::ConstIterator pit = candidates->begin();
00726 pit != candidates->end();
00727 pit++)
00728 {
00729 fprintf(stderr, "%s\n", (*pit).latin1());
00730 }
00731 #endif
00732
00733
00734 return *candidates;
00735 }
00736
00737 QStringList KStandardDirs::systemPaths( const QString& pstr )
00738 {
00739 QStringList tokens;
00740 QString p = pstr;
00741
00742 if( p.isNull() )
00743 {
00744 p = getenv( "PATH" );
00745 }
00746
00747 tokenize( tokens, p, ":\b" );
00748
00749 QStringList exePaths;
00750
00751
00752 for( unsigned i = 0; i < tokens.count(); i++ )
00753 {
00754 p = tokens[ i ];
00755
00756 if ( p[ 0 ] == '~' )
00757 {
00758 int len = p.find( '/' );
00759 if ( len == -1 )
00760 len = p.length();
00761 if ( len == 1 )
00762 {
00763 p.replace( 0, 1, QDir::homeDirPath() );
00764 }
00765 else
00766 {
00767 QString user = p.mid( 1, len - 1 );
00768 struct passwd *dir = getpwnam( user.local8Bit().data() );
00769 if ( dir && strlen( dir->pw_dir ) )
00770 p.replace( 0, len, QString::fromLocal8Bit( dir->pw_dir ) );
00771 }
00772 }
00773
00774 exePaths << p;
00775 }
00776
00777 return exePaths;
00778 }
00779
00780
00781 QString KStandardDirs::findExe( const QString& appname,
00782 const QString& pstr, bool ignore)
00783 {
00784 QFileInfo info;
00785
00786
00787 if (appname.startsWith(QString::fromLatin1("/")))
00788 {
00789 info.setFile( appname );
00790 if( info.exists() && ( ignore || info.isExecutable() )
00791 && info.isFile() ) {
00792 return appname;
00793 }
00794 return QString::null;
00795 }
00796
00797 QString p = QString("%1/%2").arg(__KDE_BINDIR).arg(appname);
00798 info.setFile( p );
00799 if( info.exists() && ( ignore || info.isExecutable() )
00800 && ( info.isFile() || info.isSymLink() ) ) {
00801 return p;
00802 }
00803
00804 QStringList exePaths = systemPaths( pstr );
00805 for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); it++)
00806 {
00807 p = (*it) + "/";
00808 p += appname;
00809
00810
00811 info.setFile( p );
00812
00813 if( info.exists() && ( ignore || info.isExecutable() )
00814 && ( info.isFile() || info.isSymLink() ) ) {
00815 return p;
00816 }
00817 }
00818
00819
00820
00821
00822 return QString::null;
00823 }
00824
00825 int KStandardDirs::findAllExe( QStringList& list, const QString& appname,
00826 const QString& pstr, bool ignore )
00827 {
00828 QFileInfo info;
00829 QString p;
00830 list.clear();
00831
00832 QStringList exePaths = systemPaths( pstr );
00833 for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); it++)
00834 {
00835 p = (*it) + "/";
00836 p += appname;
00837
00838 info.setFile( p );
00839
00840 if( info.exists() && (ignore || info.isExecutable())
00841 && info.isFile() ) {
00842 list.append( p );
00843 }
00844 }
00845
00846 return list.count();
00847 }
00848
00849 static int tokenize( QStringList& tokens, const QString& str,
00850 const QString& delim )
00851 {
00852 int len = str.length();
00853 QString token = "";
00854
00855 for( int index = 0; index < len; index++)
00856 {
00857 if ( delim.find( str[ index ] ) >= 0 )
00858 {
00859 tokens.append( token );
00860 token = "";
00861 }
00862 else
00863 {
00864 token += str[ index ];
00865 }
00866 }
00867 if ( token.length() > 0 )
00868 {
00869 tokens.append( token );
00870 }
00871
00872 return tokens.count();
00873 }
00874
00875 QString KStandardDirs::kde_default(const char *type) {
00876 if (!strcmp(type, "data"))
00877 return "share/apps/";
00878 if (!strcmp(type, "html"))
00879 return "share/doc/HTML/";
00880 if (!strcmp(type, "icon"))
00881 return "share/icons/";
00882 if (!strcmp(type, "config"))
00883 return "share/config/";
00884 if (!strcmp(type, "pixmap"))
00885 return "share/pixmaps/";
00886 if (!strcmp(type, "apps"))
00887 return "share/applnk/";
00888 if (!strcmp(type, "sound"))
00889 return "share/sounds/";
00890 if (!strcmp(type, "locale"))
00891 return "share/locale/";
00892 if (!strcmp(type, "services"))
00893 return "share/services/";
00894 if (!strcmp(type, "servicetypes"))
00895 return "share/servicetypes/";
00896 if (!strcmp(type, "mime"))
00897 return "share/mimelnk/";
00898 if (!strcmp(type, "cgi"))
00899 return "cgi-bin/";
00900 if (!strcmp(type, "wallpaper"))
00901 return "share/wallpapers/";
00902 if (!strcmp(type, "templates"))
00903 return "share/templates/";
00904 if (!strcmp(type, "exe"))
00905 return "bin/";
00906 if (!strcmp(type, "lib"))
00907 return LIBDIR_NAME "/";
00908 if (!strcmp(type, "module"))
00909 return LIBDIR_NAME "/kde3/";
00910 if (!strcmp(type, "qtplugins"))
00911 return LIBDIR_NAME "/kde3/plugins/";
00912 if (!strcmp(type, "xdgdata-apps"))
00913 return "applications/";
00914 if (!strcmp(type, "xdgdata-dirs"))
00915 return "desktop-directories/";
00916 if (!strcmp(type, "xdgconf-menu"))
00917 return "menus/";
00918 if (!strcmp(type, "kcfg"))
00919 return "share/config.kcfg";
00920 qFatal("unknown resource type %s", type);
00921 return QString::null;
00922 }
00923
00924 QString KStandardDirs::saveLocation(const char *type,
00925 const QString& suffix,
00926 bool create) const
00927 {
00928 checkConfig();
00929
00930 QString *pPath = savelocations.find(type);
00931 if (!pPath)
00932 {
00933 QStringList *dirs = relatives.find(type);
00934 if (!dirs && (
00935 (strcmp(type, "socket") == 0) ||
00936 (strcmp(type, "tmp") == 0) ||
00937 (strcmp(type, "cache") == 0) ))
00938 {
00939 (void) resourceDirs(type);
00940 dirs = relatives.find(type);
00941 }
00942 if (dirs)
00943 {
00944
00945 if (strncmp(type, "xdgdata-", 8) == 0)
00946 pPath = new QString(realPath(localxdgdatadir() + dirs->last()));
00947 else if (strncmp(type, "xdgconf-", 8) == 0)
00948 pPath = new QString(realPath(localxdgconfdir() + dirs->last()));
00949 else
00950 pPath = new QString(realPath(localkdedir() + dirs->last()));
00951 }
00952 else {
00953 dirs = absolutes.find(type);
00954 if (!dirs)
00955 qFatal("KStandardDirs: The resource type %s is not registered", type);
00956 pPath = new QString(realPath(dirs->last()));
00957 }
00958
00959 savelocations.insert(type, pPath);
00960 }
00961 QString fullPath = *pPath + suffix;
00962
00963 struct stat st;
00964 if (stat(QFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
00965 if(!create) {
00966 #ifndef NDEBUG
00967 qDebug("save location %s doesn't exist", fullPath.latin1());
00968 #endif
00969 return fullPath;
00970 }
00971 if(!makeDir(fullPath, 0700)) {
00972 qWarning("failed to create %s", fullPath.latin1());
00973 return fullPath;
00974 }
00975 dircache.remove(type);
00976 }
00977 return fullPath;
00978 }
00979
00980 QString KStandardDirs::relativeLocation(const char *type, const QString &absPath)
00981 {
00982 QString fullPath = absPath;
00983 int i = absPath.findRev('/');
00984 if (i != -1)
00985 {
00986 fullPath = realPath(absPath.left(i+1))+absPath.mid(i+1);
00987 }
00988
00989 QStringList candidates = resourceDirs(type);
00990
00991 for (QStringList::ConstIterator it = candidates.begin();
00992 it != candidates.end(); it++)
00993 if (fullPath.startsWith(*it))
00994 {
00995 return fullPath.mid((*it).length());
00996 }
00997
00998 return absPath;
00999 }
01000
01001
01002 bool KStandardDirs::makeDir(const QString& dir, int mode)
01003 {
01004
01005 if (dir.at(0) != '/')
01006 return false;
01007
01008 QString target = dir;
01009 uint len = target.length();
01010
01011
01012 if (dir.at(len - 1) != '/')
01013 target += '/';
01014
01015 QString base("");
01016 uint i = 1;
01017
01018 while( i < len )
01019 {
01020 struct stat st;
01021 int pos = target.find('/', i);
01022 base += target.mid(i - 1, pos - i + 1);
01023 QCString baseEncoded = QFile::encodeName(base);
01024
01025 if (stat(baseEncoded, &st) != 0)
01026 {
01027
01028
01029 if (lstat(baseEncoded, &st) == 0)
01030 (void)unlink(baseEncoded);
01031
01032 if ( mkdir(baseEncoded, (mode_t) mode) != 0) {
01033 perror("trying to create local folder");
01034 return false;
01035 }
01036 }
01037 i = pos + 1;
01038 }
01039 return true;
01040 }
01041
01042 static QString readEnvPath(const char *env)
01043 {
01044 QCString c_path = getenv(env);
01045 if (c_path.isEmpty())
01046 return QString::null;
01047 return QFile::decodeName(c_path);
01048 }
01049
01050 void KStandardDirs::addKDEDefaults()
01051 {
01052 QStringList kdedirList;
01053
01054
01055 QString kdedirs = readEnvPath("KDEDIRS");
01056 if (!kdedirs.isEmpty())
01057 {
01058 tokenize(kdedirList, kdedirs, ":");
01059 }
01060 else
01061 {
01062 QString kdedir = readEnvPath("KDEDIR");
01063 if (!kdedir.isEmpty())
01064 {
01065 kdedir = KShell::tildeExpand(kdedir);
01066 kdedirList.append(kdedir);
01067 }
01068 }
01069 kdedirList.append(KDEDIR);
01070
01071 #ifdef __KDE_EXECPREFIX
01072 QString execPrefix(__KDE_EXECPREFIX);
01073 if (execPrefix!="NONE")
01074 kdedirList.append(execPrefix);
01075 #endif
01076
01077
01078
01079 QString localKdeDir = readEnvPath(getuid() ? "KDEHOME" : "KDEROOTHOME");
01080 if (!localKdeDir.isEmpty())
01081 {
01082 if (localKdeDir[localKdeDir.length()-1] != '/')
01083 localKdeDir += '/';
01084 }
01085 else
01086 {
01087 localKdeDir = QDir::homeDirPath() + "/.kde/";
01088 }
01089
01090 if (localKdeDir != "-/")
01091 {
01092 localKdeDir = KShell::tildeExpand(localKdeDir);
01093 addPrefix(localKdeDir);
01094 }
01095
01096 for (QStringList::ConstIterator it = kdedirList.begin();
01097 it != kdedirList.end(); it++)
01098 {
01099 QString dir = KShell::tildeExpand(*it);
01100 addPrefix(dir);
01101 }
01102
01103
01104
01105 QStringList xdgdirList;
01106 QString xdgdirs = readEnvPath("XDG_CONFIG_DIRS");
01107 if (!xdgdirs.isEmpty())
01108 {
01109 tokenize(xdgdirList, xdgdirs, ":");
01110 }
01111 else
01112 {
01113 xdgdirList.clear();
01114 xdgdirList.append("/etc/xdg");
01115 xdgdirList.append(KDESYSCONFDIR "/xdg");
01116 }
01117
01118 QString localXdgDir = readEnvPath("XDG_CONFIG_HOME");
01119 if (!localXdgDir.isEmpty())
01120 {
01121 if (localXdgDir[localXdgDir.length()-1] != '/')
01122 localXdgDir += '/';
01123 }
01124 else
01125 {
01126 localXdgDir = QDir::homeDirPath() + "/.config/";
01127 }
01128
01129 localXdgDir = KShell::tildeExpand(localXdgDir);
01130 addXdgConfigPrefix(localXdgDir);
01131
01132 for (QStringList::ConstIterator it = xdgdirList.begin();
01133 it != xdgdirList.end(); it++)
01134 {
01135 QString dir = KShell::tildeExpand(*it);
01136 addXdgConfigPrefix(dir);
01137 }
01138
01139
01140
01141 xdgdirs = readEnvPath("XDG_DATA_DIRS");
01142 if (!xdgdirs.isEmpty())
01143 {
01144 tokenize(xdgdirList, xdgdirs, ":");
01145 }
01146 else
01147 {
01148 xdgdirList.clear();
01149 for (QStringList::ConstIterator it = kdedirList.begin();
01150 it != kdedirList.end(); it++)
01151 {
01152 QString dir = *it;
01153 if (dir[dir.length()-1] != '/')
01154 dir += '/';
01155 xdgdirList.append(dir+"share/");
01156 }
01157
01158 xdgdirList.append("/usr/local/share/");
01159 xdgdirList.append("/usr/share/");
01160 }
01161
01162 localXdgDir = readEnvPath("XDG_DATA_HOME");
01163 if (!localXdgDir.isEmpty())
01164 {
01165 if (localXdgDir[localXdgDir.length()-1] != '/')
01166 localXdgDir += '/';
01167 }
01168 else
01169 {
01170 localXdgDir = QDir::homeDirPath() + "/.local/share/";
01171 }
01172
01173 localXdgDir = KShell::tildeExpand(localXdgDir);
01174 addXdgDataPrefix(localXdgDir);
01175
01176 for (QStringList::ConstIterator it = xdgdirList.begin();
01177 it != xdgdirList.end(); it++)
01178 {
01179 QString dir = KShell::tildeExpand(*it);
01180 addXdgDataPrefix(dir);
01181 }
01182
01183
01184
01185 uint index = 0;
01186 while (types[index] != 0) {
01187 addResourceType(types[index], kde_default(types[index]));
01188 index++;
01189 }
01190
01191 addResourceDir("home", QDir::homeDirPath());
01192 }
01193
01194 void KStandardDirs::checkConfig() const
01195 {
01196 if (!addedCustoms && KGlobal::_instance && KGlobal::_instance->_config)
01197 const_cast<KStandardDirs*>(this)->addCustomized(KGlobal::_instance->_config);
01198 }
01199
01200 bool KStandardDirs::addCustomized(KConfig *config)
01201 {
01202 if (addedCustoms)
01203 return false;
01204
01205
01206
01207 uint configdirs = resourceDirs("config").count();
01208
01209
01210 QString oldGroup = config->group();
01211 config->setGroup("Directories");
01212
01213 QStringList list;
01214 QStringList::ConstIterator it;
01215 list = config->readListEntry("prefixes");
01216 for (it = list.begin(); it != list.end(); it++)
01217 addPrefix(*it);
01218
01219
01220
01221 QMap<QString, QString> entries = config->entryMap("Directories");
01222
01223 QMap<QString, QString>::ConstIterator it2;
01224 for (it2 = entries.begin(); it2 != entries.end(); it2++)
01225 {
01226 QString key = it2.key();
01227 if (key.left(4) == "dir_") {
01228
01229 QStringList dirs = QStringList::split(',',
01230 *it2);
01231 QStringList::Iterator sIt(dirs.begin());
01232 QString resType = key.mid(4, key.length());
01233 for (; sIt != dirs.end(); ++sIt) {
01234 addResourceDir(resType.latin1(), *sIt);
01235 }
01236 }
01237 }
01238
01239
01240 config->setGroup("KDE Resource Restrictions");
01241 entries = config->entryMap("KDE Resource Restrictions");
01242 for (it2 = entries.begin(); it2 != entries.end(); it2++)
01243 {
01244 QString key = it2.key();
01245 if (!config->readBoolEntry(key, true))
01246 {
01247 d->restrictionsActive = true;
01248 d->restrictions.insert(key.latin1(), &d->restrictionsActive);
01249 dircache.remove(key.latin1());
01250 }
01251 }
01252
01253
01254 addedCustoms = true;
01255 config->setGroup(oldGroup);
01256
01257
01258 return (resourceDirs("config").count() != configdirs);
01259 }
01260
01261 QString KStandardDirs::localkdedir() const
01262 {
01263
01264 return prefixes.first();
01265 }
01266
01267 QString KStandardDirs::localxdgdatadir() const
01268 {
01269
01270 return d->xdgdata_prefixes.first();
01271 }
01272
01273 QString KStandardDirs::localxdgconfdir() const
01274 {
01275
01276 return d->xdgconf_prefixes.first();
01277 }
01278
01279
01280 QString locate( const char *type,
01281 const QString& filename, const KInstance* inst )
01282 {
01283 return inst->dirs()->findResource(type, filename);
01284 }
01285
01286 QString locateLocal( const char *type,
01287 const QString& filename, const KInstance* inst )
01288 {
01289 return locateLocal(type, filename, true, inst);
01290 }
01291
01292 QString locateLocal( const char *type,
01293 const QString& filename, bool createDir, const KInstance* inst )
01294 {
01295
01296
01297 int slash = filename.findRev('/')+1;
01298 if (!slash)
01299 return inst->dirs()->saveLocation(type, QString::null, createDir) + filename;
01300
01301
01302 QString dir = filename.left(slash);
01303 QString file = filename.mid(slash);
01304 return inst->dirs()->saveLocation(type, dir, createDir) + file;
01305 }