BibleTime
cswordtreekey.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 "cswordtreekey.h"
14 
15 #include <QByteArray>
16 #include "../../util/btassert.h"
17 #include "../../util/cp1252.h"
18 #include "../drivers/cswordbookmoduleinfo.h"
19 #include "../drivers/cswordmoduleinfo.h"
20 #include "cswordkey.h"
21 
22 // Sword includes:
23 #include <treekeyidx.h>
24 
25 
27  : CSwordKey(copy)
28  , m_key(copy.m_key)
29 {}
30 
31 CSwordTreeKey::CSwordTreeKey(sword::TreeKeyIdx const * k,
32  CSwordModuleInfo const * module)
33  : CSwordKey(module)
34  , m_key(*k)
35 {}
36 
37 sword::TreeKeyIdx const & CSwordTreeKey::asSwordKey() const noexcept
38 { return m_key; }
39 
41  return new CSwordTreeKey(*this);
42 }
43 
44 /** Sets the key of this instance */
45 QString CSwordTreeKey::key() const {
46  //return getTextUnicode();
48  if (m_module->isUnicode()) {
49  return QString::fromUtf8(m_key.getText());
50  }
51  else {
52  return util::cp1252::toUnicode(m_key.getText());
53  }
54 }
55 
56 const char * CSwordTreeKey::rawKey() const { return m_key.getText(); }
57 
58 bool CSwordTreeKey::setKey(const QString &newKey) {
59  //return key( newKey.toLocal8Bit().constData() );
60  //return key(m_module->getTextCodec()->fromUnicode(newKey).constData());
62  if (m_module->isUnicode()) {
63  return setKey(newKey.toUtf8().constData());
64  }
65  else {
66  return setKey(util::cp1252::fromUnicode(newKey).constData());
67  }
68 }
69 
70 bool CSwordTreeKey::setKey(const char *newKey) {
71  BT_ASSERT(newKey);
72 
73  if (newKey) {
74  m_key = newKey;
75  }
76  else {
78  }
79 
80  return !m_key.popError();
81 }
82 
84  //return m_module->getTextCodec()->toUnicode(getLocalName());
85  //Only UTF-8 and latin1 are legal Sword module encodings
87  if (m_module->isUnicode()) {
88  return QString::fromUtf8(m_key.getLocalName());
89  }
90  else {
91  return util::cp1252::toUnicode(m_key.getLocalName());
92  }
93 }
94 
96  BT_ASSERT(newModule);
97  if (m_module == newModule) return;
99  BT_ASSERT(dynamic_cast<CSwordBookModuleInfo const *>(newModule));
100 
101  m_module = newModule;
102 
103  const QString oldKey = key();
104 
105  m_key.copyFrom(
106  *static_cast<CSwordBookModuleInfo const *>(newModule)->tree());
107 
108  setKey(oldKey); //try to restore our old key
109 
110  //set the key to the root node
111  positionToRoot();
113 }
#define BT_ASSERT(...)
Definition: btassert.h:17
Class for generic book support.
const CSwordModuleInfo * m_module
Definition: cswordkey.h:112
bool isUnicode() const noexcept
ModuleType type() const
CSwordKey implementation for Sword's TreeKey.
Definition: cswordtreekey.h:43
void positionToRoot()
Definition: cswordtreekey.h:99
bool positionToFirstChild()
const char * rawKey() const final override
QString getLocalNameUnicode()
QString key() const final override
bool setKey(const QString &key) final override
void setModule(const CSwordModuleInfo *newModule) final override
CSwordTreeKey(const sword::TreeKeyIdx *k, const CSwordModuleInfo *module)
sword::TreeKeyIdx const & asSwordKey() const noexcept final override
sword::TreeKeyIdx m_key
CSwordTreeKey * copy() const final override
QString toUnicode(QByteArray const &data)
Definition: cp1252.cpp:40
QByteArray fromUnicode(QString const &str)
Definition: cp1252.cpp:52