1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 /* AbiSource Application Framework
3  * Copyright (C) 1998 AbiSource, Inc.
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_PREFS_H
22 #define XAP_PREFS_H
23 
24 /* pre-emptive dismissal; ut_types.h is needed by just about everything,
25  * so even if it's commented out in-file that's still a lot of work for
26  * the preprocessor to do...
27  */
28 #ifndef UT_TYPES_H
29 #include "ut_types.h"
30 #endif
31 #include "ut_vector.h"
32 #include "ut_hash.h"
33 #include "ut_string.h"
34 #include "ut_string_class.h"
35 #include "ut_xml.h"
36 
37 #include "xap_Prefs_SchemeIds.h"
38 
39 #include <vector>
40 
41 /* For handling the position of windows on the screen, this sets up preferences */
42 enum {
43 	PREF_FLAG_GEOMETRY_POS = 0x1,     // Position is valid
44 	PREF_FLAG_GEOMETRY_SIZE = 0x2,    // Size is valid
45 	PREF_FLAG_GEOMETRY_NOUPDATE = 0x4, // User specified, don't update it
46 	PREF_FLAG_GEOMETRY_MAXIMIZED = 0x8 // Maximize at start
47 };
48 
49 class XAP_Prefs;
50 
51 typedef void (*PrefsListener) (
52 	XAP_Prefs			*pPrefs,
53 	UT_StringPtrMap	    *phChanges,
54 	void				*data
55 	);
56 
57 /*****************************************************************
58 ** XAP_PrefsScheme is a complete set of preferences.  it contains
59 ** all the info for a UI styling set.  use XAP_Prefs to switch
60 ** between schemes.
61 ******************************************************************/
62 
63 enum XAPPrefsLog_Level
64 {
65 	Log,
66 	Warning,
67 	Error
68 };
69 
70 class ABI_EXPORT XAP_FontSettings
71 {
72   public:
XAP_FontSettings()73 	XAP_FontSettings()
74 		:m_bInclude(false){};
75 
getFonts()76 	const std::vector<UT_UTF8String> & getFonts() const
77 		{return m_vecFonts;}
78 
addFont(const char * pFace)79 	void addFont (const char * pFace)
80 		{
81 			m_vecFonts.push_back (pFace);
82 		}
83 
haveFontsToExclude()84 	bool haveFontsToExclude () const {return (!m_bInclude && m_vecFonts.size());}
haveFontsToInclude()85 	bool haveFontsToInclude () const {return (m_bInclude && m_vecFonts.size());}
86 
87 	bool isOnExcludeList (const char * name) const;
88 
setIncludeFlag(bool bInclude)89 	void setIncludeFlag (bool bInclude) {m_bInclude = bInclude;}
getIncludeFlag()90 	bool getIncludeFlag () const {return m_bInclude;}
91 
92   private:
93 	std::vector<UT_UTF8String> m_vecFonts;
94 	bool m_bInclude;
95 };
96 
97 class ABI_EXPORT XAP_PrefsScheme
98 {
99 public:
100 	XAP_PrefsScheme(XAP_Prefs *pPrefs, const gchar * szSchemeName);
101 	~XAP_PrefsScheme(void);
102 
103 	const gchar *	getSchemeName(void) const;
104 	bool				setSchemeName(const gchar * szNewSchemeName);
105 	// The idea of the tick is that some object can cache a preference
106 	// value if it makes a performance difference.  It should also save
107 	// a copy of the tick count and the scheme pointer.  If the scheme
108 	// pointer and the tick count are the same, the cached preference
109 	// value is current.  If either is changed, the object can refresh
110 	// its cached value.  The scheme pointer can be different because
111 	// the preference scheme has changed.  The tick count bumps up once
112 	// every time any preference value in the scheme is changed.
getTickCount()113     UT_uint32			getTickCount() {return m_uTick;}
114 
115 	bool				setValue(const gchar * szKey, const gchar * szValue);
116 	bool				setValueBool(const gchar * szKey, bool bValue);
117 	bool				setValueInt(const gchar * szKey, const int nValue);
118 
119 	// the get*Value*() functions return the answer in the last
120 	// argument; they return error information as the function
121 	// return value.
122 	bool				getValue(const gchar * szKey, const gchar ** pszValue) const;
123 	bool				getValue(const UT_String &szKey, UT_String &szValue) const;
124 	bool                getValue(const char* szKey, std::string &szValue) const;
125 	bool				getValueInt(const gchar * szKey, int& nValue) const;
126 	bool				getValueBool(const gchar * szKey, bool * pbValue) const;
127 	bool				getNthValue(UT_uint32 k, const gchar ** pszKey, const gchar ** pszValue);
128 
129 protected:
130 	gchar *			m_szName;
131 	UT_GenericStringMap<gchar*> m_hash;
132 	UT_GenericVector<const gchar*> m_sortedKeys;
133 	bool				m_bValidSortedKeys;
134 	XAP_Prefs *			m_pPrefs;
135 	UT_uint32			m_uTick;   // ticks up every time setValue() or setValueBool() is called
136 };
137 
138 /*****************************************************************/
139 /*****************************************************************/
140 
141 class ABI_EXPORT XAP_Prefs : public UT_XML::Listener
142 {
143 public:
144 	XAP_Prefs();
145 	virtual ~XAP_Prefs(void);
146 
147 	bool					loadPrefsFile(void);
148 	bool					loadSystemDefaultPrefsFile(const char * szSystemDefaultPrefsPathname);
149 	bool					savePrefsFile(void);
150 
151 	XAP_PrefsScheme *		getNthScheme(UT_uint32 k) const;
152 	XAP_PrefsScheme *		getNthPluginScheme(UT_uint32 k) const;
153 	XAP_PrefsScheme *		getScheme(const gchar * szSchemeName) const;
154 	XAP_PrefsScheme *		getPluginScheme(const gchar * szSchemeName) const;
155 	bool					addScheme(XAP_PrefsScheme * pNewScheme);
156 	bool					addPluginScheme(XAP_PrefsScheme * pNewScheme);
157 	XAP_PrefsScheme *		getCurrentScheme(bool bCreate = false);
158 	bool					setCurrentScheme(const gchar * szSchemeName);
159 
160 	bool					getPrefsValue(const gchar * szKey, const gchar ** pszValue, bool bAllowBuiltin = true) const;
161 	bool					getPrefsValue(const UT_String &stKey, UT_String &stValue, bool bAllowBuiltin = true) const;
162 	bool					getPrefsValueBool(const gchar * szKey, bool * pbValue, bool bAllowBuiltin = true) const;
163 	bool					getPrefsValueInt(const gchar * szKey, int& nValue, bool bAllowBuiltin = true) const;
164 
165 	bool					getAutoSavePrefs(void) const;
166 	void					setAutoSavePrefs(bool bAuto);
167 
168 	bool					getUseEnvLocale(void) const;
169 	void					setUseEnvLocale(bool bUse);
170 
171 	UT_sint32				getMaxRecent(void) const;
172 	void					setMaxRecent(UT_sint32 k);
173 	UT_sint32				getRecentCount(void) const;
174 	const char *			getRecent(UT_sint32 k) const;		// one-based
175 	void					addRecent(const char * szRecent);
176 	void					removeRecent(UT_sint32 k);			// one-based
setIgnoreNextRecent(void)177 	void                    setIgnoreNextRecent(void)
178 		{ m_bIgnoreThisOne = true;}
isIgnoreRecent(void)179 	bool                    isIgnoreRecent(void)
180 	{ return m_bIgnoreThisOne;}
181 
182 	bool					setGeometry(UT_sint32 posx, UT_sint32 posy, UT_uint32 width, UT_uint32 height, UT_uint32 flags = 0);
183 	bool					getGeometry(UT_sint32 *posx, UT_sint32 *posy, UT_uint32 *width, UT_uint32 *height, UT_uint32 *flags = 0);
184 
185 	virtual void			fullInit(void) = 0;
186 	virtual bool			loadBuiltinPrefs(void) = 0;
187 	virtual const gchar *	getBuiltinSchemeName(void) const = 0;
188 	virtual const char *	getPrefsPathname(void) const = 0;
189 
190 	void					addListener	  ( PrefsListener pFunc, void *data );
191 	void					removeListener ( PrefsListener pFunc, void *data = 0 );
192 	void					startBlockChange();
193 	void					endBlockChange();
194 
195 	void                    log(const char * where, const char * what, XAPPrefsLog_Level level = Log);
196 
getFontSettings()197 	XAP_FontSettings &      getFontSettings () {return m_fonts;}
198 
199 	// a only-to-be-used-by XAP_PrefsScheme::setValue
200 	void					_markPrefChange	( const gchar *szKey );
201 protected:
202 	void					_pruneRecent(void);
203 	XAP_PrefsScheme * 		_getNthScheme(UT_uint32 k,
204 										  const UT_GenericVector<XAP_PrefsScheme *> &vecSchemes) const;
205 
206 	bool					m_bAutoSavePrefs; /* save on any changes or only when user asks */
207 	bool					m_bUseEnvLocale; /* use POSIX env vars to set locale */
208 
209 	UT_GenericVector<XAP_PrefsScheme *>	m_vecSchemes;		/* vector of XAP_PrefsScheme */
210 	UT_GenericVector<XAP_PrefsScheme *>	m_vecPluginSchemes;	/* vector of XAP_PrefsScheme */
211 	XAP_PrefsScheme *		m_currentScheme;
212 	XAP_PrefsScheme *		m_builtinScheme;
213 
214 	UT_sint32				m_iMaxRecent;
215 	UT_GenericVector<char*>	m_vecRecent;		/* vector of (char *) */
216 	UT_GenericVector<UT_UTF8String *> m_vecLog; /* vector of UT_UTF8String */
217 
218 	XAP_FontSettings        m_fonts;
219 
220 	typedef struct					/* used to store the listener/data pairs in the vector  */
221 	{
222 		PrefsListener	m_pFunc;
223 		void			*m_pData;
224 	} tPrefsListenersPair;
225 
226 	UT_GenericVector<tPrefsListenersPair *>	m_vecPrefsListeners;	/* vectory of struct PrefListnersPair */
227 	UT_StringPtrMap		m_ahashChanges;
228 	bool				m_bInChangeBlock;
229 	void				_sendPrefsSignal( UT_StringPtrMap *hash );
230 
231 	typedef struct {
232 		UT_uint32		m_width, m_height;	/* Default width and height */
233 		UT_sint32		m_posx, m_posy;		/* Default position */
234 		UT_uint32		m_flags;			/* What is valid, what is not */
235 	} Pref_Geometry;
236 
237 	Pref_Geometry		m_geom;
238 
239 
240 	/* Implementation of UT_XML::Listener */
241 public:
242 	void				startElement(const gchar *name, const gchar **atts);
243 	void				endElement(const gchar *name);
244 	void				charData(const gchar *s, int len);
245 private:
246 	void				_startElement_SystemDefaultFile(const gchar *name,
247 														const gchar **atts);
248 	bool				m_bLoadSystemDefaultFile;
249     bool                m_bIgnoreThisOne;
250 private:
251 	struct
252 	{
253 		bool			m_parserStatus;
254 		bool			m_bFoundAbiPreferences;
255 		bool			m_bFoundSelect;
256 		gchar *			m_szSelectedSchemeName;
257 		bool			m_bFoundRecent;
258 		bool			m_bFoundGeometry;
259 		bool            m_bFoundFonts;
260 	} m_parserState;
261 
262 };
263 
264 //////////////////////////////////////////////////////////////////////////////////////
265 // The following are the default values and limits for various non-scheme-based
266 // application-independent preferences.
267 
268 #define XAP_PREF_DEFAULT_AutoSavePrefs		"1"
269 #define XAP_PREF_DEFAULT_MaxRecent			"9"
270 #define XAP_PREF_DEFAULT_UseEnvLocale		"1"
271 
272 #define XAP_PREF_LIMIT_MaxRecent			9
273 
274 #endif /* XAP_PREFS_H */
275