BibleTime
cswordbiblemoduleinfo.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "cswordbiblemoduleinfo.h"
14 
15 #include <memory>
16 #include <QFile>
17 #include "../../util/btassert.h"
18 #include "../managers/cswordbackend.h"
19 #include "../keys/cswordversekey.h"
20 
21 // Sword includes:
22 #include <versekey.h>
23 
24 
27  ModuleType type)
28  : CSwordModuleInfo(module, backend, type)
29  , m_boundsInitialized(false)
30  , m_lowerBound(nullptr)
31  , m_upperBound(nullptr)
32 {}
33 
35 
36 void CSwordBibleModuleInfo::initBounds() const {
37  /// \todo The fields calculated by this method could be cached to disk.
38 
39  BT_ASSERT(!m_boundsInitialized);
40 
41  auto & m = swordModule();
42  const bool oldStatus = m.isSkipConsecutiveLinks();
43  m.setSkipConsecutiveLinks(true);
44 
45  sword::VerseKey const * const key =
46  static_cast<sword::VerseKey *>(m.getKey());
47  m_lowerBound.setVersification(key->getVersificationSystem());
48  m_upperBound.setVersification(key->getVersificationSystem());
49 
50  m.setPosition(sword::TOP); // position to first entry
51  m_hasOT = (key->getTestament() == 1);
52  m_lowerBound.setKey(key->getText());
53 
54  m.setPosition(sword::BOTTOM);
55  m_hasNT = (key->getTestament() == 2);
56  m_upperBound.setKey(key->getText());
57 
58  m.setSkipConsecutiveLinks(oldStatus);
59  m_boundsInitialized = true;
60 }
61 
62 
63 /** Returns the books available in this module */
64 QStringList const & CSwordBibleModuleInfo::books() const {
65  {
66  CSwordBackend & b = backend();
67  if (m_cachedLocale != b.booknameLanguage()) {
68  // Reset the booklist because the locale has changed
70  m_bookList.reset();
71  }
72  }
73 
74  if (!m_bookList) {
75  m_bookList.emplace();
76 
78  initBounds();
79 
80  CSwordVerseKey key(this);
81  key.setKey(m_lowerBound.key());
82  do {
83  m_bookList->append(key.bookName());
84  } while (key.next(CSwordVerseKey::JumpType::UseBook) &&
85  key <= m_upperBound);
86  }
87 
88  return *m_bookList;
89 }
90 
91 unsigned int CSwordBibleModuleInfo::chapterCount(const unsigned int book) const {
92  int result = 0;
93 
94  std::unique_ptr<sword::VerseKey> key(
95  static_cast<sword::VerseKey *>(swordModule().createKey()));
96  key->setPosition(sword::TOP);
97 
98  // works for old and new versions
99  key->setBook(book);
100  key->setPosition(sword::MAXCHAPTER);
101  result = key->getChapter();
102 
103  return result;
104 }
105 
106 unsigned int CSwordBibleModuleInfo::chapterCount(const QString &book) const {
107  return chapterCount(bookNumber(book));
108 }
109 
110 /** Returns the number of verses for the given chapter. */
111 
112 unsigned int CSwordBibleModuleInfo::verseCount(const unsigned int book,
113  const unsigned int chapter) const
114 {
115  unsigned int result = 0;
116 
117  std::unique_ptr<sword::VerseKey> key(
118  static_cast<sword::VerseKey *>(swordModule().createKey()));
119  key->setPosition(sword::TOP);
120 
121  // works for old and new versions
122  key->setBook(book);
123  key->setChapter(chapter);
124  key->setPosition(sword::MAXVERSE);
125  result = key->getVerse();
126 
127  return result;
128 }
129 
130 unsigned int CSwordBibleModuleInfo::verseCount(const QString &book,
131  const unsigned int chapter) const
132 {
133  return verseCount(bookNumber(book), chapter);
134 }
135 
136 unsigned int CSwordBibleModuleInfo::bookNumber(const QString &book) const {
137  unsigned int bookNumber = 0;
138 
139  std::unique_ptr<sword::VerseKey> key(
140  static_cast<sword::VerseKey *>(swordModule().createKey()));
141  key->setPosition(sword::TOP);
142 
143  key->setBookName(book.toUtf8().constData());
144 
145  bookNumber = ((key->getTestament() > 1) ? key->BMAX[0] : 0) + key->getBook();
146 
147  return bookNumber;
148 }
149 
151  auto * const key = swordModule().getKey();
152  BT_ASSERT(dynamic_cast<sword::VerseKey *>(key));
153  return new CSwordVerseKey(static_cast<sword::VerseKey *>(key), this);
154 }
#define BT_ASSERT(...)
Definition: btassert.h:17
The backend layer main class, a backend implementation of Sword.
Definition: cswordbackend.h:56
QString booknameLanguage() const
Implementation for Sword Bibles.
unsigned int verseCount(const unsigned int book, const unsigned int chapter) const
unsigned int chapterCount(const unsigned int book) const
~CSwordBibleModuleInfo() noexcept override
CSwordBibleModuleInfo(sword::SWModule &module, CSwordBackend &backend, ModuleType type=Bible)
QStringList const & books() const
std::optional< QStringList > m_bookList
unsigned int bookNumber(const QString &book) const
CSwordKey * createKey() const final override
CSwordBackend & backend() const
sword::SWModule & swordModule() const
CSwordKey implementation for Sword's VerseKey.
QString bookName() const
bool next(const JumpType type=JumpType::UseVerse)
QString key() const final override
bool setKey(const QString &key) final override
std::unique_ptr< CSwordBackend > backend(sword::InstallSource const &is)