tagging release
[dasher.git] / Src / Gtk2 / DasherAppSettings.cpp
blob2398f5e111a2e4ee536474c652f8b61a7a680c18
1 #include "config.h"
3 #ifdef WITH_GCONF
4 #include <gconf/gconf.h>
5 #include <gconf/gconf-client.h>
6 #include <gconf/gconf-enum-types.h>
7 #endif
9 #include "DasherAppSettings.h"
10 #include "../Common/AppSettingsData.h"
12 // FIXME - not sure that we're loading parameters from the registry at startup right now
14 // FIXME - shouldn't need to include this, but for now we need access to the global pointer to the dasher control.
16 #include "dasher.h"
17 #include "GtkDasherControl.h"
19 // FIXME - should really do something to make this a singleton class
21 // TODO: Rename this file to fit in with naming conventions
23 // TODO: Bring this into the widget
24 static GtkWidget *pDasherWidget = NULL;
27 struct _DasherAppSettingsPrivate {
28 #ifdef WITH_GCONF
29 // GConf interface
30 GConfClient *pGConfClient;
31 #endif
32 GtkDasherControl *pWidget;
33 gboolean bWidgetSet;
36 typedef struct _DasherAppSettingsPrivate DasherAppSettingsPrivate;
38 // Private member functions
40 static void dasher_app_settings_class_init(DasherAppSettingsClass *pClass);
41 static void dasher_app_settings_init(DasherAppSettings *pAppSettings);
42 static void dasher_app_settings_destroy(GObject *pObject);
44 static void dasher_app_settings_init_gconf(DasherAppSettings *pSelf, int argc, char **argv);
45 static void dasher_app_settings_stop_gconf(DasherAppSettings *pSelf);
46 static void dasher_app_settings_load(DasherAppSettings *pSelf);
48 // Function declarations
50 GType dasher_app_settings_get_type() {
52 static GType dasher_app_settings_type = 0;
54 if(!dasher_app_settings_type) {
55 static const GTypeInfo dasher_app_settings_info = {
56 sizeof(DasherAppSettingsClass),
57 NULL,
58 NULL,
59 (GClassInitFunc) dasher_app_settings_class_init,
60 NULL,
61 NULL,
62 sizeof(DasherAppSettings),
64 (GInstanceInitFunc) dasher_app_settings_init,
65 NULL
68 dasher_app_settings_type = g_type_register_static(G_TYPE_OBJECT, "DasherAppSettings", &dasher_app_settings_info, static_cast < GTypeFlags > (0));
71 return dasher_app_settings_type;
74 static void dasher_app_settings_class_init(DasherAppSettingsClass *pClass) {
75 GObjectClass *pObjectClass = (GObjectClass *) pClass;
76 pObjectClass->finalize = dasher_app_settings_destroy;
79 static void dasher_app_settings_init(DasherAppSettings *pDasherControl) {
80 DasherAppSettingsPrivate *pPrivate = new DasherAppSettingsPrivate;
81 pDasherControl->private_data = pPrivate;
83 #ifdef WITH_GCONF
84 pPrivate->pGConfClient = NULL;
85 #endif
86 pPrivate->pWidget = NULL;
87 pPrivate->bWidgetSet = FALSE;
90 static void dasher_app_settings_destroy(GObject *pObject) {
91 #ifdef WITH_GCONF
92 dasher_app_settings_stop_gconf((DasherAppSettings *)pObject);
93 #endif
95 for(int i(0); i < NUM_OF_APP_SPS; ++i)
96 delete[] app_stringparamtable[i].value;
98 // FIXME - glib routines?
99 // FIXME - do we need a typecast here?
100 delete (DasherAppSettingsPrivate *)(((DasherAppSettings *)pObject)->private_data);
102 // FIXME - I think we need to chain up through the finalize methods
103 // of the parent classes here...
106 void dasher_app_settings_set_widget(DasherAppSettings *pSelf, GtkDasherControl *pWidget) {
107 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate*)(pSelf->private_data);
109 pDasherWidget = GTK_WIDGET(pWidget);
111 pPrivate->pWidget = pWidget;
112 pPrivate->bWidgetSet = TRUE;
115 static void dasher_app_settings_init_gconf(DasherAppSettings *pSelf, int argc, char **argv) {
116 #ifdef WITH_GCONF
117 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate*)(pSelf->private_data);
119 GError *pGConfError;
121 if(!gconf_init(argc, argv, &pGConfError))
122 g_error("Failed to initilaise gconf: %s", pGConfError->message);
124 // FIXME - apparently there's a function gnome_gconf_get_client - maybe we should use this if building with gnome
125 pPrivate->pGConfClient = gconf_client_get_default();
126 #endif
129 static void dasher_app_settings_stop_gconf(DasherAppSettings *pSelf) {
130 #ifdef WITH_GCONF
131 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate*)(pSelf->private_data);
133 g_object_unref(pPrivate->pGConfClient);
134 #endif
137 static void dasher_app_settings_load(DasherAppSettings *pSelf) {
138 #ifdef WITH_GCONF
139 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
141 GError *pGConfError = NULL;
142 GConfValue *pGConfValue;
144 for(int i(0); i < NUM_OF_APP_BPS; ++i ) {
145 if(app_boolparamtable[i].persistent) {
146 gchar szName[256];
148 strncpy(szName, "/apps/dasher4/", 256);
149 strncat(szName, app_boolparamtable[i].regName, 255 - strlen( szName ));
151 pGConfValue = gconf_client_get_without_default(pPrivate->pGConfClient, szName, &pGConfError);
153 if(pGConfValue) {
154 app_boolparamtable[i].value = gconf_value_get_bool(pGConfValue);
156 gconf_value_free(pGConfValue);
161 for(int i(0); i < NUM_OF_APP_LPS; ++i ) {
162 if(app_longparamtable[i].persistent) {
163 gchar szName[256];
165 strncpy(szName, "/apps/dasher4/", 256);
166 strncat(szName, app_longparamtable[i].regName, 255 - strlen( szName ));
168 pGConfValue = gconf_client_get_without_default(pPrivate->pGConfClient, szName, &pGConfError);
170 if(pGConfValue) {
171 app_longparamtable[i].value = gconf_value_get_int(pGConfValue);
173 gconf_value_free(pGConfValue);
178 for(int i(0); i < NUM_OF_APP_SPS; ++i ) {
179 if(app_stringparamtable[i].persistent) {
180 gchar szName[256];
182 strncpy(szName, "/apps/dasher4/", 256);
183 strncat(szName, app_stringparamtable[i].regName, 255 - strlen( szName ));
185 pGConfValue = gconf_client_get_without_default(pPrivate->pGConfClient, szName, &pGConfError);
187 if(pGConfValue) {
188 delete[] app_stringparamtable[i].value;
190 const gchar *szValue(gconf_value_get_string(pGConfValue));
192 gchar *szNew;
193 szNew = new gchar[strlen(szValue) + 1];
194 strcpy(szNew, szValue);
196 app_stringparamtable[i].value = szNew;
197 gconf_value_free(pGConfValue);
201 #endif
204 // Public methods
206 DasherAppSettings *dasher_app_settings_new(int argc, char **argv) {
207 DasherAppSettings *pDasherControl;
208 pDasherControl = (DasherAppSettings *)(g_object_new(dasher_app_settings_get_type(), NULL));
210 for(int i(0); i < NUM_OF_APP_SPS; ++i) {
211 gchar *szNew;
212 szNew = new gchar[strlen(app_stringparamtable[i].szDefaultValue) + 1];
213 strcpy(szNew, app_stringparamtable[i].szDefaultValue);
214 app_stringparamtable[i].value = szNew;
216 #ifdef WITH_GCONF
217 dasher_app_settings_init_gconf(pDasherControl, argc, argv);
218 #endif
219 dasher_app_settings_load(pDasherControl);
221 return pDasherControl;
224 void dasher_app_settings_reset(DasherAppSettings *pSelf, int iParameter) {
225 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate*)(pSelf->private_data);
227 if(iParameter < END_OF_SPS) {
228 if(pPrivate->bWidgetSet)
229 gtk_dasher_control_reset_parameter(GTK_DASHER_CONTROL(pDasherWidget), iParameter);
230 return;
232 else {
233 // pre_parameter_notification(0, iParameter, 0);
235 if(iParameter < END_OF_APP_BPS)
236 app_boolparamtable[ iParameter - FIRST_APP_BP ].value = app_boolparamtable[ iParameter - FIRST_APP_BP ].bDefaultValue;
237 else if(iParameter < END_OF_APP_LPS)
238 app_longparamtable[ iParameter - FIRST_APP_LP ].value = app_longparamtable[ iParameter - FIRST_APP_LP ].iDefaultValue;
239 else {
240 delete[] app_stringparamtable[iParameter - FIRST_APP_SP].value;
242 gchar *szNew;
243 szNew = new gchar[strlen(app_stringparamtable[iParameter - FIRST_APP_SP].szDefaultValue) + 1];
244 strcpy(szNew, app_stringparamtable[iParameter - FIRST_APP_SP].szDefaultValue);
245 app_stringparamtable[iParameter - FIRST_APP_SP].value = szNew;
248 // TODO: Use real signals to achieve this
249 parameter_notification(0, iParameter, 0);
252 bool dasher_app_settings_get_bool(DasherAppSettings *pSelf, int iParameter) {
253 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate*)(pSelf->private_data);
254 if( iParameter < END_OF_BPS ) {
255 if(pPrivate->bWidgetSet)
256 return gtk_dasher_control_get_parameter_bool(GTK_DASHER_CONTROL(pDasherWidget), iParameter);
257 else
258 return false;
260 else
261 return app_boolparamtable[ iParameter - FIRST_APP_BP ].value;
264 void dasher_app_settings_set_bool(DasherAppSettings *pSelf, int iParameter, bool bValue) {
265 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
267 if( iParameter < END_OF_BPS ) {
268 if(pPrivate->bWidgetSet) {
269 gtk_dasher_control_set_parameter_bool(GTK_DASHER_CONTROL(pDasherWidget), iParameter, bValue);
272 else {
273 if(dasher_app_settings_get_bool(pSelf, iParameter) == bValue)
274 return; // Don't attempt to change to the existing value
276 app_boolparamtable[ iParameter - FIRST_APP_BP ].value = bValue;
277 #ifdef WITH_GCONF
278 if(app_boolparamtable[ iParameter - FIRST_APP_BP ].persistent) {
279 gchar szName[256];
281 strncpy(szName, "/apps/dasher4/", 256);
282 strncat(szName, app_boolparamtable[ iParameter - FIRST_APP_BP ].regName, 255 - strlen( szName ));
284 GError *pGConfError = NULL;
285 gconf_client_set_bool(pPrivate->pGConfClient, szName, bValue, &pGConfError);
287 if(pGConfError)
288 g_message("Error");
290 #endif
292 // TODO: Use real signals to achieve this
293 parameter_notification(0, iParameter, 0);
297 gint dasher_app_settings_get_long(DasherAppSettings *pSelf, int iParameter) {
298 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
300 if( iParameter < END_OF_LPS) {
301 if(pPrivate->bWidgetSet)
302 return gtk_dasher_control_get_parameter_long(GTK_DASHER_CONTROL(pDasherWidget), iParameter);
303 else
304 return false;
306 else
307 return app_longparamtable[ iParameter - FIRST_APP_LP ].value;
310 void dasher_app_settings_set_long(DasherAppSettings *pSelf, int iParameter, gint iValue) {
311 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
313 if( iParameter < END_OF_LPS) {
314 if(pPrivate->bWidgetSet)
315 gtk_dasher_control_set_parameter_long(GTK_DASHER_CONTROL(pDasherWidget), iParameter, iValue);
317 else {
318 if(dasher_app_settings_get_long(pSelf, iParameter) == iValue)
319 return; // Don't attempt to change to the existing value
321 // pre_parameter_notification(0, iParameter, 0);
323 app_longparamtable[ iParameter - FIRST_APP_LP ].value = iValue;
325 #ifdef WITH_GCONF
326 if(app_longparamtable[ iParameter - FIRST_APP_LP ].persistent) {
327 gchar szName[256];
329 strncpy(szName, "/apps/dasher4/", 256);
330 strncat(szName, app_longparamtable[ iParameter - FIRST_APP_LP ].regName, 255 - strlen( szName ));
332 GError *pGConfError = NULL;
333 gconf_client_set_int(pPrivate->pGConfClient, szName, iValue, &pGConfError);
335 #endif
337 // TODO: Use real signals to achieve this
338 parameter_notification(0, iParameter, 0);
342 gboolean dasher_app_settings_get_free_long(DasherAppSettings *pSelf, const gchar *szName, gint &iValue) {
343 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
345 gchar szFullName[256];
347 strncpy(szFullName, "/apps/dasher4/", 256);
348 strncat(szFullName, szName, 255 - strlen(szFullName));
350 GConfValue *pGConfValue;
351 GError *pGConfError = NULL;
353 pGConfValue = gconf_client_get_without_default(pPrivate->pGConfClient, szFullName, &pGConfError);
355 if(pGConfValue) {
356 iValue = gconf_value_get_int(pGConfValue);
357 gconf_value_free(pGConfValue);
358 return true;
360 else {
361 return false;
365 void dasher_app_settings_set_free_long(DasherAppSettings *pSelf, const gchar *szName, gint iValue) {
366 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
368 gchar szFullName[256];
370 strncpy(szFullName, "/apps/dasher4/", 256);
371 strncat(szFullName, szName, 255 - strlen(szFullName));
373 GError *pGConfError = NULL;
374 gconf_client_set_int(pPrivate->pGConfClient, szFullName, iValue, &pGConfError);
377 const gchar *dasher_app_settings_get_string(DasherAppSettings *pSelf, int iParameter) {
378 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
380 if( iParameter < END_OF_SPS ) {
381 if(pPrivate->bWidgetSet)
382 return gtk_dasher_control_get_parameter_string(GTK_DASHER_CONTROL(pDasherWidget), iParameter);
383 else
384 return false;
386 else
387 return app_stringparamtable[ iParameter - FIRST_APP_SP ].value;
390 void dasher_app_settings_set_string(DasherAppSettings *pSelf, int iParameter, const gchar *szValue) {
391 DasherAppSettingsPrivate *pPrivate = (DasherAppSettingsPrivate *)(pSelf->private_data);
393 if( iParameter < END_OF_SPS ) {
394 if(pPrivate->bWidgetSet)
395 gtk_dasher_control_set_parameter_string(GTK_DASHER_CONTROL(pDasherWidget), iParameter, szValue);
397 else {
398 if(!strcmp(dasher_app_settings_get_string(pSelf, iParameter), szValue))
399 return; // Don't attempt to change to the existing value
401 // pre_parameter_notification(0, iParameter, 0);
403 delete[] app_stringparamtable[ iParameter - FIRST_APP_SP ].value;
405 gchar *szNew;
406 szNew = new gchar[strlen(szValue) + 1];
407 strcpy(szNew, szValue);
409 app_stringparamtable[ iParameter - FIRST_APP_SP ].value = szNew;
411 #ifdef WITH_GCONF
412 if(app_stringparamtable[ iParameter - FIRST_APP_SP ].persistent) {
413 gchar szName[256];
415 strncpy(szName, "/apps/dasher4/", 256);
416 strncat(szName, app_stringparamtable[ iParameter - FIRST_APP_SP ].regName, 255 - strlen( szName ));
418 GError *pGConfError = NULL;
419 gconf_client_set_string(pPrivate->pGConfClient, szName, szValue, &pGConfError);
421 #endif
423 // TODO: Use real signals to achieve this
424 parameter_notification(0, iParameter, 0);
428 int dasher_app_settings_get_count(DasherAppSettings *pSelf) {
429 return END_OF_APP_SPS;
432 int dasher_app_settings_get_parameter_type(DasherAppSettings *pSelf, int iParameter) {
433 if(iParameter < END_OF_BPS)
434 return DASHER_TYPE_BOOL;
435 else if(iParameter < END_OF_LPS)
436 return DASHER_TYPE_LONG;
437 else if(iParameter < END_OF_SPS)
438 return DASHER_TYPE_STRING;
439 else if(iParameter < END_OF_APP_BPS)
440 return DASHER_TYPE_BOOL;
441 else if(iParameter < END_OF_APP_LPS)
442 return DASHER_TYPE_LONG;
443 else
444 return DASHER_TYPE_STRING;
447 const gchar *dasher_app_settings_get_reg_name(DasherAppSettings *pSelf, int iParameter) {
448 if(iParameter < END_OF_BPS)
449 return boolparamtable[iParameter - FIRST_BP].regName;
450 else if(iParameter < END_OF_LPS)
451 return longparamtable[iParameter - FIRST_LP].regName;
452 else if(iParameter < END_OF_SPS)
453 return stringparamtable[iParameter - FIRST_SP].regName;
454 else if(iParameter < END_OF_APP_BPS)
455 return app_boolparamtable[iParameter - FIRST_APP_BP].regName;
456 else if(iParameter < END_OF_APP_LPS)
457 return app_longparamtable[iParameter - FIRST_APP_LP].regName;
458 else
459 return app_stringparamtable[iParameter - FIRST_APP_SP].regName;
462 const gchar *dasher_app_settings_get_human_name(DasherAppSettings *pSelf, int iParameter) {
463 if(iParameter < END_OF_BPS)
464 return boolparamtable[iParameter - FIRST_BP].humanReadable;
465 else if(iParameter < END_OF_LPS)
466 return longparamtable[iParameter - FIRST_LP].humanReadable;
467 else if(iParameter < END_OF_SPS)
468 return stringparamtable[iParameter - FIRST_SP].humanReadable;
469 else if(iParameter < END_OF_APP_BPS)
470 return app_boolparamtable[iParameter - FIRST_APP_BP].humanReadable;
471 else if(iParameter < END_OF_APP_LPS)
472 return app_longparamtable[iParameter - FIRST_APP_LP].humanReadable;
473 else
474 return app_stringparamtable[iParameter - FIRST_APP_SP].humanReadable;
477 bool dasher_app_settings_have_advanced(DasherAppSettings *pSelf) {
478 return(g_find_program_in_path("gconf-editor") != NULL);
481 void dasher_app_settings_launch_advanced(DasherAppSettings *pSelf) {
482 gchar *szArgs[3];
484 szArgs[0] = "gconf-editor";
485 szArgs[1] = "/apps/dasher4";
486 szArgs[2] = NULL;
488 GError *pError;
490 if(!g_spawn_async(NULL, szArgs, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &pError)) {
491 g_warning("Could not launch gconf-editor: %s", pError->message);
495 void dasher_app_settings_set_widget(DasherAppSettings *pSelf, GtkWidget *pWidget) {
496 pDasherWidget = pWidget;
499 GArray *dasher_app_settings_get_allowed_values(DasherAppSettings *pSelf, int iParameter) {
500 return gtk_dasher_control_get_allowed_values(GTK_DASHER_CONTROL(pDasherWidget), iParameter);
503 gboolean dasher_app_settings_get_module_settings(DasherAppSettings *pSelf, const gchar *szValue, SModuleSettings **pSettings, gint *iCount) {
504 return gtk_dasher_control_get_module_settings(GTK_DASHER_CONTROL(pDasherWidget), szValue, pSettings, iCount);