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