summaryrefslogtreecommitdiffstats
path: root/src/template.cpp
blob: 8c5090d95ef9e3155914985d51dca71f3047ea64 (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
/***************************************************************************
    File:         template.cpp
    Project:      Kio-Sword -- An ioslave for SWORD and KDE
    Copyright:    Copyright (C) 2005 Luke Plant
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "template.h"
#include "utils.h"
#include "swordoptions.h"

#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include <qstring.h>

namespace KioSword {

			
	// placeholders
	static const char* PAGETITLE = "{$pagetitle}";
	static const char* BASECSS   = "{$basecss}";
	static const char* CONTENT   = "{$content}";
	static const char* HOMELINK =  "{$homelink}";
	static const char* HOMELINKCAPTION = "{$homelinkcaption}";
	static const char* SEARCHLINK =  "{$searchlink}";
	static const char* SEARCHLINKCAPTION = "{$searchlinkcaption}";
	static const char* SETTINGSLINK =  "{$settingslink}";
	static const char* SETTINGSLINKCAPTION = "{$settingslinkcaption}";
	static const char* HELPLINK =  "{$helplink}";
	static const char* HELPLINKCAPTION = "{$helplinkcaption}";
	static const char* TOPNAV = "{$topnav}";
	static const char* BOTTOMNAV = "{$bottomnav}";
	static const char* TOGGLES = "{$toggles}";
	
	// static HTML fragments -------------------------------------------------------------------------------------------------------
	static const QString &html_page(QString("") + 
					"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "       // make W3C valid
					"\"http://www.w3.org/TR/html4/strict.dtd\">\n"
					"<html><head>\n"
					"<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>\n"
					"<title>" + PAGETITLE + "</title>\n"
					"<link rel=\"StyleSheet\" href=\"file:" + BASECSS + "\" TYPE=\"text/css\">\n"
					"</head>\n"
					"<body class=\"kiosword\">"
					"<div class=\"page\"><div class='inner'>"
					"	" + TOPNAV + "\n"
					"	<div class=\"content\"><div class='inner'>" + CONTENT + "</div></div>\n"
					"	" + TOGGLES + "\n"
					"	" + BOTTOMNAV + "\n"
					"	<div class=\"links\"><div class='inner'>\n"
					"		<ul>\n"
					"			<li><a accesskey=\"t\" href=\"" + HOMELINK + "\">" + HOMELINKCAPTION + "</a></li>\n"
					"			<li><a href=\"" + SEARCHLINK + "\">" + SEARCHLINKCAPTION + "</a></li>\n"
					"			<li><a href=\"" + SETTINGSLINK + "\">" + SETTINGSLINKCAPTION + "</a></li>\n"
					"			<li><a href=\"" + HELPLINK + "\">" + HELPLINKCAPTION + "</a></li>\n"
					"		</ul>\n"
					"	</div></div>\n"
					"</div></div>\n"
					"</body>\n"
					"</html>\n");

	Template::Template()
	{
 		m_showToggles = false;
 	}
 	
	QCString Template::render(const SwordOptions& options) const
	{
	
		QString cssdir = KGlobal::dirs()->findResourceDir("data", "kio_sword/kio_sword.css") + "kio_sword/";
	
		QString output = html_page;
		output = output
			.replace(HOMELINK, swordUrl("", options))
			.replace(HOMELINKCAPTION, i18n("Module list"))
			.replace(SEARCHLINK, swordUrlForPage("search", options))
			.replace(SEARCHLINKCAPTION, i18n("Search"))
			.replace(SETTINGSLINK, swordUrlForSettings(m_currentPath, options))
			.replace(SETTINGSLINKCAPTION,  i18n("Settings"))
			.replace(HELPLINK, swordUrlForPage("help", options))
			.replace(HELPLINKCAPTION, i18n("Help"))
			.replace(BASECSS,  cssdir + "kio_sword.css")
			.replace(PAGETITLE, m_title)
			.replace(CONTENT, m_content);
		if (!m_nav.isEmpty())
		{
			output = output
					.replace(TOPNAV, "<div class='navtop'><div class='inner'>" + m_nav + "</div></div>")
					.replace(BOTTOMNAV, "<div class='navbottom'><div class='inner'>" + m_nav + "</div></div>");
		}
		else
		{
			output = output
					.replace(TOPNAV, "")
					.replace(BOTTOMNAV, "");
		}
		if (m_showToggles)
		{
			QString toggles;
			SwordOptions toggledOptions(options);
			
			toggledOptions.verseNumbers.set(!toggledOptions.verseNumbers());
			toggles += "<li><a href=\"" + swordUrl(m_currentPath, toggledOptions) + "\" accesskey='v'>" 
				+ i18n("Verse Numbers") + "</a></li>";
			toggledOptions.verseNumbers.set(!toggledOptions.verseNumbers());
				
			toggledOptions.verseLineBreaks.set(!toggledOptions.verseLineBreaks());
			toggles += "<li><a href=\"" + swordUrl(m_currentPath, toggledOptions) + "\" accesskey='l'>" 
				+ i18n("Verse Line Breaks") + "</a></li>";
			toggledOptions.verseLineBreaks.set(!toggledOptions.verseLineBreaks());

			toggledOptions.strongs.set(!toggledOptions.strongs());
			toggles += "<li><a href=\"" + swordUrl(m_currentPath, toggledOptions) + "\" accesskey='s'>" 
				+ i18n("Strongs") + "</a></li>";
			toggledOptions.strongs.set(!toggledOptions.strongs());
			
			toggledOptions.morph.set(!toggledOptions.morph());
			toggles += "<li><a href=\"" + swordUrl(m_currentPath, toggledOptions) + "\" accesskey='m'>" 
				+ i18n("Morphological tags") + "</a></li>";
			toggledOptions.morph.set(!toggledOptions.morph());

			output = output.replace(TOGGLES, "<div class='toggles'><div class='inner'><ul>" + toggles + "</ul></div></div>");
			
		}
		else
		{
			output = output.replace(TOGGLES, "");
		}
		return output.utf8();
	}
	
	void Template::setTitle(const QString& title)
	{
		m_title = title;
	}
	
	void Template::setContent(const QString& content)
	{
		m_content = content;
	}
	
	void Template::setNav(const QString& nav)
	{
		m_nav = nav;
	}
	
	void Template::setCurrentPath(const QString& currentPath)
	{
		m_currentPath = currentPath;
	}
	
	void Template::setShowToggles(bool showToggles)
	{
		m_showToggles = showToggles;
	}
}