~ubuntu-branches/ubuntu/trusty/bibletime/trusty

« back to all changes in this revision

Viewing changes to src/backend/keys/cswordversekey.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Marsden
  • Date: 2009-11-18 17:30:00 UTC
  • mfrom: (1.3.4 upstream) (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091118173000-endkhjz5qai88tvr
Tags: 2.4-1
* New upstream version 2.4
* debian/control: 
  - Replace incorrect bibletime-data Depends on lib4qt-gui
    with bibletime Depends on libqtgui4 (>= 4.4.0). (Closes: #556209).
  - Add Build-depends: on zlib1g-dev and libcurl4-gnutls-dev 
    (Closes: #556805).

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
*
8
8
**********/
9
9
 
10
 
#include "cswordversekey.h"
 
10
#include "backend/keys/cswordversekey.h"
 
11
 
 
12
#include <QStringList>
 
13
#include <QDebug>
 
14
 
11
15
#include "backend/drivers/cswordbiblemoduleinfo.h"
12
16
#include "backend/drivers/cswordcommentarymoduleinfo.h"
13
17
 
14
 
//Qt
15
 
#include <QStringList>
16
 
 
17
 
//Sword
 
18
// Sword includes:
18
19
#include <swmodule.h>
19
20
#include <localemgr.h>
20
21
 
 
22
 
21
23
CSwordVerseKey::CSwordVerseKey( CSwordModuleInfo* const module ) :
22
 
        CSwordKey(module)
23
 
{
24
 
        if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module) ) {
25
 
                // Copy important settings like versification system
26
 
                copyFrom((sword::VerseKey*) bible->module()->getKey());
27
 
 
28
 
                key( bible->lowerBound().key() );
29
 
        }
30
 
}
31
 
 
32
 
CSwordVerseKey::CSwordVerseKey( const CSwordVerseKey& k ) : CSwordKey(k), VerseKey(k) {}
 
24
        CSwordKey(module) {
 
25
    if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module) ) {
 
26
        // Copy important settings like versification system
 
27
        copyFrom((sword::VerseKey*) bible->module()->getKey());
 
28
 
 
29
        key( bible->lowerBound().key() );
 
30
    }
 
31
    this->VerseKey::setAutoNormalize(true);
 
32
}
 
33
 
 
34
CSwordVerseKey::CSwordVerseKey( const CSwordVerseKey& k ) : CSwordKey(k), VerseKey(k) {
 
35
        this->VerseKey::setAutoNormalize(true);
 
36
}
33
37
 
34
38
CSwordVerseKey::CSwordVerseKey( const VerseKey* const k, CSwordModuleInfo* const module ) : CSwordKey(module), VerseKey(*k) {}
35
39
 
36
40
/** Clones this object. */
37
41
CSwordKey* CSwordVerseKey::copy() const {
38
 
        return new CSwordVerseKey(*this);
 
42
    return new CSwordVerseKey(*this);
39
43
}
40
44
 
41
45
/** Sets the module for this key */
42
46
CSwordModuleInfo* CSwordVerseKey::module( CSwordModuleInfo* const newModule ) {
43
 
        if (newModule && ((newModule->type() == CSwordModuleInfo::Bible)  || (newModule->type() == CSwordModuleInfo::Commentary) ) ) {
44
 
                m_module = newModule;
45
 
 
46
 
                //check if the module contains the key we present
47
 
                CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(newModule);
48
 
 
49
 
                if (_compare(bible->lowerBound()) < 0) {
50
 
                        key( bible->lowerBound() );
51
 
                }
52
 
 
53
 
                if (_compare(bible->upperBound()) > 0) {
54
 
                        key( bible->upperBound() );
55
 
                }
56
 
        }
57
 
 
58
 
        return dynamic_cast<CSwordBibleModuleInfo*>(m_module);
 
47
    if (newModule && ((newModule->type() == CSwordModuleInfo::Bible)  || (newModule->type() == CSwordModuleInfo::Commentary) ) ) {
 
48
        m_module = newModule;
 
49
 
 
50
        //check if the module contains the key we present
 
51
        CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(newModule);
 
52
 
 
53
        if (_compare(bible->lowerBound()) < 0) {
 
54
            key( bible->lowerBound() );
 
55
        }
 
56
 
 
57
        if (_compare(bible->upperBound()) > 0) {
 
58
            key( bible->upperBound() );
 
59
        }
 
60
    }
 
61
 
 
62
    return dynamic_cast<CSwordBibleModuleInfo*>(m_module);
59
63
}
60
64
 
