summaryrefslogtreecommitdiff
path: root/src/util/directory.cpp
blob: 64eb624541685c83ed7fad16b7a54582ccf139f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2016 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "directory.h"

#include <memory>
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QLocale>
#include "btassert.h"
#ifdef Q_OS_WIN32
#include <windows.h>
#endif


namespace util {
namespace directory {

namespace {

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
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
std::unique_ptr<QDir> cachedSwordLocalesDir;
#endif

#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
#ifdef Q_OS_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";
} // anonymous namespace

bool initDirectoryCache() {
    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;
        }
    }

#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
    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";
        return false;
    }
    if (!cachedSharedSwordDir->cd(SWORD_DIR)) {
        if (!cachedSharedSwordDir->mkdir(SWORD_DIR) || !cachedSharedSwordDir->cd(SWORD_DIR)) {
            qWarning() << "Cannot find ALLUSERSPROFILE\\Application Data\\Sword";
            return false;
        }
    }
#endif
#endif

#ifdef Q_OS_MAC
    cachedSwordLocalesDir.reset(new QDir(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

#ifdef Q_OS_WINCE
    cachedSwordPathDir.reset(new QDir(wDir));
#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()) {
        qWarning() << "Cannot find icon directory relative to" << wDir.absolutePath();
        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();
        return false;
    }

    cachedLicenseDir.reset(new QDir(wDir));
    if (!cachedLicenseDir->cd("share/bibletime/license") || !cachedLicenseDir->isReadable()) {
        qWarning() << "Cannot find license directory relative to" << wDir.absolutePath();
        return false;
    }

    cachedPicsDir.reset(new QDir(wDir));
    if (!cachedPicsDir->cd("share/bibletime/pics") || !cachedPicsDir->isReadable()) {
        qWarning() << "Cannot find pics directory relative to" << wDir.absolutePath();
        return false;
    }
#endif

    cachedLocaleDir.reset(new QDir(wDir));
    if (!cachedLocaleDir->cd("share/bibletime/locale")) {
        qWarning() << "Cannot find locale directory relative to" << wDir.absolutePath();
        return false;
    }

    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)) {
            if (!cachedHandbookDir->cd("share/bibletime/docs/handbook/en/")) {
                qWarning() << "Cannot find handbook directory relative to" << wDir.absolutePath();
                return false;
            }
        }
    }

    cachedHowtoDir.reset(new QDir(wDir));
    if (!cachedHowtoDir->cd("share/bibletime/docs/howto/" + localeName)) {
        if (!cachedHowtoDir->cd("share/bibletime/docs/howto/" + langCode)) {
            if (!cachedHowtoDir->cd("share/bibletime/docs/howto/en/")) {
                qWarning() << "Cannot find handbook directory relative to" << wDir.absolutePath();
                return false;
            }
        }
    }
#endif

    cachedDisplayTemplatesDir.reset(new QDir(wDir)); //display templates dir
    if (!cachedDisplayTemplatesDir->cd("share/bibletime/display-templates/")) {
        qWarning() << "Cannot find display template directory relative to" << wDir.absolutePath();
        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;
        }
    }
#elif defined Q_OS_WIN32
#define BUFSIZE 4096
    wchar_t homeDir[BUFSIZE];
    GetEnvironmentVariable(TEXT("APPDATA"), homeDir, BUFSIZE);
    QString qHomeDir = QString::fromWCharArray(homeDir);
    cachedUserHomeDir.reset(new QDir(qHomeDir));
#else
    cachedUserHomeDir.reset(new QDir(qgetenv("HOME")));
#endif

    cachedUserBaseDir.reset(new QDir(*cachedUserHomeDir));
    if (!cachedUserBaseDir->cd(BIBLETIME)) {
        if (!cachedUserBaseDir->mkpath(BIBLETIME) || !cachedUserBaseDir->cd(BIBLETIME)) {
            qWarning() << "Could not create user settings directory relative to" << cachedUserHomeDir->absolutePath();
            return false;
        }
    }

    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")) {
        if (!cachedUserHomeSwordModsDir->mkdir("mods.d") || !cachedUserHomeSwordModsDir->cd("mods.d")) {
            qWarning() << "Could not create user home " << SWORD_DIR << " mods.d directory.";
            return false;
        }
    }

    cachedUserSessionsDir.reset(new QDir(*cachedUserBaseDir));
    if (!cachedUserSessionsDir->cd("sessions")) {
        if (!cachedUserSessionsDir->mkdir("sessions") || !cachedUserSessionsDir->cd("sessions")) {
            qWarning() << "Could not create user sessions directory.";
            return false;
        }
    }

    cachedUserCacheDir.reset(new QDir(*cachedUserBaseDir));
    if (!cachedUserCacheDir->cd("cache")) {
        if (!cachedUserCacheDir->mkdir("cache") || !cachedUserCacheDir->cd("cache")) {
            qWarning() << "Could not create user cache directory.";
            return false;
        }
    }

    cachedUserIndexDir.reset(new QDir(*cachedUserBaseDir));
    if (!cachedUserIndexDir->cd("indices")) {
        if (!cachedUserIndexDir->mkdir("indices") || !cachedUserIndexDir->cd("indices")) {
            qWarning() << "Could not create user indices directory.";
            return false;
        }
    }

    cachedUserDisplayTemplatesDir.reset(new QDir(*cachedUserBaseDir));
    if (!cachedUserDisplayTemplatesDir->cd("display-templates")) {
        if (!cachedUserDisplayTemplatesDir->mkdir("display-templates") || !cachedUserDisplayTemplatesDir->cd("display-templates")) {
            qWarning() << "Could not create user display templates directory.";
            return false;
        }
    }

    return true;
} // bool initDirectoryCache();

