diff options
Diffstat (limited to 'src/util/directory.cpp')
-rw-r--r-- | src/util/directory.cpp | 122 |
1 files changed, 82 insertions, 40 deletions
diff --git a/src/util/directory.cpp b/src/util/directory.cpp index 809b435..ddbbaaf 100644 --- a/src/util/directory.cpp +++ b/src/util/directory.cpp @@ -2,7 +2,7 @@ * * This file is part of BibleTime's source code, http://www.bibletime.info/. * -* Copyright 1999-2008 by the BibleTime developers. +* Copyright 1999-2011 by the BibleTime developers. * The BibleTime source code is licensed under the GNU General Public License version 2.0. * **********/ @@ -39,18 +39,30 @@ QDir cachedUserHomeSwordModsDir; QDir cachedUserSessionsDir; QDir cachedUserCacheDir; QDir cachedUserIndexDir; +QDir cachedSwordPathDir; #ifdef Q_WS_WIN QDir cachedApplicationSwordDir; // Only Windows installs the sword directory which contains locales.d QDir cachedSharedSwordDir; #endif +#ifdef Q_WS_MAC +QDir cachedSwordLocalesDir; +#endif + #ifdef Q_WS_WIN static const char* BIBLETIME = "Bibletime"; static const char* SWORD_DIR = "Sword"; #else +#ifdef Q_WS_MAC +static const char* BIBLETIME = "Library/Application Support/BibleTime"; +static const char* SWORD_DIR = "Library/Application Support/Sword"; +#else static const char* BIBLETIME = ".bibletime"; static const char* SWORD_DIR = ".sword"; #endif +#endif +static const char* SWORD_PATH = "SWORD_PATH"; +static const char* UNSET_SWORD_PATH = "SWORD_PATH="; } // anonymous namespace bool initDirectoryCache() { @@ -83,6 +95,22 @@ bool initDirectoryCache() { } #endif +#ifdef Q_WS_MAC + cachedSwordLocalesDir = wDir; // application sword dir for Windows only + if (!cachedSwordLocalesDir.cd("share/sword/locales.d") || !cachedSwordLocalesDir.isReadable()) { + qWarning() << "Cannot find sword locales directory relative to" << QCoreApplication::applicationDirPath(); + return false; + } +#endif + + cachedSwordPathDir = QDir(); + char* swordPath = getenv(SWORD_PATH); + if (swordPath != 0) { + cachedSwordPathDir = QDir(swordPath); + // We unset the SWORD_PATH so libsword finds paths correctly + putenv((char*)UNSET_SWORD_PATH); + } + cachedIconDir = wDir; // Icon dir if (!cachedIconDir.cd("share/bibletime/icons") || !cachedIconDir.isReadable()) { qWarning() << "Cannot find icon directory relative to" << QCoreApplication::applicationDirPath(); @@ -146,7 +174,7 @@ bool initDirectoryCache() { cachedUserBaseDir = cachedUserHomeDir; if (!cachedUserBaseDir.cd(BIBLETIME)) { - if (!cachedUserBaseDir.mkdir(BIBLETIME) || !cachedUserBaseDir.cd(BIBLETIME)) { + if (!cachedUserBaseDir.mkpath(BIBLETIME) || !cachedUserBaseDir.cd(BIBLETIME)) { qWarning() << "Could not create user setting directory."; return false; } @@ -154,7 +182,7 @@ bool initDirectoryCache() { cachedUserHomeSwordDir = cachedUserHomeDir; if (!cachedUserHomeSwordDir.cd(SWORD_DIR)) { - if (!cachedUserHomeSwordDir.mkdir(SWORD_DIR) || !cachedUserHomeSwordDir.cd(SWORD_DIR)) { + if (!cachedUserHomeSwordDir.mkpath(SWORD_DIR) || !cachedUserHomeSwordDir.cd(SWORD_DIR)) { qWarning() << "Could not create user home " << SWORD_DIR << " directory."; return false; } @@ -188,6 +216,7 @@ bool initDirectoryCache() { if (!cachedUserIndexDir.cd("indices")) { if (!cachedUserIndexDir.mkdir("indices") || !cachedUserIndexDir.cd("indices")) { qWarning() << "Could not create user indices directory."; + return false; } } @@ -195,6 +224,7 @@ bool initDirectoryCache() { if (!cachedUserDisplayTemplatesDir.cd("display-templates")) { if (!cachedUserDisplayTemplatesDir.mkdir("display-templates") || !cachedUserDisplayTemplatesDir.cd("display-templates")) { qWarning() << "Could not create user display templates directory."; + return false; } } @@ -288,113 +318,125 @@ QString convertDirSeparators(const QString& path) { } #ifdef Q_WS_WIN -QDir getApplicationSwordDir() { +const QDir &getApplicationSwordDir() { return cachedApplicationSwordDir; } -QDir getSharedSwordDir() { +const QDir &getSharedSwordDir() { return cachedSharedSwordDir; } #endif -QDir getIconDir() { +#ifdef Q_WS_MAC +const QDir &getSwordLocalesDir() { + return cachedSwordLocalesDir; +} +#endif + +const QDir &getSwordPathDir() { + return cachedSwordPathDir; +} + +const QDir &getIconDir() { return cachedIconDir; } -QDir getJavascriptDir() { +const QDir &getJavascriptDir() { return cachedJavascriptDir; } -QDir getLicenseDir() { +const QDir &getLicenseDir() { return cachedLicenseDir; } -QIcon getIcon(const QString &name) { +const QIcon &getIcon(const QString &name) { static QMap<QString, QIcon> iconCache; - //error if trying to use name directly... - QString name2(name); - QString plainName = name2.remove(".svg", Qt::CaseInsensitive); - if (iconCache.contains(plainName)) { - return iconCache.value(plainName); + static QIcon nullIcon; + + QString plainName = name; + if (plainName.endsWith(".svg", Qt::CaseInsensitive)) { + plainName.chop(4); + } + + QMap<QString, QIcon>::const_iterator i = iconCache.find(plainName); + if (i != iconCache.end()) { + return *i; } QString iconDir = getIconDir().canonicalPath(); QString iconFileName = iconDir + "/" + plainName + ".svg"; if (QFile(iconFileName).exists()) { - QIcon ic = QIcon(iconFileName); - iconCache.insert(plainName, ic); - return ic; + return *iconCache.insert(plainName, QIcon(iconFileName)); } else { iconFileName = iconDir + "/" + plainName + ".png"; if (QFile(iconFileName).exists()) { - QIcon ic = QIcon(iconFileName); - iconCache.insert(plainName, ic); - return ic; + return *iconCache.insert(plainName, QIcon(iconFileName)); } else { - qWarning() << "Cannot find icon file" << iconFileName << ", using default icon."; - iconFileName = iconDir + "/" + "/default.svg"; - if (QFile(iconFileName).exists()) { - return QIcon(iconDir + "/default.svg"); - } - else { - return QIcon(iconDir + "default.png"); + if (plainName != "default") { + qWarning() << "Cannot find icon file" << iconFileName + << ", using default icon."; + return getIcon("default"); + } else { + qWarning() << "Cannot find default icon" << iconFileName + << ", using null icon."; + return nullIcon; } } } } -QDir getPicsDir() { +const QDir &getPicsDir() { return cachedPicsDir; } -QDir getLocaleDir() { +const QDir &getLocaleDir() { return cachedLocaleDir; } -QDir getHandbookDir() { +const QDir &getHandbookDir() { return cachedHandbookDir; } -QDir getHowtoDir() { +const QDir &getHowtoDir() { return cachedHowtoDir; } -QDir getDisplayTemplatesDir() { +const QDir &getDisplayTemplatesDir() { return cachedDisplayTemplatesDir; } -QDir getUserBaseDir() { +const QDir &getUserBaseDir() { return cachedUserBaseDir; } -QDir getUserHomeDir() { +const QDir &getUserHomeDir() { return cachedUserHomeDir; } -QDir getUserHomeSwordDir() { +const QDir &getUserHomeSwordDir() { return cachedUserHomeSwordDir; } -QDir getUserHomeSwordModsDir() { +const QDir &getUserHomeSwordModsDir() { return cachedUserHomeSwordModsDir; } -QDir getUserSessionsDir() { +const QDir &getUserSessionsDir() { return cachedUserSessionsDir; } -QDir getUserCacheDir() { +const QDir &getUserCacheDir() { return cachedUserCacheDir; } -QDir getUserIndexDir() { +const QDir &getUserIndexDir() { return cachedUserIndexDir; } -QDir getUserDisplayTemplatesDir() { +const QDir &getUserDisplayTemplatesDir() { return cachedUserDisplayTemplatesDir; } |