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