61
65
/** Returns the current book as Text, not as integer. */
62
66
QString CSwordVerseKey::book( const QString& newBook ) {
63
 
        int min = 0;
64
 
        int max = 1;
65
 
 
66
 
        if (CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module())) {
67
 
                const bool hasOT = bible->hasTestament(CSwordBibleModuleInfo::OldTestament);
68
 
                const bool hasNT = bible->hasTestament(CSwordBibleModuleInfo::NewTestament);
69
 
 
70
 
                if (hasOT && hasNT) {
71
 
                        min = 0;
72
 
                        max = 1;
73
 
                }
74
 
                else if (hasOT && !hasNT) {
75
 
                        min = 0;
76
 
                        max = 0;
77
 
                }
78
 
                else if (!hasOT && hasNT) {
79
 
                        min = 1;
80
 
                        max = 1;
81
 
                }
82
 
                else if (!hasOT && !hasNT) {
83
 
                        min = 0;
84
 
                        max = -1; //no loop
85
 
                }
86
 
        }
87
 
 
88
 
        if (!newBook.isEmpty()) {
89
 
                setBookName(newBook.toUtf8().constData());
90
 
        }
91
 
 
92
 
        if ( (Testament() >= min+1) && (Testament() <= max+1) && (Book() <= BMAX[min]) ) {
93
 
                return QString::fromUtf8( getBookName() );
94
 
        }
95
 
 
96
 
        //return QString::fromUtf8( books[min][0].name ); //return the first book, i.e. Genesis
97
 
        return QString::null;
 
67
    int min = 0;
 
68
    int max = 1;
 
69
 
 
70
    if (CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module())) {
 
71
        const bool hasOT = bible->hasTestament(CSwordBibleModuleInfo::OldTestament);
 
72
        const bool hasNT = bible->hasTestament(CSwordBibleModuleInfo::NewTestament);
 
73
 
 
74
        if (hasOT && hasNT) {
 
75
            min = 0;
 
76
            max = 1;
 
77
        }
 
78
        else if (hasOT && !hasNT) {
 
79
            min = 0;
 
80
            max = 0;
 
81
        }
 
82
        else if (!hasOT && hasNT) {
 
83
            min = 1;
 
84
            max = 1;
 
85
        }
 
86
        else if (!hasOT && !hasNT) {
 
87
            min = 0;
 
88
            max = -1; //no loop
 
89
        }
 
90
    }
 
91
 
 
92
    if (!newBook.isEmpty()) {
 
93
        setBookName(newBook.toUtf8().constData());
 
94
    }
 
95
 
 
96
    if ( (Testament() >= min + 1) && (Testament() <= max + 1) && (Book() <= BMAX[min]) ) {
 
97
        return QString::fromUtf8( getBookName() );
 
98
    }
 
99
 
 
100
    //return QString::fromUtf8( books[min][0].name ); //return the first book, i.e. Genesis
 
101
    return QString::null;
98
102
}
99
103
 
100
104
/** Sets the key we use to the parameter. */
101
105
QString CSwordVerseKey::key() const {
102
 
        return QString::fromUtf8(getText());
 
106
    return QString::fromUtf8(getText());
103
107
}
104
108
 
105
109
const char * CSwordVerseKey::rawKey() const {
106
 
        return getText();
 
110
    return getText();
107
111
}
108
112
 
109
113
bool CSwordVerseKey::key( const QString& newKey ) {
110
 
        return key( newKey.toUtf8().constData() );
 
114
    return key( newKey.toUtf8().constData() );
111
115
}
112
116
 
113
117
bool CSwordVerseKey::key( const char* newKey ) {
114
 
        if (newKey && (strlen(newKey)>0) ) {
115
 
                VerseKey::operator = (newKey);
116
 
        }
117
 
        else if (newKey && !strlen(newKey)) {
118
 
                CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module());
119
 
 
120
 
                if ( bible ) {
121
 
                        VerseKey::operator = (bible->lowerBound().key().toUtf8().constData());
122
 
                }
123
 
        }
