[PATCH] Use the divetrip index to look up divelist trip entries

Linus Torvalds torvalds at linux-foundation.org
Tue Feb 19 08:05:59 PST 2013


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Tue, 19 Feb 2013 07:59:18 -0800
Subject: [PATCH] Use the divetrip index to look up divelist trip entries

We used to look up dive trips by their date, but these days we always
create a dynamic index for a dive trip when we insert it into the
divelist model, so we can use that to unambiguously match up dive trips
with the dive model entries.

That means that we don't get confused if we have two trips with the
exact same time, which happens when you load all the test-dives, for
example.

Reported-by: Miika Turkia <miika.turkia at gmail.com>
Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

This fixes the selection confusion that I could see with the test-dives 
for me. With the date-based one, the code would find the wrong trip, and 
not properly notice that it should be selected.

Miika, was that what you used to test collapsing trips too? If not, can 
you send me your xml file so that I can see what it is about your dives 
that triggers the odd behavior?

 divelist.c | 56 ++++++++++++++++++++++----------------------------------
 1 file changed, 22 insertions(+), 34 deletions(-)

diff --git a/divelist.c b/divelist.c
index 76cce83f5a94..69d96fda25d3 100644
--- a/divelist.c
+++ b/divelist.c
@@ -1029,27 +1029,20 @@ static void dump_trip_list(void)
 }
 #endif
 
-/* this finds a trip that starts at precisely the time given */
-static dive_trip_t *find_trip_by_time(timestamp_t when)
+static dive_trip_t *find_trip_by_idx(int idx)
 {
 	dive_trip_t *trip = dive_trip_list;
 
-	while (trip && trip->when < when)
+	if (!idx)
+		return NULL;
+	if (idx < 0)
+		idx = -idx;
+
+	while (trip) {
+		if (trip->index == idx)
+			return trip;
 		trip = trip->next;
-	if (trip && trip->when == when) {
-#ifdef DEBUG_TRIP
-		struct tm tm;
-		utc_mkdate(trip->when, &tm);
-		printf("found trip %p @ %04d-%02d-%02d %02d:%02d:%02d\n",
-			trip,
-			tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
-			tm.tm_hour, tm.tm_min, tm.tm_sec);
-#endif
-		return trip;
 	}
-#ifdef DEBUG_TRIP
-	printf("no matching trip\n");
-#endif
 	return NULL;
 }
 
@@ -1382,7 +1375,7 @@ static gint dive_nr_sort(GtkTreeModel *model,
 
 	if (idx_a < 0) {
 		a = NULL;
-		tripa = find_trip_by_time(when_a);
+		tripa = find_trip_by_idx(idx_a);
 	} else {
 		a = get_dive(idx_a);
 		if (a)
@@ -1391,7 +1384,7 @@ static gint dive_nr_sort(GtkTreeModel *model,
 
 	if (idx_b < 0) {
 		b = NULL;
-		tripb = find_trip_by_time(when_b);
+		tripb = find_trip_by_idx(idx_b);
 	} else {
 		b = get_dive(idx_b);
 		if (b)
@@ -1521,13 +1514,13 @@ void add_dive_cb(GtkWidget *menuitem, gpointer data)
 
 static void edit_trip_cb(GtkWidget *menuitem, GtkTreePath *path)
 {
+	int idx;
 	GtkTreeIter iter;
-	timestamp_t when;
 	dive_trip_t *dive_trip;
 
 	gtk_tree_model_get_iter(MODEL(dive_list), &iter, path);
-	gtk_tree_model_get(MODEL(dive_list), &iter, DIVE_DATE, &when, -1);
-	dive_trip = find_trip_by_time(when);
+	gtk_tree_model_get(MODEL(dive_list), &iter, DIVE_INDEX, &idx, -1);
+	dive_trip = find_trip_by_idx(idx);
 	if (edit_trip(dive_trip))
 		gtk_tree_store_set(STORE(dive_list), &iter, DIVE_LOCATION, dive_trip->location, -1);
 }
@@ -2176,16 +2169,14 @@ void remember_tree_state()
 		return;
 	do {
 		int idx;
-		timestamp_t when;
 		GtkTreePath *path;
 
-		gtk_tree_model_get(TREEMODEL(dive_list), &iter,
-				   DIVE_INDEX, &idx, DIVE_DATE, &when, -1);
+		gtk_tree_model_get(TREEMODEL(dive_list), &iter, DIVE_INDEX, &idx, -1);
 		if (idx >= 0)
 			continue;
 		path = gtk_tree_model_get_path(TREEMODEL(dive_list), &iter);
 		if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), path)) {
-			trip = find_trip_by_time(when);
+			trip = find_trip_by_idx(idx);
 			if (trip)
 				trip->expanded = TRUE;
 		}
@@ -2196,15 +2187,14 @@ void remember_tree_state()
 static gboolean restore_node_state(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 {
 	int idx;
-	timestamp_t when;
 	struct dive *dive;
 	dive_trip_t *trip;
 	GtkTreeView *tree_view = GTK_TREE_VIEW(dive_list.tree_view);
 	GtkTreeSelection *selection = gtk_tree_view_get_selection(tree_view);
 
-	gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &when, -1);
+	gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
 	if (idx < 0) {
-		trip = find_trip_by_time(when);
+		trip = find_trip_by_idx(idx);
 		if (trip && trip->expanded)
 			gtk_tree_view_expand_row(tree_view, path, FALSE);
 		if (trip && trip->selected)
@@ -2700,17 +2690,16 @@ static gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *m
 				GtkTreePath *path, gboolean was_selected, gpointer userdata)
 {
 	int idx;
-	timestamp_t when;
 	GtkTreeIter iter;
 
 	if (!was_selected)
 		return TRUE;
 	gtk_tree_model_get_iter(model, &iter, path);
-	gtk_tree_model_get(model, &iter, DIVE_INDEX, &idx, DIVE_DATE, &when, -1);
+	gtk_tree_model_get(model, &iter, DIVE_INDEX, &idx, -1);
 	if (idx < 0) {
 		int i;
 		struct dive *dive;
-		dive_trip_t *trip = find_trip_by_time(when);
+		dive_trip_t *trip = find_trip_by_idx(idx);
 		if (!trip)
 			return TRUE;
 
@@ -2733,13 +2722,12 @@ static gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *m
 static void entry_selected(GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 {
 	int idx;
-	timestamp_t when;
 
-	gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &when, -1);
+	gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
 	if (idx < 0) {
 		int i;
 		struct dive *dive;
-		dive_trip_t *trip = find_trip_by_time(when);
+		dive_trip_t *trip = find_trip_by_idx(idx);
 
 		if (!trip)
 			return;
-- 
1.8.1.3.556.gb3310b5



More information about the subsurface mailing list