Update SWORD integration to work with sword 1.6.0 (which is now required for sword...
[kworship.git] / kworship / bible / sword / KwBibleModuleSword.cpp
blob7c44bfb4ce67f2d6852aa8c802d16f9e73ca8470
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwBibleModuleSword.cpp
22 * @brief A SWORD bible module.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBibleModuleSword.h"
27 #include "KwBiblePassage.h"
29 #include <QStringList>
31 #include <swmodule.h>
32 #include <swtext.h>
33 #include <versekey.h>
36 * Constructors + destructor
39 /// Default constructor.
40 KwBibleModuleSword::KwBibleModuleSword(sword::SWText* module)
41 : KwBibleModule()
42 , m_module(module)
44 // Get the range of books.
45 bool oldSCL = module->getSkipConsecutiveLinks();
46 module->setSkipConsecutiveLinks(true);
49 module->setPosition(sword::TOP);
50 sword::VerseKey key = module->KeyText();
51 m_firstBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
55 module->setPosition(sword::BOTTOM);
56 sword::VerseKey key = module->KeyText();
57 m_lastBibleBookIndex = testamentBookToBible(key.Testament()-1, key.Book()-1);
60 module->setSkipConsecutiveLinks(oldSCL);
63 /// Destructor.
64 KwBibleModuleSword::~KwBibleModuleSword()
69 * Main interface
72 QString KwBibleModuleSword::name()
74 return m_module->Name();
77 QString KwBibleModuleSword::description()
79 return m_module->Description();
82 QString KwBibleModuleSword::managerId()
84 return "SWORD";
87 int KwBibleModuleSword::numChapters(int book)
89 int testamentBook;
90 int testament = localToTestamentBook(book, &testamentBook);
91 if (testament >= 0)
93 sword::VerseKey key = m_module->getKey();
94 key.Testament(1+testament);
95 key.Book(1+testamentBook);
96 return key.getChapterMax();
98 return 0;
101 int KwBibleModuleSword::numVerses(int book, int chapter)
103 if (book >= 0 && chapter >= 0)
105 int testamentBook;
106 int testament = localToTestamentBook(book, &testamentBook);
107 if (testament >= 0)
109 sword::VerseKey key = m_module->getKey();
110 key.Testament(1+testament);
111 key.Book(1+testamentBook);
112 if (chapter < key.getChapterMax())
114 key.Chapter(1+chapter);
115 return key.getVerseMax();
119 return 0;
122 bool KwBibleModuleSword::fillPassageVerse(int bookIndex, int chapterIndex, int verseIndex, KwBiblePassage* outPassage)
124 int bookInTestament = -1;
125 int testament = localToTestamentBook(bookIndex, &bookInTestament);
126 if (testament >= 0)
128 sword::VerseKey verse;
129 verse.Headings(0);
130 verse.Testament(1+testament);
131 verse.Book(1+bookInTestament);
132 verse.Chapter(1+chapterIndex);
133 verse.Verse(1+verseIndex);
135 m_module->setKey(&verse);
136 const char* text = m_module->RenderText();
137 const char* preverse = m_module->getEntryAttributes()["Heading"]["Preverse"]["0"];
138 QString headings;
139 if (preverse[0] != '\0')
141 headings = QString("<h4>%1</h4>").arg(QString::fromUtf8(preverse));
143 outPassage->initVerse(bookIndex, 1+chapterIndex, 1+verseIndex,
144 headings,
145 QString::fromUtf8(text));
146 return true;
148 return false;
152 * Protected virtual interface
155 void KwBibleModuleSword::obtainBooks()
157 // List books
158 QStringList books;
159 sword::VerseKey key = m_module->getKey();
160 for (int bibleIndex = m_firstBibleBookIndex; bibleIndex <= m_lastBibleBookIndex; ++bibleIndex)
162 int testamentBook;
163 int testament = bibleToTestamentBook(bibleIndex, &testamentBook);
164 Q_ASSERT(testament >= 0);
165 key.Testament(1+testament);
166 key.Book(1+testamentBook);
167 books << QString::fromUtf8(key.getBookName());
169 setBooks(books);
173 * Internal functions
176 /// Convert a local book index to a bible book index.
177 int KwBibleModuleSword::localToBible(int localIndex) const
179 int result = -1;
180 if (localIndex >= 0 && m_firstBibleBookIndex + localIndex <= m_lastBibleBookIndex)
182 result = m_firstBibleBookIndex + localIndex;
184 return result;
187 /// Convert a bible book index to a local book index.
188 int KwBibleModuleSword::bibleToLocal(int bibleIndex) const
190 int result = -1;
191 if (bibleIndex >= m_firstBibleBookIndex && bibleIndex <= m_lastBibleBookIndex)
193 result = bibleIndex - m_firstBibleBookIndex;
195 return result;
198 /// Convert a testament & book number to a bible book index.
199 int KwBibleModuleSword::testamentBookToBible(int testament, int book) const
201 if (testament >= 0 && book >= 0)
203 sword::VerseKey key = m_module->getKey();
204 if (book < key.BMAX[testament])
206 if (testament == 0)
208 return book;
210 else if (testament == 1)
212 return key.BMAX[0] + book;
216 return -1;
219 /// Convert a bible book index into a testament and book number.
220 int KwBibleModuleSword::bibleToTestamentBook(int bibleIndex, int* book) const
222 if (bibleIndex >= 0)
224 sword::VerseKey key = m_module->getKey();
225 // Old testament?
226 if (bibleIndex < key.BMAX[0])
228 *book = bibleIndex;
229 return 0;
231 // New testament?
232 else if (bibleIndex < key.BMAX[0] + key.BMAX[1])
234 *book = bibleIndex - key.BMAX[0];
235 return 1;
238 return -1;
241 /// Convert a testament & book number to a local book index.
242 int KwBibleModuleSword::testamentBookToLocal(int testament, int book) const
244 return bibleToLocal(testamentBookToBible(testament, book));
247 /// Convert a local book index into a testament and book number.
248 int KwBibleModuleSword::localToTestamentBook(int localIndex, int* book) const
250 return bibleToTestamentBook(localToBible(localIndex), book);