summaryrefslogtreecommitdiff
path: root/src/bibletime_dbus.cpp
blob: e2976b5f1b719c2e838f9424f2b80c9e50dce554 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*********
*
* 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 "bibletime.h"

#include "frontend/cmdiarea.h"
#include "backend/config/cbtconfig.h"

#include "backend/keys/cswordversekey.h"

//Sword includes
#include <versekey.h>
#include <listkey.h>

//QT
#include <QList>
#include <QDebug>
#include <QMdiSubWindow>

//helper function
void BibleTime::syncAllModulesByType(const CSwordModuleInfo::ModuleType type, const QString& key) {
	qDebug() << "Syncing modules by type to key" << key.toLatin1();
	foreach (QMdiSubWindow* w, m_mdi->usableWindowList()) {
		CDisplayWindow* d = dynamic_cast<CDisplayWindow*>(w->widget());
		if (d && d->modules().count() && d->modules().first()->type() == type) {
			d->lookupKey(key);
		}
	}
}

void BibleTime::closeAllModuleWindows() {
	qDebug() << "DBUS: close all windows now...";
	m_mdi->deleteAll();
}

void BibleTime::syncAllBibles(const QString& key) {
	qDebug() << "DBUS: syncing all bibles ...";
	syncAllModulesByType(CSwordModuleInfo::Bible, key);
}

void BibleTime::syncAllCommentaries(const QString& key) {
	qDebug() << "DBUS: syncing all commentaries ...";
	syncAllModulesByType(CSwordModuleInfo::Commentary, key);
}

void BibleTime::syncAllLexicons(const QString& key) {
	qDebug() << "DBUS: syncing all lexicons ...";
	syncAllModulesByType(CSwordModuleInfo::Lexicon, key);
}

void BibleTime::syncAllVerseBasedModules(const QString& key) {
	qDebug() << "DBUS: syncing all verse based modules ...";
	syncAllModulesByType(CSwordModuleInfo::Bible, key);
	syncAllModulesByType(CSwordModuleInfo::Commentary, key);
}

void BibleTime::openWindow(const QString& moduleName, const QString& key) {
	qDebug() << "DBUS: open window for module" << moduleName.toLatin1() << "and key" << key.toLatin1();
	CSwordModuleInfo* module = CPointers::backend()->findModuleByName(moduleName);
	if (module) {
		createReadDisplayWindow(module, key);
	}
}

void BibleTime::openDefaultBible(const QString& key) {
	qDebug() << "DBUS: open default bible ...";
	CSwordModuleInfo* mod = CBTConfig::get(CBTConfig::standardBible);
	if (mod) {
		openWindow(mod->name(), key);
	}
}

QStringList BibleTime::searchInModule(const QString& moduleName, const QString& searchText) {
	qDebug() << "DBUS: searchInModule" << moduleName.toLatin1();
	QStringList ret;
	CSwordModuleInfo* mod = CPointers::backend()->findModuleByName(moduleName);

	if (mod) {
		//mod->search(searchText, CSwordModuleSearch::multipleWords, sword::ListKey());
		sword::ListKey scope;
		mod->searchIndexed( searchText, scope );

		sword::ListKey result = mod->searchResult();
		const QString lead = QString("[%1] ").arg(moduleName);
		;
		for ( int i = 0; i < result.Count(); ++i ) {
			sword::SWKey* key = result.getElement(i);
			Q_ASSERT(key);


			if (mod->type() == CSwordModuleInfo::Bible || mod->type() == CSwordModuleInfo::Commentary) {
				sword::VerseKey vk(key->getText());
				ret << lead + QString::fromUtf8( vk.getOSISRef() );
			}
			else {
				ret << lead + QString::fromUtf8( key->getText() );
			}
		}
	}

	return ret;

}

QStringList BibleTime::searchInOpenModules(const QString& searchText) {
	qDebug() << "DBUS: search in open modules ...";
	QStringList ret;
	foreach (QMdiSubWindow* subWindow,  m_mdi->subWindowList()) {
		if (CDisplayWindow* w = dynamic_cast<CDisplayWindow*>(subWindow->widget())) {
			QList<CSwordModuleInfo*> windowModules = w->modules();
			QList<CSwordModuleInfo*>::iterator end_it = windowModules.end();
			for (QList<CSwordModuleInfo*>::iterator it(windowModules.begin()); it != end_it; ++it) {
				ret += searchInModule((*it)->name(), searchText);
			}
		}
	}
	return ret;
}

QStringList BibleTime::searchInDefaultBible(const QString& searchText) {
	CSwordModuleInfo* bible = CBTConfig::get(CBTConfig::standardBible);
	return searchInModule(bible->name(), searchText);
}

QString BibleTime::getCurrentReference() {
	qDebug() << "BibleTime::getCurrentReference";
	QString ret = QString::null;
	
	QMdiSubWindow* activeSubWindow = m_mdi->activeSubWindow();
	if (!activeSubWindow) return ret;

	CDisplayWindow* w = dynamic_cast<CDisplayWindow*>(activeSubWindow->widget());

	if (w) {
		QString modType;
		Q_ASSERT(w->modules().first());
		switch (w->modules().first()->type()) {
			case CSwordModuleInfo::Bible:
				modType = "BIBLE";
				break;
			case CSwordModuleInfo::Commentary:
				modType = "COMMENTARY";
				break;
			case CSwordModuleInfo::GenericBook:
				modType = "BOOK";
				break;
			case CSwordModuleInfo::Lexicon:
				modType = "LEXICON";
				break;
			default:
				modType = "UNSUPPORTED";
				break;
		}

		ret.append("[").append(w->modules().first()->name()).append("] ");
		ret.append("[").append(modType).append("] ");

		CSwordVerseKey* vk = dynamic_cast<CSwordVerseKey*>( w->key() );
		if (vk) {
			ret.append( vk->getOSISRef() );
		}
		else {
			ret.append( w->key()->key() );
		}
	}

	return ret;
}

QStringList BibleTime::getModulesOfType(const QString& type) {
	QStringList ret;

	CSwordModuleInfo::ModuleType modType = CSwordModuleInfo::Unknown;
	if (type == "BIBLES") {
		modType = CSwordModuleInfo::Bible;
	}
	else if (type == "COMMENTARIES") {
		modType = CSwordModuleInfo::Commentary;
	}
	else if (type == "LEXICONS") {
		modType = CSwordModuleInfo::Lexicon;

	}
	else if (type == "BOOKS") {
		modType = CSwordModuleInfo::GenericBook;
	}

	QList<CSwordModuleInfo*> modList = CPointers::backend()->moduleList();
	for (QList<CSwordModuleInfo*>::iterator it( modList.begin() ); it != modList.end(); ++it) {
		if ((*it)->type() == modType) {
			ret.append( (*it)->name() );
		}
	}

	return ret;
}

void BibleTime::reloadModules() {
	slotSwordSetupChanged();
}