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