17 #include <QCoreApplication>
21 #include <QFileInfoList>
24 #include <QStringList>
46 std::optional<QDir> cachedApplicationSwordDir;
47 std::optional<QDir> cachedSharedSwordDir;
51 std::optional<QDir> cachedSwordLocalesDir;
55 QString
const BIBLETIME = QStringLiteral(
"Bibletime");
56 QString
const SWORD_DIR = QStringLiteral(
"Sword");
57 #elif defined(Q_OS_MAC)
59 QStringLiteral(
"Library/Application Support/BibleTime");
60 QString
const SWORD_DIR = QStringLiteral(
"Library/Application Support/Sword");
62 QString
const BIBLETIME = QStringLiteral(
".bibletime");
68 QDir wDir(QCoreApplication::applicationDirPath());
71 qWarning() <<
"Unable to use prefix one level up from " << wDir.path();
78 cachedApplicationSwordDir.emplace(wDir);
79 if (!cachedApplicationSwordDir->cd(QStringLiteral(
"share/sword"))) {
80 qWarning() <<
"Cannot find sword directory relative to"
81 << QCoreApplication::applicationDirPath();
85 cachedSharedSwordDir.emplace(qEnvironmentVariable(
"ProgramData"));
86 if (!cachedSharedSwordDir->cd(
SWORD_DIR)) {
87 if (!cachedSharedSwordDir->mkdir(
SWORD_DIR)
90 qWarning() <<
"Cannot find " << programDataDir <<
" \\Sword";
98 cachedSwordLocalesDir.emplace(wDir);
99 if (!cachedSwordLocalesDir->cd(QStringLiteral(
"share/sword/locales.d"))) {
100 qWarning() <<
"Cannot find sword locales directory relative to"
101 << QCoreApplication::applicationDirPath();
107 ::qunsetenv(
"SWORD_PATH");
110 if (!
cachedPicsDir->cd(QStringLiteral(
"share/bibletime/pics"))) {
111 qWarning() <<
"Cannot find pics directory relative to"
112 << wDir.absolutePath();
118 qWarning() <<
"Cannot find locale directory relative to"
119 << wDir.absolutePath();
125 QStringLiteral(
"share/bibletime/display-templates/")))
127 qWarning() <<
"Cannot find display template directory relative to"
128 << wDir.absolutePath();
134 #elif defined (Q_OS_WIN) && !defined(Q_OS_WIN32)
136 #elif defined Q_OS_WIN32
147 qWarning() <<
"Could not create user settings directory relative to"
154 #if !defined(Q_OS_WIN) || defined(Q_OS_WIN32)
159 qWarning() <<
"Could not create user home " <<
SWORD_DIR
168 static auto const modsDir = QStringLiteral(
"mods.d");
169 if (!userHomeSwordModsDir.cd(modsDir)) {
170 if (!userHomeSwordModsDir.mkdir(modsDir)
171 || !userHomeSwordModsDir.cd(modsDir))
173 qWarning() <<
"Could not create user home " <<
SWORD_DIR
174 <<
" mods.d directory.";
182 static auto const cacheDir = QStringLiteral(
"cache");
187 qWarning() <<
"Could not create user cache directory.";
195 static auto const indicesDir = QStringLiteral(
"indices");
200 qWarning() <<
"Could not create user indices directory.";
208 static auto const displayTemplatesDir =
209 QStringLiteral(
"display-templates");
214 qWarning() <<
"Could not create user display templates "
235 d.setFilter(QDir::Files);
236 for (
auto const & fileInfo : d.entryInfoList()) {
238 size += fileInfo.size();
242 d.setFilter(QDir::Dirs);
243 for (
auto const & dirInfo : d.entryInfoList())
245 && dirInfo.fileName() != QStringLiteral(
".")
246 && dirInfo.fileName() != QStringLiteral(
".."))
251 #if defined(Q_OS_WIN)
252 const QDir &getApplicationSwordDir() {
253 return *cachedApplicationSwordDir;
256 const QDir &getSharedSwordDir() {
257 return *cachedSharedSwordDir;
259 #elif defined(Q_OS_MACOS)
260 const QDir &getSwordLocalesDir() {
261 return *cachedSwordLocalesDir;
266 static const QDir cachedIconDir(
268 QStringLiteral(
"share/bibletime/icons")));
269 return cachedIconDir;
273 static auto const cachedLicensePath(
275 QStringLiteral(
"share/bibletime/license/LICENSE")));
276 return cachedLicensePath;
290 static QDir
const cachedDocDir(
cachedPrefix->filePath(BT_RUNTIME_DOCDIR));
292 if (
auto dir = cachedDocDir; dir.cd(docName)) {
293 QStringList tryLanguages;
294 tryLanguages.append(QLocale().name());
296 auto const & localeName = tryLanguages.back();
297 if (localeName.contains(
'_'))
298 tryLanguages.append(localeName.section(
'_', 0, 0));
300 tryLanguages.append(QStringLiteral(
"en"));
302 [&dir,&tryLanguages](QString
const & type,
303 QString
const & filename) -> QString
306 for (
auto const & tryLanguage : tryLanguages) {
307 if (dir.cd(tryLanguage)) {
308 if (dir.exists(filename))
309 return dir.absoluteFilePath(filename);
317 r = tryDoc(QStringLiteral(
"html"), QStringLiteral(
"index.html"));
319 r = tryDoc(QStringLiteral(
"pdf"), docName + QStringLiteral(
".pdf"));
QStringList r(content.left(bodyIndex))
std::optional< QDir > cachedUserIndexDir
std::optional< QDir > cachedPrefix
std::optional< QDir > cachedUserHomeSwordDir
std::optional< QDir > cachedPicsDir
QString getDocFile(QString docName)
std::optional< QDir > cachedUserBaseDir
std::optional< QDir > cachedUserHomeDir
std::optional< QDir > cachedUserDisplayTemplatesDir
std::optional< QDir > cachedDisplayTemplatesDir
std::optional< QDir > cachedUserCacheDir
std::optional< QDir > cachedLocaleDir
const QDir & getLocaleDir()
const QDir & getUserCacheDir()
const QDir & getUserHomeSwordDir()
QString const & getLicensePath()
QDir const & getIconDir()
const QDir & getDisplayTemplatesDir()
const QDir & getUserHomeDir()
const QDir & getUserIndexDir()
::qint64 getDirSizeRecursive(QString const &dir)
bool initDirectoryCache()
Initializes the directory cache and returns whether it was successful.
const QDir & getUserBaseDir()
const QDir & getUserDisplayTemplatesDir()
const QDir & getPicsDir()