Fix a Gtk warning when checking path input in the log viewer.
[anjuta-git-plugin.git] / libanjuta / anjuta-preferences.c
blob18614cf4a741974a07a744644ca81614e991eb56
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * preferences.c
4 * Copyright (C) 2000 - 2003 Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /**
22 * SECTION:anjuta-preferences
23 * @short_description: Anjuta Prefereces system.
24 * @see_also: #AnjutaPreferencesDialog
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-preferences.h
28 * #AnjutaPreferences is a way to let plugins register their preferences. There
29 * are mainly two ways a plugin could register its preferences in Anjuta.
31 * First is to not use #AnjutaPreferences at all. Simply register a
32 * preferences page in #AnjutaPreferencesDialog using the function
33 * anjuta_preferences_dialog_add_page(). The plugin should take
34 * care of loading, saving and widgets synchronization of the
35 * preferences values. It is particularly useful if the plugin
36 * uses gconf system for its preferences. Also no "changed"
37 * signal will be emitted from it.
39 * Second is to use anjuta_preferences_add_page(), which will
40 * automatically register the preferences keys and values from
41 * a glade xml file. The glade xml file contains a preferences
42 * page of the plugin. The widget names in the page are
43 * given in a particular way (see anjuta_preferences_add_page()) to
44 * let it know property key details. Loading, saving and
45 * widget synchronization are automatically done. "changed" signal is
46 * emitted when a preference is changed.
48 * anjuta_preferences_register_all_properties_from_glade_xml() only registers
49 * the preferences propery keys for automatic loading, saving and widget
50 * syncrhronization, but does not add the page in preferences dialog. It
51 * is useful if the plugin wants to show the preferences page somewhere else.
53 * anjuta_preferences_register_property_from_string() is similar to
54 * anjuta_preferences_register_all_properties_from_glade_xml(), but it only
55 * registers one property, the detail of which is given in its arguments.
56 * anjuta_preferences_register_property_custom() is used to register a
57 * property that uses a widget which is not supported by #AnjutaPreferences.
60 #ifdef HAVE_CONFIG_H
61 # include <config.h>
62 #endif
64 #include <sys/stat.h>
65 #include <unistd.h>
66 #include <string.h>
68 #include <glade/glade-parser.h>
69 #include <gconf/gconf-client.h>
70 #include <libgnomevfs/gnome-vfs.h>
72 #include <libanjuta/anjuta-preferences.h>
73 #include <libanjuta/anjuta-utils.h>
74 #include <libanjuta/resources.h>
75 #include <libanjuta/anjuta-debug.h>
76 #include <libanjuta/interfaces/ianjuta-preferences.h>
78 struct _AnjutaProperty
80 GtkWidget *object;
81 gchar *key;
82 gchar *default_value;
83 guint flags;
84 gint notify_id;
85 GConfClient *gclient;
87 /* Set true if custom set/get to be used */
88 gboolean custom;
90 /* For inbuilt generic objects */
91 AnjutaPropertyObjectType object_type;
92 AnjutaPropertyDataType data_type;
94 /* For custom objects */
95 void (*set_property) (AnjutaProperty *prop, const gchar *value);
96 gchar * (*get_property) (AnjutaProperty *prop);
100 struct _AnjutaPreferencesPriv
102 GConfClient *gclient;
103 GHashTable *properties;
104 GtkWidget *prefs_dialog;
105 AnjutaPluginManager *plugin_manager;
106 gboolean is_showing;
109 /* Internal structure for anjuta_preferences_foreach */
110 struct _AnjutaPreferencesForeachData
112 AnjutaPreferences *pr;
113 AnjutaPreferencesFilterType filter;
114 AnjutaPreferencesCallback callback;
115 gpointer callback_data;
118 #define PREFERENCE_PROPERTY_PREFIX "preferences_"
119 #define GCONF_KEY_PREFIX "/apps/anjuta/preferences"
121 static const gchar *
122 build_key (const gchar *key)
124 static gchar buffer[1024];
125 snprintf (buffer, 1024, "%s/%s", GCONF_KEY_PREFIX, key);
126 return buffer;
130 * anjuta_preferences_get:
131 * @pr: A #AnjutaPreferences object
132 * @key: Property key
134 * Gets the value of @key as string. Returned string should be g_freed() when not
135 * required.
137 * Return value: Key value as string or NULL if the key is not defined.
139 #ifdef __GNUC__
140 inline
141 #endif
142 gchar *
143 anjuta_preferences_get (AnjutaPreferences *pr, const gchar *key)
145 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), NULL);
146 g_return_val_if_fail (key != NULL, NULL);
148 return gconf_client_get_string (pr->priv->gclient, build_key (key), NULL);
152 * anjuta_preferences_get_list:
153 * @pr: A #AnjutaPreferences object
154 * @key: Property key
155 * @list_type: Type of each list element
157 * Gets the list of @key.
159 * Return value: Key list or NULL if the key is not defined.
161 #ifdef __GNUC__
162 inline
163 #endif
164 GSList *
165 anjuta_preferences_get_list (AnjutaPreferences *pr, const gchar *key,
166 GConfValueType list_type)
168 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), NULL);
169 g_return_val_if_fail (key != NULL, NULL);
171 return gconf_client_get_list(pr->priv->gclient, build_key (key), list_type, NULL);
175 * anjuta_preferences_get_pair:
176 * @pr: A #AnjutaPreferences object
177 * @key: Property key
178 * @car_type: Desired type of the pair's first field (car).
179 * @cdr_type: Desired type of the pair's second field (cdr).
180 * @car_retloc: Address of a return location for the car.
181 * @cdr_retloc: Address of a return location for the cdr.
183 * Gets the pair of @key.
185 * Return value: TRUE or FALSE.
187 #ifdef __GNUC__
188 inline
189 #endif
190 gboolean
191 anjuta_preferences_get_pair (AnjutaPreferences *pr, const gchar *key,
192 GConfValueType car_type, GConfValueType cdr_type,
193 gpointer car_retloc, gpointer cdr_retloc)
195 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), FALSE);
196 g_return_val_if_fail (key != NULL, FALSE);
198 return gconf_client_get_pair(pr->priv->gclient, build_key (key),
199 car_type, cdr_type,
200 car_retloc, cdr_retloc, NULL);
204 * anjuta_preferences_get_int:
205 * @pr: A #AnjutaPreferences object
206 * @key: Property key
208 * Gets the value of @key as integer.
210 * Return value: Key value as integer or 0 if the key is not defined.
212 #ifdef __GNUC__
213 inline
214 #endif
215 gint
216 anjuta_preferences_get_int (AnjutaPreferences *pr, const gchar *key)
218 gint ret_val;
219 GConfValue *value;
221 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), 0);
222 g_return_val_if_fail (key != NULL, 0);
224 ret_val = 0;
225 value = gconf_client_get (pr->priv->gclient, build_key (key), NULL);
226 if (value)
228 switch (value->type)
230 case GCONF_VALUE_INT:
231 ret_val = gconf_value_get_int (value);
232 break;
233 case GCONF_VALUE_BOOL:
234 ret_val = gconf_value_get_bool (value);
235 break;
236 default:
237 g_warning ("Invalid gconf type for key: %s", key);
239 gconf_value_free (value);
241 /* else
242 g_warning ("The preference key %s is not defined", key); */
243 return ret_val;
247 * anjuta_preferences_get_int_with_default:
248 * @pr: A #AnjutaPreferences object
249 * @key: Property key
250 * @default_value: Default value to return if the key is not defined.
252 * Gets the value of @key as integer.
254 * Return value: Key value as integer or @default_value if the
255 * key is not defined.
257 #ifdef __GNUC__
258 inline
259 #endif
260 gint
261 anjuta_preferences_get_int_with_default (AnjutaPreferences *pr,
262 const gchar *key, gint default_value)
264 gint ret_val;
265 GConfValue *value;
267 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), 0);
268 g_return_val_if_fail (key != NULL, 0);
270 ret_val = default_value;
271 value = gconf_client_get (pr->priv->gclient, build_key (key), NULL);
272 if (value)
274 switch (value->type)
276 case GCONF_VALUE_INT:
277 ret_val = gconf_value_get_int (value);
278 break;
279 case GCONF_VALUE_BOOL:
280 ret_val = gconf_value_get_bool (value);
281 break;
282 default:
283 g_warning ("Invalid gconf type for key: %s", key);
285 gconf_value_free (value);
287 return ret_val;
291 * anjuta_preferences_default_get:
292 * @pr: A #AnjutaPreferences object
293 * @key: Property key
295 * Gets the default value of @key as string. The default value of the key
296 * is the value defined in System defaults (generally installed during
297 * program installation). Returned value must be g_freed() when not required.
299 * Return value: Default key value as string or NULL if not defined.
301 #ifdef __GNUC__
302 inline
303 #endif
304 gchar *
305 anjuta_preferences_default_get (AnjutaPreferences * pr, const gchar * key)
307 GConfValue *val;
308 gchar *str;
309 GError *err = NULL;
311 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), NULL);
312 g_return_val_if_fail (key != NULL, NULL);
314 val = gconf_client_get_default_from_schema (pr->priv->gclient, build_key (key), &err);
315 if (err) {
316 g_error_free (err);
317 return NULL;
319 str = g_strdup (gconf_value_get_string (val));
320 gconf_value_free (val);
321 return str;
325 * anjuta_preferences_default_get_int:
326 * @pr: A #AnjutaPreferences object
327 * @key: Property key
329 * Gets the default value of @key as integer. The default value of the key
330 * is the value defined in System defaults (generally installed during
331 * program installation).
333 * Return value: Default key value as integer or 0 if the key is not defined.
335 #ifdef __GNUC__
336 inline
337 #endif
338 gint
339 anjuta_preferences_default_get_int (AnjutaPreferences *pr, const gchar *key)
341 GConfValue *val;
342 gint ret;
343 GError *err = NULL;
345 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), 0);
346 g_return_val_if_fail (key != NULL, 0);
347 val = gconf_client_get_default_from_schema (pr->priv->gclient, build_key (key), &err);
348 if (err) {
349 g_error_free (err);
350 return 0;
352 ret = gconf_value_get_int (val);
353 gconf_value_free (val);
354 return ret;
358 * anjuta_preferences_set:
359 * @pr: A #AnjutaPreferences object.
360 * @key: Property key.
361 * @value: Value of the key.
363 * Sets the value of @key in current session.
365 #ifdef __GNUC__
366 inline
367 #endif
368 void
369 anjuta_preferences_set (AnjutaPreferences *pr, const gchar *key,
370 const gchar *value)
372 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
373 g_return_if_fail (key != NULL);
375 if (value && (strlen (value) > 0))
377 gconf_client_set_string (pr->priv->gclient, build_key (key), value, NULL);
379 else
381 gconf_client_set_string (pr->priv->gclient, build_key (key), "", NULL);
386 * anjuta_preferences_set_list:
387 * @pr: A #AnjutaPreferences object.
388 * @key: Property key.
389 * @list_type: Type of each element.
390 * @list: New value of the key.
392 * Sets a list in current session.
394 #ifdef __GNUC__
395 inline
396 #endif
397 void
398 anjuta_preferences_set_list (AnjutaPreferences *pr, const gchar *key,
399 GConfValueType list_type, GSList *list)
401 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
402 g_return_if_fail (key != NULL);
404 gconf_client_set_list(pr->priv->gclient, build_key (key),
405 list_type, list, NULL);
409 * anjuta_preferences_set_pair:
410 * @pr: A #AnjutaPreferences object.
411 * @key: Property key.
412 * @car_type: Type of the pair's first field (car).
413 * @cdr_type: Type of the pair's second field (cdr).
414 * @address_of_car: Address of the car.
415 * @address_of_cdr: Address of the cdr.
418 #ifdef __GNUC__
419 inline
420 #endif
421 gboolean
422 anjuta_preferences_set_pair (AnjutaPreferences *pr, const gchar *key,
423 GConfValueType car_type, GConfValueType cdr_type,
424 gconstpointer address_of_car,
425 gconstpointer address_of_cdr)
427 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), FALSE);
428 g_return_val_if_fail (key != NULL, FALSE);
430 return gconf_client_set_pair (pr->priv->gclient, build_key (key),
431 car_type, cdr_type,
432 address_of_car, address_of_cdr,
433 NULL);
437 * anjuta_preferences_set_int:
438 * @pr: A #AnjutaPreferences object.
439 * @key: Property key.
440 * @value: Integer value of the key.
442 * Sets the value of @key in current session.
444 #ifdef __GNUC__
445 inline
446 #endif
447 void
448 anjuta_preferences_set_int (AnjutaPreferences *pr, const gchar *key,
449 gint value)
451 GConfValue *gvalue;
453 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
454 g_return_if_fail (key != NULL);
456 gvalue = gconf_client_get (pr->priv->gclient, build_key (key), NULL);
457 if (gvalue)
459 switch (gvalue->type)
461 case GCONF_VALUE_BOOL:
462 gconf_client_set_bool (pr->priv->gclient, build_key (key),
463 value, NULL);
464 break;
465 case GCONF_VALUE_INT:
466 gconf_client_set_int (pr->priv->gclient, build_key (key),
467 value, NULL);
468 break;
469 default:
470 g_warning ("Invalid gconf type for key: %s", key);
472 gconf_value_free (gvalue);
474 else
476 /* g_warning ("The preference key %s is not defined", key); */
477 gconf_client_set_int (pr->priv->gclient, build_key (key),
478 value, NULL);
482 static void
483 property_destroy (AnjutaProperty *property)
485 g_return_if_fail (property);
486 if (property->key) g_free (property->key);
487 if (property->default_value) g_free (property->default_value);
488 g_object_unref (property->object);
489 gconf_client_notify_remove (property->gclient, property->notify_id);
490 g_free (property);
494 * anjuta_property_get_widget:
495 * @prop: an #AnjutaProperty reference
497 * Gets the widget associated with the property.
499 * Returns: a #GtkWidget object associated with the property.
501 GtkWidget*
502 anjuta_property_get_widget (AnjutaProperty *prop)
504 return prop->object;
507 static AnjutaPropertyObjectType
508 get_object_type_from_string (const gchar* object_type)
510 if (strcmp (object_type, "entry") == 0)
511 return ANJUTA_PROPERTY_OBJECT_TYPE_ENTRY;
512 else if (strcmp (object_type, "combo") == 0)
513 return ANJUTA_PROPERTY_OBJECT_TYPE_COMBO;
514 else if (strcmp (object_type, "spin") == 0)
515 return ANJUTA_PROPERTY_OBJECT_TYPE_SPIN;
516 else if (strcmp (object_type, "toggle") == 0)
517 return ANJUTA_PROPERTY_OBJECT_TYPE_TOGGLE;
518 else if (strcmp (object_type, "text") == 0)
519 return ANJUTA_PROPERTY_OBJECT_TYPE_TEXT;
520 else if (strcmp (object_type, "color") == 0)
521 return ANJUTA_PROPERTY_OBJECT_TYPE_COLOR;
522 else if (strcmp (object_type, "font") == 0)
523 return ANJUTA_PROPERTY_OBJECT_TYPE_FONT;
524 else if (strcmp (object_type, "file") == 0)
525 return ANJUTA_PROPERTY_OBJECT_TYPE_FILE;
526 else if (strcmp (object_type, "folder") == 0)
527 return ANJUTA_PROPERTY_OBJECT_TYPE_FOLDER;
528 else
529 return (AnjutaPropertyObjectType)(-1);
532 static AnjutaPropertyDataType
533 get_data_type_from_string (const gchar* data_type)
535 if (strcmp (data_type, "bool") == 0)
536 return ANJUTA_PROPERTY_DATA_TYPE_BOOL;
537 else if (strcmp (data_type, "int") == 0)
538 return ANJUTA_PROPERTY_DATA_TYPE_INT;
539 else if (strcmp (data_type, "text") == 0)
540 return ANJUTA_PROPERTY_DATA_TYPE_TEXT;
541 else if (strcmp (data_type, "color") == 0)
542 return ANJUTA_PROPERTY_DATA_TYPE_COLOR;
543 else if (strcmp (data_type, "font") == 0)
544 return ANJUTA_PROPERTY_DATA_TYPE_FONT;
545 else
546 return (AnjutaPropertyDataType)(-1);
549 static gchar*
550 get_property_value_as_string (AnjutaProperty *prop)
552 gint int_value;
553 gchar** values;
554 gchar *text_value = NULL;
556 if (prop->custom)
558 if (prop->get_property != NULL)
559 return prop->get_property (prop);
560 else
562 g_warning ("%s: Undefined get_property() for custom object",
563 prop->key);
564 return NULL;
567 switch (prop->object_type)
569 case ANJUTA_PROPERTY_OBJECT_TYPE_TOGGLE:
570 int_value =
571 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (prop->object));
572 text_value = g_strdup_printf ("%d", int_value);
573 break;
575 case ANJUTA_PROPERTY_OBJECT_TYPE_SPIN:
576 int_value =
577 gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (prop->object));
578 text_value = g_strdup_printf ("%d", int_value);
579 break;
581 case ANJUTA_PROPERTY_OBJECT_TYPE_ENTRY:
582 text_value =
583 gtk_editable_get_chars (GTK_EDITABLE (prop->object), 0, -1);
584 break;
585 case ANJUTA_PROPERTY_OBJECT_TYPE_COMBO:
587 gint idx;
588 values = g_object_get_data(G_OBJECT(prop->object), "untranslated");
589 idx = gtk_combo_box_get_active(GTK_COMBO_BOX(prop->object));
590 if (values[idx] != NULL)
591 text_value = g_strdup(values[idx]);
592 break;
594 case ANJUTA_PROPERTY_OBJECT_TYPE_TEXT:
596 GtkTextBuffer *buffer;
597 GtkTextIter start_iter;
598 GtkTextIter end_iter;
599 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (prop->object));
600 gtk_text_buffer_get_start_iter (buffer, &start_iter);
601 gtk_text_buffer_get_end_iter (buffer, &end_iter);
602 text_value =
603 gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, TRUE);
604 break;
606 case ANJUTA_PROPERTY_OBJECT_TYPE_COLOR:
608 GdkColor color;
609 gtk_color_button_get_color(GTK_COLOR_BUTTON (prop->object),
610 &color);
611 text_value = anjuta_util_string_from_color (color.red, color.green, color.blue);
613 break;
614 case ANJUTA_PROPERTY_OBJECT_TYPE_FONT:
616 const gchar *font;
617 font = gtk_font_button_get_font_name (GTK_FONT_BUTTON
618 (prop->object));
619 text_value = g_strdup (font);
621 break;
622 case ANJUTA_PROPERTY_OBJECT_TYPE_FOLDER:
623 case ANJUTA_PROPERTY_OBJECT_TYPE_FILE:
624 text_value = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (prop->object));
625 break;
627 if (text_value && (strlen (text_value) == 0))
629 g_free (text_value);
630 text_value = NULL;
632 return text_value;
635 static gint
636 get_property_value_as_int (AnjutaProperty *prop)
638 gint int_value;
639 gchar *text_value;
640 text_value = get_property_value_as_string (prop);
641 int_value = atoi (text_value);
642 g_free (text_value);
643 return int_value;
646 static void
647 set_property_value_as_string (AnjutaProperty *prop, const gchar *value)
649 gint int_value;
650 char** values;
651 gint i;
653 if (prop->custom)
655 if (prop->set_property != NULL)
657 prop->set_property (prop, value);
658 return;
660 else
662 g_warning ("%s: Undefined set_property() for custom object",
663 prop->key);
664 return;
667 switch (prop->object_type)
669 case ANJUTA_PROPERTY_OBJECT_TYPE_TOGGLE:
670 if (value)
671 int_value = atoi (value);
672 else
673 int_value = 0;
675 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (prop->object),
676 int_value);
677 break;
679 case ANJUTA_PROPERTY_OBJECT_TYPE_SPIN:
680 if (value)
681 int_value = atoi (value);
682 else
683 int_value = 0;
685 gtk_spin_button_set_value (GTK_SPIN_BUTTON (prop->object), int_value);
686 break;
688 case ANJUTA_PROPERTY_OBJECT_TYPE_ENTRY:
689 if (value)
690 gtk_entry_set_text (GTK_ENTRY (prop->object), value);
691 else
692 gtk_entry_set_text (GTK_ENTRY (prop->object), "");
693 break;
694 case ANJUTA_PROPERTY_OBJECT_TYPE_COMBO:
695 values = g_object_get_data(G_OBJECT(prop->object), "untranslated");
696 if (value != NULL)
698 for (i=0; values[i] != NULL; i++)
700 if (strcmp(value, values[i]) == 0)
702 gtk_combo_box_set_active(GTK_COMBO_BOX(prop->object), i);
703 break;
707 break;
708 case ANJUTA_PROPERTY_OBJECT_TYPE_TEXT:
710 GtkTextBuffer *buffer;
711 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (prop->object));
712 if (value)
713 gtk_text_buffer_set_text (buffer, value, -1);
714 else
715 gtk_text_buffer_set_text (buffer, "", -1);
717 break;
719 case ANJUTA_PROPERTY_OBJECT_TYPE_COLOR:
721 GdkColor color;
723 if (value)
724 anjuta_util_color_from_string (value, &color.red, &color.green, &color.blue);
726 gtk_color_button_set_color(GTK_COLOR_BUTTON(prop->object), &color);
728 break;
730 case ANJUTA_PROPERTY_OBJECT_TYPE_FONT:
731 if (value)
733 /* If the font name is Xfont name, convert it into
734 Pango font description text -- Just take the family name :) */
735 if (value[0] == '-')
737 /* Font is xfont name */
738 gchar *font_name, *tmp;
739 const gchar *end, *start;
740 start = value;
741 start = g_strstr_len (&start[1], strlen (&start[1]), "-");
742 end = g_strstr_len (&start[1], strlen (&start[1]), "-");
743 font_name = g_strndup (&start[1], end-start-1);
744 tmp = font_name;
746 /* Set font size to (arbitrary) 12 points */
747 font_name = g_strconcat (tmp, " 12", NULL);
748 g_free (tmp);
750 /* DEBUG_PRINT ("Font set as: %s", font_name); */
752 gtk_font_button_set_font_name (GTK_FONT_BUTTON
753 (prop->object), font_name);
754 g_free (font_name);
756 else
758 gtk_font_button_set_font_name (GTK_FONT_BUTTON
759 (prop->object), value);
762 /* FIXME: Set a standard font as default.
763 else
765 gnome_font_picker_set_font_name (GNOME_FONT_PICKER (prop->object),
766 "A standard font");
768 break;
770 case ANJUTA_PROPERTY_OBJECT_TYPE_FOLDER:
771 if (value)
773 gchar *old_folder;
775 /* When the user change the folder, the
776 * current-folder-changed signal is emitted the
777 * gconf key is updated and this function is called.
778 * But setting the current folder here emits
779 * the current-folder-changed signal too.
780 * Moreover this signal is emitted asynchronously so
781 * it is not possible to block it here.
783 * The solution here is to update the widget only
784 * if it is really needed.
786 old_folder = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (prop->object));
787 if ((old_folder == NULL) || strcmp (old_folder, value))
789 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (prop->object), value);
791 g_free (old_folder);
793 break;
794 case ANJUTA_PROPERTY_OBJECT_TYPE_FILE:
795 if (value)
797 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (prop->object),
798 value);
800 break;
804 static void
805 set_property_value_as_int (AnjutaProperty *prop, gint value)
807 gchar *text_value;
808 text_value = g_strdup_printf ("%d", value);
809 set_property_value_as_string (prop, text_value);
810 g_free (text_value);
813 static gboolean
814 update_property_on_event_str (GtkWidget *widget, GdkEvent *event,
815 gpointer user_data)
817 AnjutaPreferences *pr;
818 AnjutaProperty *p;
819 gchar *val;
821 pr = ANJUTA_PREFERENCES (g_object_get_data (G_OBJECT (widget),
822 "AnjutaPreferences"));
823 p = (AnjutaProperty *) user_data;
824 val = get_property_value_as_string (p);
825 anjuta_preferences_set (pr, p->key, val);
826 g_free (val);
827 return FALSE;
830 static void
831 update_property_on_change_str (GtkWidget *widget, gpointer user_data)
833 AnjutaPreferences *pr;
834 AnjutaProperty *p;
835 gchar *val;
837 pr = ANJUTA_PREFERENCES (g_object_get_data (G_OBJECT (widget),
838 "AnjutaPreferences"));
839 p = (AnjutaProperty *) user_data;
840 val = get_property_value_as_string (p);
841 anjuta_preferences_set (pr, p->key, val);
842 g_free (val);
845 static gboolean
846 block_update_property_on_change_str (GtkWidget *widget, GdkEvent *event,
847 gpointer user_data)
849 AnjutaProperty *p = (AnjutaProperty *) user_data;
851 gtk_signal_handler_block_by_func (GTK_OBJECT(p->object), G_CALLBACK (update_property_on_change_str), p);
852 return FALSE;
855 static gboolean
856 unblock_update_property_on_change_str (GtkWidget *widget, GdkEvent *event,
857 gpointer user_data)
859 AnjutaProperty *p = (AnjutaProperty *) user_data;
861 gtk_signal_handler_unblock_by_func (GTK_OBJECT(p->object), G_CALLBACK (update_property_on_change_str), p);
862 return FALSE;
865 static void
866 update_property_on_change_int (GtkWidget *widget, gpointer user_data)
868 AnjutaPreferences *pr;
869 AnjutaProperty *p;
870 gint val;
872 pr = ANJUTA_PREFERENCES (g_object_get_data (G_OBJECT (widget),
873 "AnjutaPreferences"));
874 p = (AnjutaProperty *) user_data;
875 val = get_property_value_as_int (p);
876 anjuta_preferences_set_int (pr, p->key, val);
879 static void
880 update_property_on_change_color (GtkWidget *widget, gpointer user_data)
882 AnjutaPreferences *pr;
883 AnjutaProperty *p;
884 gchar *val;
886 pr = ANJUTA_PREFERENCES (g_object_get_data (G_OBJECT (widget),
887 "AnjutaPreferences"));
888 p = (AnjutaProperty *) user_data;
889 val = get_property_value_as_string (p);
890 anjuta_preferences_set (pr, p->key, val);
891 g_free (val);
894 static void
895 update_property_on_change_font (GtkWidget *widget,
896 gpointer user_data)
898 AnjutaPreferences *pr;
899 AnjutaProperty *p;
900 gchar *val;
902 pr = ANJUTA_PREFERENCES (g_object_get_data (G_OBJECT (widget),
903 "AnjutaPreferences"));
904 p = (AnjutaProperty *) user_data;
905 val = get_property_value_as_string (p);
906 anjuta_preferences_set (pr, p->key, val);
907 g_free (val);
910 static void
911 unregister_preferences_key (GtkWidget *widget,
912 gpointer user_data)
914 AnjutaProperty *p;
915 AnjutaPreferences *pr;
916 gchar *key;
918 p = (AnjutaProperty *) user_data;
919 pr = g_object_get_data (G_OBJECT (widget),
920 "AnjutaPreferences");
921 key = g_strdup (p->key);
923 g_hash_table_remove (pr->priv->properties, key);
924 g_free (key);
927 static void
928 get_property (GConfClient *gclient, guint cnxt_id,
929 GConfEntry *entry, gpointer user_data)
931 const gchar *key;
932 GConfValue *value;
934 AnjutaProperty *p = (AnjutaProperty *) user_data;
935 key = gconf_entry_get_key (entry);
936 value = gconf_entry_get_value (entry);
937 /* DEBUG_PRINT ("Preference changed %s", key); */
939 if (p->data_type == ANJUTA_PROPERTY_DATA_TYPE_BOOL)
941 gboolean gconf_value;
943 gconf_value = gconf_value_get_bool (value);
944 set_property_value_as_int (p, gconf_value);
946 else if (p->data_type == ANJUTA_PROPERTY_DATA_TYPE_INT)
948 int gconf_value;
950 gconf_value = gconf_value_get_int (value);
951 set_property_value_as_int (p, gconf_value);
953 else
955 const gchar *gconf_value;
956 gconf_value = gconf_value_get_string (value);
957 set_property_value_as_string (p, gconf_value);
961 static void
962 register_callbacks (AnjutaPreferences *pr, AnjutaProperty *p)
964 GConfClient *gclient;
965 gchar *key_error_msg;
967 gclient = pr->priv->gclient;
968 g_object_set_data (G_OBJECT (p->object), "AnjutaPreferences", pr);
969 switch (p->object_type) {
970 case ANJUTA_PROPERTY_OBJECT_TYPE_ENTRY:
971 g_signal_connect (G_OBJECT(p->object), "changed",
972 G_CALLBACK (update_property_on_change_str), p);
973 g_signal_connect (G_OBJECT(p->object), "focus_out_event",
974 G_CALLBACK (update_property_on_event_str), p);
975 g_signal_connect (G_OBJECT(p->object), "focus_out_event",
976 G_CALLBACK (unblock_update_property_on_change_str), p);
977 g_signal_connect (G_OBJECT(p->object), "focus_in_event",
978 G_CALLBACK (block_update_property_on_change_str), p);
979 break;
980 case ANJUTA_PROPERTY_OBJECT_TYPE_SPIN:
981 g_signal_connect (G_OBJECT(p->object), "value-changed",
982 G_CALLBACK (update_property_on_change_int), p);
983 break;
984 case ANJUTA_PROPERTY_OBJECT_TYPE_FONT:
985 g_signal_connect (G_OBJECT(p->object), "font-set",
986 G_CALLBACK (update_property_on_change_font), p);
987 break;
988 case ANJUTA_PROPERTY_OBJECT_TYPE_TEXT:
989 g_signal_connect (G_OBJECT(p->object), "focus_out_event",
990 G_CALLBACK (update_property_on_event_str), p);
991 break;
992 case ANJUTA_PROPERTY_OBJECT_TYPE_COMBO:
993 g_signal_connect (G_OBJECT(p->object), "changed",
994 G_CALLBACK (update_property_on_change_str), p);
995 break;
996 case ANJUTA_PROPERTY_OBJECT_TYPE_TOGGLE:
997 g_signal_connect (G_OBJECT(p->object), "toggled",
998 G_CALLBACK (update_property_on_change_int), p);
999 break;
1000 case ANJUTA_PROPERTY_OBJECT_TYPE_COLOR:
1001 g_signal_connect (G_OBJECT(p->object), "color-set",
1002 G_CALLBACK (update_property_on_change_color), p);
1003 break;
1004 case ANJUTA_PROPERTY_OBJECT_TYPE_FILE:
1005 g_signal_connect (G_OBJECT(p->object), "file-set",
1006 G_CALLBACK (update_property_on_change_str), p);
1007 break;
1008 case ANJUTA_PROPERTY_OBJECT_TYPE_FOLDER:
1009 g_signal_connect (G_OBJECT(p->object), "current-folder-changed",
1010 G_CALLBACK (update_property_on_change_str), p);
1011 break;
1012 default:
1013 break;
1015 if (!gconf_valid_key (build_key (p->key), &key_error_msg))
1017 g_warning ("Invalid key \"%s\": Error: \"%s\"", build_key (p->key),
1018 key_error_msg);
1019 g_free (key_error_msg);
1021 p->notify_id = gconf_client_notify_add (gclient, build_key (p->key),
1022 get_property, p, NULL, NULL);
1024 /* Connect to widget destroy signal so we can automatically unregister
1025 * keys so there aren't any potential conflicts or references to
1026 * nonexistent widgets on subsequent uses of the prefs dialog. */
1027 g_signal_connect (G_OBJECT (p->object), "destroy",
1028 G_CALLBACK (unregister_preferences_key),
1032 static gboolean
1033 preferences_foreach_callback (gchar *key, struct _AnjutaProperty *p,
1034 struct _AnjutaPreferencesForeachData *data)
1036 if (p->object_type != ANJUTA_PROPERTY_OBJECT_TYPE_COMBO)
1038 if (data->filter == ANJUTA_PREFERENCES_FILTER_NONE)
1039 return data->callback (data->pr, key, data->callback_data);
1040 else if (p->flags & data->filter)
1041 return data->callback (data->pr, key, data->callback_data);
1044 return TRUE;
1047 static void
1048 connect_prop_to_object (AnjutaPreferences *pr, AnjutaProperty *p)
1050 int gconf_value;
1051 gchar *value;
1053 if (p->data_type == ANJUTA_PROPERTY_DATA_TYPE_BOOL ||
1054 p->data_type == ANJUTA_PROPERTY_DATA_TYPE_INT)
1056 gconf_value = anjuta_preferences_get_int (pr, p->key);
1057 value = g_strdup_printf ("%d", gconf_value);
1058 set_property_value_as_string (p, value);
1060 else
1062 value = anjuta_preferences_get (pr, p->key);
1063 set_property_value_as_string (p, value);
1064 g_free (value);
1070 * anjuta_preferences_register_property_raw:
1071 * @pr: a #AnjutaPreferences object
1072 * @object: Widget to register
1073 * @key: Property key
1074 * @default_value: Default value of the key
1075 * @flags: Flags
1076 * @object_type: Object type of widget
1077 * @data_type: Data type of the property
1079 * This also registers only one widget, but instead of supplying the property
1080 * parameters as a single parsable string (as done in previous method), it
1081 * takes them separately.
1083 * Return value: TRUE if sucessful.
1085 gboolean
1086 anjuta_preferences_register_property_raw (AnjutaPreferences *pr,
1087 GtkWidget *object,
1088 const gchar *key,
1089 const gchar *default_value,
1090 guint flags,
1091 AnjutaPropertyObjectType object_type,
1092 AnjutaPropertyDataType data_type)
1094 AnjutaProperty *p;
1095 GConfValue *value;
1097 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), FALSE);
1098 g_return_val_if_fail (GTK_IS_WIDGET (object), FALSE);
1099 g_return_val_if_fail (key != NULL, FALSE);
1100 g_return_val_if_fail (strlen(key) > 0, FALSE);
1101 g_return_val_if_fail ((object_type != ANJUTA_PROPERTY_OBJECT_TYPE_COMBO) ||
1102 ((default_value != NULL) &&
1103 (*default_value != '\0')), FALSE);
1105 p = g_new0 (AnjutaProperty, 1);
1106 g_object_ref (object);
1107 p->object = object;
1108 p->object_type = object_type;
1109 p->data_type = data_type;
1110 p->key = g_strdup (key);
1111 p->gclient = pr->priv->gclient;
1113 value = gconf_client_get (pr->priv->gclient,
1114 build_key (p->key), NULL);
1115 if (value)
1117 /* Verify key type. Unset key if type mismatch. */
1118 if (!((value->type == GCONF_VALUE_BOOL &&
1119 data_type == ANJUTA_PROPERTY_DATA_TYPE_BOOL) ||
1120 (value->type == GCONF_VALUE_INT &&
1121 data_type == ANJUTA_PROPERTY_DATA_TYPE_INT) ||
1122 (value->type == GCONF_VALUE_STRING &&
1123 data_type != ANJUTA_PROPERTY_DATA_TYPE_BOOL &&
1124 data_type != ANJUTA_PROPERTY_DATA_TYPE_INT)))
1126 gconf_client_unset (pr->priv->gclient, build_key (key), NULL);
1128 gconf_value_free (value);
1130 if (default_value)
1132 p->default_value = g_strdup (default_value);
1133 if (strlen (default_value) > 0)
1135 /* For combo, initialize the untranslated strings */
1136 if (object_type == ANJUTA_PROPERTY_OBJECT_TYPE_COMBO)
1138 gchar *old_value;
1139 gchar **vstr;
1141 vstr = g_strsplit (default_value, ",", 100);
1142 g_object_set_data(G_OBJECT(p->object), "untranslated",
1143 vstr);
1144 old_value = anjuta_preferences_get (pr, p->key);
1145 if (old_value == NULL && vstr[0])
1147 /* DEBUG_PRINT ("Setting default pref value: %s = %s",
1148 p->key, default_value); */
1149 anjuta_preferences_set (pr, p->key, vstr[0]);
1151 if (old_value)
1152 g_free (old_value);
1154 else if (p->data_type != ANJUTA_PROPERTY_DATA_TYPE_BOOL &&
1155 p->data_type != ANJUTA_PROPERTY_DATA_TYPE_INT)
1157 gchar *old_value;
1158 old_value = anjuta_preferences_get (pr, p->key);
1159 if (old_value == NULL)
1161 /* DEBUG_PRINT ("Setting default pref value: %s = %s",
1162 p->key, default_value);*/
1163 anjuta_preferences_set (pr, p->key, default_value);
1165 if (old_value)
1166 g_free (old_value);
1168 else
1170 value = gconf_client_get (pr->priv->gclient,
1171 build_key (p->key), NULL);
1172 if (value == NULL)
1174 /* DEBUG_PRINT ("Setting default pref value: %s = %s",
1175 p->key, default_value);*/
1176 if (p->data_type == ANJUTA_PROPERTY_DATA_TYPE_INT)
1177 gconf_client_set_int (pr->priv->gclient,
1178 build_key (p->key),
1179 atoi (default_value), NULL);
1180 else
1181 gconf_client_set_bool (pr->priv->gclient,
1182 build_key (p->key),
1183 atoi (default_value), NULL);
1185 if (value)
1186 gconf_value_free (value);
1190 p->flags = flags;
1191 p->custom = FALSE;
1192 p->set_property = NULL;
1193 p->get_property = NULL;
1195 g_hash_table_insert (pr->priv->properties, g_strdup (key), p);
1196 connect_prop_to_object (pr, p);
1197 register_callbacks (pr, p);
1198 return TRUE;
1202 * anjuta_preferences_register_property_custom:
1203 * @pr: a #AnjutaPreferences object.
1204 * @object: Object to register.
1205 * @key: Property key.
1206 * @default_value: Default value of the key.
1207 * @data_type: property data type.
1208 * @flags: Flags
1209 * @set_property: Set property to widget callback.
1210 * @get_property: Get property from widget callback.
1212 * This is meant for complex widgets which can not be set/get with the
1213 * standard object set/get methods. Custom set/get methods are passed for
1214 * the property to set/get the value to/from the widget.
1216 * Return value: TRUE if sucessful.
1218 gboolean
1219 anjuta_preferences_register_property_custom (AnjutaPreferences *pr,
1220 GtkWidget *object,
1221 const gchar *key,
1222 const gchar *default_value,
1223 AnjutaPropertyDataType data_type,
1224 guint flags,
1225 void (*set_property) (AnjutaProperty *prop, const gchar *value),
1226 gchar * (*get_property) (AnjutaProperty *))
1228 AnjutaProperty *p;
1229 GConfValue *value;
1231 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), FALSE);
1232 g_return_val_if_fail (GTK_IS_WIDGET (object), FALSE);
1233 g_return_val_if_fail (key != NULL, FALSE);
1234 g_return_val_if_fail (strlen(key) > 0, FALSE);
1236 p = g_new0 (AnjutaProperty, 1);
1237 g_object_ref (object);
1238 p->object = object;
1239 p->object_type = (AnjutaPropertyObjectType) 0;
1240 p->data_type = data_type;
1241 p->key = g_strdup (key);
1242 p->gclient = pr->priv->gclient;
1244 value = gconf_client_get (pr->priv->gclient,
1245 build_key (p->key), NULL);
1246 if (value)
1248 /* Verify key type. Unset key if type mismatch. */
1249 if (!((value->type == GCONF_VALUE_BOOL &&
1250 data_type == ANJUTA_PROPERTY_DATA_TYPE_BOOL) ||
1251 (value->type == GCONF_VALUE_INT &&
1252 data_type == ANJUTA_PROPERTY_DATA_TYPE_INT) ||
1253 (value->type == GCONF_VALUE_STRING &&
1254 data_type != ANJUTA_PROPERTY_DATA_TYPE_BOOL &&
1255 data_type != ANJUTA_PROPERTY_DATA_TYPE_INT)))
1257 gconf_client_unset (pr->priv->gclient, build_key (key), NULL);
1259 gconf_value_free (value);
1261 if (default_value)
1263 p->default_value = g_strdup (default_value);
1264 if (p->data_type != ANJUTA_PROPERTY_DATA_TYPE_BOOL &&
1265 p->data_type != ANJUTA_PROPERTY_DATA_TYPE_INT)
1267 gchar *old_value;
1268 old_value = anjuta_preferences_get (pr, p->key);
1269 if (old_value == NULL)
1271 /* DEBUG_PRINT ("Setting default pref value: %s = %s",
1272 p->key, default_value); */
1273 anjuta_preferences_set (pr, p->key, default_value);
1275 if (old_value)
1276 g_free (old_value);
1278 else
1280 value = gconf_client_get (pr->priv->gclient,
1281 build_key (p->key), NULL);
1282 if (value == NULL)
1284 /* DEBUG_PRINT ("Setting default pref value: %s = %s",
1285 p->key, default_value);*/
1286 if (p->data_type == ANJUTA_PROPERTY_DATA_TYPE_INT)
1287 gconf_client_set_int (pr->priv->gclient,
1288 build_key (p->key),
1289 atoi (default_value), NULL);
1290 else
1291 gconf_client_set_bool (pr->priv->gclient,
1292 build_key (p->key),
1293 atoi (default_value), NULL);
1295 if (value)
1296 gconf_value_free (value);
1299 p->custom = TRUE;
1300 p->flags = flags;
1301 p->set_property = set_property;
1302 p->get_property = get_property;
1304 g_hash_table_insert (pr->priv->properties, g_strdup (key), p);
1306 /* Connect to widget destroy signal so we can automatically unregister
1307 * keys so there aren't any potential conflicts or references to
1308 * nonexistent widgets on subsequent uses of the prefs dialog. */
1309 g_object_set_data (G_OBJECT (p->object), "AnjutaPreferences", pr);
1310 g_signal_connect (G_OBJECT (p->object), "destroy",
1311 G_CALLBACK (unregister_preferences_key),
1313 return TRUE;
1317 * anjuta_preferences_register_property_from_string:
1318 * @pr: a #AnjutaPreferences object
1319 * @object: Widget to register
1320 * @property_desc: Property description (see anjuta_preferences_add_page())
1322 * This registers only one widget. The widget could be shown elsewhere.
1323 * the property_description should be of the form described before.
1325 * Return value: TRUE if sucessful.
1327 gboolean
1328 anjuta_preferences_register_property_from_string (AnjutaPreferences *pr,
1329 GtkWidget *object,
1330 const gchar *property_desc)
1332 gchar **fields;
1333 gint n_fields;
1335 AnjutaPropertyObjectType object_type;
1336 AnjutaPropertyDataType data_type;
1337 gchar *key;
1338 gchar *default_value;
1339 gint flags;
1341 g_return_val_if_fail (ANJUTA_IS_PREFERENCES(pr), FALSE);
1342 g_return_val_if_fail ((GTK_IS_WIDGET (object)), FALSE);
1343 g_return_val_if_fail (property_desc != NULL, FALSE);
1345 fields = g_strsplit (property_desc, ":", 5);
1346 g_return_val_if_fail (fields, FALSE);
1347 for (n_fields = 0; fields[n_fields]; n_fields++);
1348 if (n_fields != 5)
1350 g_strfreev (fields);
1351 return FALSE;
1353 object_type = get_object_type_from_string (fields[0]);
1354 data_type = get_data_type_from_string (fields[1]);
1355 default_value = fields[2];
1356 flags = atoi (fields[3]);
1357 key = fields[4];
1358 if (object_type < 0)
1360 g_warning ("Invalid property object type in property description");
1361 g_strfreev (fields);
1362 return FALSE;
1364 if (data_type < 0)
1366 g_warning ("Invalid property data type in property description");
1367 g_strfreev (fields);
1368 return FALSE;
1370 anjuta_preferences_register_property_raw (pr, object, key, default_value,
1371 flags, object_type,
1372 data_type);
1373 g_strfreev (fields);
1374 return TRUE;
1378 * anjuta_preferences_register_all_properties_from_glade_xml:
1379 * @pr: a #AnjutaPreferences Object
1380 * @gxml: GladeXML object containing the properties widgets.
1381 * @parent: Parent widget in the gxml object
1383 * This will register all the properties names of the format described above
1384 * without considering the UI. Useful if you have the widgets shown elsewhere
1385 * but you want them to be part of preferences system.
1387 void
1388 anjuta_preferences_register_all_properties_from_glade_xml (AnjutaPreferences *pr,
1389 GladeXML *gxml,
1390 GtkWidget *parent)
1392 GList *widgets;
1393 GList *node;
1395 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
1396 g_return_if_fail (gxml != NULL);
1398 widgets = glade_xml_get_widget_prefix (gxml, "preferences_");
1399 node = widgets;
1400 while (node)
1402 const gchar *name;
1403 GtkWidget *widget, *p;
1404 gboolean cont_flag = FALSE;
1406 widget = node->data;
1408 p = gtk_widget_get_parent (widget);
1409 /* Added only if it's a desendend child of the parent */
1410 while (p != parent)
1412 if (p == NULL)
1414 cont_flag = TRUE;
1415 break;
1417 p = gtk_widget_get_parent (p);
1419 if (cont_flag == TRUE)
1421 node = g_list_next (node);
1422 continue;
1425 name = glade_get_widget_name (widget);
1426 if (strncmp (name, PREFERENCE_PROPERTY_PREFIX,
1427 strlen (PREFERENCE_PROPERTY_PREFIX)) == 0)
1429 const gchar *property = &name[strlen (PREFERENCE_PROPERTY_PREFIX)];
1430 anjuta_preferences_register_property_from_string (pr, widget,
1431 property);
1433 node = g_list_next (node);
1438 * anjuta_preferences_reset_defaults:
1439 * @pr: a #AnjutaPreferences object.
1441 * Resets the default values into the keys
1443 void
1444 anjuta_preferences_reset_defaults (AnjutaPreferences * pr)
1446 GtkWidget *dlg;
1448 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
1450 dlg = gtk_message_dialog_new (GTK_WINDOW (pr),
1451 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION,
1452 GTK_BUTTONS_NONE,
1453 _("Are you sure you want to reset the preferences to\n"
1454 "their default settings?"));
1455 gtk_dialog_add_button (GTK_DIALOG (dlg), GTK_STOCK_CANCEL,
1456 GTK_RESPONSE_CANCEL);
1457 anjuta_util_dialog_add_button (GTK_DIALOG (dlg), _("_Reset"),
1458 GTK_STOCK_REVERT_TO_SAVED,
1459 GTK_RESPONSE_YES);
1460 if (gtk_dialog_run (GTK_DIALOG (dlg)) == GTK_RESPONSE_YES)
1462 /* FIXME: Reset preferences to built-in default values. */
1464 gtk_widget_destroy (dlg);
1468 * anjuta_preferences_foreach:
1469 * @pr: A #AnjutaPreferences object.
1470 * @filter: Keys to filter out from the loop.
1471 * @callback: User callback function.
1472 * @data: User data passed to @callback
1474 * Calls @callback function for each of the registered property keys. Keys
1475 * with matching @filter flags are left out of the loop. If @filter is
1476 * ANJUTA_PREFERENCES_FILTER_NONE, all properties are selected for the loop.
1478 void
1479 anjuta_preferences_foreach (AnjutaPreferences *pr,
1480 AnjutaPreferencesFilterType filter,
1481 AnjutaPreferencesCallback callback,
1482 gpointer data)
1484 struct _AnjutaPreferencesForeachData foreach_data;
1486 foreach_data.pr = pr;
1487 foreach_data.filter = filter;
1488 foreach_data.callback = callback;
1489 foreach_data.callback_data = data;
1491 g_hash_table_find (pr->priv->properties,
1492 (GHRFunc) preferences_foreach_callback,
1493 &foreach_data);
1498 * anjuta_preferences_add_page:
1499 * @pr: a #AnjutaPreferences object
1500 * @gxml: #GladeXML object containing the preferences page
1501 * @glade_widget_name: Page widget name (as give with glade interface editor).
1502 * The widget will be searched with the given name and detached
1503 * (that is, removed from the container, if present) from it's parent.
1504 * @icon_filename: File name (of the form filename.png) of the icon representing
1505 * the preference page.
1507 * Add a page to the preferences sytem.
1508 * gxml is the GladeXML object of the glade dialog containing the page widget.
1509 * The glade dialog will contain the layout of the preferences widgets.
1510 * The widgets which are preference widgets (e.g. toggle button) should have
1511 * widget names of the form:
1513 * <programlisting>
1514 * preferences_OBJECTTYPE:DATATYPE:DEFAULT:FLAGS:PROPERTYKEY
1515 * where,
1516 * OBJECTTYPE is 'toggle', 'spin', 'entry', 'text', 'color', 'font' or 'file' .
1517 * DATATYPE is 'bool', 'int', 'float', 'text', 'color' or 'font'.
1518 * DEFAULT is the default value (in the appropriate format). The format
1519 * for color is '#XXXXXX' representing RGB value and for
1520 * font, it is the pango font description.
1521 * FLAGS is any flag associated with the property. Currently it
1522 * has only two values -- 0 and 1. For normal preference
1523 * property which is saved/retrieved globally, the flag = 0.
1524 * For preference property which is also saved/retrieved
1525 * along with the project, the flag = 1.
1526 * PROPERTYKEY is the property key. e.g - 'tab.size'.
1527 * </programlisting>
1529 * All widgets having the above names in the gxml tree will be registered
1530 * and will become part of auto saving/loading. For example, refer to
1531 * anjuta preferences dialogs and study the widget names.
1533 void
1534 anjuta_preferences_add_page (AnjutaPreferences* pr, GladeXML *gxml,
1535 const gchar* glade_widget_name,
1536 const gchar* title,
1537 const gchar *icon_filename)
1539 GtkWidget *parent;
1540 GtkWidget *page;
1541 GdkPixbuf *pixbuf;
1542 gchar *image_path;
1544 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
1545 g_return_if_fail (glade_widget_name != NULL);
1546 g_return_if_fail (icon_filename != NULL);
1548 page = glade_xml_get_widget (gxml, glade_widget_name);
1549 g_object_ref (page);
1550 g_return_if_fail (GTK_IS_WIDGET (page));
1551 parent = gtk_widget_get_parent (page);
1552 if (parent && GTK_IS_CONTAINER (parent))
1554 if (GTK_IS_NOTEBOOK (parent))
1556 gint page_num;
1558 page_num = gtk_notebook_page_num (GTK_NOTEBOOK (parent), page);
1559 gtk_notebook_remove_page (GTK_NOTEBOOK (parent), page_num);
1561 else
1563 gtk_container_remove (GTK_CONTAINER (parent), page);
1566 image_path = anjuta_res_get_pixmap_file (icon_filename);
1567 pixbuf = gdk_pixbuf_new_from_file (image_path, NULL);
1568 anjuta_preferences_dialog_add_page (ANJUTA_PREFERENCES_DIALOG (pr->priv->prefs_dialog),
1569 glade_widget_name, title, pixbuf, page);
1570 anjuta_preferences_register_all_properties_from_glade_xml (pr, gxml, page);
1571 g_object_unref (page);
1572 g_free (image_path);
1573 g_object_unref (pixbuf);
1576 void
1577 anjuta_preferences_remove_page (AnjutaPreferences *pr,
1578 const gchar *page_name)
1580 if (pr->priv->prefs_dialog)
1582 anjuta_preferences_dialog_remove_page (ANJUTA_PREFERENCES_DIALOG (pr->priv->prefs_dialog),
1583 page_name);
1587 static void
1588 on_preferences_dialog_destroyed (GtkWidget *preferencess_dialog,
1589 AnjutaPreferences *pr)
1591 GList *plugins;
1592 GList *current_plugin;
1594 plugins = anjuta_plugin_manager_get_active_plugin_objects (pr->priv->plugin_manager);
1595 current_plugin = plugins;
1597 while (current_plugin)
1599 if (IANJUTA_IS_PREFERENCES (current_plugin->data))
1601 ianjuta_preferences_unmerge (IANJUTA_PREFERENCES (current_plugin->data),
1602 pr, NULL);
1605 current_plugin = g_list_next (current_plugin);
1609 g_object_unref (pr->priv->prefs_dialog);
1611 g_list_free (plugins);
1612 pr->priv->prefs_dialog = NULL;
1615 GtkWidget *
1616 anjuta_preferences_get_dialog (AnjutaPreferences *pr)
1618 GList *plugins;
1619 GList *current_plugin;
1621 if (pr->priv->prefs_dialog)
1622 return pr->priv->prefs_dialog;
1623 else
1625 pr->priv->prefs_dialog = anjuta_preferences_dialog_new ();
1627 g_signal_connect (G_OBJECT (pr->priv->prefs_dialog), "destroy",
1628 G_CALLBACK (on_preferences_dialog_destroyed),
1629 pr);
1631 plugins = anjuta_plugin_manager_get_active_plugin_objects (pr->priv->plugin_manager);
1632 current_plugin = plugins;
1634 while (current_plugin)
1636 if (IANJUTA_IS_PREFERENCES (current_plugin->data))
1638 ianjuta_preferences_merge (IANJUTA_PREFERENCES (current_plugin->data),
1639 pr, NULL);
1642 current_plugin = g_list_next (current_plugin);
1645 g_list_free (plugins);
1647 return g_object_ref_sink (pr->priv->prefs_dialog);
1651 gboolean
1652 anjuta_preferences_is_dialog_created (AnjutaPreferences *pr)
1654 return (pr->priv->prefs_dialog != NULL);
1657 static void anjuta_preferences_class_init (AnjutaPreferencesClass *class);
1658 static void anjuta_preferences_instance_init (AnjutaPreferences *pr);
1660 GType
1661 anjuta_preferences_get_type ()
1663 static GType obj_type = 0;
1665 if (!obj_type)
1667 static const GTypeInfo obj_info =
1669 sizeof (AnjutaPreferencesClass),
1670 (GBaseInitFunc) NULL,
1671 (GBaseFinalizeFunc) NULL,
1672 (GClassInitFunc) anjuta_preferences_class_init,
1673 (GClassFinalizeFunc) NULL,
1674 NULL, /* class_data */
1675 sizeof (AnjutaPreferencesClass),
1676 0, /* n_preallocs */
1677 (GInstanceInitFunc) anjuta_preferences_instance_init,
1678 NULL /* value_table */
1680 obj_type = g_type_register_static (G_TYPE_OBJECT,
1681 "AnjutaPreferences", &obj_info, 0);
1683 return obj_type;
1686 static void
1687 anjuta_preferences_dispose (GObject *obj)
1689 AnjutaPreferences *pr = ANJUTA_PREFERENCES (obj);
1691 if (pr->priv->properties)
1693 /* This will release the refs on property objects */
1694 g_hash_table_destroy (pr->priv->properties);
1695 pr->priv->properties = NULL;
1699 static void
1700 anjuta_preferences_instance_init (AnjutaPreferences *pr)
1702 pr->priv = g_new0 (AnjutaPreferencesPriv, 1);
1704 pr->priv->properties = g_hash_table_new_full (g_str_hash, g_str_equal,
1705 g_free,
1706 (GDestroyNotify) property_destroy);
1708 pr->priv->gclient = gconf_client_get_default();
1709 gconf_client_add_dir (pr->priv->gclient, GCONF_KEY_PREFIX,
1710 GCONF_CLIENT_PRELOAD_ONELEVEL, NULL);
1714 static void
1715 anjuta_preferences_finalize (GObject *obj)
1717 AnjutaPreferences *pr = ANJUTA_PREFERENCES (obj);
1719 if (pr->priv->prefs_dialog)
1720 gtk_widget_destroy (pr->priv->prefs_dialog);
1722 g_object_unref (pr->priv->plugin_manager);
1723 g_free (pr->priv);
1726 static void
1727 anjuta_preferences_class_init (AnjutaPreferencesClass *class)
1729 GObjectClass *object_class = G_OBJECT_CLASS (class);
1731 object_class->dispose = anjuta_preferences_dispose;
1732 object_class->finalize = anjuta_preferences_finalize;
1736 * anjuta_preferences_new:
1738 * Creates a new #AnjutaPreferences object
1740 * Return value: A #AnjutaPreferences object.
1742 AnjutaPreferences *
1743 anjuta_preferences_new (AnjutaPluginManager *plugin_manager)
1745 AnjutaPreferences *pr;
1747 pr = g_object_new (ANJUTA_TYPE_PREFERENCES, NULL);
1748 pr->priv->plugin_manager = g_object_ref (plugin_manager);
1750 return pr;
1755 * anjuta_preferences_notify_add:
1756 * @pr: A #AnjutaPreferences object.
1757 * @key: Key to monitor.
1758 * @func: User callback function.
1759 * @data: User data passed to @func
1760 * @destroy_notify: Destroy notify function - called when notify is removed.
1762 * This is similar to gconf_client_notify_add(), except that the key is not
1763 * given as full path. Only anjuta preference key is given. The key prefix
1764 * is added internally.
1766 * Return value: Notify ID.
1768 guint
1769 anjuta_preferences_notify_add (AnjutaPreferences *pr,
1770 const gchar *key,
1771 GConfClientNotifyFunc func,
1772 gpointer data,
1773 GFreeFunc destroy_notify)
1775 return gconf_client_notify_add (pr->priv->gclient,
1776 build_key (key),
1777 func, data, destroy_notify, NULL);
1781 * anjuta_preferences_notify_remove:
1782 * @pr: A #AnjutaPreferences object.
1783 * @notify_id: Notify ID returned by anjuta_preferences_notify_add().
1785 * Removes the notify callback added with anjuta_preferences_notify_add().
1787 void
1788 anjuta_preferences_notify_remove (AnjutaPreferences *pr, guint notify_id)
1790 gconf_client_notify_remove (pr->priv->gclient, notify_id);
1794 * anjuta_preferences_get_prefix:
1795 * @pr: A #AnjutaPreferences object.
1797 * Returns the gconf key prefix used by anjuta to store its preferences.
1799 * Return value: preferences keys prefix.
1801 const gchar*
1802 anjuta_preferences_get_prefix (AnjutaPreferences *pr)
1804 return PREFERENCE_PROPERTY_PREFIX;
1808 * anjuta_preferences_dir_exists:
1809 * @pr: A #AnjutaPreferences object.
1810 * @dir: Directory to checkfor.
1812 * Returns TRUE if dir exists.
1814 #ifdef __GNUC__
1815 inline
1816 #endif
1817 gboolean
1818 anjuta_preferences_dir_exists (AnjutaPreferences *pr, const gchar *dir)
1820 g_return_val_if_fail (ANJUTA_IS_PREFERENCES (pr), FALSE);
1821 g_return_val_if_fail (dir != NULL, FALSE);
1823 return gconf_client_dir_exists(pr->priv->gclient, build_key (dir), NULL);
1827 * anjuta_preferences_add_dir:
1828 * @pr: A #AnjutaPreferences object.
1829 * @dir: Directory to add to the list.
1830 * @preload: Degree of preload.
1832 * Add a directory to the list of directories the GConfClient.
1834 #ifdef __GNUC__
1835 inline
1836 #endif
1837 void
1838 anjuta_preferences_add_dir (AnjutaPreferences *pr, const gchar *dir,
1839 GConfClientPreloadType preload)
1841 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
1842 g_return_if_fail (dir != NULL);
1844 gconf_client_add_dir(pr->priv->gclient, build_key (dir),
1845 preload, NULL);
1849 * anjuta_preferences_remove_dir:
1850 * @pr: A #AnjutaPreferences object.
1851 * @dir: Directory to remove from the list.
1853 * Remove a directory from the list of directories.
1855 #ifdef __GNUC__
1856 inline
1857 #endif
1858 void
1859 anjuta_preferences_remove_dir (AnjutaPreferences *pr, const gchar *dir)
1861 g_return_if_fail (ANJUTA_IS_PREFERENCES (pr));
1862 g_return_if_fail (dir != NULL);
1864 gconf_client_remove_dir(pr->priv->gclient, build_key (dir), NULL);