summaryrefslogtreecommitdiffstats
path: root/src/utils.cpp
blob: dcbbe17fed7a30624864f45651d87de39510715e (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
209
210
211
212
/***************************************************************************
    File:         utils.cpp
    Project:      Kio-Sword -- An ioslave for SWORD and KDE
    Copyright:    Copyright (C) 2004-2005 Luke Plant
    Description:  Misc utility functions that don't belong anywhere else
 ***************************************************************************/

/***************************************************************************
 *   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 "utils.h"
#include "swordoptions.h"

#include <kurl.h>
#include <ntqstring.h>

namespace KioSword {

	const char* DEFBIBLE_STR = "bible";
	const char* GREEKSTRONGS_STR = "greekstrongs";
	const char* HEBREWSTRONGS_STR = "hebrewstrongs";
	const char* GREEKMORPH_STR = "greekmorph";
	const char* HEBREWMORPH_STR = "hebrewmorph";
	
	const char* SWORD_PROTOCOL = "sword";

	static void mergeOptionsToURL(KURL& url, const SwordOptions* options)
	{
		TQMap<TQString, TQString> items = options->getQueryStringParams();
		TQMap<TQString, TQString>::const_iterator it;
		TQMap<TQString, TQString>::const_iterator it_end = items.end();
		for(it = items.begin(); it != it_end; ++it) {
			url.addQueryItem(it.key(), it.data());
		}
	}
	
	static void mergeOptionsToURL(KURL& url, const SwordOptions& options)
	{
		return mergeOptionsToURL(url, &options);
	}
		
	static TQString htmlEncode(const TQString& text)
	{
		TQString output = text;
		return output
			.replace("&", "&amp;")
			.replace("<", "&lt;")
			.replace(">", "&gt;")
			.replace("\"", "&quot;");
	}
	
	/** Returns options that need to be propagated as HTML for a form */
	TQString optionsAsHiddenFields(const SwordOptions& options)
	{
		TQString output;
		TQMap<TQString, TQString> items = options.getQueryStringParams();
		TQMap<TQString, TQString>::const_iterator it;
		TQMap<TQString, TQString>::const_iterator it_end = items.end();
		for(it = items.begin(); it != it_end; ++it) {
			output += TQString("<input type=\"hidden\" name=\"%1\" value=\"%2\">")
					.arg(it.key())
					.arg(htmlEncode(it.data()));
		}
		return output;
	}
	
	/**
	* return a valid sword URL to be used in anchors
	*
	* Many key names have spaces in them, and even trailing spaces
	* which are stripped by Konqueror.  We must encode them to
	* ensure we can access them.
	*
	* @param path path to be encode
	*/
	TQString swordUrl(const TQString& path, const SwordOptions& options, bool htmlEncodeOutput) {
		TQString output;
		KURL url;
		url.setProtocol(SWORD_PROTOCOL);
		if (path.at(0) != '/')
			url.addPath("/");
		url.addPath(path);
		mergeOptionsToURL(url, options);
		if (htmlEncodeOutput)
			return htmlEncode(url.url(0, 106));  // return as utf-8
		else
			return url.url(0, 106);  // return as utf-8
	}
	
	/** 
	* return a valid sword URL to be used in anchors
	*
	* @param module name of module
	* @param reference within module
	*/
	TQString swordUrl(const TQString& module, const TQString& ref, const SwordOptions& options, bool htmlEncodeOutput) {
		if (ref.at(0) == '/')
			return swordUrl(module + ref, options, htmlEncodeOutput);
		else
			return swordUrl(module + "/" + ref, options, htmlEncodeOutput);
	}
	
	/**
	 * return a valid sword URL for 'pages' such as 'help', 'settings' etc,
	 * which are defined using a query parameter
	 */
	TQString swordUrlForPage(const TQString& page, const SwordOptions& options, bool htmlEncodeOutput)
	{
		TQString output;
		KURL url;
		url.setProtocol(SWORD_PROTOCOL);
		url.addPath("/");
		url.addQueryItem(page, "");
		mergeOptionsToURL(url, options);
		if (htmlEncodeOutput)
			return htmlEncode(url.url(0, 106));  // return as utf-8
		else
			return url.url(0, 106);  // return as utf-8
	}
	
	TQString swordUrlForSettings(const TQString& path, const SwordOptions& options, bool htmlEncodeOutput)
	{
		TQString output;
		KURL url;
		url.setProtocol(SWORD_PROTOCOL);
		url.addPath("/");
		url.addQueryItem("settings", "");
		
		// Up to KDE 3.5.2 at least, there is a bug in KURL::url which
		// doesn't take into account the encoding_hint for the query items,
		// so we can't use addQueryItem for anything which has non-ascii chars
		//   url.addQueryItem("previouspath", path);
		mergeOptionsToURL(url, options);
		output = url.url(0, 106);   // return as utf-8
		
		// Add 'previouspath' manually
		output += ( (url.queryItems().count() > 0) ? "&" : "?");
		output += "previouspath=" + KURL::encode_string(path, 106);
		
		if (htmlEncodeOutput)
			return htmlEncode(output);
		else
			return output;
	}
	
	/** Get a URL for doing a search */
	TQString swordUrlForSearch(DefModuleType modType, const TQString& searchQuery, const SwordOptions& options, bool htmlEncodeOutput)
	{
		return swordUrlForSearch(modType, searchQuery, &options, htmlEncodeOutput);
	}
	
	/** Get a URL for doing a search */
	TQString swordUrlForSearch(DefModuleType modType, const TQString& searchQuery, const SwordOptions* options, bool htmlEncodeOutput)
	{
		TQString modTypeStr;
		TQString output;
		KURL url;
		// FIXME - make this into a dictionary or something better?
		switch (modType)
		{
			case DEFBIBLE:
				modTypeStr = DEFBIBLE_STR;
				break;
			case GREEKSTRONGS:
				modTypeStr = GREEKSTRONGS_STR;
				break;
			case HEBREWSTRONGS:
				modTypeStr = HEBREWSTRONGS_STR;
				break;
			case GREEKMORPH:
				modTypeStr = GREEKMORPH_STR;
				break;
			case HEBREWMORPH:
				modTypeStr = HEBREWMORPH_STR;
				break;
			default:
				return output;
		}
		url.setProtocol(SWORD_PROTOCOL);
		url.addPath("/");
		url.addQueryItem("modtype", modTypeStr);
		url.addQueryItem("query", searchQuery);
		mergeOptionsToURL(url, options);
		if (htmlEncodeOutput)
			return htmlEncode(url.url(0, 106));  // return as utf-8
		else
			return url.url(0, 106);  // return as utf-8
	}
	
	/** truncate a string to len chars, adding an ellipsis if necessary */
	TQString shorten(const TQString &ref, uint len) {
		TQString output = ref.stripWhiteSpace();
		if (output.length() > len)
			output = output.left(len-2) + "...";
		return output;
	}
	
}