void removeRecursive(const QString &dir) {
    //Check for validity of argument
    if (dir.isEmpty()) return;
    QDir d(dir);
    if (!d.exists()) return;

    //remove all files in this dir
    d.setFilter( QDir::Files | QDir::Hidden | QDir::NoSymLinks );
    const QFileInfoList fileList = d.entryInfoList();
    for (QFileInfoList::const_iterator it_file = fileList.begin(); it_file != fileList.end(); ++it_file) {
        d.remove( it_file->fileName() );
    }

    //remove all subdirs recursively
    d.setFilter( QDir::Dirs | QDir::NoSymLinks );
    const QFileInfoList dirList = d.entryInfoList();
    for (QFileInfoList::const_iterator it_dir = dirList.begin(); it_dir != dirList.end(); ++it_dir) {
        if ( !it_dir->isDir() || it_dir->fileName() == "." || it_dir->fileName() == ".." ) {
            continue;
        }
        removeRecursive( it_dir->absoluteFilePath() );
    }
    d.rmdir(dir);
}

/** Returns the size of the directory including the size of all it's files and it's subdirs.
 */
size_t getDirSizeRecursive(QString const & dir) {
    //Check for validity of argument
    QDir d(dir);
    if (!d.exists())
        return 0u;

    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();
    }

    //Then add the sizes of all subdirectories
    d.setFilter(QDir::Dirs);
    const QFileInfoList dirInfoList = d.entryInfoList();
    for (QFileInfoList::const_iterator it_dir = dirInfoList.begin(); it_dir != dirInfoList.end(); ++it_dir) {
        if ( !it_dir->isDir() || it_dir->fileName() == "." || it_dir->fileName() == ".." ) {
            continue;
        }
        size += getDirSizeRecursive( it_dir->absoluteFilePath() );
    }
    return size;
}

QString convertDirSeparators(const QString& path) {
    QString result = path;
#ifdef Q_OS_WIN
    result.replace("/", "\\");
#else
    result.replace("\\", "/");
#endif
    return result;
}

#ifdef Q_OS_WIN
const QDir &getApplicationSwordDir() {
    return *cachedApplicationSwordDir;
}
#endif

#if defined Q_OS_WIN || defined Q_OS_ANDROID
const QDir &getSharedSwordDir() {
    return *cachedSharedSwordDir;
}

#endif

#ifdef Q_OS_MAC
const QDir &getSwordLocalesDir() {
    return *cachedSwordLocalesDir;
}
#endif

const QDir &getSwordPathDir() {
    return *cachedSwordPathDir;
}

const QDir &getIconDir() {
    return *cachedIconDir;
}

const QDir &getJavascriptDir() {
    return *cachedJavascriptDir;
}

const QDir &getLicenseDir() {
    return *cachedLicenseDir;
}

const QDir &getPicsDir() {
    return *cachedPicsDir;
}

const QDir &getLocaleDir() {
    return *cachedLocaleDir;
}

const QDir &getHandbookDir() {
    return *cachedHandbookDir;
}

const QDir &getHowtoDir() {
    return *cachedHowtoDir;
}

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;
}

const QDir &getUserHomeDir() {
    return *cachedUserHomeDir;
}

const QDir &getUserHomeSwordDir() {
    return *cachedUserHomeSwordDir;
}

const QDir &getUserHomeSwordModsDir() {
    return *cachedUserHomeSwordModsDir;
}

const QDir &getUserSessionsDir() {
    return *cachedUserSessionsDir;
}

const QDir &getUserCacheDir() {
    return *cachedUserCacheDir;
}

const QDir &getUserIndexDir() {
    return *cachedUserIndexDir;
}

const QDir &getUserDisplayTemplatesDir() {
    return *cachedUserDisplayTemplatesDir;
}

} // namespace directory
} // namespace util