start of proper filtering
authorThomas Thurman <tthurman@gnome.org>
Fri, 28 Aug 2009 17:42:29 +0000 (13:42 -0400)
committerThomas Thurman <tthurman@gnome.org>
Fri, 28 Aug 2009 17:42:29 +0000 (13:42 -0400)
belltower.c

index 9649b04..0572fd4 100644 (file)
 
 GtkWidget *window;
 
+typedef enum {
+  /** stop scanning the database */
+  FILTER_STOP,
+  /** ignore this one */
+  FILTER_PASS,
+  /** add this one to the list */
+  FILTER_ACCEPT
+} FilterResult;
+
 /*
   FIXME:
   We should really do this by looking at the header row of the table.
@@ -260,7 +269,7 @@ get_countries_cb (tower *details,
                           g_strdup (details->fields[FieldCountry]));
     }
 
-  return TRUE;
+  return FILTER_PASS;
 }
 
 typedef struct {
@@ -275,10 +284,10 @@ get_counties_cb (tower *details,
   country_cb_data *d = (country_cb_data *)data;
 
   if (details->serial==0)
-    return TRUE; /* header row */
+    return FILTER_PASS; /* header row */
 
   if (strcmp(details->fields[FieldCountry], d->country_name)!=0)
-    return TRUE; /* wrong country */
+    return FILTER_PASS; /* wrong country */
 
   if (!g_hash_table_lookup_extended (d->hash,
                                    details->fields[FieldCounty],
@@ -289,7 +298,7 @@ get_counties_cb (tower *details,
                           g_strdup (details->fields[FieldCounty]));
     }
 
-  return TRUE;
+  return FILTER_PASS;
 }
 
 static gboolean
@@ -306,7 +315,7 @@ single_tower_cb (tower *details,
   if (strcmp(details->fields[FieldPrimaryKey], primary_key)!=0)
     {
       /* not this one; keep going */
-      return TRUE;
+      return FILTER_PASS;
     }
 
   tower_window = hildon_stackable_window_new ();
@@ -399,11 +408,12 @@ single_tower_cb (tower *details,
         details->fields[FieldLong]);
   gtk_widget_show_all (GTK_WIDGET (tower_window));
 
-  return FALSE;
+  return FILTER_STOP;
 }
 
 static void
 parse_dove (ParseDoveCallback callback,
+           GSList **filter_results,
            gpointer data)
 {
   FILE *dove = fopen("/usr/share/belltower/dove.txt", "r");
@@ -450,10 +460,22 @@ parse_dove (ParseDoveCallback callback,
          result.fields[FieldCountry] = "England";
        }
 
-      if (!callback (&result, data))
+      switch (callback (&result, data))
        {
+       case FILTER_PASS:
+         /* nothing */
+         break;
+
+       case FILTER_STOP:
          fclose (dove);
          return;
+
+       case FILTER_ACCEPT:
+         if (filter_results)
+           {
+             *filter_results = g_slist_append (*filter_results,
+                                               result.fields[FieldPrimaryKey]);
+           }
        }
 
       result.serial++;
@@ -490,8 +512,7 @@ nearby_towers (void)
 static void
 show_tower (char *primary_key)
 {
-  parse_dove (single_tower_cb,
-             primary_key);
+  parse_dove (single_tower_cb, NULL, primary_key);
 }
 
 static gint strcmp_f (gconstpointer a,
@@ -532,8 +553,7 @@ towers_by_subarea (gchar *area)
   gtk_window_set_title (GTK_WINDOW (dialog), title);
   g_free (title);
 
-  parse_dove (get_counties_cb,
-             &d);
+  parse_dove (get_counties_cb, NULL, &d);
 
   g_hash_table_foreach (hash,
                        put_areas_into_list,
@@ -572,8 +592,7 @@ towers_by_area (void)
 
   gtk_window_set_title (GTK_WINDOW (dialog), "Areas of the world");
 
-  parse_dove (get_countries_cb,
-             hash);
+  parse_dove (get_countries_cb, NULL, hash);
 
   g_hash_table_foreach (hash,
                        put_areas_into_list,