diff options
Diffstat (limited to 'libnautilus-private')
-rw-r--r-- | libnautilus-private/nautilus-global-preferences.c | 366 | ||||
-rw-r--r-- | libnautilus-private/nautilus-global-preferences.h | 29 | ||||
-rw-r--r-- | libnautilus-private/nautilus-icon-container.c | 17 | ||||
-rw-r--r-- | libnautilus-private/nautilus-list.c | 6 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preference.c | 307 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preference.h | 18 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preferences-group.c | 2 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preferences-item.c | 4 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preferences.c | 516 | ||||
-rw-r--r-- | libnautilus-private/nautilus-preferences.h | 15 | ||||
-rw-r--r-- | libnautilus-private/nautilus-user-level-manager.c | 136 | ||||
-rw-r--r-- | libnautilus-private/nautilus-user-level-manager.h | 9 | ||||
-rw-r--r-- | libnautilus-private/test-nautilus-preferences.c | 23 |
13 files changed, 935 insertions, 513 deletions
diff --git a/libnautilus-private/nautilus-global-preferences.c b/libnautilus-private/nautilus-global-preferences.c index 9c6892007..d8b52981f 100644 --- a/libnautilus-private/nautilus-global-preferences.c +++ b/libnautilus-private/nautilus-global-preferences.c @@ -50,6 +50,29 @@ static GList * global_preferences_get_sidebar_panel_view_identifiers (void); static gboolean global_preferences_close_dialog_callback (GtkWidget *dialog, gpointer user_data); static void global_preferences_initialize_if_needed (void); +static void global_preferences_register_with_defaults (const char *name, + const char *description, + NautilusPreferenceType type, + gconstpointer novice_default, + gconstpointer intermediate_default, + gconstpointer hacker_default); +static void global_preferences_register_boolean_with_defaults (const char *name, + const char *description, + gboolean novice_default, + gboolean intermediate_default, + gboolean hacker_default); +static void global_preferences_register_string_with_defaults (const char *name, + const char *description, + const char *novice_default, + const char *intermediate_default, + const char *hacker_default); +static void global_preferences_register_enum_with_defaults (const char *name, + const char *description, + int novice_default, + int intermediate_default, + int hacker_default); + +static GtkWidget *global_prefs_dialog = NULL; /* * Private stuff @@ -263,8 +286,6 @@ nautilus_global_preferences_get_disabled_sidebar_panel_view_identifiers (void) static GtkWidget * global_preferences_get_dialog (void) { - static GtkWidget * global_prefs_dialog = NULL; - global_preferences_initialize_if_needed (); if (!global_prefs_dialog) @@ -292,12 +313,16 @@ global_preferences_register_sidebar_panels_preferences_for_ui (void) preference_key = global_preferences_get_sidebar_panel_key (identifier->iid); g_assert (preference_key != NULL); - - nautilus_preferences_set_info (preference_key, - identifier->name, - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) TRUE); - + + /* FIXME: The actual defaults fed to this function need to be queried + * so that only the appropiate sidebar panels show for a user level. + */ + global_preferences_register_boolean_with_defaults (preference_key, + identifier->name, + TRUE, + TRUE, + TRUE); + g_free (preference_key); } @@ -334,6 +359,72 @@ global_preferences_is_sidebar_panel_enabled (NautilusViewIdentifier *panel_ident } static void +global_preferences_register_with_defaults (const char *name, + const char *description, + NautilusPreferenceType type, + gconstpointer novice_default, + gconstpointer intermediate_default, + gconstpointer hacker_default) +{ + gconstpointer defaults[3]; + + defaults[0] = novice_default; + defaults[1] = intermediate_default; + defaults[2] = hacker_default; + + nautilus_preference_set_info_by_name (name, + description, + type, + defaults, + 3); +} + +static void +global_preferences_register_boolean_with_defaults (const char *name, + const char *description, + gboolean novice_default, + gboolean intermediate_default, + gboolean hacker_default) +{ + global_preferences_register_with_defaults (name, + description, + NAUTILUS_PREFERENCE_BOOLEAN, + (gconstpointer) novice_default, + (gconstpointer) intermediate_default, + (gconstpointer) hacker_default); +} + +static void +global_preferences_register_string_with_defaults (const char *name, + const char *description, + const char *novice_default, + const char *intermediate_default, + const char *hacker_default) +{ + global_preferences_register_with_defaults (name, + description, + NAUTILUS_PREFERENCE_STRING, + (gconstpointer) novice_default, + (gconstpointer) intermediate_default, + (gconstpointer) hacker_default); +} + +static void +global_preferences_register_enum_with_defaults (const char *name, + const char *description, + int novice_default, + int intermediate_default, + int hacker_default) +{ + global_preferences_register_with_defaults (name, + description, + NAUTILUS_PREFERENCE_ENUM, + (gconstpointer) novice_default, + (gconstpointer) intermediate_default, + (gconstpointer) hacker_default); +} + +static void global_preferences_register_for_ui (void) { static gboolean preference_for_ui_registered = FALSE; @@ -343,65 +434,119 @@ global_preferences_register_for_ui (void) preference_for_ui_registered = TRUE; + /* * In the soon to come star trek future, the following information * will be fetched using the latest xml techniques. */ - + /* Window create new */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_WINDOW_ALWAYS_NEW, - "Open each item in a new window", - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) FALSE); + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_WINDOW_ALWAYS_NEW, + "Open each item in a new window", + FALSE, + FALSE, + FALSE); /* Click activation type */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_CLICK_POLICY, - "Click policy", - NAUTILUS_PREFERENCE_ENUM, - (gconstpointer) NAUTILUS_CLICK_POLICY_SINGLE); - - nautilus_preferences_enum_add_entry (NAUTILUS_PREFERENCES_CLICK_POLICY, - "single", - "Activate items with a single click", - NAUTILUS_CLICK_POLICY_SINGLE); - - nautilus_preferences_enum_add_entry (NAUTILUS_PREFERENCES_CLICK_POLICY, - "double", - "Activate items with a double click", - NAUTILUS_CLICK_POLICY_DOUBLE); - + global_preferences_register_enum_with_defaults (NAUTILUS_PREFERENCES_CLICK_POLICY, + "Click policy", + NAUTILUS_CLICK_POLICY_SINGLE, + NAUTILUS_CLICK_POLICY_SINGLE, + NAUTILUS_CLICK_POLICY_SINGLE); + + nautilus_preference_enum_add_entry_by_name (NAUTILUS_PREFERENCES_CLICK_POLICY, + "single", + "Activate items with a single click", + NAUTILUS_CLICK_POLICY_SINGLE); + + nautilus_preference_enum_add_entry_by_name (NAUTILUS_PREFERENCES_CLICK_POLICY, + "double", + "Activate items with a double click", + NAUTILUS_CLICK_POLICY_DOUBLE); + /* Remote views */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_SHOW_TEXT_IN_REMOTE_ICONS, - "Display text in icons even for remote text files", - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) FALSE); + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_SHOW_TEXT_IN_REMOTE_ICONS, + "Display text in icons even for remote text files", + FALSE, + FALSE, + FALSE); /* Sidebar panels */ global_preferences_register_sidebar_panels_preferences_for_ui (); /* Appearance options */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS, - "Use smoother (but slower) graphics", - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) FALSE); - + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS, + "Use smoother (but slower) graphics", + FALSE, + FALSE, + FALSE); + /* Directory View */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_DIRECTORY_VIEW_FONT_FAMILY, - "Font familiy used to display file names", - NAUTILUS_PREFERENCE_STRING, - (gconstpointer) "helvetica"); + global_preferences_register_string_with_defaults (NAUTILUS_PREFERENCES_DIRECTORY_VIEW_FONT_FAMILY, + "Font familiy used to display file names", + "helvetica", + "helvetica", + "helvetica"); /* toolbar icons */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_EAZEL_TOOLBAR_ICONS, - "Use Eazel's toolbar icons", - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) FALSE); + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_EAZEL_TOOLBAR_ICONS, + "Use Eazel's toolbar icons", + FALSE, + FALSE, + FALSE); + + /* + * These dont have a UI (yet ? maybe in the advanced settings ?). + * They do need to have appropiate defaults nontheless. + */ + + global_preferences_register_string_with_defaults (NAUTILUS_PREFERENCES_ICON_THEME, + "Show entire filename", + "default", + "default", + "default"); + + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_SHOW_REAL_FILE_NAME, + "Show entire filename", + FALSE, + FALSE, + TRUE); + + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES, + "Show hidden files", + FALSE, + TRUE, + TRUE); + + global_preferences_register_boolean_with_defaults (NAUTILUS_PREFERENCES_CAN_ADD_CONTENT, + "Can add Content", + FALSE, + TRUE, + TRUE); - /* Miscellaneous */ - nautilus_preferences_set_info (NAUTILUS_PREFERENCES_SHOW_REAL_FILE_NAME, - "Show entire file file", - NAUTILUS_PREFERENCE_BOOLEAN, - (gconstpointer) FALSE); + { + const char *user_main_directory; + char *novice_home_location; + char *intermediate_home_location; + char *hacker_home_location; + + /* FIXME bugzilla.eazel.com 715: This call needs to be spanked to conform. Should return a strduped string */ + user_main_directory = nautilus_get_user_main_directory (); + + novice_home_location = g_strdup_printf ("file://%s", user_main_directory); + intermediate_home_location = g_strdup_printf ("file://%s", g_get_home_dir()); + hacker_home_location = g_strdup_printf ("file://%s", g_get_home_dir()); + + global_preferences_register_string_with_defaults (NAUTILUS_PREFERENCES_HOME_URI, + "Home Location", + novice_home_location, + intermediate_home_location, + hacker_home_location); + + g_free (novice_home_location); + g_free (intermediate_home_location); + g_free (hacker_home_location); + } } static gboolean @@ -413,76 +558,6 @@ global_preferences_close_dialog_callback (GtkWidget *dialog, return TRUE; } -#define USER_LEVEL_NOVICE 0 -#define USER_LEVEL_INTERMEDIATE 1 -#define USER_LEVEL_HACKER 2 - -static void -user_level_changed_callback (GtkObject *user_level_manager, - gpointer user_data) -{ - int new_user_level; - char *home_uri_string; - - gboolean show_hidden_files = FALSE; - gboolean use_real_home = TRUE; - gboolean show_real_file_name = FALSE; - gboolean can_add_content = TRUE; - - const char *user_main_directory; - - new_user_level = nautilus_user_level_manager_get_user_level (); - - /* Set some preferences according to the user level */ - switch (new_user_level) { - case USER_LEVEL_NOVICE: - show_hidden_files = FALSE; - use_real_home = FALSE; - show_real_file_name = FALSE; - can_add_content = FALSE; - break; - - case USER_LEVEL_INTERMEDIATE: - show_hidden_files = FALSE; - use_real_home = TRUE; - show_real_file_name = FALSE; - can_add_content = TRUE; - break; - - case USER_LEVEL_HACKER: - default: - show_hidden_files = TRUE; - use_real_home = TRUE; - show_real_file_name = TRUE; - can_add_content = TRUE; - break; - } - - nautilus_preferences_set_boolean (NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES, - show_hidden_files); - - nautilus_preferences_set_boolean (NAUTILUS_PREFERENCES_SHOW_REAL_FILE_NAME, - show_real_file_name); - - nautilus_preferences_set_boolean (NAUTILUS_PREFERENCES_CAN_ADD_CONTENT, - can_add_content); - - /* FIXME bugzilla.eazel.com 715: This call needs to be spanked to conform. Should return a strduped string */ - user_main_directory = nautilus_get_user_main_directory (); - - if (use_real_home) - home_uri_string = g_strdup_printf ("file://%s", g_get_home_dir()); - else - home_uri_string = g_strdup_printf ("file://%s", user_main_directory); - - g_assert (home_uri_string != NULL); - - nautilus_preferences_set (NAUTILUS_PREFERENCES_HOME_URI, - home_uri_string); - - g_free (home_uri_string); -} - static void global_preferences_initialize_if_needed (void) { @@ -493,15 +568,6 @@ global_preferences_initialize_if_needed (void) } global_preferences_register_for_ui (); - - /* Register to find out about user level changes */ - gtk_signal_connect (GTK_OBJECT (nautilus_user_level_manager_get ()), - "user_level_changed", - user_level_changed_callback, - NULL); - - /* Invoke the callback once to make sure stuff is properly setup */ - user_level_changed_callback (NULL, NULL); initialized = TRUE; } @@ -512,40 +578,56 @@ global_preferences_initialize_if_needed (void) void nautilus_global_preferences_show_dialog (void) { - GtkWidget * global_prefs_dialog = global_preferences_get_dialog (); + GtkWidget *dialog = global_preferences_get_dialog (); - gtk_widget_show (global_prefs_dialog); + gtk_widget_show (dialog); } void nautilus_global_preferences_hide_dialog (void) { - GtkWidget * global_prefs_dialog = global_preferences_get_dialog (); + GtkWidget *dialog = global_preferences_get_dialog (); - gtk_widget_hide (global_prefs_dialog); + gtk_widget_hide (dialog); } void nautilus_global_preferences_set_dialog_title (const char *title) { - GtkWidget *global_prefs_dialog; + GtkWidget *dialog; g_return_if_fail (title != NULL); - global_prefs_dialog = global_preferences_get_dialog (); + dialog = global_preferences_get_dialog (); - gtk_window_set_title (GTK_WINDOW (global_prefs_dialog), title); + gtk_window_set_title (GTK_WINDOW (dialog), title); } void -nautilus_global_preferences_shutdown (void) +nautilus_global_preferences_dialog_update (void) { + gboolean was_showing = FALSE; + /* Free the dialog first, cause it has refs to preferences */ - GtkWidget * global_prefs_dialog = global_preferences_get_dialog (); - gtk_widget_destroy (global_prefs_dialog); + if (global_prefs_dialog != NULL) { + was_showing = GTK_WIDGET_VISIBLE (global_prefs_dialog); + + gtk_widget_destroy (global_prefs_dialog); + } + + global_prefs_dialog = global_preferences_create_dialog (); - gtk_signal_disconnect_by_func (GTK_OBJECT (nautilus_user_level_manager_get ()), - user_level_changed_callback, - NULL); + if (was_showing) { + nautilus_global_preferences_show_dialog (); + } +} + +void +nautilus_global_preferences_shutdown (void) +{ + /* Free the dialog first, cause it has refs to preferences */ + if (global_prefs_dialog != NULL) { + gtk_widget_destroy (global_prefs_dialog); + } /* Now free the preferences tables and stuff */ nautilus_preferences_shutdown (); diff --git a/libnautilus-private/nautilus-global-preferences.h b/libnautilus-private/nautilus-global-preferences.h index 1b6f462c3..d5c2ef746 100644 --- a/libnautilus-private/nautilus-global-preferences.h +++ b/libnautilus-private/nautilus-global-preferences.h @@ -31,39 +31,39 @@ BEGIN_GNOME_DECLS /* Window options */ -#define NAUTILUS_PREFERENCES_WINDOW_ALWAYS_NEW "/nautilus/preferences/window_always_new" +#define NAUTILUS_PREFERENCES_WINDOW_ALWAYS_NEW "preferences/window_always_new" /* Show hidden files */ -#define NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES "/nautilus/preferences/show_hidden_files" +#define NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES "preferences/show_hidden_files" /* sidebar width */ -#define NAUTILUS_PREFERENCES_SIDEBAR_WIDTH "/nautilus/preferences/sidebar_width" +#define NAUTILUS_PREFERENCES_SIDEBAR_WIDTH "preferences/sidebar_width" /* Home URI */ -#define NAUTILUS_PREFERENCES_HOME_URI "/nautilus/preferences/home_uri" +#define NAUTILUS_PREFERENCES_HOME_URI "preferences/home_uri" /* adding/removing from property browser */ -#define NAUTILUS_PREFERENCES_CAN_ADD_CONTENT "/nautilus/preferences/can_add_content" +#define NAUTILUS_PREFERENCES_CAN_ADD_CONTENT "preferences/can_add_content" /* Preferences not (currently?) displayed in dialog */ -#define NAUTILUS_PREFERENCES_ICON_VIEW_TEXT_ATTRIBUTE_NAMES "/nautilus/icon_view/text_attribute_names" -#define NAUTILUS_PREFERENCES_SHOW_REAL_FILE_NAME "/nautilus/preferences/show_real_file_name" +#define NAUTILUS_PREFERENCES_ICON_VIEW_TEXT_ATTRIBUTE_NAMES "icon_view/text_attribute_names" +#define NAUTILUS_PREFERENCES_SHOW_REAL_FILE_NAME "preferences/show_real_file_name" /* Single/Double click preference */ -#define NAUTILUS_PREFERENCES_CLICK_POLICY "/nautilus/preferences/click_policy" +#define NAUTILUS_PREFERENCES_CLICK_POLICY "preferences/click_policy" /* use anti-aliased canvas */ -#define NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS "/nautilus/preferences/anti_aliased_canvas" +#define NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS "preferences/anti_aliased_canvas" /* Sidebar panels */ -#define NAUTILUS_PREFERENCES_SIDEBAR_PANELS_NAMESPACE "/nautilus/sidebar-panels" +#define NAUTILUS_PREFERENCES_SIDEBAR_PANELS_NAMESPACE "sidebar-panels" /* Directory view */ -#define NAUTILUS_PREFERENCES_DIRECTORY_VIEW_FONT_FAMILY "/nautilus/directory-view/font_family" +#define NAUTILUS_PREFERENCES_DIRECTORY_VIEW_FONT_FAMILY "directory-view/font_family" /* themes */ -#define NAUTILUS_PREFERENCES_EAZEL_TOOLBAR_ICONS "/nautilus/preferences/eazel_toolbar_icons" -#define NAUTILUS_PREFERENCES_ICON_THEME "/nautilus/preferences/icon_theme" +#define NAUTILUS_PREFERENCES_EAZEL_TOOLBAR_ICONS "preferences/eazel_toolbar_icons" +#define NAUTILUS_PREFERENCES_ICON_THEME "preferences/icon_theme" enum { @@ -71,12 +71,13 @@ enum NAUTILUS_CLICK_POLICY_DOUBLE }; -#define NAUTILUS_PREFERENCES_SHOW_TEXT_IN_REMOTE_ICONS "/nautilus/preferences/remote_icon_text" +#define NAUTILUS_PREFERENCES_SHOW_TEXT_IN_REMOTE_ICONS "preferences/remote_icon_text" void nautilus_global_preferences_shutdown (void); void nautilus_global_preferences_show_dialog (void); void nautilus_global_preferences_hide_dialog (void); void nautilus_global_preferences_set_dialog_title (const char *title); +void nautilus_global_preferences_dialog_update (void); /* Sidebar */ GList *nautilus_global_preferences_get_enabled_sidebar_panel_view_identifiers (void); diff --git a/libnautilus-private/nautilus-icon-container.c b/libnautilus-private/nautilus-icon-container.c index 193658d2b..c89b96475 100644 --- a/libnautilus-private/nautilus-icon-container.c +++ b/libnautilus-private/nautilus-icon-container.c @@ -2495,15 +2495,14 @@ nautilus_icon_container_initialize (NautilusIconContainer *container) mode == NAUTILUS_CLICK_POLICY_SINGLE; /* Keep track of changes in clicking policy */ - nautilus_preferences_add_enum_callback - (NAUTILUS_PREFERENCES_CLICK_POLICY, - click_policy_changed_callback, - container); - - /* add callback for preference changes */ - nautilus_preferences_add_boolean_callback(NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS, - (NautilusPreferencesCallback) anti_aliased_preferences_changed, - container); + nautilus_preferences_add_callback (NAUTILUS_PREFERENCES_CLICK_POLICY, + click_policy_changed_callback, + container); + + /* Keep track of changes in graphics trade offs */ + nautilus_preferences_add_callback (NAUTILUS_PREFERENCES_ANTI_ALIASED_CANVAS, + (NautilusPreferencesCallback) anti_aliased_preferences_changed, + container); } diff --git a/libnautilus-private/nautilus-list.c b/libnautilus-private/nautilus-list.c index fd7e58670..c10ca9f89 100644 --- a/libnautilus-private/nautilus-list.c +++ b/libnautilus-private/nautilus-list.c @@ -486,9 +486,9 @@ nautilus_list_initialize (NautilusList *list) update_single_click_mode_from_preferences (list); /* Keep track of changes in clicking policy */ - nautilus_preferences_add_enum_callback (NAUTILUS_PREFERENCES_CLICK_POLICY, - click_policy_changed_callback, - list); + nautilus_preferences_add_callback (NAUTILUS_PREFERENCES_CLICK_POLICY, + click_policy_changed_callback, + list); } static void diff --git a/libnautilus-private/nautilus-preference.c b/libnautilus-private/nautilus-preference.c index c3115b147..74fee096f 100644 --- a/libnautilus-private/nautilus-preference.c +++ b/libnautilus-private/nautilus-preference.c @@ -32,6 +32,21 @@ #include <libnautilus-extensions/nautilus-string-list.h> #include "nautilus-widgets-self-check-functions.h" +#include <nautilus-widgets/nautilus-user-level-manager.h> + +/* + * PreferenceHashNode: + * + * A structure to manage preference hash table nodes. + * Preferences are hash tables. The hash key is the preference name + * (a string). The hash value is a pointer of the following struct: + */ +typedef struct { + NautilusPreference *preference; +} PreferenceHashNode; + +static GHashTable *global_preference_table = NULL; + static const char PREFERENCE_NO_DESCRIPTION[] = "No Description"; /* * NautilusPreferenceDetail: @@ -62,6 +77,43 @@ static void nautilus_preference_destroy (GtkObject *objec /* Type info functions */ static void preference_free_type_info (NautilusPreference *preference); +static gboolean preference_initialize_if_needed (void); + +void nautilus_preference_shutdown (void); + +/* PreferenceHashNode functions */ +static PreferenceHashNode * preference_hash_node_alloc (char *name, + char *description, + NautilusPreferenceType type); +static void preference_hash_node_free (PreferenceHashNode *node); +static void preference_hash_node_free_func (gpointer key, + gpointer value, + gpointer user_data); + +/* Private stuff */ +static PreferenceHashNode * preference_hash_node_lookup (const char *name); +static PreferenceHashNode * preference_hash_node_lookup_with_registration (const char *pref_name, + NautilusPreferenceType pref_type); +static void preference_register (char *name, + char *description, + NautilusPreferenceType type); +static gboolean +preference_initialize_if_needed (void) +{ + if (global_preference_table) { + return TRUE; + } + + g_assert (global_preference_table == NULL); + + global_preference_table = g_hash_table_new (g_str_hash, g_str_equal); + + g_assert (global_preference_table != NULL); + + return TRUE; +} + + NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusPreference, nautilus_preference, GTK_TYPE_OBJECT) /** @@ -481,6 +533,261 @@ nautilus_preference_enum_get_num_entries (const NautilusPreference *preference) return 0; } + + + + +/** + * preference_hash_node_alloc + * + * Allocate a preference hash node. + * @info: Pointer to info structure to use for the node memebers. + * + * Return value: A newly allocated node. + **/ +static PreferenceHashNode * +preference_hash_node_alloc (char *name, + char *description, + NautilusPreferenceType type) +{ + PreferenceHashNode * node; + + g_assert (name != NULL); + + node = g_new (PreferenceHashNode, 1); + + node->preference = NAUTILUS_PREFERENCE (nautilus_preference_new_from_type (name, type)); + + g_assert (node->preference != NULL); + + if (description) { + nautilus_preference_set_description (node->preference, description); + } + + return node; +} + +/** + * preference_hash_node_free_func + * + * A function that frees a pref hash node. It is meant to be fed to + * g_hash_table_foreach () + * @key: The hash key privately maintained by the GHashTable. + * @value: The hash value privately maintained by the GHashTable. + * @callback_data: The callback_data privately maintained by the GHashTable. + **/ +static void +preference_hash_node_free_func (gpointer key, + gpointer value, + gpointer user_data) +{ + g_assert (value != NULL); + + preference_hash_node_free ((PreferenceHashNode *) value); +} + +/** + * preference_hash_node_free + * + * Free a preference hash node members along with the node itself. + * @preference_hash_node: The node to free. + **/ +static void +preference_hash_node_free (PreferenceHashNode *node) +{ + g_assert (node != NULL); + + g_assert (node->preference != NULL); + + gtk_object_unref (GTK_OBJECT (node->preference)); + node->preference = NULL; + + g_free (node); +} + +static void +preference_register (char *name, + char *description, + NautilusPreferenceType type) +{ + PreferenceHashNode *node; + + g_return_if_fail (name != NULL); + g_return_if_fail (description != NULL); + + preference_initialize_if_needed (); + + node = preference_hash_node_lookup (name); + + if (node) { + g_warning ("the '%s' preference is already registered", name); + return; + } + + node = preference_hash_node_alloc (name, description, type); + + g_hash_table_insert (global_preference_table, (gpointer) name, (gpointer) node); + + g_assert (node->preference != NULL); +} + +static PreferenceHashNode * +preference_hash_node_lookup (const char *name) +{ + gpointer hash_value; + + g_assert (name != NULL); + + preference_initialize_if_needed (); + + hash_value = g_hash_table_lookup (global_preference_table, (gconstpointer) name); + + return (PreferenceHashNode *) hash_value; +} + +static PreferenceHashNode * +preference_hash_node_lookup_with_registration (const char *name, + NautilusPreferenceType type) +{ + PreferenceHashNode * node; + + g_assert (name != NULL); + + preference_initialize_if_needed (); + + node = preference_hash_node_lookup (name); + + if (!node) { + preference_register (g_strdup (name), + "Unspecified Description", + type); + + node = preference_hash_node_lookup (name); + } + + g_assert (node != NULL); + + return node; +} + +void +nautilus_preference_shutdown (void) +{ + if (global_preference_table == NULL) { + return; + } + + if (global_preference_table != NULL) { + g_hash_table_foreach (global_preference_table, + preference_hash_node_free_func, + NULL); + + g_hash_table_destroy (global_preference_table); + + global_preference_table = NULL; + } +} + + +/* + * Public functions + */ + + +/** + * nautilus_preference_find_by_name + * + * Search for a named preference in the given preference and return it. + * @preference: The preference to search + * + * Return value: A referenced pointer to the preference object that corresponds + * to the given preference name. The caller should gtk_object_unref() the return + * value of this function. + **/ +NautilusPreference * +nautilus_preference_find_by_name (const char *name) +{ + PreferenceHashNode *node; + + g_return_val_if_fail (name != NULL, NULL); + + preference_initialize_if_needed (); + + node = preference_hash_node_lookup (name); + + g_assert (node != NULL); + + gtk_object_ref (GTK_OBJECT (node->preference)); + + return node->preference; +} + +void +nautilus_preference_set_info_by_name (const char *name, + const char *description, + NautilusPreferenceType type, + gconstpointer *default_values, + guint num_default_values) +{ + PreferenceHashNode *node; + + g_return_if_fail (name != NULL); + + preference_initialize_if_needed (); + + node = preference_hash_node_lookup_with_registration (name, type); + + g_assert (node != NULL); + g_assert (node->preference != NULL); + + if (description) { + nautilus_preference_set_description (node->preference, description); + } + + if (default_values && num_default_values) { + guint i; + + for (i = 0; i < num_default_values; i++) + { + nautilus_user_level_manager_set_default_value_if_needed (name, + type, + i, + default_values[i]); + } + } +} + +void +nautilus_preference_enum_add_entry_by_name (const char *name, + const char *entry_name, + const char *entry_description, + int entry_value) +{ + PreferenceHashNode *node; + + g_return_if_fail (name != NULL); + + preference_initialize_if_needed (); + + node = preference_hash_node_lookup_with_registration (name, NAUTILUS_PREFERENCE_ENUM); + + g_assert (node != NULL); + g_assert (node->preference != NULL); + + g_assert (nautilus_preference_get_preference_type (node->preference) == NAUTILUS_PREFERENCE_ENUM); + + nautilus_preference_enum_add_entry (node->preference, + entry_name, + entry_description, + entry_value); +} + + + + + + + #if !defined (NAUTILUS_OMIT_SELF_CHECK) void diff --git a/libnautilus-private/nautilus-preference.h b/libnautilus-private/nautilus-preference.h index a80d22409..728727a90 100644 --- a/libnautilus-private/nautilus-preference.h +++ b/libnautilus-private/nautilus-preference.h @@ -89,6 +89,24 @@ gint nautilus_preference_enum_get_nth_entry_value (const guint n); guint nautilus_preference_enum_get_num_entries (const NautilusPreference *preference); + +/* + * + */ +NautilusPreference *nautilus_preference_find_by_name (const char *name); +void nautilus_preference_set_info_by_name (const char *name, + const char *description, + NautilusPreferenceType type, + gconstpointer *default_values, + guint num_default_values); +void nautilus_preference_enum_add_entry_by_name (const char *name, + const char *entry_name, + const char *entry_description, + int entry_value); + +void nautilus_preference_shutdown (void); + + BEGIN_GNOME_DECLS #endif /* NAUTILUS_PREFERENCE_H */ diff --git a/libnautilus-private/nautilus-preferences-group.c b/libnautilus-private/nautilus-preferences-group.c index 53add1edf..10cd4fe3d 100644 --- a/libnautilus-private/nautilus-preferences-group.c +++ b/libnautilus-private/nautilus-preferences-group.c @@ -208,7 +208,7 @@ nautilus_preferences_group_add_item (NautilusPreferencesGroup *group, g_return_val_if_fail (preference_name != NULL, NULL); - preference = nautilus_preferences_find_preference (preference_name); + preference = nautilus_preference_find_by_name (preference_name); g_assert (preference != NULL); diff --git a/libnautilus-private/nautilus-preferences-item.c b/libnautilus-private/nautilus-preferences-item.c index d02593696..3e2723c25 100644 --- a/libnautilus-private/nautilus-preferences-item.c +++ b/libnautilus-private/nautilus-preferences-item.c @@ -248,7 +248,7 @@ preferences_item_construct (NautilusPreferencesItem *item, item->details->preference_name = g_strdup (preference_name); - preference = nautilus_preferences_find_preference (item->details->preference_name); + preference = nautilus_preference_find_by_name (item->details->preference_name); g_assert (preference != NULL); @@ -432,7 +432,7 @@ enum_radio_group_changed_callback (GtkWidget *buttons, GtkWidget * button, gpoin g_assert (item->details->preference_name != NULL); - preference = nautilus_preferences_find_preference (item->details->preference_name); + preference = nautilus_preference_find_by_name (item->details->preference_name); i = nautilus_radio_button_group_get_active_index (NAUTILUS_RADIO_BUTTON_GROUP (buttons)); diff --git a/libnautilus-private/nautilus-preferences.c b/libnautilus-private/nautilus-preferences.c index b117c5896..6bb4703fb 100644 --- a/libnautilus-private/nautilus-preferences.c +++ b/libnautilus-private/nautilus-preferences.c @@ -35,6 +35,8 @@ #include <gconf/gconf.h> #include <gconf/gconf-client.h> +#include <gtk/gtksignal.h> + static const char PREFERENCES_GCONF_PATH[] = "/nautilus"; /* @@ -45,9 +47,9 @@ static const char PREFERENCES_GCONF_PATH[] = "/nautilus"; * (a string). The hash value is a pointer of the following struct: */ typedef struct { - NautilusPreference *preference; GList *callback_list; - int gconf_connection; + int gconf_connections[3]; + char *name; } PreferencesHashNode; /* @@ -72,20 +74,21 @@ typedef struct { typedef struct { GHashTable *preference_table; GConfClient *gconf_client; + guint old_user_level; + } PreferencesGlobalData; -static PreferencesGlobalData GLOBAL = { NULL, NULL }; +static PreferencesGlobalData GLOBAL = { NULL, NULL, 0 }; /* PreferencesHashNode functions */ -static PreferencesHashNode * preferences_hash_node_alloc (char *name, - char *description, - NautilusPreferenceType type, - gconstpointer default_value, - gpointer data); +static PreferencesHashNode * preferences_hash_node_alloc (const char *name); static void preferences_hash_node_free (PreferencesHashNode *node); static void preferences_hash_node_free_func (gpointer key, gpointer value, gpointer callback_data); +static void preferences_hash_node_check_changes_func (gpointer key, + gpointer value, + gpointer callback_data); @@ -108,17 +111,8 @@ static void preferences_hash_node_remove_callback ( /* Private stuff */ static PreferencesHashNode * preferences_hash_node_lookup (const char *name); -static PreferencesHashNode * preferences_hash_node_lookup_with_registration (const char *pref_name, - NautilusPreferenceType pref_type, - gconstpointer default_value); -static void preferences_register (char *name, - char *description, - NautilusPreferenceType type, - gconstpointer default_value, - gpointer data); -static void set_default_value_if_needed (const char *name, - NautilusPreferenceType type, - gconstpointer default_value); +static PreferencesHashNode * preferences_hash_node_lookup_with_registration (const char *pref_name); +static void preferences_register (const char *name); /* Gconf callbacks */ static void preferences_gconf_callback (GConfClient *client, @@ -129,7 +123,6 @@ static void preferences_gconf_callback ( gpointer user_data); static gboolean preferences_initialize_if_needed (void); - /** * preferences_hash_node_alloc * @@ -139,11 +132,7 @@ static gboolean preferences_initialize_if_needed ( * Return value: A newly allocated node. **/ static PreferencesHashNode * -preferences_hash_node_alloc (char *name, - char *description, - NautilusPreferenceType type, - gconstpointer default_value, - gpointer data) +preferences_hash_node_alloc (const char *name) { PreferencesHashNode * node; @@ -151,19 +140,13 @@ preferences_hash_node_alloc (char *name, node = g_new (PreferencesHashNode, 1); - node->preference = NAUTILUS_PREFERENCE (nautilus_preference_new_from_type (name, type)); - - g_assert (node->preference != NULL); - - if (description) { - nautilus_preference_set_description (node->preference, description); - } - - set_default_value_if_needed (name, type, default_value); + node->name = g_strdup(name); node->callback_list = NULL; - node->gconf_connection = 0; + node->gconf_connections[0] = 0; + node->gconf_connections[1] = 0; + node->gconf_connections[2] = 0; return node; } @@ -177,28 +160,30 @@ preferences_hash_node_alloc (char *name, static void preferences_hash_node_free (PreferencesHashNode *node) { - g_assert (node != NULL); + guint i; - g_assert (node->preference != NULL); + g_assert (node != NULL); /* Remove the gconf notification if its still lingering */ - if (node->gconf_connection != 0) + for (i = 0; i < 3; i++) { - gconf_client_notify_remove (GLOBAL.gconf_client, - node->gconf_connection); - - node->gconf_connection = 0; + if (node->gconf_connections[i] != 0) + { + gconf_client_notify_remove (GLOBAL.gconf_client, + node->gconf_connections[i]); + + node->gconf_connections[i] = 0; + } } nautilus_g_list_free_deep_custom (node->callback_list, preferneces_callback_node_free_func, NULL); - gtk_object_unref (GTK_OBJECT (node->preference)); - node->preference = NULL; - node->callback_list = NULL; + g_free (node->name); + g_free (node); } @@ -217,6 +202,7 @@ preferences_hash_node_add_callback (PreferencesHashNode *node, gpointer callback_data) { PreferencesCallbackNode *preferneces_callback_node; + guint i; g_assert (node != NULL); @@ -236,27 +222,26 @@ preferences_hash_node_add_callback (PreferencesHashNode *node, * Otherwise, we would invoke the installed callbacks more than once * per registered callback. */ - if (node->gconf_connection == 0) { - char *name = nautilus_preference_get_name (node->preference); - - g_assert (name != NULL); - - /* - * Ref the preference here, cause we use for the gconf callback data. - * See preferences_hash_node_remove_callback() to make sure the ref is balanced. - */ - g_assert (node->preference != NULL); - gtk_object_ref (GTK_OBJECT (node->preference)); - - g_assert (node->gconf_connection == 0); - - node->gconf_connection = gconf_client_notify_add (GLOBAL.gconf_client, - name, - preferences_gconf_callback, - node->preference, - NULL, - NULL); - g_free (name); + for (i = 0; i < 3; i++) + { + if (node->gconf_connections[i] == 0) { + char *key; + g_assert (key); + g_assert (node->name != NULL); + g_assert (node->gconf_connections[i] == 0); + + key = nautilus_user_level_manager_make_gconf_key (node->name, i); + g_assert (key); + + node->gconf_connections[i] = gconf_client_notify_add (GLOBAL.gconf_client, + key, + preferences_gconf_callback, + node, + NULL, + NULL); + + g_free (key); + } } } @@ -304,19 +289,17 @@ preferences_hash_node_remove_callback (PreferencesHashNode *node, * notification as well. */ if (node->callback_list == NULL) { - g_assert (node->gconf_connection != 0); - - gconf_client_notify_remove (GLOBAL.gconf_client, - node->gconf_connection); - - node->gconf_connection = 0; - - /* - * Unref the preference here to balance the ref added in - * preferences_hash_node_add_callback(). - */ - g_assert (node->preference != NULL); - gtk_object_unref (GTK_OBJECT (node->preference)); + guint i; + + for (i = 0; i < 3; i++) + { + g_assert (node->gconf_connections[i] != 0); + + gconf_client_notify_remove (GLOBAL.gconf_client, + node->gconf_connections[i]); + + node->gconf_connections[i] = 0; + } } } @@ -339,6 +322,34 @@ preferences_hash_node_free_func (gpointer key, preferences_hash_node_free ((PreferencesHashNode *) value); } +static void +preferences_hash_node_check_changes_func (gpointer key, + gpointer value, + gpointer user_data) +{ + PreferencesHashNode *node; + guint old_user_level; + guint new_user_level; + + g_assert (value != NULL); + + node = (PreferencesHashNode *) value; + old_user_level = (guint) user_data; + new_user_level = nautilus_user_level_manager_get_user_level (); + + /* FIXME: This is currently only works for keys, it doenst work with whole namespaces */ + if (!nautilus_user_level_manager_compare_preference_between_user_levels (node->name, + old_user_level, + new_user_level)) { + /* Invoke callbacks for this node */ + if (node->callback_list) { + g_list_foreach (node->callback_list, + preferneces_callback_node_invoke_func, + (gpointer) NULL); + } + } +} + /** * preferneces_callback_node_alloc * @@ -416,33 +427,21 @@ preferneces_callback_node_invoke_func (gpointer data, gpointer callback_data) { PreferencesCallbackNode *callback_node; - char *preference_name; callback_node = (PreferencesCallbackNode *) data; g_assert (callback_node != NULL); g_assert (callback_node->callback_proc != NULL); - preference_name = nautilus_preference_get_name (callback_node->hash_node->preference); - - g_assert (preference_name != NULL); - (* callback_node->callback_proc) (callback_node->callback_data); - - g_free (preference_name); } static void -preferences_register (char *name, - char *description, - NautilusPreferenceType type, - gconstpointer default_value, - gpointer data) +preferences_register (const char *name) { PreferencesHashNode *node; g_return_if_fail (name != NULL); - g_return_if_fail (description != NULL); preferences_initialize_if_needed (); @@ -453,73 +452,9 @@ preferences_register (char *name, return; } - node = preferences_hash_node_alloc (name, description, type, default_value, data); + node = preferences_hash_node_alloc (name); g_hash_table_insert (GLOBAL.preference_table, (gpointer) name, (gpointer) node); - - g_assert (node->preference != NULL); -} - -/** - * set_default_value_if_needed - * - * This function will ask gconf for a value. If - * - * The value is not found in the user's database: - * It will be added to the database using the given default value. - * - * The value is found in the user's database: - * Nothing. - * - * @name: The name of the preference. - * @type: The type of preference. - * @default_value: The default_value to use. - **/ -static void -set_default_value_if_needed (const char *name, - NautilusPreferenceType type, - gconstpointer default_value) -{ - GConfValue *value = NULL; - - g_return_if_fail (name != NULL); - - preferences_initialize_if_needed (); - - /* Find out if the preference exists at all */ - value = gconf_client_get_without_default (GLOBAL.gconf_client, - name, - NULL); - - /* The value does not exist, so create one */ - if (!value) { - switch (type) - { - case NAUTILUS_PREFERENCE_STRING: - /* Gconf will not grok NULL strings, so for this case dont do it. */ - if (default_value) { - value = gconf_value_new (GCONF_VALUE_STRING); - gconf_value_set_string (value, (const char *) default_value); - } - break; - case NAUTILUS_PREFERENCE_BOOLEAN: - value = gconf_value_new (GCONF_VALUE_BOOL); - gconf_value_set_bool (value, GPOINTER_TO_INT (default_value)); - break; - case NAUTILUS_PREFERENCE_ENUM: - value = gconf_value_new (GCONF_VALUE_INT); - gconf_value_set_int (value, GPOINTER_TO_INT (default_value)); - break; - } - - if (value) { - gconf_client_set (GLOBAL.gconf_client, name, value, NULL); - } - } - - if (value) { - gconf_value_destroy (value); - } } static PreferencesHashNode * @@ -537,9 +472,7 @@ preferences_hash_node_lookup (const char *name) } static PreferencesHashNode * -preferences_hash_node_lookup_with_registration (const char *name, - NautilusPreferenceType type, - gconstpointer default_value) +preferences_hash_node_lookup_with_registration (const char *name) { PreferencesHashNode * node; @@ -550,11 +483,7 @@ preferences_hash_node_lookup_with_registration (const char *name, node = preferences_hash_node_lookup (name); if (!node) { - preferences_register (g_strdup (name), - "Unspecified Description", - type, - default_value, - (gpointer) NULL); + preferences_register (name); node = preferences_hash_node_lookup (name); } @@ -573,17 +502,15 @@ preferences_gconf_callback (GConfClient *client, gpointer user_data) { PreferencesHashNode *node; - NautilusPreference *expected_preference; + const char *expected_name; char *expected_key; - g_assert (key != NULL); - - preferences_initialize_if_needed (); + g_return_if_fail (user_data != NULL); - g_assert (user_data != NULL); - g_assert (NAUTILUS_IS_PREFERENCE (user_data)); + node = (PreferencesHashNode *) user_data; - expected_preference = NAUTILUS_PREFERENCE (user_data); + expected_name = node->name; + g_assert (expected_name != NULL); /* * This gconf notification was installed with an expected key in mind. @@ -603,22 +530,31 @@ preferences_gconf_callback (GConfClient *client, * So we can use this mechanism to keep track of changes within a whole * namespace by comparing the expected_key to the given key. */ - expected_key = nautilus_preference_get_name (expected_preference); - - g_assert (expected_key != NULL); + expected_key = nautilus_user_level_manager_make_current_gconf_key (expected_name); if (strcmp (key, expected_key) != 0) { + /* The prefix should be the same */ - g_assert (strncmp (key, expected_key, strlen (expected_key)) == 0); + if (strncmp (key, expected_key, strlen (expected_key)) != 0) { + + /* FIXME: This is triggering the first time the beast runs + * without an existing ~/.gconf directory. + */ +#if 0 + g_warning ("preferences_gconf_callback: Wrong prefix! This indicates a bug.\n"); +#endif + g_free (expected_key); + return; + } + key = expected_key; } - + g_assert (key != NULL); - node = preferences_hash_node_lookup (key); + node = preferences_hash_node_lookup (expected_name); g_assert (node != NULL); - g_assert (node->preference != NULL); gconf_client_suggest_sync (GLOBAL.gconf_client, NULL); @@ -632,6 +568,21 @@ preferences_gconf_callback (GConfClient *client, g_free (expected_key); } +static void +user_level_changed_callback (GtkObject *user_level_manager, + gpointer user_data) +{ + guint new_user_level; + + new_user_level = nautilus_user_level_manager_get_user_level (); + + g_hash_table_foreach (GLOBAL.preference_table, + preferences_hash_node_check_changes_func, + (gpointer) GLOBAL.old_user_level); + + GLOBAL.old_user_level = new_user_level; +} + static gboolean preferences_initialize_if_needed (void) { @@ -674,139 +625,21 @@ preferences_initialize_if_needed (void) g_assert (GLOBAL.gconf_client != NULL); + GLOBAL.old_user_level = nautilus_user_level_manager_get_user_level (); + + /* Register to find out about user level changes */ + gtk_signal_connect (GTK_OBJECT (nautilus_user_level_manager_get ()), + "user_level_changed", + user_level_changed_callback, + NULL); + return TRUE; } + /* * Public functions */ - - -/** - * nautilus_preferences_find_preference - * - * Search for a named preference in the given preferences and return it. - * @preferences: The preferences to search - * - * Return value: A referenced pointer to the preference object that corresponds - * to the given preference name. The caller should gtk_object_unref() the return - * value of this function. - **/ -NautilusPreference * -nautilus_preferences_find_preference (const char *name) -{ - PreferencesHashNode *node; - - g_return_val_if_fail (name != NULL, NULL); - - preferences_initialize_if_needed (); - - node = preferences_hash_node_lookup (name); - - g_assert (node != NULL); - - gtk_object_ref (GTK_OBJECT (node->preference)); - - return node->preference; -} - -void -nautilus_preferences_set_info (const char *name, - const char *description, - NautilusPreferenceType type, - gconstpointer default_value) -{ - PreferencesHashNode *node; - - g_return_if_fail (name != NULL); - - preferences_initialize_if_needed (); - - node = preferences_hash_node_lookup_with_registration (name, type, default_value); - - g_assert (node != NULL); - g_assert (node->preference != NULL); - - if (description) { - nautilus_preference_set_description (node->preference, description); - } - - set_default_value_if_needed (name, type, default_value); -} - -void -nautilus_preferences_enum_add_entry (const char *name, - const char *entry_name, - const char *entry_description, - int entry_value) -{ - PreferencesHashNode *node; - - g_return_if_fail (name != NULL); - - preferences_initialize_if_needed (); - - node = preferences_hash_node_lookup_with_registration (name, NAUTILUS_PREFERENCE_ENUM, NULL); - - g_assert (node != NULL); - g_assert (node->preference != NULL); - - g_assert (nautilus_preference_get_preference_type (node->preference) == NAUTILUS_PREFERENCE_ENUM); - - nautilus_preference_enum_add_entry (node->preference, - entry_name, - entry_description, - entry_value); -} - -gboolean -nautilus_preferences_add_boolean_callback (const char *name, - NautilusPreferencesCallback callback_proc, - gpointer callback_data) -{ - PreferencesHashNode *node; - - g_return_val_if_fail (name != NULL, FALSE); - g_return_val_if_fail (callback_proc != NULL, FALSE); - - preferences_initialize_if_needed (); - - node = preferences_hash_node_lookup_with_registration (name, NAUTILUS_PREFERENCE_BOOLEAN, NULL); - - if (node == NULL) { - g_warning ("trying to add a callback for an unregistered preference"); - return FALSE; - } - - preferences_hash_node_add_callback (node, callback_proc, callback_data); - - return TRUE; -} - -gboolean -nautilus_preferences_add_enum_callback (const char *name, - NautilusPreferencesCallback callback_proc, - gpointer callback_data) -{ - PreferencesHashNode *node; - - g_return_val_if_fail (name != NULL, FALSE); - g_return_val_if_fail (callback_proc != NULL, FALSE); - - preferences_initialize_if_needed (); - - node = preferences_hash_node_lookup_with_registration (name, NAUTILUS_PREFERENCE_ENUM, NULL); - - if (node == NULL) { - g_warning ("trying to add a callback for an unregistered preference"); - return FALSE; - } - - preferences_hash_node_add_callback (node, callback_proc, callback_data); - - return TRUE; -} - gboolean nautilus_preferences_add_callback (const char *name, NautilusPreferencesCallback callback_proc, @@ -819,7 +652,7 @@ nautilus_preferences_add_callback (const char *name, preferences_initialize_if_needed (); - node = preferences_hash_node_lookup_with_registration (name, NAUTILUS_PREFERENCE_STRING, NULL); + node = preferences_hash_node_lookup_with_registration (name); if (node == NULL) { g_warning ("trying to add a callback for an unregistered preference"); @@ -856,33 +689,51 @@ void nautilus_preferences_set_boolean (const char *name, gboolean boolean_value) { + char *key; + gboolean gconf_result; g_return_if_fail (name != NULL); preferences_initialize_if_needed (); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + /* Make sure the preference value is indeed different */ - if (gconf_client_get_bool (GLOBAL.gconf_client, name, NULL) == boolean_value) { + if (gconf_client_get_bool (GLOBAL.gconf_client, key, NULL) == boolean_value) { + g_free (key); return; } - gconf_result = gconf_client_set_bool (GLOBAL.gconf_client, name, boolean_value, NULL); + gconf_result = gconf_client_set_bool (GLOBAL.gconf_client, key, boolean_value, NULL); g_assert (gconf_result); gconf_client_suggest_sync (GLOBAL.gconf_client, NULL); + + g_free (key); } gboolean nautilus_preferences_get_boolean (const char *name, gboolean default_value) { + gboolean result; + char *key; + g_return_val_if_fail (name != NULL, FALSE); preferences_initialize_if_needed (); - return gconf_client_get_bool (GLOBAL.gconf_client, name, NULL); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + + result = gconf_client_get_bool (GLOBAL.gconf_client, key, NULL); + + g_free (key); + + return result; } void @@ -890,32 +741,47 @@ nautilus_preferences_set_enum (const char *name, int enum_value) { gboolean gconf_result; + char *key; g_return_if_fail (name != NULL); preferences_initialize_if_needed (); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + /* Make sure the preference value is indeed different */ - if (gconf_client_get_int (GLOBAL.gconf_client, name, NULL) == enum_value) { + if (gconf_client_get_int (GLOBAL.gconf_client, key, NULL) == enum_value) { + g_free (key); return; } - gconf_result = gconf_client_set_int (GLOBAL.gconf_client, name, enum_value, NULL); + gconf_result = gconf_client_set_int (GLOBAL.gconf_client, key, enum_value, NULL); g_assert (gconf_result); gconf_client_suggest_sync (GLOBAL.gconf_client, NULL); + + g_free (key); } int nautilus_preferences_get_enum (const char *name, int default_value) { + int result; + char *key; + g_return_val_if_fail (name != NULL, FALSE); preferences_initialize_if_needed (); - return gconf_client_get_int (GLOBAL.gconf_client, name, NULL); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + + result = gconf_client_get_int (GLOBAL.gconf_client, key, NULL); + + return result; } void @@ -923,14 +789,18 @@ nautilus_preferences_set (const char *name, const char *value) { gboolean gconf_result; + char *key; g_return_if_fail (name != NULL); preferences_initialize_if_needed (); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + /* Make sure the preference value is indeed different */ if (value) { - char *current_value = gconf_client_get_string (GLOBAL.gconf_client, name, NULL); + char *current_value = gconf_client_get_string (GLOBAL.gconf_client, key, NULL); int result = nautilus_strcmp (current_value, value); if (current_value) { @@ -938,15 +808,18 @@ nautilus_preferences_set (const char *name, } if (result == 0) { + g_free (key); return; } } - gconf_result = gconf_client_set_string (GLOBAL.gconf_client, name, value, NULL); + gconf_result = gconf_client_set_string (GLOBAL.gconf_client, key, value, NULL); g_assert (gconf_result); gconf_client_suggest_sync (GLOBAL.gconf_client, NULL); + + g_free (key); } char * @@ -954,16 +827,22 @@ nautilus_preferences_get (const char *name, const char *default_value) { gchar *value = NULL; + char *key; g_return_val_if_fail (name != NULL, FALSE); preferences_initialize_if_needed (); - value = gconf_client_get_string (GLOBAL.gconf_client, name, NULL); + key = nautilus_user_level_manager_make_current_gconf_key (name); + g_assert (key != NULL); + + value = gconf_client_get_string (GLOBAL.gconf_client, key, NULL); if (!value && default_value) { value = g_strdup (default_value); } + + g_free (key); return value; } @@ -974,6 +853,10 @@ nautilus_preferences_shutdown (void) if (GLOBAL.preference_table == NULL && GLOBAL.gconf_client == NULL) { return; } + + gtk_signal_disconnect_by_func (GTK_OBJECT (nautilus_user_level_manager_get ()), + user_level_changed_callback, + NULL); if (GLOBAL.preference_table != NULL) { g_hash_table_foreach (GLOBAL.preference_table, @@ -990,5 +873,6 @@ nautilus_preferences_shutdown (void) GLOBAL.gconf_client = NULL; } + } diff --git a/libnautilus-private/nautilus-preferences.h b/libnautilus-private/nautilus-preferences.h index faaf2b7ff..4b624c372 100644 --- a/libnautilus-private/nautilus-preferences.h +++ b/libnautilus-private/nautilus-preferences.h @@ -38,21 +38,6 @@ BEGIN_GNOME_DECLS */ typedef void (*NautilusPreferencesCallback) (gpointer callback_data); -NautilusPreference *nautilus_preferences_find_preference (const char *name); -void nautilus_preferences_set_info (const char *name, - const char *description, - NautilusPreferenceType type, - gconstpointer default_value); -void nautilus_preferences_enum_add_entry (const char *name, - const char *entry_name, - const char *entry_description, - int entry_value); -gboolean nautilus_preferences_add_enum_callback (const char *name, - NautilusPreferencesCallback callback, - gpointer callback_data); -gboolean nautilus_preferences_add_boolean_callback (const char *name, - NautilusPreferencesCallback callback, - gpointer callback_data); gboolean nautilus_preferences_add_callback (const char *name, NautilusPreferencesCallback callback, gpointer callback_data); diff --git a/libnautilus-private/nautilus-user-level-manager.c b/libnautilus-private/nautilus-user-level-manager.c index 0a7c2b997..753e8c2ab 100644 --- a/libnautilus-private/nautilus-user-level-manager.c +++ b/libnautilus-private/nautilus-user-level-manager.c @@ -371,5 +371,141 @@ nautilus_user_level_manager_get_user_level_as_string (void) user_level_string = gconf_client_get_string (manager->gconf_client, USER_LEVEL_KEY, NULL); + if (!user_level_string) + user_level_string = g_strdup ("novice"); + return user_level_string; } + + +/** + * nautilus_user_level_manager_set_default_value_if_needed + * + * This function will ask gconf for a value. If + * + * The value is not found in the user's database: + * It will be added to the database using the given default value. + * + * The value is found in the user's database: + * Nothing. + * + * @name: The name of the preference. + * @type: The type of preference. + * @default_value: The default_value to use. + **/ +void +nautilus_user_level_manager_set_default_value_if_needed (const char *preference_name, + NautilusPreferenceType type, + guint user_level, + gconstpointer default_value) +{ + NautilusUserLevelManager *manager = nautilus_user_level_manager_get (); + GConfValue *value = NULL; + char *key; + + g_return_if_fail (preference_name != NULL); + + key = nautilus_user_level_manager_make_gconf_key (preference_name, user_level); + g_assert (key != NULL); + + /* Find out if the preference exists at all */ + value = gconf_client_get_without_default (manager->gconf_client, key, NULL); + + /* The value does not exist, so create one */ + if (!value) { + switch (type) + { + case NAUTILUS_PREFERENCE_STRING: + /* Gconf will not grok NULL strings, so for this case dont do it. */ + if (default_value) { + value = gconf_value_new (GCONF_VALUE_STRING); + gconf_value_set_string (value, (const char *) default_value); + } + break; + case NAUTILUS_PREFERENCE_BOOLEAN: + value = gconf_value_new (GCONF_VALUE_BOOL); + gconf_value_set_bool (value, GPOINTER_TO_INT (default_value)); + break; + case NAUTILUS_PREFERENCE_ENUM: + value = gconf_value_new (GCONF_VALUE_INT); + gconf_value_set_int (value, GPOINTER_TO_INT (default_value)); + break; + } + + if (value) { + gconf_client_set (manager->gconf_client, key, value, NULL); + } + } + + if (value) { + gconf_value_destroy (value); + } + + g_free (key); +} + +gboolean +nautilus_user_level_manager_compare_preference_between_user_levels (const char *preference_name, + guint user_level_a, + guint user_level_b) +{ + NautilusUserLevelManager *manager = nautilus_user_level_manager_get (); + gboolean result = FALSE; + char *key_a; + char *key_b; + GConfValue *value_a; + GConfValue *value_b; + + g_return_val_if_fail (preference_name != NULL, FALSE); + + key_a = nautilus_user_level_manager_make_gconf_key (preference_name, user_level_a); + g_assert (key_a != NULL); + + key_b = nautilus_user_level_manager_make_gconf_key (preference_name, user_level_b); + g_assert (key_b != NULL); + + value_a = gconf_client_get (manager->gconf_client, key_a, NULL); + value_b = gconf_client_get (manager->gconf_client, key_b, NULL); + + g_free (key_a); + g_free (key_b); + + if (value_a && value_b) + { + g_assert (value_a->type == value_b->type); + + switch (value_a->type) + { + case GCONF_VALUE_STRING: + result = (gconf_value_string (value_a) + && gconf_value_string (value_b) + && (strcmp (gconf_value_string (value_a), gconf_value_string (value_b)) == 0)); + break; + + case GCONF_VALUE_INT: + result = (gconf_value_int (value_a) == gconf_value_int (value_b)); + break; + + case GCONF_VALUE_BOOL: + result = (gconf_value_bool (value_a) == gconf_value_bool (value_b)); + break; + + default: + g_assert_not_reached (); + } + } + else + { + result = TRUE; + } + + if (value_a) { + gconf_value_destroy (value_a); + } + + if (value_b) { + gconf_value_destroy (value_b); + } + + return result; +} diff --git a/libnautilus-private/nautilus-user-level-manager.h b/libnautilus-private/nautilus-user-level-manager.h index 248d01757..7f6d7b3cf 100644 --- a/libnautilus-private/nautilus-user-level-manager.h +++ b/libnautilus-private/nautilus-user-level-manager.h @@ -28,6 +28,7 @@ #include <gtk/gtkobject.h> #include <libgnome/gnome-defs.h> #include <libnautilus-extensions/nautilus-string-list.h> +#include <nautilus-widgets/nautilus-preference.h> BEGIN_GNOME_DECLS @@ -48,7 +49,13 @@ char *nautilus_user_level_manager_make_current_gconf_key (const char *pref char *nautilus_user_level_manager_make_gconf_key (const char *preference_name, guint user_level); char *nautilus_user_level_manager_get_user_level_as_string (void); - +void nautilus_user_level_manager_set_default_value_if_needed (const char *preference_name, + NautilusPreferenceType type, + guint user_level, + gconstpointer default_value); +gboolean nautilus_user_level_manager_compare_preference_between_user_levels (const char *preference_name, + guint user_level_a, + guint user_level_b); BEGIN_GNOME_DECLS diff --git a/libnautilus-private/test-nautilus-preferences.c b/libnautilus-private/test-nautilus-preferences.c index e112fe507..5ba9b4c7f 100644 --- a/libnautilus-private/test-nautilus-preferences.c +++ b/libnautilus-private/test-nautilus-preferences.c @@ -94,22 +94,25 @@ create_enum_item (const char *preference_name) static void register_global_preferences (void) { - nautilus_preferences_set_info (FRUIT_PREFERENCE, - "Fruits", - NAUTILUS_PREFERENCE_ENUM, - (gconstpointer) FRUIT_ORANGE); + gconstpointer default_values[3] = { (gconstpointer)FRUIT_ORANGE, (gconstpointer)FRUIT_ORANGE, (gconstpointer)FRUIT_ORANGE }; - nautilus_preferences_enum_add_entry (FRUIT_PREFERENCE, - "apple", - "Apple", - FRUIT_APPLE); + nautilus_preference_set_info_by_name (FRUIT_PREFERENCE, + "Fruits", + NAUTILUS_PREFERENCE_ENUM, + default_values, + 3); + + nautilus_preference_enum_add_entry_by_name (FRUIT_PREFERENCE, + "apple", + "Apple", + FRUIT_APPLE); - nautilus_preferences_enum_add_entry (FRUIT_PREFERENCE, + nautilus_preference_enum_add_entry_by_name (FRUIT_PREFERENCE, "orange", "Orange", FRUIT_ORANGE); - nautilus_preferences_enum_add_entry (FRUIT_PREFERENCE, + nautilus_preference_enum_add_entry_by_name (FRUIT_PREFERENCE, "bannana", "Bannana", FRUIT_BANNANA); |