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