[ghex] Split the huge configuration callback up



commit b92fc86bd7eab9642f7d5e71bc26a2f2fa093038
Author: Kalev Lember <kalevlember gmail com>
Date:   Mon Aug 8 19:18:50 2011 +0300

    Split the huge configuration callback up

 src/config.c |  254 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 151 insertions(+), 103 deletions(-)
---
diff --git a/src/config.c b/src/config.c
index 068e92c..4adcfa7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -129,107 +129,142 @@ void ghex_load_configuration () {
 }
 
 static void
-ghex_prefs_changed_cb (GSettings   *settings,
-                       const gchar *key,
-                       gpointer     user_data)
+offsets_column_changed_cb (GSettings   *settings,
+                           const gchar *key,
+                           gpointer     user_data)
 {
-	const GList *winn, *docn;
-
-	if (!strcmp (key, GHEX_PREF_OFFSETS_COLUMN)) {
-		gboolean show_off = g_settings_get_boolean (settings, key);
-		show_offsets_column = show_off;
-		winn = ghex_window_get_list();
-		while(winn) {
-			if(GHEX_WINDOW(winn->data)->gh)
-				gtk_hex_show_offsets(GHEX_WINDOW(winn->data)->gh, show_off);
-			winn = g_list_next(winn);
-		}
-	}
-	else if (!strcmp (key, GHEX_PREF_GROUP)) {
-		def_group_type = g_settings_get_int (settings, key);
-	}
-	else if (!strcmp (key, GHEX_PREF_MAX_UNDO_DEPTH)) {
-		gint new_undo_max = g_settings_get_int (settings, key);
-		max_undo_depth = new_undo_max;
-		docn = hex_document_get_list();
-		while(docn) {
-			hex_document_set_max_undo(HEX_DOCUMENT(docn->data), max_undo_depth);
-			docn = g_list_next(docn);
-		}
-	}
-	else if (!strcmp (key, GHEX_PREF_BOX_SIZE)) {
-		shaded_box_size = g_settings_get_int (settings, key);
-	}
-	else if (!strcmp (key, GHEX_PREF_OFFSET_FORMAT) &&
-			strcmp (g_settings_get_string (settings, key), offset_fmt)) {
-		gchar *old_offset_fmt = offset_fmt;
-		gint len, i;
-		gboolean expect_spec;
-
-		offset_fmt = g_strdup (g_settings_get_string (settings, key));
-
-		/* check for a valid format string */
-		len = strlen(offset_fmt);
-		expect_spec = FALSE;
-		for(i = 0; i < len; i++) {
-			if(offset_fmt[i] == '%')
-				expect_spec = TRUE;
-			if( expect_spec &&
-				( (offset_fmt[i] >= 'a' && offset_fmt[i] <= 'z') ||
-				  (offset_fmt[i] >= 'A' && offset_fmt[i] <= 'Z') ) ) {
-				expect_spec = FALSE;
-				if(offset_fmt[i] != 'x' && offset_fmt[i] != 'd' &&
-				   offset_fmt[i] != 'o' && offset_fmt[i] != 'X' &&
-				   offset_fmt[i] != 'P' && offset_fmt[i] != 'p') {
-					g_free(offset_fmt);
-					offset_fmt = old_offset_fmt;
-					g_settings_set_string (settings, GHEX_PREF_OFFSET_FORMAT, "%X");
-				}
-			}
-		}
-		if(offset_fmt != old_offset_fmt)
-			g_free(old_offset_fmt);
-	}
-	else if (!strcmp (key, GHEX_PREF_FONT)) {
-		if (g_settings_get_string (settings, key) != NULL) {
-			const gchar *font_name = g_settings_get_string (settings, key);
-			PangoFontMetrics *new_metrics;
-			PangoFontDescription *new_desc;
-
-			if((new_metrics = gtk_hex_load_font(font_name)) != NULL) {
-				new_desc = pango_font_description_from_string (font_name);
-				winn = ghex_window_get_list();
-				while(winn) {
-					if(GHEX_WINDOW(winn->data)->gh)
-						gtk_hex_set_font(GHEX_WINDOW(winn->data)->gh, new_metrics, new_desc);
-					winn = g_list_next(winn);
-				}
-		
-				if (def_metrics)
-					pango_font_metrics_unref (def_metrics);
-			
-				if (def_font_desc)
-					pango_font_description_free (def_font_desc);
-
-				if (def_font_name)
-					g_free(def_font_name);
-
-				def_metrics = new_metrics;
-				def_font_name = g_strdup(font_name);
-				def_font_desc = new_desc;
-			}
-		}
-	}
-	else if (!strcmp (key, GHEX_PREF_DATA_FONT)) {
-		if(data_font_name)
-			g_free(data_font_name);
-		data_font_name = g_strdup (g_settings_get_string (settings, key));
-	}
-	else if (!strcmp (key, GHEX_PREF_HEADER_FONT)) {
-		if(header_font_name)
-			g_free(header_font_name);
-		header_font_name = g_strdup (g_settings_get_string (settings, key));
-	}
+    const GList *winn;
+    gboolean show_off = g_settings_get_boolean (settings, key);
+
+    show_offsets_column = show_off;
+    winn = ghex_window_get_list ();
+    while (winn) {
+        if (GHEX_WINDOW (winn->data)->gh)
+            gtk_hex_show_offsets (GHEX_WINDOW (winn->data)->gh, show_off);
+        winn = g_list_next (winn);
+    }
+}
+
+static void
+group_changed_cb (GSettings   *settings,
+                  const gchar *key,
+                  gpointer     user_data)
+{
+    def_group_type = g_settings_get_int (settings, key);
+}
+
+static void
+max_undo_depth_changed_cb (GSettings   *settings,
+                           const gchar *key,
+                           gpointer     user_data)
+{
+    const GList *docn;
+    gint new_undo_max = g_settings_get_int (settings, key);
+
+    max_undo_depth = new_undo_max;
+    docn = hex_document_get_list ();
+    while (docn) {
+        hex_document_set_max_undo (HEX_DOCUMENT (docn->data), max_undo_depth);
+        docn = g_list_next (docn);
+    }
+}
+
+static void
+box_size_changed_cb (GSettings   *settings,
+                     const gchar *key,
+                     gpointer     user_data)
+{
+    shaded_box_size = g_settings_get_int (settings, key);
+}
+
+static void
+offset_format_changed_cb (GSettings   *settings,
+                          const gchar *key,
+                          gpointer     user_data)
+{
+    gchar *old_offset_fmt = offset_fmt;
+    gint len, i;
+    gboolean expect_spec;
+
+    offset_fmt = g_strdup (g_settings_get_string (settings, key));
+
+    /* check for a valid format string */
+    len = strlen (offset_fmt);
+    expect_spec = FALSE;
+    for (i = 0; i < len; i++) {
+        if (offset_fmt[i] == '%')
+            expect_spec = TRUE;
+        if (expect_spec &&
+            ((offset_fmt[i] >= 'a' && offset_fmt[i] <= 'z') ||
+             (offset_fmt[i] >= 'A' && offset_fmt[i] <= 'Z'))) {
+            expect_spec = FALSE;
+            if (offset_fmt[i] != 'x' && offset_fmt[i] != 'd' &&
+                offset_fmt[i] != 'o' && offset_fmt[i] != 'X' &&
+                offset_fmt[i] != 'P' && offset_fmt[i] != 'p') {
+                g_free (offset_fmt);
+                offset_fmt = old_offset_fmt;
+                g_settings_set_string (settings, GHEX_PREF_OFFSET_FORMAT, "%X");
+            }
+        }
+    }
+    if (offset_fmt != old_offset_fmt)
+        g_free (old_offset_fmt);
+}
+
+static void
+font_changed_cb (GSettings   *settings,
+                 const gchar *key,
+                 gpointer     user_data)
+{
+    const GList *winn;
+    const gchar *font_name = g_settings_get_string (settings, key);
+    PangoFontMetrics *new_metrics;
+    PangoFontDescription *new_desc;
+
+    g_return_if_fail (font_name != NULL);
+
+    if ((new_metrics = gtk_hex_load_font (font_name)) != NULL) {
+        new_desc = pango_font_description_from_string (font_name);
+        winn = ghex_window_get_list ();
+        while (winn) {
+            if (GHEX_WINDOW (winn->data)->gh)
+                gtk_hex_set_font (GHEX_WINDOW (winn->data)->gh, new_metrics, new_desc);
+            winn = g_list_next (winn);
+        }
+
+        if (def_metrics)
+            pango_font_metrics_unref (def_metrics);
+
+        if (def_font_desc)
+            pango_font_description_free (def_font_desc);
+
+        if (def_font_name)
+            g_free (def_font_name);
+
+        def_metrics = new_metrics;
+        def_font_name = g_strdup (font_name);
+        def_font_desc = new_desc;
+    }
+}
+
+static void
+data_font_changed_cb (GSettings   *settings,
+                      const gchar *key,
+                      gpointer     user_data)
+{
+    if (data_font_name)
+        g_free (data_font_name);
+    data_font_name = g_strdup (g_settings_get_string (settings, key));
+}
+
+static void
+header_font_changed_cb (GSettings   *settings,
+                        const gchar *key,
+                        gpointer     user_data)
+{
+    if (header_font_name)
+        g_free (header_font_name);
+    header_font_name = g_strdup (g_settings_get_string (settings, key));
 }
 
 void ghex_init_configuration ()
@@ -238,7 +273,20 @@ void ghex_init_configuration ()
 
     g_return_if_fail (settings != NULL);
 
-    g_signal_connect (settings, "changed",
-                      G_CALLBACK (ghex_prefs_changed_cb),
-                      NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_OFFSETS_COLUMN,
+                      G_CALLBACK (offsets_column_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_GROUP,
+                      G_CALLBACK (group_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_MAX_UNDO_DEPTH,
+                      G_CALLBACK (max_undo_depth_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_BOX_SIZE,
+                      G_CALLBACK (box_size_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_OFFSET_FORMAT,
+                      G_CALLBACK (offset_format_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_FONT,
+                      G_CALLBACK (font_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_DATA_FONT,
+                      G_CALLBACK (data_font_changed_cb), NULL);
+    g_signal_connect (settings, "changed::" GHEX_PREF_HEADER_FONT,
+                      G_CALLBACK (header_font_changed_cb), NULL);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]