124
 
 
125
 
        return !Error();
 
118
    if (newKey && (strlen(newKey) > 0) ) {
 
119
        VerseKey::operator = (newKey);
 
120
    }
 
121
    else if (newKey && !strlen(newKey)) {
 
122
        CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module());
 
123
 
 
124
        if ( bible ) {
 
125
            VerseKey::operator = (bible->lowerBound().key().toUtf8().constData());
 
126
        }
 
127
    }
 
128
 
 
129
    return !Error();
126
130
}
127
131
 
128
132
bool CSwordVerseKey::next( const JumpType type ) {
129
 
        Error(); //clear Error status
130
 
        bool ret = true;
131
 
 
132
 
        switch (type) {
133
 
 
134
 
                case UseBook: {
135
 
                        const int currentTestament = Testament();
136
 
                        const int currentBook = Book();
137
 
 
138
 
                        if ((currentTestament == 2) && (currentBook >= BMAX[currentTestament-1])) { //Revelation, i.e. end of navigation
139
 
                                return false;
140
 
                        }
141
 
                        else if ((currentTestament == 1) && (currentBook >= BMAX[currentTestament-1])) { //Malachi, switch to the NT
142
 
                                Testament(currentTestament+1);
143
 
                                Book(1);
144
 
                        }
145
 
                        else {
146
 
                                Book(Book()+1);
147
 
                        }
148
 
                        break;
149
 
                }
150
 
 
151
 
                case UseChapter: {
152
 
                        Chapter(Chapter()+1);
153
 
                        break;
154
 
                }
155
 
 
156
 
                case UseVerse: {
157
 
                        if (m_module && m_module->module()) {
158
 
                                const bool oldStatus = m_module->module()->getSkipConsecutiveLinks();
159
 
                                m_module->module()->setSkipConsecutiveLinks(true);
160
 
 
161
 
                                //disable headings for next verse
162
 
                                const bool useHeaders = (Verse() == 0);
163
 
                                const bool oldHeadingsStatus = ((VerseKey*)(m_module->module()->getKey()))->Headings( useHeaders );
164
 
                                //don't use setKey(), that would create a new key without Headings set
165
 
                                m_module->module()->getKey()->setText( key().toUtf8().constData() );
166
 
 
167
 
                                (*(m_module->module()) )++;
168
 
 
169
 
                                ((VerseKey*)(m_module->module()->getKey()))->Headings(oldHeadingsStatus);
170
 
                                m_module->module()->setSkipConsecutiveLinks(oldStatus);
171
 
 
172
 
                                if (!m_module->module()->Error()) {
173
 
                                        key( QString::fromUtf8(m_module->module()->KeyText()) );
174
 
                                }
175
 
                                else {
176
 
                                        //         Verse(Verse()+1);
177
 
                                        //don't change the key, restore the module's position
178
 
                                        m_module->module()->getKey()->setText( key().toUtf8().constData() );
179
 
                                        ret = false;
180
 
                                        break;
181
 
                                }
182
 
 
183
 
                        }
184
 
                        else {
185
 
                                Verse(Verse()+1);
186
 
                        }
187
 
 
188
 
                        break;
189
 
                }
190
 
 
191
 
                default:
192
 
                        return false;
193
 
        }
194
 
 
195
 
        if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
196
 
                if (_compare(bible->lowerBound()) < 0 ) {
197
 
                        key( bible->lowerBound() );
198
 
                        ret = false;
199
 
                }
200
 
 
201
 
                if (_compare(bible->upperBound()) > 0 ) {
202
 
                        key( bible->upperBound() );
203
 
                        ret = false;
204
 
                }
205
 
 
206
 
                return ret;
207
 
        }
208
 
        else if (Error()) { //we have no module, so take care of VerseKey::Error()
209
 
                return false;
210
 
        }
211
 
 
212
 
        return ret;
 
133
    Error(); //clear Error status
 
134
    bool ret = true;
 
