diff options
-rw-r--r-- | src/datetime-prefs-locations.c | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/src/datetime-prefs-locations.c b/src/datetime-prefs-locations.c index 24efd04..90b6f97 100644 --- a/src/datetime-prefs-locations.c +++ b/src/datetime-prefs-locations.c @@ -33,13 +33,56 @@ with this program. If not, see <http://www.gnu.org/licenses/>. #define DATETIME_DIALOG_UI_FILE PKGDATADIR "/datetime-dialog.ui" static void -handle_add (GtkWidget * button, gpointer user_data) +handle_add (GtkWidget * button, GtkTreeView * tree) { + GtkListStore * store = GTK_LIST_STORE (gtk_tree_view_get_model (tree)); + + GtkTreeIter iter; + gtk_list_store_append (store, &iter); + + GtkTreePath * path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter); + gtk_tree_view_set_cursor (tree, path, gtk_tree_view_get_column (tree, 0), TRUE); + gtk_tree_path_free (path); } static void -handle_remove (GtkWidget * button, gpointer user_data) +handle_remove (GtkWidget * button, GtkTreeView * tree) { + GtkListStore * store = GTK_LIST_STORE (gtk_tree_view_get_model (tree)); + GtkTreeSelection * selection = gtk_tree_view_get_selection (tree); + + GList * paths = gtk_tree_selection_get_selected_rows (selection, NULL); + + /* Convert all paths to iters so we can safely delete multiple paths. For a + GtkListStore, iters persist past model changes. */ + GList * tree_iters = NULL; + GList * iter; + for (iter = paths; iter; iter = iter->next) { + GtkTreeIter * tree_iter = g_new(GtkTreeIter, 1); + if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), tree_iter, (GtkTreePath *)iter->data)) { + tree_iters = g_list_prepend (tree_iters, tree_iter); + } + gtk_tree_path_free (iter->data); + } + g_list_free (paths); + + /* Now delete each iterator */ + for (iter = tree_iters; iter; iter = iter->next) { + gtk_list_store_remove (store, (GtkTreeIter *)iter->data); + g_free (iter->data); + } + g_list_free (tree_iters); +} + +static void +handle_edit (GtkCellRendererText * renderer, gchar * path, gchar * new_text, + GtkListStore * store) +{ + 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); + } } static void @@ -69,8 +112,12 @@ save_to_settings (GtkWidget * dlg, GObject * store) if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter)) { do { GValue value = {0}; + const gchar * strval; gtk_tree_model_get_value (GTK_TREE_MODEL (store), &iter, 0, &value); - g_variant_builder_add (&builder, "s", g_value_get_string (&value)); + strval = g_value_get_string (&value); + if (strval != NULL && strval[0] != 0) { + g_variant_builder_add (&builder, "s", strval); + } g_value_unset (&value); } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)); } @@ -109,6 +156,7 @@ datetime_setup_locations_dialog (GtkWindow * parent) /* Configure tree */ GtkCellRenderer * cell = gtk_cell_renderer_text_new (); g_object_set (cell, "editable", TRUE, NULL); + 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); @@ -118,8 +166,8 @@ datetime_setup_locations_dialog (GtkWindow * parent) "text", 1, NULL); gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree)), GTK_SELECTION_MULTIPLE); - g_signal_connect (WIG ("addButton"), "clicked", G_CALLBACK (handle_add), NULL); - g_signal_connect (WIG ("removeButton"), "clicked", G_CALLBACK (handle_remove), NULL); + g_signal_connect (WIG ("addButton"), "clicked", G_CALLBACK (handle_add), tree); + g_signal_connect (WIG ("removeButton"), "clicked", G_CALLBACK (handle_remove), tree); fill_from_settings (store, conf); g_object_set_data_full (G_OBJECT (dlg), "conf", g_object_ref (conf), g_object_unref); |