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