135
 
 
136
    switch (type) {
 
137
 
 
138
        case UseBook: {
 
139
            const int currentTestament = Testament();
 
140
            const int currentBook = Book();
 
141
 
 
142
            if ((currentTestament == 2) && (currentBook >= BMAX[currentTestament-1])) { //Revelation, i.e. end of navigation
 
143
                return false;
 
144
            }
 
145
            else if ((currentTestament == 1) && (currentBook >= BMAX[currentTestament-1])) { //Malachi, switch to the NT
 
146
                Testament(currentTestament + 1);
 
147
                Book(1);
 
148
            }
 
149
            else {
 
150
                Book(Book() + 1);
 
151
            }
 
152
            break;
 
153
        }
 
154
 
 
155
        case UseChapter: {
 
156
            Chapter(Chapter() + 1);
 
157
            break;
 
158
        }
 
159
 
 
160
        case UseVerse: {
 
161
            if (m_module && m_module->module()) {
 
162
                const bool oldStatus = m_module->module()->getSkipConsecutiveLinks();
 
163
                m_module->module()->setSkipConsecutiveLinks(true);
 
164
 
 
165
                //disable headings for next verse
 
166
                const bool useHeaders = (Verse() == 0);
 
167
                const bool oldHeadingsStatus = ((VerseKey*)(m_module->module()->getKey()))->Headings( useHeaders );
 
168
                //don't use setKey(), that would create a new key without Headings set
 
169
                m_module->module()->getKey()->setText( key().toUtf8().constData() );
 
170
 
 
171
                (*(m_module->module()) )++;
 
172
 
 
173
                ((VerseKey*)(m_module->module()->getKey()))->Headings(oldHeadingsStatus);
 
174
                m_module->module()->setSkipConsecutiveLinks(oldStatus);
 
175
 
 
176
                if (!m_module->module()->Error()) {
 
177
                    key( QString::fromUtf8(m_module->module()->KeyText()) );
 
178
                }
 
179
                else {
 
180
                    //         Verse(Verse()+1);
 
181
                    //don't change the key, restore the module's position
 
182
                    m_module->module()->getKey()->setText( key().toUtf8().constData() );
 
183
                    ret = false;
 
184
                    break;
 
185
                }
 
186
 
 
187
            }
 
188
            else {
 
189
                Verse(Verse() + 1);
 
190
            }
 
191
 
 
192
            break;
 
193
        }
 
194
 
 
195
        default:
 
196
            return false;
 
197
    }
 
198
 
 
199
    if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
 
200
        if (_compare(bible->lowerBound()) < 0 ) {
 
201
            key( bible->lowerBound() );
 
202
            ret = false;
 
203
        }
 
204
 
 
205
        if (_compare(bible->upperBound()) > 0 ) {
 
206
            key( bible->upperBound() );
 
207
            ret = false;
 
208
        }
 
209
 
 
210
        return ret;
 
211
    }
 
212
    else if (Error()) { //we have no module, so take care of VerseKey::Error()
 
213
        return false;
 
214
    }
 
215
 
 
216
    return ret;
213
217
}
214
218
 
215
219
bool CSwordVerseKey::previous( const JumpType type ) {
216
 
        bool ret = true;
217
 
 
218
 
        switch (type) {
219
 
 
220
 
                case UseBook: {
221
 
                        if ( (Book() == 1) && (Testament() == 1) ) { //Genesis
222
 
                                return false;
223
 
                        }
224
 
                        else if ( (Book() == 1) && (Testament() == 2) ){ //Matthew
225
 
                                Testament(1);
226
 
                                Book(BMAX[0]);
227
 
                        }
228
 
                        else{
229
 
                                Book( Book()-1 );
230
 
                        }
231
 
 
232
 
                        break;
233
 
                }
234
 
 
235
 
                case UseChapter: {
236
 
                        Chapter(Chapter()-1);
237
 
                        break;
238
 
                }
239
 
 
240
 
                case UseVerse: {
241
 
                        if (m_module && m_module->module()) {
242
 
                                const bool useHeaders = (Verse() == 0);
243
 
                                const bool oldHeadingsStatus = ((VerseKey*)(m_module->module()->getKey()))->Headings( useHeaders );
244
 
 
245
 
                                m_module->module()->getKey()->setText( key().toUtf8().constData() );
246
 
 
247
 
                                const bool oldStatus = m_module->module()->getSkipConsecutiveLinks();
248
 
                                m_module->module()->setSkipConsecutiveLinks(true);
249
 
                                ( *( m_module->module() ) )--;
250
 
 
251
 
                                ((VerseKey*)(m_module->module()->getKey()))->Headings( oldHeadingsStatus );
252
 
                                m_module->module()->setSkipConsecutiveLinks(oldStatus);
253
 
 
254
 
                                if (!m_module->module()->Error()) {
255
 
                                        key( QString::fromUtf8(m_module->module()->KeyText()) );//don't use fromUtf8
256
 
                                }
257
 
                                else {
258
 
                                        ret = false;
259
 
                                        //         Verse(Verse()-1);
260
 
                                        m_module->module()->getKey()->setText( key().toUtf8().constData() ); //restore module's key
261
 
                                }
262
 
                        }
263
 
                        else {
264
 
                                Verse(Verse()-1);
265
 
                        }
266
 
 
267
 
                        break;
268
 
                }
269
 
 
270
 
                default:
271
 
                        return false;
272
 
        }
273
 
 
274
 
        if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
275
 
                if (_compare(bible->lowerBound()) < 0 ) {
276
 
                        key( bible->lowerBound() );
277
 
                        ret = false;
278
 
                }
279
 
 
280
 
                if (_compare(bible->upperBound()) > 0 ) {
281
 
                        key( bible->upperBound() );
282
 
                        ret = false;
283
 
                }
284
 
 
285
 
                return ret;
286
 
        }
