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 void KStandardDirs::createSpecialResource(const char *type)
00545 {
00546 char hostname[256];
00547 hostname[0] = 0;
00548 gethostname(hostname, 255);
00549 QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
00550 char link[1024];
00551 link[1023] = 0;
00552 int result = readlink(QFile::encodeName(dir).data(), link, 1023);
00553 bool relink = (result == -1) && (errno == ENOENT);
00554 if ((result > 0) && (link[0] == '/'))
00555 {
00556 link[result] = 0;
00557 struct stat stat_buf;
00558 int res = lstat(link, &stat_buf);
00559 if ((res == -1) && (errno == ENOENT))
00560 {
00561 relink = true;
00562 }
00563 else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
00564 {
00565 fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
00566 relink = true;
00567 }
00568 else if (stat_buf.st_uid != getuid())
00569 {
00570 fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
00571 relink = true;
00572 }
00573 }
00574 if (relink)
00575 {
00576 QString srv = findExe(QString::fromLatin1("lnusertemp"), KDEDIR+QString::fromLatin1("/bin"));
00577 if (srv.isEmpty())
00578 srv = findExe(QString::fromLatin1("lnusertemp"));
00579 if (!srv.isEmpty())
00580 {
00581 system(QFile::encodeName(srv)+" "+type);
00582 result = readlink(QFile::encodeName(dir).data(), link, 1023);
00583 }
00584 }
00585 if (result > 0)
00586 {
00587 link[result] = 0;
00588 if (link[0] == '/')
00589 dir = QFile::decodeName(link);
00590 else
00591 dir = QDir::cleanDirPath(dir+QFile::decodeName(link));
00592 }
00593 addResourceDir(type, dir+'/');
00594 }
00595
00596 QStringList KStandardDirs::resourceDirs(const char *type) const
00597 {
00598 QStringList *candidates = dircache.find(type);
00599
00600 if (!candidates) {
00601 if (strcmp(type, "socket") == 0)
00602 const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00603 else if (strcmp(type, "tmp") == 0)
00604 const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00605
00606 QDir testdir;
00607
00608 candidates = new QStringList();
00609 QStringList *dirs;
00610
00611 bool restrictionActive = false;
00612 if (d && d->restrictionsActive)
00613 {
00614 if (d->dataRestrictionActive)
00615 restrictionActive = true;
00616 else if (d->restrictions["all"])
00617 restrictionActive = true;
00618 else if (d->restrictions[type])
00619 restrictionActive = true;
00620 d->dataRestrictionActive = false;
00621 }
00622
00623 dirs = relatives.find(type);
00624 if (dirs)
00625 {
00626 bool local = true;
00627 for (QStringList::ConstIterator pit = prefixes.begin();
00628 pit != prefixes.end();
00629 pit++)
00630 {
00631 for (QStringList::ConstIterator it = dirs->begin();
00632 it != dirs->end(); ++it) {
00633 QString path = realPath(*pit + *it);
00634 testdir.setPath(path);
00635 if (local && restrictionActive)
00636 continue;
00637 if ((local || testdir.exists()) && !candidates->contains(path))
00638 candidates->append(path);
00639 }
00640 local = false;
00641 }
00642 }
00643 dirs = absolutes.find(type);
00644 if (dirs)
00645 for (QStringList::ConstIterator it = dirs->begin();
00646 it != dirs->end(); ++it)
00647 {
00648 testdir.setPath(*it);
00649 if (testdir.exists())
00650 {
00651 QString filename = realPath(*it);
00652 if (!candidates->contains(filename))
00653 candidates->append(filename);
00654 }
00655 }
00656 dircache.insert(type, candidates);
00657 }
00658
00659 #if 0
00660 kdDebug() << "found dirs for resource " << type << ":" << endl;
00661 for (QStringList::ConstIterator pit = candidates->begin();
00662 pit != candidates->end();
00663 pit++)
00664 {
00665 fprintf(stderr, "%s\n", (*pit).latin1());
00666 }
00667 #endif
00668
00669
00670 return *candidates;
00671 }
00672
00673 QString KStandardDirs::findExe( const QString& appname,
00674 const QString& pstr, bool ignore)
00675 {
00676 QFileInfo info;
00677
00678
00679 if (appname.startsWith(QString::fromLatin1("/")))
00680 {
00681 info.setFile( appname );
00682 if( info.exists() && ( ignore || info.isExecutable() )
00683 && info.isFile() ) {
00684 return appname;
00685 }
00686 return QString::null;
00687 }
00688
00689 QString p = QString("%1/%2").arg(__KDE_BINDIR).arg(appname);
00690 info.setFile( p );
00691 if( info.exists() && ( ignore || info.isExecutable() )
00692 && ( info.isFile() || info.isSymLink() ) ) {
00693 return p;
00694 }
00695
00696 QStringList tokens;
00697 p = pstr;
00698
00699 if( p == QString::null ) {
00700 p = getenv( "PATH" );
00701 }
00702
00703 tokenize( tokens, p, ":\b" );
00704
00705
00706 for( unsigned i = 0; i < tokens.count(); i++ ) {
00707 p = tokens[ i ];
00708
00709 if ( p[ 0 ] == '~' )
00710 {
00711 int len = p.find( '/' );
00712 if ( len == -1 )
00713 len = p.length();
00714 if ( len == 1 )
00715 p.replace( 0, 1, QDir::homeDirPath() );
00716 else
00717 {
00718 QString user = p.mid( 1, len - 1 );
00719 struct passwd *dir = getpwnam( user.local8Bit().data() );
00720 if ( dir && strlen( dir->pw_dir ) )
00721 p.replace( 0, len, QString::fromLocal8Bit( dir->pw_dir ) );
00722 }
00723 }
00724
00725 p += "/";
00726 p += appname;
00727
00728
00729 info.setFile( p );
00730
00731 if( info.exists() && ( ignore || info.isExecutable() )
00732 && ( info.isFile() || info.isSymLink() ) ) {
00733 return p;
00734 }
00735 }
00736
00737
00738
00739
00740 return QString::null;
00741 }
00742
00743 int KStandardDirs::findAllExe( QStringList& list, const QString& appname,
00744 const QString& pstr, bool ignore )
00745 {
00746 QString p = pstr;
00747 QFileInfo info;
00748 QStringList tokens;
00749
00750 if( p == QString::null ) {
00751 p = getenv( "PATH" );
00752 }
00753
00754 list.clear();
00755 tokenize( tokens, p, ":\b" );
00756
00757 for ( unsigned i = 0; i < tokens.count(); i++ ) {
00758 p = tokens[ i ];
00759 p += "/";
00760 p += appname;
00761
00762 info.setFile( p );
00763
00764 if( info.exists() && (ignore || info.isExecutable())
00765 && info.isFile() ) {
00766 list.append( p );
00767 }
00768
00769 }
00770
00771 return list.count();
00772 }
00773
00774 static int tokenize( QStringList& tokens, const QString& str,
00775 const QString& delim )
00776 {
00777 int len = str.length();
00778 QString token = "";
00779
00780 for( int index = 0; index < len; index++)
00781 {
00782 if ( delim.find( str[ index ] ) >= 0 )
00783 {
00784 tokens.append( token );
00785 token = "";
00786 }
00787 else
00788 {
00789 token += str[ index ];
00790 }
00791 }
00792 if ( token.length() > 0 )
00793 {
00794 tokens.append( token );
00795 }
00796
00797 return tokens.count();
00798 }
00799
00800 QString KStandardDirs::kde_default(const char *type) {
00801 if (!strcmp(type, "data"))
00802 return "share/apps/";
00803 if (!strcmp(type, "html"))
00804 return "share/doc/HTML/";
00805 if (!strcmp(type, "icon"))
00806 return "share/icons/";
00807 if (!strcmp(type, "config"))
00808 return "share/config/";
00809 if (!strcmp(type, "pixmap"))
00810 return "share/pixmaps/";
00811 if (!strcmp(type, "apps"))
00812 return "share/applnk-redhat/";
00813 if (!strcmp(type, "sound"))
00814 return "share/sounds/";
00815 if (!strcmp(type, "locale"))
00816 return "share/locale/";
00817 if (!strcmp(type, "services"))
00818 return "share/services/";
00819 if (!strcmp(type, "servicetypes"))
00820 return "share/servicetypes/";
00821 if (!strcmp(type, "mime"))
00822 return "share/mimelnk/";
00823 if (!strcmp(type, "cgi"))
00824 return "cgi-bin/";
00825 if (!strcmp(type, "wallpaper"))
00826 return "share/wallpapers/";
00827 if (!strcmp(type, "templates"))
00828 return "share/templates/";
00829 if (!strcmp(type, "exe"))
00830 return "bin/";
00831 if (!strcmp(type, "lib"))
00832 return LIBDIR_NAME "/";
00833 if (!strcmp(type, "module"))
00834 return LIBDIR_NAME "/kde3/";
00835 if (!strcmp(type, "qtplugins"))
00836 return LIBDIR_NAME "/kde3/plugins";
00837 qFatal("unknown resource type %s", type);
00838 return QString::null;
00839 }
00840
00841 QString KStandardDirs::saveLocation(const char *type,
00842 const QString& suffix,
00843 bool create) const
00844 {
00845 checkConfig();
00846
00847 QString *pPath = savelocations.find(type);
00848 if (!pPath)
00849 {
00850 QStringList *dirs = relatives.find(type);
00851 if (!dirs && ((strcmp(type, "socket") == 0) || (strcmp(type, "tmp") == 0)))
00852 {
00853 (void) resourceDirs(type);
00854 dirs = relatives.find(type);
00855 }
00856 if (dirs)
00857 {
00858
00859 pPath = new QString(realPath(localkdedir() + dirs->last()));
00860 }
00861 else {
00862 dirs = absolutes.find(type);
00863 if (!dirs)
00864 qFatal("KStandardDirs: The resource type %s is not registered", type);
00865 pPath = new QString(realPath(dirs->last()));
00866 }
00867
00868 savelocations.insert(type, pPath);
00869 }
00870 QString fullPath = *pPath + suffix;
00871
00872 struct stat st;
00873 if (stat(QFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
00874 if(!create) {
00875 #ifndef NDEBUG
00876 qDebug("save location %s doesn't exist", fullPath.latin1());
00877 #endif
00878 return localkdedir()+suffix;
00879 }
00880 if(!makeDir(fullPath, 0700)) {
00881 qWarning("failed to create %s", fullPath.latin1());
00882 return localkdedir()+suffix;
00883 }
00884 dircache.remove(type);
00885 }
00886 return fullPath;
00887 }
00888
00889 QString KStandardDirs::relativeLocation(const char *type, const QString &absPath)
00890 {
00891 QString fullPath = absPath;
00892 int i = absPath.findRev('/');
00893 if (i != -1)
00894 {
00895 fullPath = realPath(absPath.left(i+1))+absPath.mid(i+1);
00896 }
00897
00898 QStringList candidates = resourceDirs(type);
00899
00900 for (QStringList::ConstIterator it = candidates.begin();
00901 it != candidates.end(); it++)
00902 if (fullPath.startsWith(*it))
00903 {
00904 return fullPath.mid((*it).length());
00905 }
00906
00907 return absPath;
00908 }
00909
00910
00911 bool KStandardDirs::makeDir(const QString& dir, int mode)
00912 {
00913
00914 if (dir.at(0) != '/')
00915 return false;
00916
00917 QString target = dir;
00918 uint len = target.length();
00919
00920
00921 if (dir.at(len - 1) != '/')
00922 target += '/';
00923
00924 QString base("");
00925 uint i = 1;
00926
00927 while( i < len )
00928 {
00929 struct stat st;
00930 int pos = target.find('/', i);
00931 base += target.mid(i - 1, pos - i + 1);
00932 QCString baseEncoded = QFile::encodeName(base);
00933
00934 if (stat(baseEncoded, &st) != 0)
00935 {
00936
00937
00938 if (lstat(baseEncoded, &st) == 0)
00939 (void)unlink(baseEncoded);
00940
00941 if ( mkdir(baseEncoded, (mode_t) mode) != 0) {
00942 perror("trying to create local folder");
00943 return false;
00944 }
00945 }
00946 i = pos + 1;
00947 }
00948 return true;
00949 }
00950
00951 static QString readEnvPath(const char *env)
00952 {
00953 QCString c_path = getenv(env);
00954 if (c_path.isEmpty())
00955 return QString::null;
00956 return QFile::decodeName(c_path);
00957 }
00958
00959 static void fixHomeDir(QString &dir)
00960 {
00961 if (dir[0] == '~')
00962 {
00963 dir = QDir::homeDirPath() + dir.mid(1);
00964 }
00965 }
00966
00967 void KStandardDirs::addKDEDefaults()
00968 {
00969 QStringList kdedirList;
00970
00971 QString kdedirs = readEnvPath("KDEDIRS");
00972 if (!kdedirs.isEmpty())
00973 {
00974 tokenize(kdedirList, kdedirs, ":");
00975 }
00976 else
00977 {
00978 QString kdedir = readEnvPath("KDEDIR");
00979 if (!kdedir.isEmpty())
00980 {
00981 fixHomeDir(kdedir);
00982 kdedirList.append(kdedir);
00983 }
00984 }
00985 kdedirList.append(KDEDIR);
00986
00987 #ifdef __KDE_EXECPREFIX
00988 QString execPrefix(__KDE_EXECPREFIX);
00989 if (execPrefix!="NONE")
00990 kdedirList.append(execPrefix);
00991 #endif
00992
00993 QString localKdeDir;
00994 if (getuid())
00995 {
00996 localKdeDir = readEnvPath("KDEHOME");
00997 if (!localKdeDir.isEmpty())
00998 {
00999 if (localKdeDir[localKdeDir.length()-1] != '/')
01000 localKdeDir += '/';
01001 }
01002 else
01003 {
01004 localKdeDir = QDir::homeDirPath() + "/.kde/";
01005 }
01006 }
01007 else
01008 {
01009
01010
01011 localKdeDir = readEnvPath("KDEROOTHOME");
01012 if (!localKdeDir.isEmpty())
01013 {
01014 if (localKdeDir[localKdeDir.length()-1] != '/')
01015 localKdeDir += '/';
01016 }
01017 else
01018 {
01019 struct passwd *pw = getpwuid(0);
01020 localKdeDir = QFile::decodeName((pw && pw->pw_dir) ? pw->pw_dir : "/root") + "/.kde/";
01021 }
01022
01023 }
01024
01025 fixHomeDir(localKdeDir);
01026 addPrefix(localKdeDir);
01027
01028 for (QStringList::ConstIterator it = kdedirList.begin();
01029 it != kdedirList.end(); it++)
01030 {
01031 QString dir = *it;
01032 fixHomeDir(dir);
01033 addPrefix(dir);
01034 }
01035
01036 uint index = 0;
01037 while (types[index] != 0) {
01038 addResourceType(types[index], kde_default(types[index]));
01039 index++;
01040 }
01041
01042 QString dir = QString("%1share/cache/").arg(localKdeDir);
01043 addResourceDir("cache", dir);
01044
01045 addResourceDir("home", QDir::homeDirPath());
01046 }
01047
01048 void KStandardDirs::checkConfig() const
01049 {
01050 if (!addedCustoms && KGlobal::_instance && KGlobal::_instance->_config)
01051 const_cast<KStandardDirs*>(this)->addCustomized(KGlobal::_instance->_config);
01052 }
01053
01054 bool KStandardDirs::addCustomized(KConfig *config)
01055 {
01056 if (addedCustoms)
01057 return false;
01058
01059
01060
01061 uint configdirs = resourceDirs("config").count();
01062
01063
01064 QString oldGroup = config->group();
01065 config->setGroup("Directories");
01066
01067 QStringList list;
01068 QStringList::ConstIterator it;
01069 list = config->readListEntry("prefixes");
01070 for (it = list.begin(); it != list.end(); it++)
01071 addPrefix(*it);
01072
01073
01074
01075 QMap<QString, QString> entries = config->entryMap("Directories");
01076
01077 QMap<QString, QString>::ConstIterator it2;
01078 for (it2 = entries.begin(); it2 != entries.end(); it2++)
01079 {
01080 QString key = it2.key();
01081 if (key.left(4) == "dir_") {
01082
01083 QStringList dirs = QStringList::split(',',
01084 *it2);
01085 QStringList::Iterator sIt(dirs.begin());
01086 QString resType = key.mid(4, key.length());
01087 for (; sIt != dirs.end(); ++sIt) {
01088 addResourceDir(resType.latin1(), *sIt);
01089 }
01090 }
01091 }
01092
01093
01094 config->setGroup("KDE Resource Restrictions");
01095 entries = config->entryMap("KDE Resource Restrictions");
01096 for (it2 = entries.begin(); it2 != entries.end(); it2++)
01097 {
01098 QString key = it2.key();
01099 if (!config->readBoolEntry(key, true))
01100 {
01101 if (!d)
01102 {
01103 d = new KStandardDirsPrivate;
01104 d->restrictionsActive = true;
01105 }
01106 d->restrictions.insert(key.latin1(), &d->restrictionsActive);
01107 dircache.remove(key.latin1());
01108 }
01109 }
01110
01111
01112 addedCustoms = true;
01113 config->setGroup(oldGroup);
01114
01115
01116 return (resourceDirs("config").count() != configdirs);
01117 }
01118
01119 QString KStandardDirs::localkdedir() const
01120 {
01121
01122 return prefixes.first();
01123 }
01124
01125
01126 QString locate( const char *type,
01127 const QString& filename, const KInstance* inst )
01128 {
01129 return inst->dirs()->findResource(type, filename);
01130 }
01131
01132 QString locateLocal( const char *type,
01133 const QString& filename, const KInstance* inst )
01134 {
01135
01136
01137 int slash = filename.findRev('/')+1;
01138 if (!slash)
01139 return inst->dirs()->saveLocation(type) + filename;
01140
01141
01142 QString dir = filename.left(slash);
01143 QString file = filename.mid(slash);
01144 return inst->dirs()->saveLocation(type, dir) + file;
01145 }