diff options
author | Teus Benschop <teusjannette@gmail.com> | 2017-10-06 12:24:31 +0200 |
---|---|---|
committer | Teus Benschop <teusjannette@gmail.com> | 2017-10-06 12:24:31 +0200 |
commit | 90d2181239761f8950b95768d3b037843e9e8b50 (patch) | |
tree | 6cc667ab420cc04029de2de7e361d2305e214595 /src/util/directory.cpp | |
parent | 1ea03c0fce8066c1e22188447b4a6ca4dcef1201 (diff) |
New upstream version 2.11.0
Diffstat (limited to 'src/util/directory.cpp')
-rw-r--r-- | src/util/directory.cpp | 174 |
1 files changed, 116 insertions, 58 deletions
diff --git a/src/util/directory.cpp b/src/util/directory.cpp index 6ce80b4..53086ec 100644 --- a/src/util/directory.cpp +++ b/src/util/directory.cpp @@ -2,13 +2,14 @@ * * This file is part of BibleTime's source code, http://www.bibletime.info/. * -* Copyright 1999-2014 by the BibleTime developers. +* Copyright 1999-2016 by the BibleTime developers. * The BibleTime source code is licensed under the GNU General Public License version 2.0. * **********/ -#include "util/directory.h" +#include "directory.h" +#include <memory> #include <QCoreApplication> #include <QDebug> #include <QDir> @@ -16,6 +17,7 @@ #include <QFileInfo> #include <QFileInfoList> #include <QLocale> +#include "btassert.h" namespace util { @@ -23,33 +25,39 @@ namespace directory { namespace { -QScopedPointer<QDir> cachedIconDir; -QScopedPointer<QDir> cachedJavascriptDir; -QScopedPointer<QDir> cachedLicenseDir; -QScopedPointer<QDir> cachedPicsDir; -QScopedPointer<QDir> cachedLocaleDir; -QScopedPointer<QDir> cachedHandbookDir; -QScopedPointer<QDir> cachedHowtoDir; -QScopedPointer<QDir> cachedDisplayTemplatesDir; -QScopedPointer<QDir> cachedUserDisplayTemplatesDir; -QScopedPointer<QDir> cachedUserBaseDir; -QScopedPointer<QDir> cachedUserHomeDir; -QScopedPointer<QDir> cachedUserHomeSwordDir; -QScopedPointer<QDir> cachedUserHomeSwordModsDir; -QScopedPointer<QDir> cachedUserSessionsDir; -QScopedPointer<QDir> cachedUserCacheDir; -QScopedPointer<QDir> cachedUserIndexDir; -QScopedPointer<QDir> cachedSwordPathDir; +std::unique_ptr<QDir> cachedIconDir; +std::unique_ptr<QDir> cachedJavascriptDir; +std::unique_ptr<QDir> cachedLicenseDir; +std::unique_ptr<QDir> cachedPicsDir; +std::unique_ptr<QDir> cachedLocaleDir; +std::unique_ptr<QDir> cachedHandbookDir; +std::unique_ptr<QDir> cachedHowtoDir; +std::unique_ptr<QDir> cachedDisplayTemplatesDir; +std::unique_ptr<QDir> cachedQmlDir; +std::unique_ptr<QDir> cachedUserDisplayTemplatesDir; +std::unique_ptr<QDir> cachedUserBaseDir; +std::unique_ptr<QDir> cachedUserHomeDir; +std::unique_ptr<QDir> cachedUserHomeSwordDir; +std::unique_ptr<QDir> cachedUserHomeSwordModsDir; +std::unique_ptr<QDir> cachedUserSessionsDir; +std::unique_ptr<QDir> cachedUserCacheDir; +std::unique_ptr<QDir> cachedUserIndexDir; +std::unique_ptr<QDir> cachedSwordPathDir; #ifdef Q_OS_WIN -QScopedPointer<QDir> cachedApplicationSwordDir; // Only Windows installs the sword directory which contains locales.d -QScopedPointer<QDir> cachedSharedSwordDir; +std::unique_ptr<QDir> cachedApplicationSwordDir; // Only Windows installs the sword directory which contains locales.d +std::unique_ptr<QDir> cachedSharedSwordDir; #endif #ifdef Q_OS_MAC -QScopedPointer<QDir> cachedSwordLocalesDir; +std::unique_ptr<QDir> cachedSwordLocalesDir; #endif -#ifdef Q_OS_WIN +#ifdef Q_OS_ANDROID +std::unique_ptr<QDir> cachedSharedSwordDir; // Directory that AndBible uses +static const char AND_BIBLE[] = "/sdcard/Android/data/net.bible.android.activity/files"; +#endif + +#if defined Q_OS_WIN || defined Q_OS_SYMBIAN static const char BIBLETIME[] = "Bibletime"; static const char SWORD_DIR[] = "Sword"; #else @@ -65,22 +73,37 @@ static const char SWORD_PATH[] = "SWORD_PATH"; } // anonymous namespace bool initDirectoryCache() { - QDir wDir(QCoreApplication::applicationDirPath()); - wDir.makeAbsolute(); + QDir wDir(":/share/bibletime"); // check if resources would be read from qrc + if(wDir.exists()) + wDir = ":/"; + else { + wDir = QCoreApplication::applicationDirPath(); + wDir.makeAbsolute(); + if (!wDir.cdUp()) { // Installation prefix + qWarning() << "Cannot cd up from directory " << QCoreApplication::applicationDirPath(); + return false; + } + } - if (!wDir.cdUp()) { // Installation prefix - qWarning() << "Cannot cd up from directory " << QCoreApplication::applicationDirPath(); - return false; +#ifdef Q_OS_ANDROID + cachedSharedSwordDir.reset(new QDir()); + if (!cachedSharedSwordDir->cd(AND_BIBLE)) { + cachedSharedSwordDir->mkpath(AND_BIBLE); + cachedSharedSwordDir->cd(AND_BIBLE); } +#endif -#ifdef Q_OS_WIN +#ifdef Q_OS_WIN cachedApplicationSwordDir.reset(new QDir(wDir)); // application sword dir for Windows only +#if !defined BT_MINI && !defined BT_MOBILE if (!cachedApplicationSwordDir->cd("share/sword") || !cachedApplicationSwordDir->isReadable()) { qWarning() << "Cannot find sword directory relative to" << QCoreApplication::applicationDirPath(); return false; } +#endif +#if !defined Q_OS_WINCE && !defined BT_MOBILE && !defined Q_OS_WINRT cachedSharedSwordDir.reset(new QDir(qgetenv("ALLUSERSPROFILE"))); // sword dir for Windows only if (!cachedSharedSwordDir->cd("Application Data")) { qWarning() << "Cannot find ALLUSERSPROFILE\\Application Data"; @@ -93,6 +116,7 @@ bool initDirectoryCache() { } } #endif +#endif #ifdef Q_OS_MAC cachedSwordLocalesDir.reset(new QDir(wDir)); // application sword dir for Windows only @@ -102,13 +126,18 @@ bool initDirectoryCache() { } #endif +#ifdef Q_OS_WINCE cachedSwordPathDir.reset(new QDir(wDir)); - char* swordPath = qgetenv(SWORD_PATH).data(); - if (swordPath != 0) { - cachedSwordPathDir.reset(new QDir(swordPath)); +#elif defined (ANDROID) +#else + cachedSwordPathDir.reset(new QDir()); + auto swordPath(qgetenv(SWORD_PATH)); + if (swordPath.data()) { + cachedSwordPathDir.reset(new QDir(swordPath.data())); // We unset the SWORD_PATH so libsword finds paths correctly qputenv(SWORD_PATH, ""); } +#endif cachedIconDir.reset(new QDir(wDir)); // Icon dir if (!cachedIconDir->cd("share/bibletime/icons") || !cachedIconDir->isReadable()) { @@ -116,6 +145,7 @@ bool initDirectoryCache() { return false; } +#ifndef BT_MINI cachedJavascriptDir.reset(new QDir(wDir)); if (!cachedJavascriptDir->cd("share/bibletime/javascript") || !cachedJavascriptDir->isReadable()) { qWarning() << "Cannot find javascript directory relative to" << wDir.absolutePath(); @@ -133,6 +163,7 @@ bool initDirectoryCache() { qWarning() << "Cannot find pics directory relative to" << wDir.absolutePath(); return false; } +#endif cachedLocaleDir.reset(new QDir(wDir)); if (!cachedLocaleDir->cd("share/bibletime/locale")) { @@ -143,6 +174,7 @@ bool initDirectoryCache() { QString localeName(QLocale::system().name()); QString langCode(localeName.section('_', 0, 0)); +#ifndef BT_MINI cachedHandbookDir.reset(new QDir(wDir)); if (!cachedHandbookDir->cd("share/bibletime/docs/handbook/" + localeName)) { if (!cachedHandbookDir->cd("share/bibletime/docs/handbook/" + langCode)) { @@ -162,6 +194,7 @@ bool initDirectoryCache() { } } } +#endif cachedDisplayTemplatesDir.reset(new QDir(wDir)); //display templates dir if (!cachedDisplayTemplatesDir->cd("share/bibletime/display-templates/")) { @@ -169,7 +202,36 @@ bool initDirectoryCache() { return false; } +#ifdef BT_MOBILE + cachedQmlDir.reset(new QDir(wDir)); //qml files dir + if (!cachedQmlDir->cd("share/bibletime/qml/")) { + qWarning() << "Cannot find qml relative to" << wDir.absolutePath(); + return false; + } +#endif + +#ifdef Q_OS_WINRT + cachedUserHomeDir.reset(new QDir("")); +#elif defined (Q_OS_WIN) && !defined(Q_OS_WIN32) + cachedUserHomeDir.reset(new QDir(QCoreApplication::applicationDirPath())); +#elif defined(ANDROID) + cachedUserHomeDir.reset(new QDir(qgetenv("EXTERNAL_STORAGE"))); + if(!cachedUserHomeDir->exists() || !cachedUserHomeDir->isReadable()) + { + qWarning() << "No external storage found, use application home."; + cachedUserHomeDir->setPath(QDir::homePath()); + } +#elif defined Q_OS_SYMBIAN + cachedUserHomeDir.reset(new QDir(QCoreApplication::applicationDirPath()[0] + ":\\")); + if (!cachedUserHomeDir->cd("data")) { + if(!cachedUserHomeDir->mkpath("data") || !cachedUserHomeDir->cd("data")) { + qWarning() << "Could not create user home directory" << cachedUserHomeDir->absolutePath(); + return false; + } + } +#else cachedUserHomeDir.reset(new QDir(qgetenv("HOME"))); +#endif cachedUserBaseDir.reset(new QDir(*cachedUserHomeDir)); if (!cachedUserBaseDir->cd(BIBLETIME)) { @@ -180,12 +242,20 @@ bool initDirectoryCache() { } cachedUserHomeSwordDir.reset(new QDir(*cachedUserHomeDir)); +#if defined(Q_OS_WIN) && !defined(Q_OS_WIN32) +#else if (!cachedUserHomeSwordDir->cd(SWORD_DIR)) { if (!cachedUserHomeSwordDir->mkpath(SWORD_DIR) || !cachedUserHomeSwordDir->cd(SWORD_DIR)) { qWarning() << "Could not create user home " << SWORD_DIR << " directory."; return false; } } +#endif + +#if defined Q_OS_ANDROID || defined Q_OS_SYMBIAN + // help for SWMgr to find the right place + qputenv(SWORD_PATH, cachedUserHomeSwordDir->absolutePath().toLocal8Bit()); +#endif cachedUserHomeSwordModsDir.reset(new QDir(*cachedUserHomeSwordDir)); if (!cachedUserHomeSwordModsDir->cd("mods.d")) { @@ -257,17 +327,19 @@ void removeRecursive(const QString &dir) { /** Returns the size of the directory including the size of all it's files and it's subdirs. */ -unsigned long getDirSizeRecursive(const QString &dir) { +size_t getDirSizeRecursive(QString const & dir) { //Check for validity of argument QDir d(dir); - if (!d.exists()) return 0; + if (!d.exists()) + return 0u; - unsigned long size = 0; + size_t size = 0u; //First get the size of all files int this folder d.setFilter(QDir::Files); const QFileInfoList infoList = d.entryInfoList(); for (QFileInfoList::const_iterator it = infoList.begin(); it != infoList.end(); ++it) { + BT_ASSERT(it->size() > 0); size += it->size(); } @@ -283,29 +355,6 @@ unsigned long getDirSizeRecursive(const QString &dir) { return size; } -/**Recursively copies a directory, overwriting existing files*/ -void copyRecursive(const QString &src, const QString &dest) { - QDir srcDir(src); - QDir destDir(dest); - //Copy files - QStringList files = srcDir.entryList(QDir::Files); - for (QStringList::iterator it = files.begin(); it != files.end(); ++it) { - QFile currFile(src + "/" + *it); - QString newFileLoc = dest + "/" + *it; - QFile newFile(newFileLoc); - newFile.remove(); - currFile.copy(newFileLoc); - } - QStringList dirs = srcDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot); - for (QStringList::iterator it = dirs.begin(); it != dirs.end(); ++it) { - QString temp = *it; - if (!destDir.cd(*it)) { - destDir.mkdir(*it); - } - copyRecursive(src + "/" + *it, dest + "/" + *it); - } -} - QString convertDirSeparators(const QString& path) { QString result = path; #ifdef Q_OS_WIN @@ -320,7 +369,9 @@ QString convertDirSeparators(const QString& path) { const QDir &getApplicationSwordDir() { return *cachedApplicationSwordDir; } +#endif +#if defined Q_OS_WIN || defined Q_OS_ANDROID const QDir &getSharedSwordDir() { return *cachedSharedSwordDir; } @@ -369,6 +420,13 @@ const QDir &getDisplayTemplatesDir() { return *cachedDisplayTemplatesDir; } +const QDir &getQmlDir() { +#ifndef BT_MOBILE + BT_ASSERT(false && "Qml files currently required for BibleTime Mobile frontend only."); +#endif + return *cachedQmlDir; +} + const QDir &getUserBaseDir() { return *cachedUserBaseDir; } |