From 58519b5b8db0d0f16973d2298f02afc069e889c4 Mon Sep 17 00:00:00 2001 From: Zwarf Date: Mon, 21 Mar 2022 21:37:19 +0100 Subject: [PATCH] Improve search functionality --- src/window/picplanner-window.c | 68 ++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/src/window/picplanner-window.c b/src/window/picplanner-window.c index 74b6dc7..cc214a9 100644 --- a/src/window/picplanner-window.c +++ b/src/window/picplanner-window.c @@ -65,6 +65,51 @@ struct _PicplannerWindow G_DEFINE_TYPE (PicplannerWindow, picplanner_window, ADW_TYPE_APPLICATION_WINDOW) +static gboolean +find_loc_children (GWeatherLocation *location, + const char *search_str, + GWeatherLocation **locations, + int *iteration) +{ + GWeatherLocation *child = NULL; + while ((child = gweather_location_next_child (location, child)) != NULL) + { + if (gweather_location_get_level (child) == GWEATHER_LOCATION_CITY && + *iteration < 10) + { + const char *city = gweather_location_get_name (child); + const char *city_norm = g_utf8_normalize (city, strlen (city), G_NORMALIZE_ALL_COMPOSE); + const char *city_string = g_utf8_casefold (city_norm, strlen (city_norm)); + if (strstr (city_string, search_str)) + { + locations[*iteration] = g_object_ref(child); + (*iteration)++; + //g_print ("Search: %s, City to append: %s\n",search_str, city_string); + } + } + else + { + if (find_loc_children (child, search_str, locations, iteration)) + return TRUE; + } + } + + return FALSE; +} + +static void +find_loc (GWeatherLocation *world, + const char *search_str, + GWeatherLocation **locations) +{ + int *iteration; + int iter = 0; + iteration = &iter; + + find_loc_children (world, search_str, locations, iteration); +} + + static void search_location (GtkWidget *self, gpointer user_data) @@ -73,12 +118,29 @@ search_location (GtkWidget *self, PicplannerWindow *window = user_data; (void) window; - const char *search_string = gtk_editable_get_text (GTK_EDITABLE (window->search_entry)); - const char *search_string_casefold = g_utf8_casefold (search_string, strlen (search_string)); - g_print ("Search string: %s\n", search_string_casefold); + const char *search_text = gtk_editable_get_text (GTK_EDITABLE (window->search_entry)); + const char *search_norm = g_utf8_normalize (search_text, strlen (search_text), G_NORMALIZE_ALL_COMPOSE); + const char *search_string = g_utf8_casefold (search_text, strlen (search_norm)); + g_print ("Search string: %s\n", search_string); GWeatherLocation *world = gweather_location_get_world (); + GWeatherLocation *locations[10]; + + for (int i=0; i<10; i++) + locations[i] = NULL; + + if (!strstr ("", search_string)) + { + find_loc (world, search_string, locations); + } + + for (int i=0; i<10; i++) + { + if (locations[i]) + g_print ("Found locations: %s\n", gweather_location_get_name (locations[i])); + } + } static void