summaryrefslogtreecommitdiff
path: root/src/backend/managers/cswordbackend.cpp
blob: b5f4bff5900140e7a2bc3584c1fb7cb27f5c5700 (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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2011 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "backend/managers/cswordbackend.h"

#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QSet>
#include <QString>
#include <QTextCodec>
#include "backend/config/cbtconfig.h"
#include "backend/drivers/cswordbiblemoduleinfo.h"
#include "backend/drivers/cswordbookmoduleinfo.h"
#include "backend/drivers/cswordcommentarymoduleinfo.h"
#include "backend/drivers/cswordlexiconmoduleinfo.h"
#include "backend/filters/thmltoplain.h"
#include "backend/filters/osismorphsegmentation.h"
#include "btglobal.h"
#include "util/directory.h"

// Sword includes:
#include <encfiltmgr.h>
#include <filemgr.h>
#include <rtfhtml.h>
#include <swdisp.h>
#include <swfiltermgr.h>
#include <swfilter.h>
#include <utilstr.h>


using namespace Rendering;

CSwordBackend *CSwordBackend::m_instance = 0;

CSwordBackend::CSwordBackend()
        : sword::SWMgr(0, 0, false,
                       new sword::EncodingFilterMgr(sword::ENC_UTF8), true),
          m_dataModel(this)
{
    filterInit();
}

CSwordBackend::CSwordBackend(const QString& path, const bool augmentHome)
        : sword::SWMgr(!path.isEmpty() ? path.toLocal8Bit().constData() : 0, false, new sword::EncodingFilterMgr( sword::ENC_UTF8 ), false, augmentHome) { // don't allow module renaming, because we load from a path
    filterInit();
}

CSwordBackend::~CSwordBackend() {
    shutdownModules();
}

void CSwordBackend::filterInit() {
    //HACK: replace Sword's OSISMorphSegmentation filter, seems to be buggy, ours works
    if (sword::SWOptionFilter* filter = optionFilters["OSISMorphSegmentation"]) {
        cleanupFilters.remove(filter);
        optionFilters.erase("OSISMorphSegmentation");
        delete filter;
    }
    sword::SWOptionFilter *tmpFilter = new Filters::OSISMorphSegmentation();
    optionFilters.insert(sword::OptionFilterMap::value_type("OSISMorphSegmentation", tmpFilter));
    cleanupFilters.push_back(tmpFilter);

    //HACK: replace Sword's ThML strip filter with our own version
    //remove this hack as soon as Sword is fixed
    cleanupFilters.remove(thmlplain);
    delete thmlplain;
    thmlplain = new Filters::ThmlToPlain();
    cleanupFilters.push_back(thmlplain);
}

QList<CSwordModuleInfo*> CSwordBackend::takeModulesFromList(const QStringList &names) {
    QList<CSwordModuleInfo*> list;
    Q_FOREACH (const QString &name, names) {
        CSwordModuleInfo* mInfo = findModuleByName(name);
        if (mInfo) {
            m_dataModel.removeModule(mInfo);
            list.append(mInfo);
        }
    }
    if (!list.isEmpty())
        emit sigSwordSetupChanged(RemovedModules);
    return list;
}

QList<CSwordModuleInfo*> CSwordBackend::getPointerList(const QStringList &names) const {
    QList<CSwordModuleInfo*> list;
    Q_FOREACH (const QString &name, names) {
        CSwordModuleInfo *mInfo = findModuleByName(name);
        if (mInfo) {
            list.append(mInfo);
        }
    }
    return list;
}

QList<const CSwordModuleInfo*> CSwordBackend::getConstPointerList(
        const QStringList &names) const
{
    QList<const CSwordModuleInfo*> list;
    Q_FOREACH (const QString &name, names) {
        const CSwordModuleInfo *mInfo = findModuleByName(name);
        if (mInfo) {
            list.append(mInfo);
        }
    }
    return list;
}


/** Initializes the Sword modules. */
CSwordBackend::LoadError CSwordBackend::initModules(SetupChangedReason reason) {
    //  qWarning("globalSwordConfigPath is %s", globalConfPath);
    LoadError ret = NoError;

    shutdownModules(); //remove previous modules
    m_dataModel.clear();

    sword::ModMap::iterator end = Modules.end();
    ret = LoadError( Load() );

    for (sword::ModMap::iterator it = Modules.begin(); it != end; it++) {
        sword::SWModule* const curMod = (*it).second;
        CSwordModuleInfo* newModule = 0;

        if (!strcmp(curMod->Type(), "Biblical Texts")) {
            newModule = new CSwordBibleModuleInfo(curMod, this);
            newModule->module()->setDisplay(&m_chapterDisplay);
        }
        else if (!strcmp(curMod->Type(), "Commentaries")) {
            newModule = new CSwordCommentaryModuleInfo(curMod, this);
            newModule->module()->setDisplay(&m_entryDisplay);
        }
        else if (!strcmp(curMod->Type(), "Lexicons / Dictionaries")) {
            newModule = new CSwordLexiconModuleInfo(curMod, this);
            newModule->module()->setDisplay(&m_entryDisplay);
        }
        else if (!strcmp(curMod->Type(), "Generic Books")) {
            newModule = new CSwordBookModuleInfo(curMod, this);
            newModule->module()->setDisplay(&m_bookDisplay);
        }

        if (newModule) {
            //Append the new modules to our list, but only if it's supported
            //The constructor of CSwordModuleInfo prints a warning on stdout
            if (!newModule->hasVersion() || (newModule->minimumSwordVersion() <= sword::SWVersion::currentVersion)) {
                m_dataModel.addModule(newModule);
            }
            else {
                delete newModule;
            }
        }
    }

    Q_FOREACH(CSwordModuleInfo* mod, m_dataModel.moduleList()) {
        //unlock modules if keys are present
        if ( mod->isEncrypted() ) {
            const QString unlockKey = CBTConfig::getModuleEncryptionKey( mod->name() );
            if (!unlockKey.isNull()) {
                setCipherKey( mod->name().toUtf8().constData(), unlockKey.toUtf8().constData() );
            }
        }
    }

    emit sigSwordSetupChanged(reason);
    return ret;
}

void CSwordBackend::AddRenderFilters(sword::SWModule *module, sword::ConfigEntMap &section) {
    sword::SWBuf moduleDriver;
    sword::SWBuf sourceformat;
    sword::ConfigEntMap::iterator entry;
    bool noDriver = true;

    sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (sword::SWBuf) "";
    moduleDriver = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (sword::SWBuf) "";

    if (sourceformat == "OSIS") {
        module->AddRenderFilter(&m_osisFilter);
        noDriver = false;
    }
    else if (sourceformat == "ThML") {
        module->AddRenderFilter(&m_thmlFilter);
        noDriver = false;
    }
    else if (sourceformat == "TEI") {
        module->AddRenderFilter(&m_teiFilter);
        noDriver = false;
    }
    else if (sourceformat == "GBF") {
        module->AddRenderFilter(&m_gbfFilter);
        noDriver = false;
    }
    else if (sourceformat == "PLAIN") {
        module->AddRenderFilter(&m_plainFilter);
        noDriver = false;
    }

    if (noDriver) { //no driver found
        if ( (moduleDriver == "RawCom") || (moduleDriver == "RawLD") ) {
            module->AddRenderFilter(&m_plainFilter);
            noDriver = false;
        }
    }
}

/** This function deinitializes the modules and deletes them. */
bool CSwordBackend::shutdownModules() {
    m_dataModel.clear(true);
    //BT  mods are deleted now, delete Sword mods, too.
    DeleteMods();

    /* Cipher filters must be handled specially, because SWMgr creates them,
     * stores them in cipherFilters and cleanupFilters and attaches them to locked
     * modules. If these modules are removed, the filters need to be removed as well,
     * so that they are re-created for the new module objects.
     */
    sword::FilterMap::iterator cipher_it;
    for (cipher_it = cipherFilters.begin(); cipher_it != cipherFilters.end(); cipher_it++) {
        //Delete the Filter and remove it from the cleanup list
        cleanupFilters.remove(cipher_it->second);
        delete cipher_it->second;
    }
    cipherFilters.clear();

    return true;
}

void CSwordBackend::setOption( const CSwordModuleInfo::FilterTypes type, const int state ) {
    sword::SWBuf value;

    switch (type) {

        case CSwordModuleInfo::textualVariants:

            if (state == 0) {
                value = "Primary Reading";
            }
            else if (state == 1) {
                value = "Secondary Reading";
            }
            else {
                value = "All Readings";
            }

            break;

        default:
            value = state ? "On" : "Off";
            break;
    };

    if (value.length())
        setGlobalOption(optionName(type).toUtf8().constData(), value.c_str());
}

void CSwordBackend::setFilterOptions(const FilterOptions &options) {
    setOption( CSwordModuleInfo::footnotes,      options.footnotes );
    setOption( CSwordModuleInfo::strongNumbers,    options.strongNumbers );
    setOption( CSwordModuleInfo::headings,       options.headings );
    setOption( CSwordModuleInfo::morphTags,      options.morphTags );
    setOption( CSwordModuleInfo::lemmas,        options.lemmas );
    setOption( CSwordModuleInfo::hebrewPoints,     options.hebrewPoints );
    setOption( CSwordModuleInfo::hebrewCantillation,  options.hebrewCantillation );
    setOption( CSwordModuleInfo::greekAccents,     options.greekAccents );
    setOption( CSwordModuleInfo::redLetterWords,   options.redLetterWords );
    setOption( CSwordModuleInfo::textualVariants,   options.textualVariants );
    setOption( CSwordModuleInfo::morphSegmentation,  options.morphSegmentation );
    //  setOption( CSwordModuleInfo::transliteration,   options.transliteration );
    setOption( CSwordModuleInfo::scriptureReferences, options.scriptureReferences);
}

/** This function searches for a module with the specified description */
CSwordModuleInfo* CSwordBackend::findModuleByDescription(const QString &description) const {
    Q_FOREACH (CSwordModuleInfo *mod, m_dataModel.moduleList()) {
        if (mod->config(CSwordModuleInfo::Description) == description) return mod;
    }
    return 0;
}

/** This function searches for a module with the specified name */
CSwordModuleInfo* CSwordBackend::findModuleByName(const QString &name) const {
    Q_FOREACH (CSwordModuleInfo *mod, m_dataModel.moduleList()) {
        if (mod->name() == name) return mod;
    }
    return 0;
}

CSwordModuleInfo* CSwordBackend::findSwordModuleByPointer(const sword::SWModule * const swmodule) const {
    Q_FOREACH (CSwordModuleInfo *mod, m_dataModel.moduleList()) {
        if (mod->module() == swmodule) return mod;
    }
    return 0;
}

/** Returns the text used for the option given as parameter. */
const QString CSwordBackend::optionName( const CSwordModuleInfo::FilterTypes option ) {
    switch (option) {
        case CSwordModuleInfo::footnotes:
            return QString("Footnotes");
        case CSwordModuleInfo::strongNumbers:
            return QString("Strong's Numbers");
        case CSwordModuleInfo::headings:
            return QString("Headings");
        case CSwordModuleInfo::morphTags:
            return QString("Morphological Tags");
        case CSwordModuleInfo::lemmas:
            return QString("Lemmas");
        case CSwordModuleInfo::hebrewPoints:
            return QString("Hebrew Vowel Points");
        case CSwordModuleInfo::hebrewCantillation:
            return QString("Hebrew Cantillation");
        case CSwordModuleInfo::greekAccents:
            return QString("Greek Accents");
        case CSwordModuleInfo::redLetterWords:
            return QString("Words of Christ in Red");
        case CSwordModuleInfo::textualVariants:
            return QString("Textual Variants");
        case CSwordModuleInfo::scriptureReferences:
            return QString("Cross-references");
        case CSwordModuleInfo::morphSegmentation:
            return QString("Morph Segmentation");
    }
    return QString::null;
}

/** Returns the translated name of the option given as parameter. */
const QString CSwordBackend::translatedOptionName(const CSwordModuleInfo::FilterTypes option) {
    switch (option) {
        case CSwordModuleInfo::footnotes:
            return QObject::tr("Footnotes");
        case CSwordModuleInfo::strongNumbers:
            return QObject::tr("Strong's numbers");
        case CSwordModuleInfo::headings:
            return QObject::tr("Headings");
        case CSwordModuleInfo::morphTags:
            return QObject::tr("Morphological tags");
        case CSwordModuleInfo::lemmas:
            return QObject::tr("Lemmas");
        case CSwordModuleInfo::hebrewPoints:
            return QObject::tr("Hebrew vowel points");
        case CSwordModuleInfo::hebrewCantillation:
            return QObject::tr("Hebrew cantillation marks");
        case CSwordModuleInfo::greekAccents:
            return QObject::tr("Greek accents");
        case CSwordModuleInfo::redLetterWords:
            return QObject::tr("Red letter words");
        case CSwordModuleInfo::textualVariants:
            return QObject::tr("Textual variants");
        case CSwordModuleInfo::scriptureReferences:
            return QObject::tr("Scripture cross-references");
        case CSwordModuleInfo::morphSegmentation:
            return QObject::tr("Morph segmentation");
    }
    return QString::null;
}


const QString CSwordBackend::configOptionName( const CSwordModuleInfo::FilterTypes option ) {
    switch (option) {
        case CSwordModuleInfo::footnotes:
            return QString("Footnotes");
        case CSwordModuleInfo::strongNumbers:
            return QString("Strongs");
        case CSwordModuleInfo::headings:
            return QString("Headings");
        case CSwordModuleInfo::morphTags:
            return QString("Morph");
        case CSwordModuleInfo::lemmas:
            return QString("Lemma");
        case CSwordModuleInfo::hebrewPoints:
            return QString("HebrewPoints");
        case CSwordModuleInfo::hebrewCantillation:
            return QString("Cantillation");
        case CSwordModuleInfo::greekAccents:
            return QString("GreekAccents");
        case CSwordModuleInfo::redLetterWords:
            return QString("RedLetterWords");
        case CSwordModuleInfo::textualVariants:
            return QString("Variants");
        case CSwordModuleInfo::scriptureReferences:
            return QString("Scripref");
        case CSwordModuleInfo::morphSegmentation:
            return QString("MorphSegmentation");
    }
    return QString::null;
}

const QString CSwordBackend::booknameLanguage( const QString& language ) {
    if (!language.isEmpty()) {
        sword::LocaleMgr::getSystemLocaleMgr()->setDefaultLocaleName( language.toUtf8().constData() );

        //refresh the locale of all Bible and commentary modules!
        //use what sword returns, language may be different
        QString newLocaleName( sword::LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName()  );

        Q_FOREACH (CSwordModuleInfo *mod, m_dataModel.moduleList()) {
            if ( (mod->type() == CSwordModuleInfo::Bible) || (mod->type() == CSwordModuleInfo::Commentary) ) {
                //Create a new key, it will get the default bookname language
                ((sword::VerseKey*)(mod->module()->getKey()))->setLocale( newLocaleName.toUtf8().constData() );
            }
        }

    }
    return QString( sword::LocaleMgr::getSystemLocaleMgr()->getDefaultLocaleName() );
}


/** Reload all Sword modules. */
void CSwordBackend::reloadModules(SetupChangedReason reason) {
    shutdownModules();

    //delete Sword's config to make Sword reload it!

    if (myconfig) { // force reload on config object because we may have changed the paths
        delete myconfig;
        config = myconfig = 0;
        // we need to call findConfig to make sure that augPaths are reloaded
        findConfig(&configType, &prefixPath, &configPath, &augPaths, &sysConfig);
        // now re-read module configuration files
        loadConfigDir(configPath);
    }
    else if (config) {
        config->Load();
    }

    initModules(reason);
}

// Get one or more shared sword config (sword.conf) files
QStringList CSwordBackend::getSharedSwordConfigFiles() const {
    QStringList configPath;
#ifdef Q_WS_WIN
    //  %ALLUSERSPROFILE%\Sword\sword.conf
    QString tmp = util::directory::getSharedSwordDir().filePath("sword.conf");
    QString globalPath = util::directory::convertDirSeparators(QString(getenv("SWORD_PATH")));
    configPath << globalPath.append("/Sword/sword.conf");
#else
    // /etc/sword.conf, /usr/local/etc/sword.conf
    configPath = QString(globalConfPath).split(":");
#endif
    return configPath;
}

// Get the private sword directory
QString CSwordBackend::getPrivateSwordConfigPath() const {
    return util::directory::getUserHomeSwordDir().absolutePath();
}

QString CSwordBackend::getPrivateSwordConfigFile() const {
    QString file(getPrivateSwordConfigPath() + "/sword.conf");
    return util::directory::convertDirSeparators(file);
}

// Return a list of used Sword dirs. Useful for the installer.
QStringList CSwordBackend::swordDirList() const {
    namespace DU = util::directory;
    typedef QStringList::const_iterator SLCI;
    typedef sword::ConfigEntMap::const_iterator CEMCI;

    // Get the set of sword directories that could contain modules:
    QSet<QString> swordDirSet;
    QStringList configs;

    if (QFile(getPrivateSwordConfigFile()).exists()) {
        // Use the private sword.conf file:
        configs << getPrivateSwordConfigFile();
    }
    else {
        /*
          Did not find private sword.conf, will use shared sword.conf files to
          build the private one. Once the private sword.conf exist, the shared
          ones will not be searched again.
        */
        configs = getSharedSwordConfigFiles();

#ifdef Q_WS_WIN
        /*
          On Windows, add the shared sword directory to the set so the new
          private sword.conf will have it. The user could decide to delete this
          shared path and it will not automatically come back.
        */
        swordDirSet << DU::convertDirSeparators(QString(getenv("SWORD_PATH")));
#endif
    }

    // Search the sword.conf file(s) for sword directories that could contain modules
    for (SLCI it(configs.begin()); it != configs.end(); it++) {
        if (!QFileInfo(*it).exists()) {
            continue;
        }

        /*
          Get all DataPath and AugmentPath entries from the config file and add
          them to the list:
        */
        sword::SWConfig conf((*it).toUtf8().constData());
        swordDirSet << QDir(QTextCodec::codecForLocale()->toUnicode(conf["Install"]["DataPath"].c_str())).absolutePath();

        sword::ConfigEntMap group(conf["Install"]);
        const sword::ConfigEntMap::iterator start(group.equal_range("AugmentPath").first);
        const sword::ConfigEntMap::iterator end(group.equal_range("AugmentPath").second);

        for (CEMCI it(start); it != end; it++) {
            QDir(QTextCodec::codecForLocale()->toUnicode(it->second.c_str())).absolutePath();
            // Added augment path:
            swordDirSet << QDir(QTextCodec::codecForLocale()->toUnicode(it->second.c_str())).absolutePath();
        }
    }

    // Add the private sword path to the set if not there already:
    swordDirSet << getPrivateSwordConfigPath();

    return swordDirSet.values();
}

void CSwordBackend::deleteOrphanedIndices() {
    QDir dir(CSwordModuleInfo::getGlobalBaseIndexLocation());
    dir.setFilter(QDir::Dirs);
    CSwordModuleInfo* module;

    for (unsigned int i = 0; i < dir.count(); i++) {
        if (dir[i] != "." && dir[i] != "..") {
            if ( (module = this->findModuleByName(dir[i])) ) { //mod exists
                if (!module->hasIndex()) { //index files found, but wrong version etc.
                    qDebug() << "deleting outdated index for module" << dir[i];
                    CSwordModuleInfo::deleteIndexForModule( dir[i] );
                }
            }
            else { //no module exists
                if (CBTConfig::get( CBTConfig::autoDeleteOrphanedIndices ) ) {
                    qDebug() << "deleting orphaned index in directory" << dir[i];
                    CSwordModuleInfo::deleteIndexForModule( dir[i] );
                }
            }
        }
    }
}