summaryrefslogtreecommitdiff
path: root/bibletime/backend/cswordversekey.cpp
blob: cebf311a3429dca9288cb9af5be66de4328720a9 (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
/***************************************************************************
                          cswordversekey.cpp  -  description
                             -------------------
    begin                : Thu Oct 5 2000
    copyright            : (C) 2000 by The BibleTime team
    email                : info@bibletime.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

//BibleTime includes
#include "cswordversekey.h"
#include "cswordbiblemoduleinfo.h"
#include "cswordcommentarymoduleinfo.h"

//Qt includes
#include <qstringlist.h>

//Sword includes
#include <swmodule.h>

CSwordVerseKey::CSwordVerseKey( CSwordModuleInfo* module ) : CSwordKey(module) {
  if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module) ) {
    key( bible->lowerBound().key() );
  }
}

CSwordVerseKey::CSwordVerseKey( const CSwordVerseKey& k ) : CSwordKey(k), VerseKey(k) {

}

CSwordVerseKey::CSwordVerseKey( const VerseKey* k, CSwordModuleInfo* module) : CSwordKey(module), VerseKey(*k) {
}

/** Clones this object. */
CSwordKey* CSwordVerseKey::copy() const {
	return new CSwordVerseKey(*this);
}

/** Sets the module for this key */
CSwordModuleInfo* const CSwordVerseKey::module( CSwordModuleInfo* const newModule ){
	if (newModule && ( (newModule->type() == CSwordModuleInfo::Bible) || (newModule->type() == CSwordModuleInfo::Commentary) )) {
	//	const QString& oldKey	= key();
		m_module = newModule;
	//	key(oldKey);

		//check if the module contains the key we present
		CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(newModule);
   	if (_compare(bible->lowerBound()) < 0) {
			qWarning("setting key to lower bound %s", bible->lowerBound().key().latin1());
      key( bible->lowerBound() );
    }
    if (_compare(bible->upperBound()) > 0) {
			qWarning("setting key to upper bound %s", bible->upperBound().key().latin1());
      key( bible->upperBound() );
    }
	}
	return dynamic_cast<CSwordBibleModuleInfo*>(m_module);
}

/** Returns the current book as Text, not as integer. */
const QString CSwordVerseKey::book( const QString& newBook ) {
	int min = 0;
	int max = 1;
	
	if (CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module())) {
		const bool hasOT = bible->hasTestament(CSwordBibleModuleInfo::OldTestament);		
		const bool hasNT = bible->hasTestament(CSwordBibleModuleInfo::NewTestament);
		if (hasOT && hasNT) {
			min = 0;
			max = 1;
		}
		else if (hasOT && !hasNT) {
			min = 0;
			max = 0;
		}
		else if (!hasOT && hasNT) {
			min = 1;
			max = 1;
		}
		else if (!hasOT && !hasNT) {
			min = 0;
			max = -1; //no loop
		}
  }

	if (!newBook.isEmpty()) {
		bool finished = false;
		for (int testament = min; testament <= max && !finished; ++testament) {
			for (int book = 0; book < BMAX[testament] && !finished; ++book) {
				if ( !strcmp((const char*)newBook.local8Bit(),books[testament][book].name ) ) {
					Testament(testament+1);
					Book(book+1);
					finished = true;
				}
			}
		}
	}
	if ( Testament() >= min+1 && Testament() <= max+1 && Book() <= BMAX[min] )
		return QString::fromLocal8Bit( books[Testament()-1][Book()-1].name );
	return QString::fromLocal8Bit( books[min][0].name ); //return the first book, i.e. Genesis
}

/** Sets the key we use to the parameter. */
const QString CSwordVerseKey::key(){	
	return QString::fromLocal8Bit(getText()); //don't use fromUtf8 here!
//	return QString::fromLocal8Bit((const char*)*this); //don't use fromUtf8 here!
}

void CSwordVerseKey::key( const QString& newKey ) {
	if (newKey.isEmpty()) {
    if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
      VerseKey::operator = ((const char*)bible->lowerBound().key().local8Bit());
    }
  }
  else {
    VerseKey::operator = ((const char*)newKey.local8Bit());
  }
}

void CSwordVerseKey::key( const char* newKey ){
  if (newKey && strlen(newKey)>0) {
		VerseKey::operator = (newKey);
	}
  else if (!strlen(newKey)) {
    if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
      VerseKey::operator = ((const char*)bible->lowerBound().key().local8Bit());
    }
  }
}

const bool CSwordVerseKey::next( const JumpType type ) {
	//clear Error status
	Error();

	switch (type) {
		case UseBook: {
			if ((Book() <= 0) || (Book() >= BMAX[Testament()-1]) && (Testament() > 1)) {
				return false;
			}
			Book(Book()+1);
			break;
		}
		case UseChapter: {
			Chapter(Chapter()+1);
			break;
		}
		case UseVerse: {
    	if (m_module && m_module->module()) {
				//qWarning("module is %s", m_module->name().latin1());
    		m_module->module()->SetKey(this);	//use this key as base for the next one!
        m_module->module()->setSkipConsecutiveLinks(true);
    		(*(m_module->module()) )++;
        m_module->module()->setSkipConsecutiveLinks(false);

    		if (!m_module->module()->Error()) {
    			key( QString::fromLocal8Bit(m_module->module()->KeyText()) );//don't use fromUtf8
        }
    		else {
					qWarning("VerseKey::next: module error");
	    	  Verse(Verse()+1);
	    	  break;
	    	}
    	}
    	else {
    	  Verse(Verse()+1);
      }
    	break;
		}
		default:
			return false;
	};

  if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
    bool ret = true;
    if (Error()) {
      ret = false;
		}
		//check if the key if out of the modules bounds
    else if (_compare(bible->lowerBound()) < 0) {
      key( bible->lowerBound() );
      ret = false;
    }
    if (_compare(bible->upperBound()) > 0) {
      key( bible->upperBound() );
      ret = false;
    }
    return ret;
  }
  else if (Error()) { //we have no module, so take care of VerseKey::Error()
    return false;
	}

  return true;
};

const bool CSwordVerseKey::previous( const JumpType type ) {
	switch (type) {
		case UseBook: {
			if (Book()<=1 || Book() > BMAX[Testament()-1] && Testament() > 1)
				return false;
			Book(Book()-1);
			break;
		}
		case UseChapter: {
			Chapter(Chapter()-1);
			break;
		}
		case UseVerse: {
    	if (m_module && m_module->module()) {
    		m_module->module()->SetKey(this);	//use this key as base for the next one!
        m_module->module()->setSkipConsecutiveLinks(true);
    		( *( m_module->module() ) )--;
        m_module->module()->setSkipConsecutiveLinks(true);      
    		if (!m_module->module()->Error())
    			key( QString::fromLocal8Bit(m_module->module()->KeyText()) );//don't use fromUtf8
    		else
	    	  Verse(Verse()-1);
    	}
    	else {
    		Verse(Verse()-1);
      }
      break;
		}
		default:	
			return false;
	};

  if ( CSwordBibleModuleInfo* bible = dynamic_cast<CSwordBibleModuleInfo*>(module()) ) {
    bool ret = true;
    if (_compare(bible->lowerBound()) < 0 ) {
      key( bible->lowerBound() );
      ret = false;
    }
    if (_compare(bible->upperBound()) > 0 ) {
      key( bible->upperBound() );
      ret = false;
    }
    return ret;
  }
  else if (Error())
    return false;
  return true;
};

/** Assignment operator for more ease of use. */
CSwordVerseKey& CSwordVerseKey::operator = (const QString& keyname){
  key(keyname);
  return *this;
}