QStringList KStandardDirs::findDirs( const char *type, const QString& reldir ) const { QDir testdir; QStringList list; if (!QDir::isRelativePath(reldir)) { testdir.setPath(reldir); if (testdir.exists()) { if (reldir.endsWith("/")) list.append(reldir); else list.append(reldir+'/'); } return list; } checkConfig(); if (d && d->restrictionsActive && (strcmp(type, "data")==0)) applyDataRestrictions(reldir); QStringList candidates = resourceDirs(type); for (QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) { testdir.setPath(*it + reldir); if (testdir.exists()) list.append(testdir.absPath() + '/'); } return list; }
QString KStandardDirs::findResourceDir(const char *type, const QString &filename) const { #ifndef NDEBUG if(filename.isEmpty()) { kdWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!" << endl; return QString::null; } #endif if(d && d->restrictionsActive && (strcmp(type, "data") == 0)) applyDataRestrictions(filename); QStringList candidates = resourceDirs(type); QString fullPath; for(QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) { if(exists(*it + filename)) { return *it; } } #ifndef NDEBUG if(false && strcmp(type, "locale")) kdDebug() << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\"." << endl; #endif return QString::null; }
QString KStandardDirs::saveLocation(const char *type, const QString& suffix, bool create) const { checkConfig(); QString *pPath = savelocations.find(type); if (!pPath) { QStringList *dirs = relatives.find(type); if (!dirs && ( (strcmp(type, "socket") == 0) || (strcmp(type, "tmp") == 0) || (strcmp(type, "cache") == 0) )) { (void) resourceDirs(type); // Generate socket|tmp|cache resource. dirs = relatives.find(type); // Search again. } if (dirs) { // Check for existence of typed directory + suffix if (strncmp(type, "xdgdata-", 8) == 0) pPath = new QString(realPath(localxdgdatadir() + dirs->last())); else if (strncmp(type, "xdgconf-", 8) == 0) pPath = new QString(realPath(localxdgconfdir() + dirs->last())); else pPath = new QString(realPath(localkdedir() + dirs->last())); } else { dirs = absolutes.find(type); if (!dirs) qFatal("KStandardDirs: The resource type %s is not registered", type); pPath = new QString(realPath(dirs->last())); } savelocations.insert(type, pPath); } QString fullPath = *pPath + (pPath->endsWith("/") ? "" : "/") + suffix; KDE_struct_stat st; if (KDE_stat(QFile::encodeName(fullPath), &st) != 0 || !(S_ISDIR(st.st_mode))) { if(!create) { #ifndef NDEBUG kdDebug() << QString("save location %1 doesn't exist").arg(fullPath) << endl; #endif return fullPath; } if(!makeDir(fullPath, 0700)) { return fullPath; } dircache.remove(type); } if (!fullPath.endsWith("/")) fullPath += "/"; return fullPath; }
QStringList KStandardDirs::findAllResources( const char *type, const QString& filter, bool recursive, bool unique, QStringList &relList) const { QStringList list; QString filterPath; QString filterFile; if (filter.length()) { int slash = filter.findRev('/'); if (slash < 0) filterFile = filter; else { filterPath = filter.left(slash + 1); filterFile = filter.mid(slash + 1); } } checkConfig(); QStringList candidates; if (!QDir::isRelativePath(filter)) // absolute path { #ifdef Q_OS_WIN candidates << filterPath.left(3); //e.g. "C:\" filterPath = filterPath.mid(3); #else candidates << "/"; filterPath = filterPath.mid(1); #endif } else { if (d && d->restrictionsActive && (strcmp(type, "data")==0)) applyDataRestrictions(filter); candidates = resourceDirs(type); } if (filterFile.isEmpty()) filterFile = "*"; QRegExp regExp(filterFile, true, true); for (QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) { lookupPrefix(*it, filterPath, "", regExp, list, relList, recursive, unique); } return list; }
QString KStandardDirs::relativeLocation(const char *type, const QString &absPath) { QString fullPath = absPath; int i = absPath.findRev('/'); if(i != -1) { fullPath = realPath(absPath.left(i + 1)) + absPath.mid(i + 1); // Normalize } QStringList candidates = resourceDirs(type); for(QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) if(fullPath.startsWith(*it)) { return fullPath.mid((*it).length()); } return absPath; }
Q_UINT32 KStandardDirs::calcResourceHash(const char *type, const QString &filename, bool deep) const { Q_UINT32 hash = 0; if(!QDir::isRelativePath(filename)) { // absolute dirs are absolute dirs, right? :-/ return updateHash(filename, hash); } if(d && d->restrictionsActive && (strcmp(type, "data") == 0)) applyDataRestrictions(filename); QStringList candidates = resourceDirs(type); QString fullPath; for(QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) { hash = updateHash(*it + filename, hash); if(!deep && hash) return hash; } return hash; }
QString KStandardDirs::findResourceDir( const char *type, const QString& filename) const { #ifndef NDEBUG if (filename.isEmpty()) { kdWarning() << "filename for type " << type << " in KStandardDirs::findResourceDir is not supposed to be empty!!" << endl; return QString::null; } #endif if (d && d->restrictionsActive && (strcmp(type, "data")==0)) applyDataRestrictions(filename); QStringList candidates = resourceDirs(type); QString fullPath; for (QStringList::ConstIterator it = candidates.begin(); it != candidates.end(); ++it) { if (exists(*it + filename)) { #ifdef Q_WS_WIN //this ensures we're using installed .la files if ((*it).isEmpty() && filename.right(3)==".la") { #ifndef NDEBUG kdDebug() << "KStandardDirs::findResourceDir() found .la in cwd: skipping. (fname=" << filename << ")" << endl; #endif continue; } #endif //Q_WS_WIN return *it; } } #ifndef NDEBUG if(false && strcmp(type, "locale")) kdDebug() << "KStdDirs::findResDir(): can't find \"" << filename << "\" in type \"" << type << "\"." << endl; #endif return QString::null; }
bool KStandardDirs::addCustomized(KConfig *config) { if (addedCustoms && !d->checkRestrictions) // there are already customized entries return false; // we just quit and hope they are the right ones // save the numbers of config directories. If this changes, // we will return true to give KConfig a chance to reparse uint configdirs = resourceDirs("config").count(); // Remember original group QString oldGroup = config->group(); if (!addedCustoms) { // We only add custom entries once addedCustoms = true; // reading the prefixes in QString group = QString::fromLatin1("Directories"); config->setGroup(group); QString kioskAdmin = config->readEntry("kioskAdmin"); if (!kioskAdmin.isEmpty() && !kde_kiosk_admin) { int i = kioskAdmin.find(':'); QString user = kioskAdmin.left(i); QString host = kioskAdmin.mid(i+1); KUser thisUser; char hostname[ 256 ]; hostname[ 0 ] = '\0'; if (!gethostname( hostname, 255 )) hostname[sizeof(hostname)-1] = '\0'; if ((user == thisUser.loginName()) && (host.isEmpty() || (host == hostname))) { kde_kiosk_admin = true; } } bool readProfiles = true; if (kde_kiosk_admin && !QCString(getenv("KDE_KIOSK_NO_PROFILES")).isEmpty()) readProfiles = false; QString userMapFile = config->readEntry("userProfileMapFile"); QString profileDirsPrefix = config->readEntry("profileDirsPrefix"); if (!profileDirsPrefix.isEmpty() && !profileDirsPrefix.endsWith("/")) profileDirsPrefix.append('/'); QStringList profiles; if (readProfiles) profiles = lookupProfiles(userMapFile); QString profile; bool priority = false; while(true) { config->setGroup(group); QStringList list = config->readListEntry("prefixes"); for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it) { addPrefix(*it, priority); addXdgConfigPrefix(*it+"/etc/xdg", priority); addXdgDataPrefix(*it+"/share", priority); } // If there are no prefixes defined, check if there is a directory // for this profile under <profileDirsPrefix> if (list.isEmpty() && !profile.isEmpty() && !profileDirsPrefix.isEmpty()) { QString dir = profileDirsPrefix + profile; addPrefix(dir, priority); addXdgConfigPrefix(dir+"/etc/xdg", priority); addXdgDataPrefix(dir+"/share", priority); } // iterating over all entries in the group Directories // to find entries that start with dir_$type QMap<QString, QString> entries = config->entryMap(group); for (QMap<QString, QString>::ConstIterator it2 = entries.begin(); it2 != entries.end(); it2++) { QString key = it2.key(); if (key.startsWith("dir_")) { // generate directory list, there may be more than 1. QStringList dirs = QStringList::split(',', *it2); QStringList::Iterator sIt(dirs.begin()); QString resType = key.mid(4, key.length()); for (; sIt != dirs.end(); ++sIt) { addResourceDir(resType.latin1(), *sIt, priority); } } } if (profiles.isEmpty()) break; profile = profiles.back(); group = QString::fromLatin1("Directories-%1").arg(profile); profiles.pop_back(); priority = true; } } // Process KIOSK restrictions. if (!kde_kiosk_admin || QCString(getenv("KDE_KIOSK_NO_RESTRICTIONS")).isEmpty()) { config->setGroup("KDE Resource Restrictions"); QMap<QString, QString> entries = config->entryMap("KDE Resource Restrictions"); for (QMap<QString, QString>::ConstIterator it2 = entries.begin(); it2 != entries.end(); it2++) { QString key = it2.key(); if (!config->readBoolEntry(key, true)) { d->restrictionsActive = true; d->restrictions.insert(key.latin1(), &d->restrictionsActive); // Anything will do dircache.remove(key.latin1()); } } } config->setGroup(oldGroup); // check if the number of config dirs changed bool configDirsChanged = (resourceDirs("config").count() != configdirs); // If the config dirs changed, we check kiosk restrictions again. d->checkRestrictions = configDirsChanged; // return true if the number of config dirs changed: reparse config file return configDirsChanged; }