[PATCH] Use gtk_tree_model_get_value() to get the index and date of a dive

Lubomir I. Ivanov neolit123 at gmail.com
Tue Sep 18 15:44:49 PDT 2012


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

When retrieving the index and date from the model and iterator
in divelist.c:date_data_func(), use gtk_tree_model_get_value()
to write to Gvalue entries, which should be technically safer
than the varargs list of gtk_tree_model_get(), but shouldn't break either.

This seems to solve a bug on Ubuntu 12.0.4, where the DIVE_INDEX in the
abovementioned functon is never set to -1 (entry is a Trip).

The same bug cannot be reporoduced on Debian 6.0.4 and Windows 7
with the latest GTK / GLib.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---
 dive.h     |  5 +++++
 divelist.c | 16 ++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/dive.h b/dive.h
index 5637edf..5ff883a 100644
--- a/dive.h
+++ b/dive.h
@@ -8,6 +8,11 @@
 #include <glib/gstdio.h>
 #include <libxml/tree.h>
 
+/* Since glib-2.30 */
+#ifndef G_VALUE_INIT
+	#define G_VALUE_INIT  { 0, { { 0 } } }
+#endif
+
 /*
  * Some silly typedefs to make our units very explicit.
  *
diff --git a/divelist.c b/divelist.c
index aedcdf0..8df006a 100644
--- a/divelist.c
+++ b/divelist.c
@@ -375,18 +375,23 @@ static void date_data_func(GtkTreeViewColumn *col,
 			   GtkTreeIter *iter,
 			   gpointer data)
 {
-	int val, idx, nr;
+	int nr;
 	struct tm *tm;
 	time_t when;
 	char buffer[40];
 
-	gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, -1);
+	GValue gv_idx = G_VALUE_INIT;
+	GValue gv_date = G_VALUE_INIT;
+
+	gtk_tree_model_get_value(model, iter, DIVE_INDEX, &gv_idx);
+	gtk_tree_model_get_value(model, iter, DIVE_DATE, &gv_date);
+
 	nr = gtk_tree_model_iter_n_children(model, iter);
 	/* 2038 problem */
-	when = val;
+	when = g_value_get_long(&gv_date);
 
 	tm = gmtime(&when);
-	if (idx < 0) {
+	if (g_value_get_int(&gv_idx) < 0) {
 		snprintf(buffer, sizeof(buffer),
 			"Trip %s, %s %d, %d (%d dive%s)",
 			weekday(tm->tm_wday),
@@ -401,6 +406,9 @@ static void date_data_func(GtkTreeViewColumn *col,
 			tm->tm_mday, tm->tm_year + 1900,
 			tm->tm_hour, tm->tm_min);
 	}
+
+	g_value_unset(&gv_idx);
+	g_value_unset(&gv_date);
 	g_object_set(renderer, "text", buffer, NULL);
 }
 
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list