kdecore Library API Documentation

kstandarddirs.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1999 Sirtaj Singh Kang <taj@kde.org>
00003    Copyright (C) 1999 Stephan Kulow <coolo@kde.org>
00004    Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 /*
00022  * Author: Stephan Kulow <coolo@kde.org> and Sirtaj Singh Kang <taj@kde.org>
00023  * Version: $Id: kstandarddirs.cpp,v 1.151.2.3 2003/06/16 09:31:43 waba Exp $
00024  * Generated:   Thu Mar  5 16:05:28 EST 1998
00025  */
00026 
00027 #include "config.h"
00028 
00029 #include <stdlib.h>
00030 #include <assert.h>
00031 #include <errno.h>
00032 #ifdef HAVE_SYS_STAT_H
00033 #include <sys/stat.h>
00034 #endif
00035 #include <sys/types.h>
00036 #include <dirent.h>
00037 #include <pwd.h>
00038 
00039 #include <qregexp.h>
00040 #include <qasciidict.h>
00041 #include <qdict.h>
00042 #include <qdir.h>
00043 #include <qfileinfo.h>
00044 #include <qstring.h>
00045 #include <qstringlist.h>
00046 
00047 #include "kstandarddirs.h"
00048 #include "kconfig.h"
00049 #include "kdebug.h"
00050 #include "kinstance.h"
00051 #include <sys/param.h>
00052 #include <unistd.h>
00053 
00054 template class QDict<QStringList>;
00055 
00056 class KStandardDirs::KStandardDirsPrivate
00057 {
00058 public:
00059    KStandardDirsPrivate()
00060     : restrictionsActive(false),
00061       dataRestrictionActive(false)
00062    { }
00063 
00064    bool restrictionsActive;
00065    bool dataRestrictionActive;
00066    QAsciiDict<bool> restrictions;
00067 };
00068 
00069 static const char* const types[] = {"html", "icon", "apps", "sound",
00070                   "data", "locale", "services", "mime",
00071                   "servicetypes", "config", "exe",
00072                   "wallpaper", "lib", "pixmap", "templates", "module", "qtplugins", 0 };
00073 
00074 #if defined(__x86_64__) || defined(__s390x__) || defined(__powerpc64__) || defined(__sparc64__)
00075 #  define LIBDIR_NAME "lib64"
00076 #else
00077 #  define LIBDIR_NAME "lib"
00078 #endif
00079 
00080 static int tokenize( QStringList& token, const QString& str,
00081         const QString& delim );
00082 
00083 KStandardDirs::KStandardDirs( ) : addedCustoms(false), d(0)
00084 {
00085     dircache.setAutoDelete(true);
00086     relatives.setAutoDelete(true);
00087     absolutes.setAutoDelete(true);
00088     savelocations.setAutoDelete(true);
00089     addKDEDefaults();
00090 }
00091 
00092 KStandardDirs::~KStandardDirs()
00093 {
00094     delete d;
00095     d = 0L;
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 QString KStandardDirs::kfsstnd_prefixes()
00156 {
00157    return prefixes.join(":");
00158 }
00159 
00160 bool KStandardDirs::addResourceType( const char *type,
00161                      const QString& relativename )
00162 {
00163     if (relativename.isNull())
00164        return false;
00165 
00166     QStringList *rels = relatives.find(type);
00167     if (!rels) {
00168     rels = new QStringList();
00169     relatives.insert(type, rels);
00170     }
00171     QString copy = relativename;
00172     if (copy.at(copy.length() - 1) != '/')
00173     copy += '/';
00174     if (!rels->contains(copy)) {
00175     rels->prepend(copy);
00176     dircache.remove(type); // clean the cache
00177     return true;
00178     }
00179     return false;
00180 }
00181 
00182 bool KStandardDirs::addResourceDir( const char *type,
00183                     const QString& absdir)
00184 {
00185     QStringList *paths = absolutes.find(type);
00186     if (!paths) {
00187     paths = new QStringList();
00188     absolutes.insert(type, paths);
00189     }
00190     QString copy = absdir;
00191     if (copy.at(copy.length() - 1) != '/')
00192       copy += '/';
00193 
00194     if (!paths->contains(copy)) {
00195     paths->append(copy);
00196     dircache.remove(type); // clean the cache
00197     return true;
00198     }
00199     return false;
00200 }
00201 
00202 QString KStandardDirs::findResource( const char *type,
00203                      const QString& filename ) const
00204 {
00205     if (filename.at(0) == '/')
00206     return filename; // absolute dirs are absolute dirs, right? :-/
00207 
00208 #if 0
00209 kdDebug() << "Find resource: " << type << endl;
00210 for (QStringList::ConstIterator pit = prefixes.begin();
00211      pit != prefixes.end();
00212      pit++)
00213 {
00214   kdDebug() << "Prefix: " << *pit << endl;
00215 }
00216 #endif
00217 
00218     QString dir = findResourceDir(type, filename);
00219     if (dir.isNull())
00220     return dir;
00221     else return dir + filename;
00222 }
00223 
00224 static Q_UINT32 updateHash(const QString &file, Q_UINT32 hash)
00225 {
00226     QCString cFile = QFile::encodeName(file);
00227     struct stat buff;
00228     if ((access(cFile, R_OK) == 0) &&
00229         (stat( cFile, &buff ) == 0) &&
00230         (S_ISREG( buff.st_mode )))
00231     {
00232        hash = hash + (Q_UINT32) buff.st_ctime;
00233     }
00234     return hash;
00235 }
00236 
00237 Q_UINT32 KStandardDirs::calcResourceHash( const char *type,
00238                   const QString& filename, bool deep) const
00239 {
00240     Q_UINT32 hash = 0;
00241 
00242     if (filename.at(0) == '/')
00243     {
00244         // absolute dirs are absolute dirs, right? :-/
00245     return updateHash(filename, hash);
00246     }
00247     if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00248        applyDataRestrictions(filename);
00249     QStringList candidates = resourceDirs(type);
00250     QString fullPath;
00251 
00252     for (QStringList::ConstIterator it = candidates.begin();
00253      it != candidates.end(); it++)
00254     {
00255         hash = updateHash(*it + filename, hash);
00256         if (!deep && hash)
00257            return hash;
00258     }
00259     return hash;
00260 }
00261 
00262 
00263 QStringList KStandardDirs::findDirs( const char *type,
00264                                      const QString& reldir ) const
00265 {
00266     QStringList list;
00267 
00268     checkConfig();
00269 
00270     if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00271        applyDataRestrictions(reldir);
00272     QStringList candidates = resourceDirs(type);
00273     QDir testdir;
00274 
00275     for (QStringList::ConstIterator it = candidates.begin();
00276          it != candidates.end(); it++) {
00277         testdir.setPath(*it + reldir);
00278         if (testdir.exists())
00279             list.append(testdir.absPath() + '/');
00280     }
00281 
00282     return list;
00283 }
00284 
00285 QString KStandardDirs::findResourceDir( const char *type,
00286                     const QString& filename) const
00287 {
00288 #ifndef NDEBUG
00289     if (filename.isEmpty()) {
00290       kdWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!" << endl;
00291       return QString::null;
00292     }
00293 #endif
00294 
00295     if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00296        applyDataRestrictions(filename);
00297     QStringList candidates = resourceDirs(type);
00298     QString fullPath;
00299 
00300     for (QStringList::ConstIterator it = candidates.begin();
00301      it != candidates.end(); it++)
00302       if (exists(*it + filename))
00303     return *it;
00304 
00305 #ifndef NDEBUG
00306     if(false && type != "locale")
00307       kdDebug() << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\"." << endl;
00308 #endif
00309 
00310     return QString::null;
00311 }
00312 
00313 bool KStandardDirs::exists(const QString &fullPath)
00314 {
00315     struct stat buff;
00316     if (access(QFile::encodeName(fullPath), R_OK) == 0 && stat( QFile::encodeName(fullPath), &buff ) == 0)
00317     if (fullPath.at(fullPath.length() - 1) != '/') {
00318         if (S_ISREG( buff.st_mode ))
00319         return true;
00320     } else
00321         if (S_ISDIR( buff.st_mode ))
00322         return true;
00323     return false;
00324 }
00325 
00326 static void lookupDirectory(const QString& path, const QString &relPart,
00327                 const QRegExp &regexp,
00328                 QStringList& list,
00329                 QStringList& relList,
00330                 bool recursive, bool uniq)
00331 {
00332   QString pattern = regexp.pattern();
00333   if (recursive || pattern.contains('?') || pattern.contains('*'))
00334   {
00335     // We look for a set of files.
00336     DIR *dp = opendir( QFile::encodeName(path));
00337     if (!dp)
00338       return;
00339 
00340     assert(path.at(path.length() - 1) == '/');
00341 
00342     struct dirent *ep;
00343     struct stat buff;
00344 
00345     QString _dot(".");
00346     QString _dotdot("..");
00347 
00348     while( ( ep = readdir( dp ) ) != 0L )
00349     {
00350       QString fn( QFile::decodeName(ep->d_name));
00351       if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1).latin1() == '~')
00352     continue;
00353 
00354       if (!recursive && !regexp.exactMatch(fn))
00355     continue; // No match
00356 
00357       QString pathfn = path + fn;
00358       if ( stat( QFile::encodeName(pathfn), &buff ) != 0 ) {
00359     kdDebug() << "Error stat'ing " << pathfn << " : " << perror << endl;
00360     continue; // Couldn't stat (e.g. no read permissions)
00361       }
00362       if ( recursive ) {
00363     if ( S_ISDIR( buff.st_mode )) {
00364       lookupDirectory(pathfn + '/', relPart + fn + '/', regexp, list, relList, recursive, uniq);
00365     }
00366         if (!regexp.exactMatch(fn))
00367       continue; // No match
00368       }
00369       if ( S_ISREG( buff.st_mode))
00370       {
00371         if (!uniq || !relList.contains(relPart + fn))
00372         {
00373         list.append( pathfn );
00374         relList.append( relPart + fn );
00375         }
00376       }
00377     }
00378     closedir( dp );
00379   }
00380   else
00381   {
00382      // We look for a single file.
00383      QString fn = pattern;
00384      QString pathfn = path + fn;
00385      struct stat buff;
00386      if ( stat( QFile::encodeName(pathfn), &buff ) != 0 )
00387         return; // File not found
00388      if ( S_ISREG( buff.st_mode))
00389      {
00390        if (!uniq || !relList.contains(relPart + fn))
00391        {
00392          list.append( pathfn );
00393          relList.append( relPart + fn );
00394        }
00395      }
00396   }
00397 }
00398 
00399 static void lookupPrefix(const QString& prefix, const QString& relpath,
00400                          const QString& relPart,
00401              const QRegExp &regexp,
00402              QStringList& list,
00403              QStringList& relList,
00404              bool recursive, bool uniq)
00405 {
00406     if (relpath.isNull()) {
00407        lookupDirectory(prefix, relPart, regexp, list,
00408                relList, recursive, uniq);
00409        return;
00410     }
00411     QString path;
00412     QString rest;
00413 
00414     if (relpath.length())
00415     {
00416        int slash = relpath.find('/');
00417        if (slash < 0)
00418        rest = relpath.left(relpath.length() - 1);
00419        else {
00420        path = relpath.left(slash);
00421        rest = relpath.mid(slash + 1);
00422        }
00423     }
00424 
00425     assert(prefix.at(prefix.length() - 1) == '/');
00426 
00427     struct stat buff;
00428 
00429     if (path.contains('*') || path.contains('?')) {
00430 
00431     QRegExp pathExp(path, true, true);
00432     DIR *dp = opendir( QFile::encodeName(prefix) );
00433     if (!dp) {
00434         return;
00435     }
00436 
00437     struct dirent *ep;
00438 
00439         QString _dot(".");
00440         QString _dotdot("..");
00441 
00442     while( ( ep = readdir( dp ) ) != 0L )
00443         {
00444         QString fn( QFile::decodeName(ep->d_name));
00445         if (fn == _dot || fn == _dotdot || fn.at(fn.length() - 1) == '~')
00446             continue;
00447 
00448         if (pathExp.search(fn) == -1)
00449             continue; // No match
00450         QString rfn = relPart+fn;
00451         fn = prefix + fn;
00452         if ( stat( QFile::encodeName(fn), &buff ) != 0 ) {
00453             kdDebug() << "Error statting " << fn << " : " << perror << endl;
00454             continue; // Couldn't stat (e.g. no permissions)
00455         }
00456         if ( S_ISDIR( buff.st_mode ))
00457             lookupPrefix(fn + '/', rest, rfn + '/', regexp, list, relList, recursive, uniq);
00458         }
00459 
00460     closedir( dp );
00461     } else {
00462         // Don't stat, if the dir doesn't exist we will find out
00463         // when we try to open it.
00464         lookupPrefix(prefix + path + '/', rest,
00465                      relPart + path + '/', regexp, list,
00466                      relList, recursive, uniq);
00467     }
00468 }
00469 
00470 QStringList
00471 KStandardDirs::findAllResources( const char *type,
00472                      const QString& filter,
00473                  bool recursive,
00474                      bool uniq,
00475                                  QStringList &relList) const
00476 {
00477     QStringList list;
00478     if (filter.at(0) == '/') // absolute paths we return
00479     {
00480         list.append( filter);
00481     return list;
00482     }
00483 
00484     QString filterPath;
00485     QString filterFile;
00486 
00487     if (filter.length())
00488     {
00489        int slash = filter.findRev('/');
00490        if (slash < 0)
00491        filterFile = filter;
00492        else {
00493        filterPath = filter.left(slash + 1);
00494        filterFile = filter.mid(slash + 1);
00495        }
00496     }
00497 
00498     checkConfig();
00499 
00500     if (d && d->restrictionsActive && (strcmp(type, "data")==0))
00501        applyDataRestrictions(filter);
00502     QStringList candidates = resourceDirs(type);
00503     if (filterFile.isEmpty())
00504     filterFile = "*";
00505 
00506     QRegExp regExp(filterFile, true, true);
00507 
00508     for (QStringList::ConstIterator it = candidates.begin();
00509          it != candidates.end(); it++)
00510     {
00511         lookupPrefix(*it, filterPath, "", regExp, list,
00512                      relList, recursive, uniq);
00513     }
00514 
00515     return list;
00516 }
00517 
00518 QStringList
00519 KStandardDirs::findAllResources( const char *type,
00520                      const QString& filter,
00521                  bool recursive,
00522                      bool uniq) const
00523 {
00524     QStringList relList;
00525     return findAllResources(type, filter, recursive, uniq, relList);
00526 }
00527 
00528 QString 
00529 KStandardDirs::realPath(const QString &dirname)
00530 {
00531     char realpath_buffer[MAXPATHLEN + 1];
00532     memset(realpath_buffer, 0, MAXPATHLEN + 1);
00533 
00534     /* If the path contains symlinks, get the real name */
00535     if (realpath( QFile::encodeName(dirname).data(), realpath_buffer) != 0) {
00536         // succes, use result from realpath
00537         int len = strlen(realpath_buffer);
00538         realpath_buffer[len] = '/';
00539         realpath_buffer[len+1] = 0;
00540         return QFile::decodeName(realpath_buffer);
00541     }
00542 
00543     return dirname;
00544 }
00545 
00546 void KStandardDirs::createSpecialResource(const char *type)
00547 {
00548    char hostname[256];
00549    hostname[0] = 0;
00550    gethostname(hostname, 255);
00551    QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
00552    char link[1024];
00553    link[1023] = 0;
00554    int result = readlink(QFile::encodeName(dir).data(), link, 1023);
00555    bool relink = (result == -1) && (errno == ENOENT);
00556    if ((result > 0) && (link[0] == '/'))
00557    {
00558       link[result] = 0;
00559       struct stat stat_buf;
00560       int res = lstat(link, &stat_buf);
00561       if ((res == -1) && (errno == ENOENT))
00562       {
00563          relink = true;
00564       }
00565       else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
00566       {
00567          fprintf(stderr, "Error: \"%s\" is not a directory.\n", link);
00568          relink = true;
00569       }
00570       else if (stat_buf.st_uid != getuid())
00571       {
00572          fprintf(stderr, "Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
00573          relink = true;
00574       }
00575    }
00576    if (relink)
00577    {
00578       QString srv = findExe(QString::fromLatin1("lnusertemp"), KDEDIR+QString::fromLatin1("/bin"));
00579       if (srv.isEmpty())
00580          srv = findExe(QString::fromLatin1("lnusertemp"));
00581       if (!srv.isEmpty())
00582       {
00583          system(QFile::encodeName(srv)+" "+type);
00584          result = readlink(QFile::encodeName(dir).data(), link, 1023);
00585       }
00586    }
00587    if (result > 0)
00588    {
00589       link[result] = 0;
00590       if (link[0] == '/')
00591          dir = QFile::decodeName(link);
00592       else
00593          dir = QDir::cleanDirPath(dir+QFile::decodeName(link));
00594    }
00595    addResourceDir(type, dir+'/');
00596 }
00597 
00598 QStringList KStandardDirs::resourceDirs(const char *type) const
00599 {
00600     QStringList *candidates = dircache.find(type);
00601 
00602     if (!candidates) { // filling cache
00603         if (strcmp(type, "socket") == 0)
00604            const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00605         else if (strcmp(type, "tmp") == 0)
00606            const_cast<KStandardDirs *>(this)->createSpecialResource(type);
00607 
00608         QDir testdir;
00609 
00610         candidates = new QStringList();
00611         QStringList *dirs;
00612 
00613         bool restrictionActive = false;
00614         if (d && d->restrictionsActive)
00615         {
00616            if (d->dataRestrictionActive)
00617               restrictionActive = true;
00618            else if (d->restrictions["all"])
00619               restrictionActive = true;
00620            else if (d->restrictions[type])
00621               restrictionActive = true;
00622            d->dataRestrictionActive = false; // Reset
00623         }
00624 
00625         dirs = relatives.find(type);
00626         if (dirs)
00627         {
00628             bool local = true;
00629             for (QStringList::ConstIterator pit = prefixes.begin();
00630                  pit != prefixes.end();
00631                  pit++)
00632             {
00633                 for (QStringList::ConstIterator it = dirs->begin();
00634                      it != dirs->end(); ++it) {
00635                     QString path = realPath(*pit + *it);
00636                     testdir.setPath(path);
00637                     if (local && restrictionActive)
00638                        continue;
00639                     if ((local || testdir.exists()) && !candidates->contains(path))
00640                         candidates->append(path);
00641                 }
00642                 local = false;
00643             }
00644         }
00645         dirs = absolutes.find(type);
00646         if (dirs)
00647             for (QStringList::ConstIterator it = dirs->begin();
00648                  it != dirs->end(); ++it)
00649             {
00650                 testdir.setPath(*it);
00651                 if (testdir.exists())
00652                 {
00653                     QString filename = realPath(*it);
00654                     if (!candidates->contains(filename))
00655                         candidates->append(filename);
00656                 }
00657             }
00658         dircache.insert(type, candidates);
00659     }
00660 
00661 #if 0
00662     kdDebug() << "found dirs for resource " << type << ":" << endl;
00663     for (QStringList::ConstIterator pit = candidates->begin();
00664      pit != candidates->end();
00665      pit++)
00666     {
00667     fprintf(stderr, "%s\n", (*pit).latin1());
00668     }
00669 #endif
00670 
00671 
00672   return *candidates;
00673 }
00674 
00675 QString KStandardDirs::findExe( const QString& appname,
00676                 const QString& pstr, bool ignore)
00677 {
00678     QFileInfo info;
00679 
00680     // absolute path ?
00681     if (appname.startsWith(QString::fromLatin1("/")))
00682     {
00683         info.setFile( appname );
00684         if( info.exists() && ( ignore || info.isExecutable() )
00685             && info.isFile() ) {
00686             return appname;
00687         }
00688         return QString::null;
00689     }
00690 
00691     QString p = QString("%1/%2").arg(__KDE_BINDIR).arg(appname);
00692     info.setFile( p );
00693     if( info.exists() && ( ignore || info.isExecutable() )
00694          && ( info.isFile() || info.isSymLink() )  ) {
00695          return p;
00696     }
00697 
00698     QStringList tokens;
00699     p = pstr;
00700 
00701     if( p == QString::null ) {
00702     p = getenv( "PATH" );
00703     }
00704 
00705     tokenize( tokens, p, ":\b" );
00706 
00707     // split path using : or \b as delimiters
00708     for( unsigned i = 0; i < tokens.count(); i++ ) {
00709     p = tokens[ i ];
00710 
00711         if ( p[ 0 ] == '~' )
00712         {
00713             int len = p.find( '/' );
00714             if ( len == -1 )
00715                 len = p.length();
00716             if ( len == 1 )
00717                 p.replace( 0, 1, QDir::homeDirPath() );
00718             else
00719             {
00720                 QString user = p.mid( 1, len - 1 );
00721                 struct passwd *dir = getpwnam( user.local8Bit().data() );
00722                 if ( dir && strlen( dir->pw_dir ) )
00723                     p.replace( 0, len, QString::fromLocal8Bit( dir->pw_dir ) );
00724             }
00725         }
00726 
00727     p += "/";
00728     p += appname;
00729 
00730     // Check for executable in this tokenized path
00731     info.setFile( p );
00732 
00733     if( info.exists() && ( ignore || info.isExecutable() )
00734            && ( info.isFile() || info.isSymLink() )  ) {
00735         return p;
00736     }
00737     }
00738 
00739     // If we reach here, the executable wasn't found.
00740     // So return empty string.
00741 
00742     return QString::null;
00743 }
00744 
00745 int KStandardDirs::findAllExe( QStringList& list, const QString& appname,
00746             const QString& pstr, bool ignore )
00747 {
00748     QString p = pstr;
00749     QFileInfo info;
00750     QStringList tokens;
00751 
00752     if( p == QString::null ) {
00753     p = getenv( "PATH" );
00754     }
00755 
00756     list.clear();
00757     tokenize( tokens, p, ":\b" );
00758 
00759     for ( unsigned i = 0; i < tokens.count(); i++ ) {
00760     p = tokens[ i ];
00761     p += "/";
00762     p += appname;
00763 
00764     info.setFile( p );
00765 
00766     if( info.exists() && (ignore || info.isExecutable())
00767         && info.isFile() ) {
00768         list.append( p );
00769     }
00770 
00771     }
00772 
00773     return list.count();
00774 }
00775 
00776 static int tokenize( QStringList& tokens, const QString& str,
00777              const QString& delim )
00778 {
00779     int len = str.length();
00780     QString token = "";
00781 
00782     for( int index = 0; index < len; index++)
00783     {
00784     if ( delim.find( str[ index ] ) >= 0 )
00785     {
00786         tokens.append( token );
00787         token = "";
00788     }
00789     else
00790     {
00791         token += str[ index ];
00792     }
00793     }
00794     if ( token.length() > 0 )
00795     {
00796     tokens.append( token );
00797     }
00798 
00799     return tokens.count();
00800 }
00801 
00802 QString KStandardDirs::kde_default(const char *type) {
00803     if (!strcmp(type, "data"))
00804     return "share/apps/";
00805     if (!strcmp(type, "html"))
00806     return "share/doc/HTML/";
00807     if (!strcmp(type, "icon"))
00808     return "share/icons/";
00809     if (!strcmp(type, "config"))
00810     return "share/config/";
00811     if (!strcmp(type, "pixmap"))
00812     return "share/pixmaps/";
00813     if (!strcmp(type, "apps"))
00814     return "share/applnk-redhat/";
00815     if (!strcmp(type, "sound"))
00816     return "share/sounds/";
00817     if (!strcmp(type, "locale"))
00818     return "share/locale/";
00819     if (!strcmp(type, "services"))
00820     return "share/services/";
00821     if (!strcmp(type, "servicetypes"))
00822     return "share/servicetypes/";
00823     if (!strcmp(type, "mime"))
00824     return "share/mimelnk/";
00825     if (!strcmp(type, "cgi"))
00826     return "cgi-bin/";
00827     if (!strcmp(type, "wallpaper"))
00828     return "share/wallpapers/";
00829     if (!strcmp(type, "templates"))
00830     return "share/templates/";
00831     if (!strcmp(type, "exe"))
00832     return "bin/";
00833     if (!strcmp(type, "lib"))
00834     return LIBDIR_NAME "/";
00835     if (!strcmp(type, "module"))
00836     return LIBDIR_NAME "/kde3/";
00837     if (!strcmp(type, "qtplugins"))
00838         return LIBDIR_NAME "/kde3/plugins";
00839     qFatal("unknown resource type %s", type);
00840     return QString::null;
00841 }
00842 
00843 QString KStandardDirs::saveLocation(const char *type,
00844                     const QString& suffix,
00845                     bool create) const
00846 {
00847     checkConfig();
00848 
00849     QString *pPath = savelocations.find(type);
00850     if (!pPath)
00851     {
00852        QStringList *dirs = relatives.find(type);
00853        if (!dirs && ((strcmp(type, "socket") == 0) || (strcmp(type, "tmp") == 0)))
00854        {
00855           (void) resourceDirs(type); // Generate socket resource.
00856           dirs = relatives.find(type); // Search again.
00857        }
00858        if (dirs)
00859        {
00860           // Check for existance of typed directory + suffix
00861           pPath = new QString(realPath(localkdedir() + dirs->last()));
00862        }
00863        else {
00864           dirs = absolutes.find(type);
00865           if (!dirs)
00866              qFatal("KStandardDirs: The resource type %s is not registered", type);
00867           pPath = new QString(realPath(dirs->last()));
00868        }
00869 
00870        savelocations.insert(type, pPath);
00871     }
00872     QString fullPath = *pPath + suffix;
00873 
00874     struct stat st;
00875     if (stat(QFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
00876     if(!create) {
00877 #ifndef NDEBUG
00878         qDebug("save location %s doesn't exist", fullPath.latin1());
00879 #endif
00880         return localkdedir()+suffix;
00881     }
00882     if(!makeDir(fullPath, 0700)) {
00883             qWarning("failed to create %s", fullPath.latin1());
00884         return localkdedir()+suffix;
00885     }
00886         dircache.remove(type);
00887     }
00888     return fullPath;
00889 }
00890 
00891 QString KStandardDirs::relativeLocation(const char *type, const QString &absPath)
00892 {
00893     QString fullPath = absPath;
00894     int i = absPath.findRev('/');
00895     if (i != -1)
00896     {
00897        fullPath = realPath(absPath.left(i+1))+absPath.mid(i+1); // Normalize
00898     }
00899 
00900     QStringList candidates = resourceDirs(type);
00901 
00902     for (QStringList::ConstIterator it = candidates.begin();
00903      it != candidates.end(); it++)
00904       if (fullPath.startsWith(*it))
00905       {
00906     return fullPath.mid((*it).length());
00907       }
00908 
00909     return absPath;
00910 }
00911 
00912 
00913 bool KStandardDirs::makeDir(const QString& dir, int mode)
00914 {
00915     // we want an absolute path
00916     if (dir.at(0) != '/')
00917         return false;
00918 
00919     QString target = dir;
00920     uint len = target.length();
00921 
00922     // append trailing slash if missing
00923     if (dir.at(len - 1) != '/')
00924         target += '/';
00925 
00926     QString base("");
00927     uint i = 1;
00928 
00929     while( i < len )
00930     {
00931         struct stat st;
00932         int pos = target.find('/', i);
00933         base += target.mid(i - 1, pos - i + 1);
00934         QCString baseEncoded = QFile::encodeName(base);
00935         // bail out if we encountered a problem
00936         if (stat(baseEncoded, &st) != 0)
00937         {
00938           // Directory does not exist....
00939           // Or maybe a dangling symlink ?
00940           if (lstat(baseEncoded, &st) == 0)
00941               (void)unlink(baseEncoded); // try removing
00942 
00943       if ( mkdir(baseEncoded, (mode_t) mode) != 0) {
00944         perror("trying to create local folder");
00945         return false; // Couldn't create it :-(
00946       }
00947         }
00948         i = pos + 1;
00949     }
00950     return true;
00951 }
00952 
00953 static QString readEnvPath(const char *env)
00954 {
00955    QCString c_path = getenv(env);
00956    if (c_path.isEmpty())
00957       return QString::null;
00958    return QFile::decodeName(c_path);
00959 }
00960 
00961 static void fixHomeDir(QString &dir)
00962 {
00963    if (dir[0] == '~')
00964    {
00965       dir = QDir::homeDirPath() + dir.mid(1);
00966    }
00967 }
00968 
00969 void KStandardDirs::addKDEDefaults()
00970 {
00971     QStringList kdedirList;
00972 
00973     QString kdedirs = readEnvPath("KDEDIRS");
00974     if (!kdedirs.isEmpty())
00975     {
00976     tokenize(kdedirList, kdedirs, ":");
00977     }
00978     else
00979     {
00980     QString kdedir = readEnvPath("KDEDIR");
00981     if (!kdedir.isEmpty())
00982         {
00983            fixHomeDir(kdedir);
00984        kdedirList.append(kdedir);
00985         }
00986     }
00987     kdedirList.append(KDEDIR);
00988 
00989 #ifdef __KDE_EXECPREFIX
00990     QString execPrefix(__KDE_EXECPREFIX);
00991     if (execPrefix!="NONE")
00992        kdedirList.append(execPrefix);
00993 #endif
00994 
00995     QString localKdeDir;
00996     if (getuid())
00997     {
00998        localKdeDir = readEnvPath("KDEHOME");
00999        if (!localKdeDir.isEmpty())
01000        {
01001           if (localKdeDir[localKdeDir.length()-1] != '/')
01002              localKdeDir += '/';
01003        }
01004        else
01005        {
01006           localKdeDir =  QDir::homeDirPath() + "/.kde/";
01007        }
01008     }
01009     else
01010     {
01011        // We treat root different to prevent root messing up the
01012        // file permissions in the users home directory.
01013        localKdeDir = readEnvPath("KDEROOTHOME");
01014        if (!localKdeDir.isEmpty())
01015        {
01016           if (localKdeDir[localKdeDir.length()-1] != '/')
01017              localKdeDir += '/';
01018        }
01019        else
01020        {
01021           struct passwd *pw = getpwuid(0);
01022           localKdeDir =  QFile::decodeName((pw && pw->pw_dir) ? pw->pw_dir : "/root")  + "/.kde/";
01023        }
01024 
01025     }
01026 
01027     if (localKdeDir != "-/")
01028     {
01029         fixHomeDir(localKdeDir);
01030         addPrefix(localKdeDir);
01031     }
01032 
01033     for (QStringList::ConstIterator it = kdedirList.begin();
01034      it != kdedirList.end(); it++)
01035     {
01036         QString dir = *it;
01037         fixHomeDir(dir);
01038     addPrefix(dir);
01039     }
01040 
01041     uint index = 0;
01042     while (types[index] != 0) {
01043     addResourceType(types[index], kde_default(types[index]));
01044     index++;
01045     }
01046 
01047     QString dir = QString("%1share/cache/").arg(localKdeDir);
01048     addResourceDir("cache", dir);
01049 
01050     addResourceDir("home", QDir::homeDirPath());
01051 }
01052 
01053 void KStandardDirs::checkConfig() const
01054 {
01055     if (!addedCustoms && KGlobal::_instance && KGlobal::_instance->_config)
01056         const_cast<KStandardDirs*>(this)->addCustomized(KGlobal::_instance->_config);
01057 }
01058 
01059 bool KStandardDirs::addCustomized(KConfig *config)
01060 {
01061     if (addedCustoms) // there are already customized entries
01062         return false; // we just quite and hope they are the right ones
01063 
01064     // save the numbers of config directories. If this changes,
01065     // we will return true to give KConfig a chance to reparse
01066     uint configdirs = resourceDirs("config").count();
01067 
01068     // reading the prefixes in
01069     QString oldGroup = config->group();
01070     config->setGroup("Directories");
01071 
01072     QStringList list;
01073     QStringList::ConstIterator it;
01074     list = config->readListEntry("prefixes");
01075     for (it = list.begin(); it != list.end(); it++)
01076     addPrefix(*it);
01077 
01078     // iterating over all entries in the group Directories
01079     // to find entries that start with dir_$type
01080     QMap<QString, QString> entries = config->entryMap("Directories");
01081 
01082     QMap<QString, QString>::ConstIterator it2;
01083     for (it2 = entries.begin(); it2 != entries.end(); it2++)
01084     {
01085     QString key = it2.key();
01086     if (key.left(4) == "dir_") {
01087         // generate directory list, there may be more than 1.
01088         QStringList dirs = QStringList::split(',',
01089                           *it2);
01090         QStringList::Iterator sIt(dirs.begin());
01091         QString resType = key.mid(4, key.length());
01092         for (; sIt != dirs.end(); ++sIt) {
01093         addResourceDir(resType.latin1(), *sIt);
01094         }
01095     }
01096     }
01097 
01098     // Process KIOSK restrictions.
01099     config->setGroup("KDE Resource Restrictions");
01100     entries = config->entryMap("KDE Resource Restrictions");
01101     for (it2 = entries.begin(); it2 != entries.end(); it2++)
01102     {
01103     QString key = it2.key();
01104         if (!config->readBoolEntry(key, true))
01105         {
01106            if (!d)
01107            {
01108                d = new KStandardDirsPrivate;
01109                d->restrictionsActive = true;
01110            }
01111            d->restrictions.insert(key.latin1(), &d->restrictionsActive); // Anything will do
01112            dircache.remove(key.latin1());
01113         }
01114     }
01115 
01116     // save it for future calls - that will return
01117     addedCustoms = true;
01118     config->setGroup(oldGroup);
01119 
01120     // return true if the number of config dirs changed
01121     return (resourceDirs("config").count() != configdirs);
01122 }
01123 
01124 QString KStandardDirs::localkdedir() const
01125 {
01126     // Return the prefix to use for saving
01127     return prefixes.first();
01128 }
01129 
01130 // just to make code more readable without macros
01131 QString locate( const char *type,
01132         const QString& filename, const KInstance* inst )
01133 {
01134     return inst->dirs()->findResource(type, filename);
01135 }
01136 
01137 QString locateLocal( const char *type,
01138                  const QString& filename, const KInstance* inst )
01139 {
01140     // try to find slashes. If there are some, we have to
01141     // create the subdir first
01142     int slash = filename.findRev('/')+1;
01143     if (!slash) // only one filename
01144     return inst->dirs()->saveLocation(type) + filename;
01145 
01146     // split path from filename
01147     QString dir = filename.left(slash);
01148     QString file = filename.mid(slash);
01149     return inst->dirs()->saveLocation(type, dir) + file;
01150 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:14:47 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001