287
 
        else if (Error()) {
288
 
                return false;
289
 
        }
290
 
 
291
 
        return ret;
 
220
    bool ret = true;
 
221
 
 
222
    switch (type) {
 
223
 
 
224
        case UseBook: {
 
225
            if ( (Book() == 1) && (Testament() == 1) ) { //Genesis
 
226
                return false;
 
227
            }
 
228
            else if ( (Book() == 1) && (Testament() == 2) ) { //Matthew
 
229
                Testament(1);
 
230
                Book(BMAX[0]);
 
231
            }
 
232
            else {
 
233
                Book( Book() - 1 );
 
234
            }
 
235
 
 
236
            break;
 
237
        }
 
238
 
 
239
        case UseChapter: {
 
240
            Chapter(Chapter() - 1);
 
241
            break;
 
242
        }
 
243
 
 
244
        case UseVerse: {
 
245
            if (m_module && m_module->module()) {
 
246
                const bool useHeaders = (Verse() == 0);
 
247
                const bool oldHeadingsStatus = ((VerseKey*)(m_module->module()->getKey()))->Headings( useHeaders );
 
248
 
 
249
                m_module->module()->getKey()->setText( key().toUtf8().constData() );
 
250
 
 
251
                const bool oldStatus = m_module->module()->getSkipConsecutiveLinks();
 
252
                m_module->module()->setSkipConsecutiveLinks(true);
 
253
                ( *( m_module->module() ) )--;
 
254
 
 
255
                ((VerseKey*)(m_module->module()->getKey()))->Headings( oldHeadingsStatus );
 
256
                m_module->module()->setSkipConsecutiveLinks(oldStatus);
 
257
 
 
258
                if (!m_module->module()->Error()) {
 
259
                    key( QString::fromUtf8(m_module->module()->KeyText()) );//don't use fromUtf8
 
260
                }
 
261
                else {
 
262
                    ret = false;
 
263
                    //         Verse(Verse()-1);
 
264
                    m_module->module()->getKey()->setText( key().toUtf8().constData() ); //restore module's key
 
265
                }
 
266
            }
 
267
            else {
 
268
                Verse(Verse() - 1);
 
269
            }
 
270
 
 
271
            break;
 
272
        }
 
273
 
 
274
        default:
 
275
            return false;
 
276
    }
 
277
 
 
278
    if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
 
279
        if (_compare(bible->lowerBound()) < 0 ) {
 
280
            key( bible->lowerBound() );
 
281
            ret = false;
 
282
        }
 
283
 
 
284
        if (_compare(bible->upperBound()) > 0 ) {
 
285
            key( bible->upperBound() );
 
286
            ret = false;
 
287
        }
 
288
 
 
289
        return ret;
 
290
    }
 
291
    else if (Error()) {
 
292
        return false;
 
293
    }
 
294
 
 
295
    return ret;
292
296
}