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