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-2026 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
31CSwordTreeKey::CSwordTreeKey(sword::TreeKeyIdx const * k,
32 CSwordModuleInfo const * module)
33 : CSwordKey(module)
34 , m_key(*k)
35{}
36
37sword::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 */
45QString CSwordTreeKey::key() const {
46 //return getTextUnicode();
48 if (m_module->isUnicode()) {
49 return QString::fromUtf8(m_key.getText());
50 }
51 else {
52 bool error;
53 auto converted = util::cp1252::toUnicode(m_key.getText(), error);
54 BT_ASSERT(!error);
55 return converted;
56 }
57}
58
59const char * CSwordTreeKey::rawKey() const { return m_key.getText(); }
60
61bool CSwordTreeKey::setKey(const QString &newKey) {
62 //return key( newKey.toLocal8Bit().constData() );
63 //return key(m_module->getTextCodec()->fromUnicode(newKey).constData());
65 if (m_module->isUnicode()) {
66 return setKey(newKey.toUtf8().constData());
67 }
68 else {
69 bool error;
70 auto converted = util::cp1252::fromUnicode(newKey, error);
71 return error ? false : setKey(converted.constData());
72 }
73}
74
75bool CSwordTreeKey::setKey(const char *newKey) {
76 BT_ASSERT(newKey);
77
78 if (newKey) {
79 m_key = newKey;
80 }
81 else {
83 }
84
85 return !m_key.popError();
86}
87
89 //return m_module->getTextCodec()->toUnicode(getLocalName());
90 //Only UTF-8 and latin1 are legal Sword module encodings
92 if (m_module->isUnicode()) {
93 return QString::fromUtf8(m_key.getLocalName());
94 }
95 else {
96 bool error;
97 auto converted = util::cp1252::toUnicode(m_key.getLocalName(), error);
98 BT_ASSERT(!error);
99 return converted;
100 }
101}
102
104 BT_ASSERT(newModule);
105 if (m_module == newModule) return;
107 BT_ASSERT(dynamic_cast<CSwordBookModuleInfo const *>(newModule));
108
109 m_module = newModule;
110
111 const QString oldKey = key();
112
113 m_key.copyFrom(
114 *static_cast<CSwordBookModuleInfo const *>(newModule)->tree());
115
116 setKey(oldKey); //try to restore our old key
117
118 //set the key to the root node
121}
#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.
void positionToRoot()
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
QByteArray fromUnicode(QString const &str, bool &error)
Definition cp1252.cpp:145
QString toUnicode(QByteArray const &data, bool &error)
Definition cp1252.cpp:142