1 /* AbiSource Application Framework
2  * Copyright (C) 1998 AbiSource, Inc.
3  * Copyright (C) 2009 Hubert Figuiere
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA.
19  */
20 
21 #ifndef XAP_STRINGS_H
22 #define XAP_STRINGS_H
23 
24 #include <map>
25 #include <string>
26 
27 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
28  * so even if it's commented out in-file that's still a lot of work for
29  * the preprocessor to do...
30  */
31 #ifndef UT_TYPES_H
32 #include "ut_types.h"
33 #endif
34 #include "ut_xml.h"
35 #include "ut_vector.h"
36 #include "ut_string.h"
37 
38 class XAP_App;
39 
40 //////////////////////////////////////////////////////////////////
41 // build a table of XAP ID values
42 //////////////////////////////////////////////////////////////////
43 
44 #define dcl(id,s)					XAP_STRING_ID_##id,
45 
46 typedef enum _XAP_String_Id_Enum
47 {
48 	XAP_STRING_ID__FIRST__			= 0,	/* must be first */
49 #include "xap_String_Id.h"
50 	XAP_STRING_ID__LAST__					/* must be last */
51 } XAP_String_Id_Enum;
52 
53 #undef dcl
54 
55 //////////////////////////////////////////////////////////////////
56 // Both XAP_ and AP_ enum sets fold into XAP_String_Id
57 //////////////////////////////////////////////////////////////////
58 
59 typedef UT_uint32			XAP_String_Id;
60 
61 //////////////////////////////////////////////////////////////////
62 // base class provides interface regardless of how we got the strings
63 //////////////////////////////////////////////////////////////////
64 
65 class ABI_EXPORT XAP_StringSet
66 {
67 public:
68 	XAP_StringSet(XAP_App * pApp, const gchar * szLanguageName);
69 	virtual ~XAP_StringSet(void);
70 
71 	const gchar *			getLanguageName(void) const;
72 
73 	virtual const gchar *	getValue(XAP_String_Id id) const = 0;
74 
75 	bool getValue(XAP_String_Id id, const char * inEncoding, std::string &s) const;
76 	bool getValueUTF8(XAP_String_Id id, std::string &s) const;
77 
78 	void setEncoding(const char * inEndcoding);
79 	const char * getEncoding() const;
80 
81 protected:
82 	XAP_App *					m_pApp;
83 	const gchar *			m_szLanguageName;
84 
85  private:
86 	std::string m_encoding ;
87 };
88 
89 //////////////////////////////////////////////////////////////////
90 // a sub-class to wrap the compiled-in (english) strings
91 //////////////////////////////////////////////////////////////////
92 
93 class ABI_EXPORT XAP_BuiltinStringSet : public XAP_StringSet
94 {
95 public:
96 	XAP_BuiltinStringSet(XAP_App * pApp, const gchar * szLanguageName);
97 	virtual ~XAP_BuiltinStringSet(void);
98 
99 	virtual const gchar *	getValue(XAP_String_Id id) const;
100 
101 private:
102 	const gchar **			m_arrayXAP;
103 };
104 
105 //////////////////////////////////////////////////////////////////
106 // a sub-class to deal with disk-based string sets (translations)
107 //////////////////////////////////////////////////////////////////
108 
109 class ABI_EXPORT XAP_DiskStringSet : public XAP_StringSet, public UT_XML::Listener
110 {
111 public:
112 	XAP_DiskStringSet(XAP_App * pApp);
113 	virtual ~XAP_DiskStringSet(void);
114 
115 	virtual bool				setValue(XAP_String_Id id, const gchar * szString);
116 	virtual bool				setValue(const gchar * szId, const gchar * szString);
117 	virtual const gchar *	getValue(XAP_String_Id id) const;
118 	virtual bool				loadStringsFromDisk(const char * szFilename);
119 
120 	bool						setLanguage(const gchar * szLanguageName);
121 	void						setFallbackStringSet(XAP_StringSet * pFallback);
122 
123 public:
124 	/* Implementation of UT_XML::Listener
125 	 */
126 	void					startElement(const gchar *name, const gchar **atts);
127 	void					endElement(const gchar *name);
128 	void					charData(const gchar *s, int len);
129 
130 protected:
131 	XAP_StringSet *				m_pFallbackStringSet;
132 
133 private:
134 	UT_GenericVector<gchar*>	m_vecStringsXAP;
135 	std::map<std::string, UT_uint32> 	m_hash;
136 
137 	struct
138 	{
139 		bool				m_parserStatus;
140 	} m_parserState;
141 };
142 
143 #endif /* XAP_STRINGS_H */
144