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 QStringList KStandardDirs::resourceDirs(const char *type) const
00547 {
00548     QStringList *candidates = dircache.find(type);
00549 
00550     if (!candidates) { // filling cache
00551         if (strcmp(type, "socket") == 0)
00552         {
00553           char hostname[256];
00554           hostname[0] = 0;
00555           gethostname(hostname, 255);
00556           QString dir = QString("%1socket-%2").arg(localkdedir()).arg(hostname);
00557           char link[1024];
00558           link[1023] = 0;
00559           int result = readlink(QFile::encodeName(dir).data(), link, 1023);
00560           if ((result == -1) && (errno == ENOENT))
00561           {
00562              QString srv = findExe(QString::fromLatin1("lnusertemp"), KDEDIR+QString::fromLatin1("/bin"));
00563              if (srv.isEmpty())
00564                 srv = findExe(QString::fromLatin1("lnusertemp"));
00565              if (!srv.isEmpty())
00566              {
00567                 system(QFile::encodeName(srv)+" socket");
00568                 result = readlink(QFile::encodeName(dir).data(), link, 1023);
00569              }
00570           }
00571           if (result > 0)
00572           {
00573              link[result] = 0;
00574              if (link[0] == '/')
00575                 dir = QFile::decodeName(link);
00576              else
00577                 dir = QDir::cleanDirPath(dir+QFile::decodeName(link));
00578           }
00579           const_cast<KStandardDirs *>(this)->addResourceDir("socket", dir+'/');
00580         }
00581         if (strcmp(type, "tmp") == 0)
00582         {
00583           char hostname[256];
00584           hostname[0] = 0;
00585           gethostname(hostname, 255);
00586           QString dir = QString("%1tmp-%2").arg(localkdedir()).arg(hostname);
00587           char link[1024];
00588           link[1023] = 0;
00589           int result = readlink(QFile::encodeName(dir).data(), link, 1023);
00590           if ((result == -1) && (errno == ENOENT))
00591           {
00592              QString srv = findExe(QString::fromLatin1("lnusertemp"), KDEDIR+QString::fromLatin1("/bin"));
00593              if (srv.isEmpty())
00594                 srv = findExe(QString::fromLatin1("lnusertemp"));
00595              if (!srv.isEmpty())
00596              {
00597                 system(QFile::encodeName(srv)+" tmp");
00598                 result = readlink(QFile::encodeName(dir).data(), link, 1023);
00599              }
00600           }
00601           if (result > 0)
00602           {
00603              link[result] = 0;
00604              if (link[0] == '/')
00605                 dir = QFile::decodeName(link);
00606              else
00607                 dir = QDir::cleanDirPath(dir+QFile::decodeName(link));
00608           }
00609           const_cast<KStandardDirs *>(this)->addResourceDir("tmp", dir+'/');
00610         }
00611         QDir testdir;
00612 
00613         candidates = new QStringList();
00614         QStringList *dirs;
00615 
00616         bool restrictionActive = false;
00617         if (d && d->restrictionsActive)
00618         {
00619            if (d->dataRestrictionActive)
00620               restrictionActive = true;
00621            else if (d->restrictions["all"])
00622               restrictionActive = true;
00623            else if (d->restrictions[type])
00624               restrictionActive = true;
00625            d->dataRestrictionActive = false; // Reset
00626         }
00627 
00628         dirs = relatives.find(type);
00629         if (dirs)
00630         {
00631             bool local = true;
00632             for (QStringList::ConstIterator pit = prefixes.begin();
00633                  pit != prefixes.end();
00634                  pit++)
00635             {
00636                 for (QStringList::ConstIterator it = dirs->begin();
00637                      it != dirs->end(); ++it) {
00638                     QString path = realPath(*pit + *it);
00639                     testdir.setPath(path);
00640                     if (local && restrictionActive)
00641                        continue;
00642                     if ((local || testdir.exists()) && !candidates->contains(path))
00643                         candidates->append(path);
00644                 }
00645                 local = false;
00646             }
00647         }
00648         dirs = absolutes.find(type);
00649         if (dirs)
00650             for (QStringList::ConstIterator it = dirs->begin();
00651                  it != dirs->end(); ++it)
00652             {
00653                 testdir.setPath(*it);
00654                 if (testdir.exists())
00655                 {
00656                     QString filename = realPath(*it);
00657                     if (!candidates->contains(filename))
00658                         candidates->append(filename);
00659                 }
00660             }
00661         dircache.insert(type, candidates);
00662     }
00663 
00664 #if 0
00665     kdDebug() << "found dirs for resource " << type << ":" << endl;
00666     for (QStringList::ConstIterator pit = candidates->begin();
00667      pit != candidates->end();
00668      pit++)
00669     {
00670     fprintf(stderr, "%s\n", (*pit).latin1());
00671     }
00672 #endif
00673 
00674 
00675   return *candidates;
00676 }
00677 
00678 QString KStandardDirs::findExe( const QString& appname,
00679                 const QString& pstr, bool ignore)
00680 {
00681     QFileInfo info;
00682 
00683     // absolute path ?
00684     if (appname.startsWith(QString::fromLatin1("/")))
00685     {
00686         info.setFile( appname );
00687         if( info.exists() && ( ignore || info.isExecutable() )
00688             && info.isFile() ) {
00689             return appname;
00690         }
00691         return QString::null;
00692     }
00693 
00694     QString p = QString("%1/%2").arg(__KDE_BINDIR).arg(appname);
00695     info.setFile( p );
00696     if( info.exists() && ( ignore || info.isExecutable() )
00697          && ( info.isFile() || info.isSymLink() )  ) {
00698          return p;
00699     }
00700 
00701     QStringList tokens;
00702     p = pstr;
00703 
00704     if( p == QString::null ) {
00705     p = getenv( "PATH" );
00706     }
00707 
00708     tokenize( tokens, p, ":\b" );
00709 
00710     // split path using : or \b as delimiters
00711     for( unsigned i = 0; i < tokens.count(); i++ ) {
00712     p = tokens[ i ];
00713 
00714         if ( p[ 0 ] == '~' )
00715         {
00716             int len = p.find( '/' );
00717             if ( len == -1 )
00718                 len = p.length();
00719             if ( len == 1 )
00720                 p.replace( 0, 1, QDir::homeDirPath() );
00721             else
00722             {
00723                 QString user = p.mid( 1, len - 1 );
00724                 struct passwd *dir = getpwnam( user.local8Bit().data() );
00725                 if ( dir && strlen( dir->pw_dir ) )
00726                     p.replace( 0, len, QString::fromLocal8Bit( dir->pw_dir ) );
00727             }
00728         }
00729 
00730     p += "/";
00731     p += appname;
00732 
00733     // Check for executable in this tokenized path
00734     info.setFile( p );
00735 
00736     if( info.exists() && ( ignore || info.isExecutable() )
00737            && ( info.isFile() || info.isSymLink() )  ) {
00738         return p;
00739     }
00740     }
00741 
00742     // If we reach here, the executable wasn't found.
00743     // So return empty string.
00744 
00745     return QString::null;
00746 }
00747 
00748 int KStandardDirs::findAllExe( QStringList& list, const QString& appname,
00749             const QString& pstr, bool ignore )
00750 {
00751     QString p = pstr;
00752     QFileInfo info;
00753     QStringList tokens;
00754 
00755     if( p == QString::null ) {
00756     p = getenv( "PATH" );
00757     }
00758 
00759     list.clear();
00760     tokenize( tokens, p, ":\b" );
00761 
00762     for ( unsigned i = 0; i < tokens.count(); i++ ) {
00763     p = tokens[ i ];
00764     p += "/";
00765     p += appname;
00766 
00767     info.setFile( p );
00768 
00769     if( info.exists() && (ignore || info.isExecutable())
00770         && info.isFile() ) {
00771         list.append( p );
00772     }
00773 
00774     }
00775 
00776     return list.count();
00777 }
00778 
00779 static int tokenize( QStringList& tokens, const QString& str,
00780              const QString& delim )
00781 {
00782     int len = str.length();
00783     QString token = "";
00784 
00785     for( int index = 0; index < len; index++)
00786     {
00787     if ( delim.find( str[ index ] ) >= 0 )
00788     {
00789         tokens.append( token );
00790         token = "";
00791     }
00792     else
00793     {
00794         token += str[ index ];
00795     }
00796     }
00797     if ( token.length() > 0 )
00798     {
00799     tokens.append( token );
00800     }
00801 
00802     return tokens.count();
00803 }
00804 
00805 QString KStandardDirs::kde_default(const char *type) {
00806     if (!strcmp(type, "data"))
00807     return "share/apps/";
00808     if (!strcmp(type, "html"))
00809     return "share/doc/HTML/";
00810     if (!strcmp(type, "icon"))
00811     return "share/icons/";
00812     if (!strcmp(type, "config"))
00813     return "share/config/";
00814     if (!strcmp(type, "pixmap"))
00815     return "share/pixmaps/";
00816     if (!strcmp(type, "apps"))
00817     return "share/applnk-redhat/";
00818     if (!strcmp(type, "sound"))
00819     return "share/sounds/";
00820     if (!strcmp(type, "locale"))
00821     return "share/locale/";
00822     if (!strcmp(type, "services"))
00823     return "share/services/";
00824     if (!strcmp(type, "servicetypes"))
00825     return "share/servicetypes/";
00826     if (!strcmp(type, "mime"))
00827     return "share/mimelnk/";
00828     if (!strcmp(type, "cgi"))
00829     return "cgi-bin/";
00830     if (!strcmp(type, "wallpaper"))
00831     return "share/wallpapers/";
00832     if (!strcmp(type, "templates"))
00833     return "share/templates/";
00834     if (!strcmp(type, "exe"))
00835     return "bin/";
00836     if (!strcmp(type, "lib"))
00837     return LIBDIR_NAME "/";
00838     if (!strcmp(type, "module"))
00839     return LIBDIR_NAME "/kde3/";
00840     if (!strcmp(type, "qtplugins"))
00841         return LIBDIR_NAME "/kde3/plugins";
00842     qFatal("unknown resource type %s", type);
00843     return QString::null;
00844 }
00845 
00846 QString KStandardDirs::saveLocation(const char *type,
00847                     const QString& suffix,
00848                     bool create) const
00849 {
00850     checkConfig();
00851 
00852     QString *pPath = savelocations.find(type);
00853     if (!pPath)
00854     {
00855        QStringList *dirs = relatives.find(type);
00856        if (!dirs && ((strcmp(type, "socket") == 0) || (strcmp(type, "tmp") == 0)))
00857        {
00858           (void) resourceDirs(type); // Generate socket resource.
00859           dirs = relatives.find(type); // Search again.
00860        }
00861        if (dirs)
00862        {
00863           // Check for existance of typed directory + suffix
00864           pPath = new QString(realPath(localkdedir() + dirs->last()));
00865        }
00866        else {
00867           dirs = absolutes.find(type);
00868           if (!dirs)
00869              qFatal("KStandardDirs: The resource type %s is not registered", type);
00870           pPath = new QString(realPath(dirs->last()));
00871        }
00872 
00873        savelocations.insert(type, pPath);
00874     }
00875     QString fullPath = *pPath + suffix;
00876 
00877     struct stat st;
00878     if (stat(QFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) {
00879     if(!create) {
00880 #ifndef NDEBUG
00881         qDebug("save location %s doesn't exist", fullPath.latin1());
00882 #endif
00883         return localkdedir()+suffix;
00884     }
00885     if(!makeDir(fullPath, 0700)) {
00886             qWarning("failed to create %s", fullPath.latin1());
00887         return localkdedir()+suffix;
00888     }
00889         dircache.remove(type);
00890     }
00891     return fullPath;
00892 }
00893 
00894 QString KStandardDirs::relativeLocation(const char *type, const QString &absPath)
00895 {
00896     QString fullPath = absPath;
00897     int i = absPath.findRev('/');
00898     if (i != -1)
00899     {
00900        fullPath = realPath(absPath.left(i+1))+absPath.mid(i+1); // Normalize
00901     }
00902 
00903     QStringList candidates = resourceDirs(type);
00904 
00905     for (QStringList::ConstIterator it = candidates.begin();
00906      it != candidates.end(); it++)
00907       if (fullPath.startsWith(*it))
00908       {
00909     return fullPath.mid((*it).length());
00910       }
00911 
00912     return absPath;
00913 }
00914 
00915 
00916 bool KStandardDirs::makeDir(const QString& dir, int mode)
00917 {
00918     // we want an absolute path
00919     if (dir.at(0) != '/')
00920         return false;
00921 
00922     QString target = dir;
00923     uint len = target.length();
00924 
00925     // append trailing slash if missing
00926     if (dir.at(len - 1) != '/')
00927         target += '/';
00928 
00929     QString base("");
00930     uint i = 1;
00931 
00932     while( i < len )
00933     {
00934         struct stat st;
00935         int pos = target.find('/', i);
00936         base += target.mid(i - 1, pos - i + 1);
00937         QCString baseEncoded = QFile::encodeName(base);
00938         // bail out if we encountered a problem
00939         if (stat(baseEncoded, &st) != 0)
00940         {
00941           // Directory does not exist....
00942           // Or maybe a dangling symlink ?
00943           if (lstat(baseEncoded, &st) == 0)
00944               (void)unlink(baseEncoded); // try removing
00945 
00946       if ( mkdir(baseEncoded, (mode_t) mode) != 0) {
00947         perror("trying to create local folder");
00948         return false; // Couldn't create it :-(
00949       }
00950         }
00951         i = pos + 1;
00952     }
00953     return true;
00954 }
00955 
00956 static QString readEnvPath(const char *env)
00957 {
00958    QCString c_path = getenv(env);
00959    if (c_path.isEmpty())
00960       return QString::null;
00961    return QFile::decodeName(c_path);
00962 }
00963 
00964 static void fixHomeDir(QString &dir)
00965 {
00966    if (dir[0] == '~')
00967    {
00968       dir = QDir::homeDirPath() + dir.mid(1);
00969    }
00970 }
00971 
00972 void KStandardDirs::addKDEDefaults()
00973 {
00974     QStringList kdedirList;
00975 
00976     QString kdedirs = readEnvPath("KDEDIRS");
00977     if (!kdedirs.isEmpty())
00978     {
00979     tokenize(kdedirList, kdedirs, ":");
00980     }
00981     else
00982     {
00983     QString kdedir = readEnvPath("KDEDIR");
00984     if (!kdedir.isEmpty())
00985         {
00986            fixHomeDir(kdedir);
00987        kdedirList.append(kdedir);
00988         }
00989     }
00990     kdedirList.append(KDEDIR);
00991 
00992 #ifdef __KDE_EXECPREFIX
00993     QString execPrefix(__KDE_EXECPREFIX);
00994     if (execPrefix!="NONE")
00995        kdedirList.append(execPrefix);
00996 #endif
00997 
00998     QString localKdeDir;
00999     if (getuid())
01000     {
01001        localKdeDir = readEnvPath("KDEHOME");
01002        if (!localKdeDir.isEmpty())
01003        {
01004           if (localKdeDir[localKdeDir.length()-1] != '/')
01005              localKdeDir += '/';
01006        }
01007        else
01008        {
01009           localKdeDir =  QDir::homeDirPath() + "/.kde/";
01010        }
01011     }
01012     else
01013     {
01014        // We treat root different to prevent root messing up the
01015        // file permissions in the users home directory.
01016        localKdeDir = readEnvPath("KDEROOTHOME");
01017        if (!localKdeDir.isEmpty())
01018        {
01019           if (localKdeDir[localKdeDir.length()-1] != '/')
01020              localKdeDir += '/';
01021        }
01022        else
01023        {
01024           struct passwd *pw = getpwuid(0);
01025           localKdeDir =  QFile::decodeName((pw && pw->pw_dir) ? pw->pw_dir : "/root")  + "/.kde/";
01026        }
01027 
01028     }
01029 
01030     if (localKdeDir != "-/")
01031     {
01032         fixHomeDir(localKdeDir);
01033         addPrefix(localKdeDir);
01034     }
01035 
01036     for (QStringList::ConstIterator it = kdedirList.begin();
01037      it != kdedirList.end(); it++)
01038     {
01039         QString dir = *it;
01040         fixHomeDir(dir);
01041     addPrefix(dir);
01042     }
01043 
01044     uint index = 0;
01045     while (types[index] != 0) {
01046     addResourceType(types[index], kde_default(types[index]));
01047     index++;
01048     }
01049 
01050     QString dir = QString("%1share/cache/").arg(localKdeDir);
01051     addResourceDir("cache", dir);
01052 
01053     addResourceDir("home", QDir::homeDirPath());
01054 }
01055 
01056 void KStandardDirs::checkConfig() const
01057 {
01058     if (!addedCustoms && KGlobal::_instance && KGlobal::_instance->_config)
01059         const_cast<KStandardDirs*>(this)->addCustomized(KGlobal::_instance->_config);
01060 }
01061 
01062 bool KStandardDirs::addCustomized(KConfig *config)
01063 {
01064     if (addedCustoms) // there are already customized entries
01065         return false; // we just quite and hope they are the right ones
01066 
01067     // save the numbers of config directories. If this changes,
01068     // we will return true to give KConfig a chance to reparse
01069     uint configdirs = resourceDirs("config").count();
01070 
01071     // reading the prefixes in
01072     QString oldGroup = config->group();
01073     config->setGroup("Directories");
01074 
01075     QStringList list;
01076     QStringList::ConstIterator it;
01077     list = config->readListEntry("prefixes");
01078     for (it = list.begin(); it != list.end(); it++)
01079     addPrefix(*it);
01080 
01081     // iterating over all entries in the group Directories
01082     // to find entries that start with dir_$type
01083     QMap<QString, QString> entries = config->entryMap("Directories");
01084 
01085     QMap<QString, QString>::ConstIterator it2;
01086     for (it2 = entries.begin(); it2 != entries.end(); it2++)
01087     {
01088     QString key = it2.key();
01089     if (key.left(4) == "dir_") {
01090         // generate directory list, there may be more than 1.
01091         QStringList dirs = QStringList::split(',',
01092                           *it2);
01093         QStringList::Iterator sIt(dirs.begin());
01094         QString resType = key.mid(4, key.length());
01095         for (; sIt != dirs.end(); ++sIt) {
01096         addResourceDir(resType.latin1(), *sIt);
01097         }
01098     }
01099     }
01100 
01101     // Process KIOSK restrictions.
01102     config->setGroup("KDE Resource Restrictions");
01103     entries = config->entryMap("KDE Resource Restrictions");
01104     for (it2 = entries.begin(); it2 != entries.end(); it2++)
01105     {
01106     QString key = it2.key();
01107         if (!config->readBoolEntry(key, true))
01108         {
01109            if (!d)
01110            {
01111                d = new KStandardDirsPrivate;
01112                d->restrictionsActive = true;
01113            }
01114            d->restrictions.insert(key.latin1(), &d->restrictionsActive); // Anything will do
01115            dircache.remove(key.latin1());
01116         }
01117     }
01118 
01119     // save it for future calls - that will return
01120     addedCustoms = true;
01121     config->setGroup(oldGroup);
01122 
01123     // return true if the number of config dirs changed
01124     return (resourceDirs("config").count() != configdirs);
01125 }
01126 
01127 QString KStandardDirs::localkdedir() const
01128 {
01129     // Return the prefix to use for saving
01130     return prefixes.first();
01131 }
01132 
01133 // just to make code more readable without macros
01134 QString locate( const char *type,
01135         const QString& filename, const KInstance* inst )
01136 {
01137     return inst->dirs()->findResource(type, filename);
01138 }
01139 
01140 QString locateLocal( const char *type,
01141                  const QString& filename, const KInstance* inst )
01142 {
01143     // try to find slashes. If there are some, we have to
01144     // create the subdir first
01145     int slash = filename.findRev('/')+1;
01146     if (!slash) // only one filename
01147     return inst->dirs()->saveLocation(type) + filename;
01148 
01149     // split path from filename
01150     QString dir = filename.left(slash);
01151     QString file = filename.mid(slash);
01152     return inst->dirs()->saveLocation(type, dir) + file;
01153 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.3.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Oct 26 11:25:20 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001