summaryrefslogtreecommitdiffstats
path: root/bibletime/backend/cdisplaytemplatemgr.cpp
blob: bb06e49af1e16220d5fea9747fc243929134dacb (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
//
// C++ Implementation: cdisplaytemplatemgr
//
// Description:
//
//
// Author: The BibleTime team <info@bibletime.info>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//

//BibleTime includes
#include "cdisplaytemplatemgr.h"
#include "cswordmoduleinfo.h"
#include "clanguagemgr.h"

#include "frontend/cbtconfig.h"

#include "util/cpointers.h"

#include "config.h"

//TQt includes
#include <tqstringlist.h>
#include <tqfile.h>
#include <tqfileinfo.h>

//KDE includes
#include <klocale.h>
#include <kstandarddirs.h>

#include <iostream>

CDisplayTemplateMgr::CDisplayTemplateMgr() {
	init();
	loadUserTemplates();
}

CDisplayTemplateMgr::~CDisplayTemplateMgr() {}


/*!
    \fn CDisplayTemplateMgr::fillTemplate( const TQString& name, const TQString& title, const TQString& content )
 */
const TQString CDisplayTemplateMgr::fillTemplate( const TQString& name, const TQString& content, Settings& settings ) {
	const TQString templateName = m_templateMap.contains(name) ? name : defaultTemplate();

	TQString displayTypeString;

	if (!settings.pageCSS_ID.isEmpty()) {
		displayTypeString = settings.pageCSS_ID;
	}
	else {
		if (settings.modules.count()) {
			switch (settings.modules.first()->type()) {

				case CSwordModuleInfo::Bible:
				displayTypeString = "bible";
				break;

				case CSwordModuleInfo::GenericBook:
				displayTypeString = "book";
				break;

				case CSwordModuleInfo::Commentary:

				case CSwordModuleInfo::Lexicon:

				default:
				displayTypeString = "singleentry";
				break;
			};
		}
		else { //use bible as default type if no modules are set
			displayTypeString = "bible";
		};
	}

	TQString newContent = content;
	const int moduleCount = settings.modules.count();

	if (moduleCount >= 2) {
		//create header for the modules
		TQString header;

		ListCSwordModuleInfo::iterator end_it = settings.modules.end();

		for (ListCSwordModuleInfo::iterator it(settings.modules.begin()); it != end_it; ++it) {
			header.append("<th style=\"width:")
			.append(TQString::number(int( 100.0 / (float)moduleCount )))
			.append("%;\">")
			.append((*it)->name())
			.append("</th>");
		}

		newContent.setLatin1("<table><tr>")
		.append(header)
		.append("</tr>")
		.append(content)
		.append("</table>");
	}

	TQString langCSS;
	CLanguageMgr::LangMap langMap = CPointers::languageMgr()->availableLanguages();

	for ( CLanguageMgr::LangMapIterator it( langMap ); it.current(); ++it ) {
		const CLanguageMgr::Language* lang = it.current();


		//if (lang->isValid() && CBTConfig::get(lang).first) {
		if (!lang->abbrev().isEmpty() && CBTConfig::get(lang).first) {
			const TQFont f = CBTConfig::get(lang).second;

			//don't use important, because it would reset the title formatting, etc. to the setup font
			TQString css("{ ");
			css.append("font-family:").append(f.family())/*.append(" !important")*/;
			css.append("; font-size:").append(TQString::number(f.pointSize())).append("pt /*!important*/");
			css.append("; font-weight:").append(f.bold() ? "bold" : "normal /*!important*/");
			css.append("; font-style:").append(f.italic() ? "italic" : "normal /*!important*/");
			css.append("; }\n");

			langCSS +=
				TQString("\n*[lang=%1] %2")
				.arg(lang->abbrev())
				.arg(css);
		}
	}

	//at first append the font standard settings for all languages without configured font
	CLanguageMgr::LangMapIterator it( langMap );

	const CLanguageMgr::Language* lang = it.current();

	if (lang && !lang->abbrev().isEmpty()/*&& lang->isValid()*/) {
		const TQFont standardFont = CBTConfig::getDefault(lang); //we just need a dummy lang param
		langCSS.prepend(
			TQString("\n#content {font-family:%1; font-size:%2pt; font-weight:%3; font-style: %4;}\n")
			.arg(standardFont.family())
			.arg(standardFont.pointSize())
			.arg(standardFont.bold() ? "bold" : "normal")
			.arg(standardFont.italic() ? "italic" : "normal")
		);
	}

// 	qWarning("Outputing unformated text");
	const TQString t = TQString(m_templateMap[ templateName ]) //don't change the map's content directly, use  a copy
	 		.replace("#TITLE#", settings.title)
	 		.replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? TQString("en") : settings.langAbbrev)
	 		.replace("#DISPLAYTYPE#", displayTypeString)
	 		.replace("#LANG_CSS#", langCSS)
	 		.replace("#PAGE_DIRECTION#", settings.pageDirection)
	 		.replace("#CONTENT#", newContent);
 	 //printf("%s\n\n", t.latin1());

	return t;
/*			TQString(m_templateMap[ templateName ]) //don't change the map's content directly, use  a copy
		   .replace("#TITLE#", settings.title)
		   .replace("#LANG_ABBREV#", settings.langAbbrev.isEmpty() ? TQString("en") : settings.langAbbrev)
		   .replace("#DISPLAYTYPE#", displayTypeString)
		   .replace("#LANG_CSS#", langCSS)
		   .replace("#PAGE_DIRECTION#", settings.pageDirection)
		   .replace("#CONTENT#", newContent);*/
}


/*!
    \fn CDisplayTemplateMgr::loadUserTemplates
 */
void CDisplayTemplateMgr::loadUserTemplates() {
	qDebug("Loading user templates");
	TQStringList files = KGlobal::dirs()->findAllResources("BT_DisplayTemplates");

	for ( TQStringList::iterator it( files.begin() ); it != files.end(); ++it) {
		qDebug("Found user template %s", (*it).latin1());

		TQFile f( *it );
		Q_ASSERT( f.exists() );

		if (f.open( IO_ReadOnly )) {
			TQString fileContent = TQTextStream( &f ).read();

			if (!fileContent.isEmpty()) {
				m_templateMap[ TQFileInfo(*it).fileName() + TQString(" ") + i18n("(user template)")] = fileContent;
			}
		}
	}
}

//Include the HTML templates which were put into a cpp file by a Perl script
#include "../display-templates/template-init.cpp"