diff options
author | Ted Gould <ted@gould.cx> | 2011-03-28 21:33:20 -0500 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2011-03-28 21:33:20 -0500 |
commit | 9f4c4935225b9d301528453553d372b02eb1b219 (patch) | |
tree | 1a18ae158e4fd3acd07da5fded4ab6729b2249c0 /src | |
parent | 1daab282087696f4e83dd0da5ffd794d94e3622a (diff) | |
parent | 7df6b79140cf5184e95169682534af60e947d6bd (diff) | |
download | ayatana-indicator-datetime-9f4c4935225b9d301528453553d372b02eb1b219.tar.gz ayatana-indicator-datetime-9f4c4935225b9d301528453553d372b02eb1b219.tar.bz2 ayatana-indicator-datetime-9f4c4935225b9d301528453553d372b02eb1b219.zip |
Clean up tree model usage and sort the items in the popup list
Diffstat (limited to 'src')
-rw-r--r-- | src/datetime-prefs-locations.c | 58 | ||||
-rw-r--r-- | src/timezone-completion.c | 57 |
2 files changed, 70 insertions, 45 deletions
diff --git a/src/datetime-prefs-locations.c b/src/datetime-prefs-locations.c index 0c9a93c..5e93a56 100644 --- a/src/datetime-prefs-locations.c +++ b/src/datetime-prefs-locations.c @@ -35,6 +35,10 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #define DATETIME_DIALOG_UI_FILE PKGDATADIR "/datetime-dialog.ui" +#define COL_NAME 0 +#define COL_TIME 1 +#define COL_ZONE 2 + static void handle_add (GtkWidget * button, GtkTreeView * tree) { @@ -84,7 +88,7 @@ handle_edit (GtkCellRendererText * renderer, gchar * path, gchar * new_text, GtkTreeIter iter; if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (store), &iter, path)) { - gtk_list_store_set (store, &iter, 0, new_text, -1); + gtk_list_store_set (store, &iter, COL_NAME, new_text, -1); } } @@ -92,28 +96,28 @@ static gboolean timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model, GtkTreeIter * iter, gpointer user_data) { - GValue zone_value = {0}, name_value = {0}; const gchar * zone, * name; - gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_ZONE, &zone_value); - zone = g_value_get_string (&zone_value); + gtk_tree_model_get (model, iter, + TIMEZONE_COMPLETION_ZONE, &zone, + TIMEZONE_COMPLETION_NAME, &name, + -1); - gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_NAME, &name_value); - name = g_value_get_string (&name_value); + g_debug("match selected: %s, %s", name, zone); if (zone == NULL || zone[0] == 0) { - GValue lon_value = {0}, lat_value = {0}; const gchar * strlon, * strlat; gdouble lon = 0.0, lat = 0.0; - gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LONGITUDE, &lon_value); - strlon = g_value_get_string (&lon_value); + gtk_tree_model_get (model, iter, + TIMEZONE_COMPLETION_LONGITUDE, &strlon, + TIMEZONE_COMPLETION_LATITUDE, &strlat, + -1); + if (strlon != NULL && strlon[0] != 0) { lon = strtod(strlon, NULL); } - gtk_tree_model_get_value (model, iter, TIMEZONE_COMPLETION_LATITUDE, &lat_value); - strlat = g_value_get_string (&lat_value); if (strlat != NULL && strlat[0] != 0) { lat = strtod(strlat, NULL); } @@ -125,12 +129,9 @@ timezone_selected (GtkEntryCompletion * widget, GtkTreeModel * model, GtkListStore * store = GTK_LIST_STORE (g_object_get_data (G_OBJECT (widget), "store")); GtkTreeIter * store_iter = (GtkTreeIter *)g_object_get_data (G_OBJECT (widget), "store_iter"); if (store != NULL && store_iter != NULL) { - gtk_list_store_set (store, store_iter, 0, name, 2, zone, -1); + gtk_list_store_set (store, store_iter, COL_NAME, name, COL_ZONE, zone, -1); } - g_value_unset (&name_value); - g_value_unset (&zone_value); - return FALSE; // Do normal action too } @@ -169,11 +170,9 @@ update_times (TimezoneCompletion * completion) GtkTreeIter iter; if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) { do { - GValue zone_value = {0}; const gchar * strzone; - gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 2, &zone_value); - strzone = g_value_get_string (&zone_value); + gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, COL_ZONE, &strzone, -1); if (strzone != NULL && strzone[0] != 0) { GTimeZone * tz = g_time_zone_new (strzone); @@ -181,15 +180,13 @@ update_times (TimezoneCompletion * completion) gchar * format = generate_format_string_at_time (now_tz); gchar * time_str = g_date_time_format (now_tz, format); - gtk_list_store_set (store, &iter, 1, time_str, -1); + gtk_list_store_set (store, &iter, COL_TIME, time_str, -1); g_free (time_str); g_free (format); g_date_time_unref (now_tz); g_time_zone_unref (tz); } - - g_value_unset (&zone_value); } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)); } @@ -211,7 +208,7 @@ fill_from_settings (GObject * store, GSettings * conf) split_settings_location (*striter, &zone, &name); gtk_list_store_append (GTK_LIST_STORE (store), &iter); - gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, name, 2, zone, -1); + gtk_list_store_set (GTK_LIST_STORE (store), &iter, COL_NAME, name, COL_ZONE, zone, -1); g_free (zone); g_free (name); @@ -234,23 +231,18 @@ dialog_closed (GtkWidget * dlg, GObject * store) GtkTreeIter iter; if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) { do { - GValue zone_value = {0}, name_value = {0}; const gchar * strzone, * strname; - gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 0, &name_value); - gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 2, &zone_value); - - strzone = g_value_get_string (&zone_value); - strname = g_value_get_string (&name_value); + gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, + COL_NAME, &strname, + COL_ZONE, &strzone, + -1); if (strzone != NULL && strzone[0] != 0 && strname != NULL && strname[0] != 0) { gchar * settings_string = g_strdup_printf("%s %s", strzone, strname); g_variant_builder_add (&builder, "s", settings_string); g_free (settings_string); } - - g_value_unset (&zone_value); - g_value_unset (&name_value); } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)); } @@ -304,7 +296,7 @@ datetime_setup_locations_dialog (GtkWindow * parent, CcTimezoneMap * map) g_signal_connect (cell, "edited", G_CALLBACK (handle_edit), store); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1, _("Location"), cell, - "text", 0, NULL); + "text", COL_NAME, NULL); GtkTreeViewColumn * loc_col = gtk_tree_view_get_column (GTK_TREE_VIEW (tree), 0); gtk_tree_view_column_set_expand (loc_col, TRUE); g_object_set_data (G_OBJECT (completion), "name-cell", cell); @@ -312,7 +304,7 @@ datetime_setup_locations_dialog (GtkWindow * parent, CcTimezoneMap * map) cell = gtk_cell_renderer_text_new (); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree), -1, _("Time"), cell, - "text", 1, NULL); + "text", COL_TIME, NULL); GtkTreeSelection * selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree)); gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE); diff --git a/src/timezone-completion.c b/src/timezone-completion.c index aaf6bdc..a2fa643 100644 --- a/src/timezone-completion.c +++ b/src/timezone-completion.c @@ -87,6 +87,39 @@ save_and_use_model (TimezoneCompletion * completion, GtkTreeModel * model) g_signal_emit_by_name (priv->entry, "changed"); } +static gint +sort_zone (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, + gpointer user_data) +{ + /* Anything that has text as a prefix goes first, in mostly sorted order. + Then everything else goes after, in mostly sorted order. */ + const gchar *casefolded_text = (const gchar *)user_data; + + const gchar *namea = NULL, *nameb = NULL; + gtk_tree_model_get (model, a, TIMEZONE_COMPLETION_NAME, &namea, -1); + gtk_tree_model_get (model, b, TIMEZONE_COMPLETION_NAME, &nameb, -1); + + gchar *casefolded_namea = NULL, *casefolded_nameb = NULL; + casefolded_namea = g_utf8_casefold (namea, -1); + casefolded_nameb = g_utf8_casefold (nameb, -1); + + gboolean amatches = FALSE, bmatches = FALSE; + amatches = strncmp (casefolded_text, casefolded_namea, strlen(casefolded_text)) == 0; + bmatches = strncmp (casefolded_text, casefolded_nameb, strlen(casefolded_text)) == 0; + + gint rv; + if (amatches && !bmatches) + rv = -1; + else if (bmatches && !amatches) + rv = 1; + else + rv = g_utf8_collate (casefolded_namea, casefolded_nameb); + + g_free (casefolded_namea); + g_free (casefolded_nameb); + return rv; +} + static void json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data) { @@ -179,6 +212,13 @@ json_parse_ready (GObject *object, GAsyncResult *res, gpointer user_data) TIMEZONE_COMPLETION_LONGITUDE, longitude, TIMEZONE_COMPLETION_LATITUDE, latitude, -1); + gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (store), + TIMEZONE_COMPLETION_NAME, sort_zone, + g_utf8_casefold(priv->request_text, -1), + g_free); + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), + TIMEZONE_COMPLETION_NAME, + GTK_SORT_ASCENDING); } prev_name = name; @@ -340,16 +380,13 @@ static void data_func (GtkCellLayout *cell_layout, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer user_data) { - GValue name_val = {0}, admin1_val = {0}, country_val = {0}; const gchar * name, * admin1, * country; - gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_NAME, &name_val); - gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_ADMIN1, &admin1_val); - gtk_tree_model_get_value (GTK_TREE_MODEL (tree_model), iter, TIMEZONE_COMPLETION_COUNTRY, &country_val); - - name = g_value_get_string (&name_val); - admin1 = g_value_get_string (&admin1_val); - country = g_value_get_string (&country_val); + gtk_tree_model_get (GTK_TREE_MODEL (tree_model), iter, + TIMEZONE_COMPLETION_NAME, &name, + TIMEZONE_COMPLETION_ADMIN1, &admin1, + TIMEZONE_COMPLETION_COUNTRY, &country, + -1); gchar * user_name; if (admin1 == NULL || admin1[0] == 0) { @@ -359,10 +396,6 @@ data_func (GtkCellLayout *cell_layout, GtkCellRenderer *cell, } g_object_set (G_OBJECT (cell), "markup", user_name, NULL); - - g_value_unset (&name_val); - g_value_unset (&admin1_val); - g_value_unset (&country_val); } static void |