summaryrefslogtreecommitdiff
path: root/src/backend/drivers/cswordlexiconmoduleinfo.cpp
blob: c4a04deefc89cf2d2f84ff433ddb3069b78babcc (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "backend/drivers/cswordlexiconmoduleinfo.h"

#include <algorithm>
#include <QFile>
#include <QDataStream>
#include <QTextCodec>
#include <QDebug>

#include "util/directory.h"

// Sword includes:
#include <swmodule.h>


//Change it once the format changed to make all systems rebuild their caches
#define CACHE_FORMAT "3"

CSwordLexiconModuleInfo::CSwordLexiconModuleInfo( sword::SWModule* module, CSwordBackend* const backend ) : CSwordModuleInfo(module, backend) {
    m_entryList = 0;
}

CSwordLexiconModuleInfo::CSwordLexiconModuleInfo( const CSwordLexiconModuleInfo& m ) : CSwordModuleInfo(m) {
    delete m_entryList;
    m_entryList = 0;

    if (m.m_entryList) {
        m_entryList = new QStringList();
        *m_entryList = *m.m_entryList;//copy list items
    }
}

CSwordLexiconModuleInfo::~CSwordLexiconModuleInfo() {
    delete m_entryList;
    m_entryList = 0;
}

/** Returns the entries of the module. */
QStringList* CSwordLexiconModuleInfo::entries() {
    namespace DU = util::directory;

    if (!module()) {
        return 0;
    }

    if (m_entryList) return m_entryList;

    m_entryList = new QStringList();

    //Check for buggy modules! They will not be loaded any more.
    if ( name() == QString("ZhEnglish")) {
        qWarning() << "Module ZhEnglish is buggy and will not be loaded.";
        return m_entryList;
    }
    if ( name() == QString("EReo_en")) {
        qWarning() << "Module EReo_en is buggy and will not be loaded.";
        return m_entryList;
    }

    QString dir(DU::getUserCacheDir().absolutePath());
    QFile f1( QString(dir).append("/").append(name()));

    /*
     * Try the module's cache
     */
    if ( f1.open( QIODevice::ReadOnly ) ) {
        QDataStream s( &f1 );
        QString ModuleVersion, CacheVersion, QDataStreamVersion;
        s >> ModuleVersion;
        s >> CacheVersion;
        s >> QDataStreamVersion;

        qDebug() << "Lexicon cache metadata"
        << "Name" << name()
        << "ModuleVersion" << ModuleVersion
        << "CacheVersion" << CacheVersion
        << "QDataStreamVersion" << QDataStreamVersion;

        // Check if cache is valid
        if (ModuleVersion == config(CSwordModuleInfo::ModuleVersion)
                && CacheVersion == CACHE_FORMAT
                && QDataStreamVersion == QString::number(s.version())) {
            s >> *m_entryList;

            f1.close();
            qDebug() << "Read" << m_entryList->count() << "entries from lexicon cache for module" << name();
            return m_entryList;
        }

        f1.close();
    }

    /*
     * Ok, no cache or invalid.
     */
    qDebug() << "Read all entries of lexicon" << name();

    sword::SWModule* my_module = module();
    my_module->setSkipConsecutiveLinks(true);
    (*my_module) = sword::TOP;
    snap(); //snap to top entry

    do {
        if ( isUnicode() ) {
            m_entryList->append(QString::fromUtf8(my_module->KeyText()));
        }
        else {
            //for latin1 modules use fromLatin1 because of speed
            QTextCodec* codec = QTextCodec::codecForName("Windows-1252");
            m_entryList->append(codec->toUnicode(my_module->KeyText()));
        }

        (*my_module)++;
    }
    while ( !my_module->Error() );

    (*my_module) = sword::TOP; //back to the first entry

    my_module->setSkipConsecutiveLinks(false);

    if (m_entryList->count()) {
        m_entryList->first().simplified();

        if (m_entryList->first().trimmed().isEmpty()) {
            m_entryList->erase( m_entryList->begin() );
        }
    }

    qDebug() << "Writing cache file for lexicon module" << name();

    if (m_entryList->count()) {
        //create cache
        QString dir(DU::getUserCacheDir().absolutePath());
        QFile f2( QString(dir).append("/").append(name()) );

        if (f2.open( QIODevice::WriteOnly )) {
            QDataStream s( &f2 );
            s << config(CSwordModuleInfo::ModuleVersion) //store module version
            << QString(CACHE_FORMAT) //store BT version -- format may change
            << QString::number(s.version()) //store QDataStream version -- format may change
            << *m_entryList;
            f2.close();
        }
    }

    return m_entryList;
}

/** Jumps to the closest entry in the module.  */
bool CSwordLexiconModuleInfo::snap() {
    if (module()->getRawEntry()) { // Snap to the current entry
        return true;
    }

    return false;
}

/** No descriptions */
CSwordModuleInfo* CSwordLexiconModuleInfo::clone() {
    return new CSwordLexiconModuleInfo(*this);
}