summaryrefslogtreecommitdiff
path: root/src/backend/drivers/cswordbookmoduleinfo.cpp
blob: 0a8c1e6add8026fca25db7cedc546cdd7612460a (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

//BibleTime includes
#include "cswordbookmoduleinfo.h"
#include "backend/keys/cswordtreekey.h"

//Sword includes
#include <treekeyidx.h>
#include <treekey.h>

CSwordBookModuleInfo::CSwordBookModuleInfo( sword::SWModule* module, CSwordBackend* const usedBackend )
: CSwordModuleInfo(module, usedBackend),
m_depth(-1) {}

CSwordBookModuleInfo::CSwordBookModuleInfo( const CSwordBookModuleInfo& module )
: CSwordModuleInfo(module) {
	m_depth = module.m_depth;
}

CSwordBookModuleInfo::~CSwordBookModuleInfo() {}

int CSwordBookModuleInfo::depth() {
	if (m_depth == -1) {
		sword::TreeKeyIdx* key = tree();

		if (key) {
			key->root();
			computeDepth(key, 0);
		}
	}

	return m_depth;
}

void CSwordBookModuleInfo::computeDepth(sword::TreeKeyIdx* key, int level ) {
	std::string savedKey;
	//	savedKey = key->getFullName(); //sword 1.5.8
	savedKey = key->getText();

	if (level > m_depth) {
		m_depth = level;
	}

	if (key->hasChildren()) {
		key->firstChild();
		computeDepth(key, level+1);

		key->setText( savedKey.c_str() );//return to the initial value
	}

	if (key->nextSibling()) {
		computeDepth(key, level);
	}
}

/** Returns a treekey filled with the structure of this module */
sword::TreeKeyIdx* CSwordBookModuleInfo::tree() const {
	sword::TreeKeyIdx* treeKey = dynamic_cast<sword::TreeKeyIdx*>((sword::SWKey*)*(module()));
	Q_ASSERT(treeKey);
	return treeKey;
}