From ca0ced533c3b234b1347ac4998eddbe4bf4a699c Mon Sep 17 00:00:00 2001 From: Paolo Stivanin Date: Fri, 28 Jun 2024 08:48:44 +0200 Subject: [PATCH] Fixes for ctx menu handling --- src/gui/edit-row-cb.c | 7 +-- src/gui/edit-row-cb.h | 9 ++- src/gui/show-qr-cb.c | 7 +-- src/gui/show-qr-cb.h | 9 ++- src/gui/treeview.c | 140 +++++++++++++++++++++--------------------- src/gui/treeview.h | 5 -- 6 files changed, 84 insertions(+), 93 deletions(-) diff --git a/src/gui/edit-row-cb.c b/src/gui/edit-row-cb.c index 023bf0b0..73d7e588 100644 --- a/src/gui/edit-row-cb.c +++ b/src/gui/edit-row-cb.c @@ -37,9 +37,8 @@ static void set_data_in_lstore_and_json (EditData *edit_data); void -edit_row_cb (GSimpleAction *simple UNUSED, - GVariant *parameter UNUSED, - gpointer user_data) +edit_row_cb (GtkMenuItem *menu_item UNUSED, + gpointer user_data) { EditData *edit_data = g_new0 (EditData, 1); CAST_USER_DATA(AppData, app_data, user_data); @@ -84,7 +83,7 @@ void edit_row_cb_shortcut (GtkWidget *w UNUSED, gpointer user_data) { - edit_row_cb (NULL, NULL, user_data); + edit_row_cb (NULL, user_data); } diff --git a/src/gui/edit-row-cb.h b/src/gui/edit-row-cb.h index b9aa5ee5..32addc9b 100644 --- a/src/gui/edit-row-cb.h +++ b/src/gui/edit-row-cb.h @@ -4,11 +4,10 @@ G_BEGIN_DECLS -void edit_row_cb (GSimpleAction *simple, - GVariant *parameter, - gpointer user_data); +void edit_row_cb (GtkMenuItem *menu_item, + gpointer user_data); -void edit_row_cb_shortcut (GtkWidget *w, - gpointer user_data); +void edit_row_cb_shortcut (GtkWidget *w, + gpointer user_data); G_END_DECLS \ No newline at end of file diff --git a/src/gui/show-qr-cb.c b/src/gui/show-qr-cb.c index f4e13bd6..a721af36 100644 --- a/src/gui/show-qr-cb.c +++ b/src/gui/show-qr-cb.c @@ -20,9 +20,8 @@ static int write_png (const QRcode *qrcode); void -show_qr_cb (GSimpleAction *simple UNUSED, - GVariant *parameter UNUSED, - gpointer user_data) +show_qr_cb (GtkMenuItem *menu_item UNUSED, + gpointer user_data) { CAST_USER_DATA(AppData, app_data, user_data); @@ -71,7 +70,7 @@ void show_qr_cb_shortcut (GtkWidget *w UNUSED, gpointer user_data) { - show_qr_cb (NULL, NULL, user_data); + show_qr_cb (NULL, user_data); } diff --git a/src/gui/show-qr-cb.h b/src/gui/show-qr-cb.h index 449c4297..125b411d 100644 --- a/src/gui/show-qr-cb.h +++ b/src/gui/show-qr-cb.h @@ -4,11 +4,10 @@ G_BEGIN_DECLS -void show_qr_cb (GSimpleAction *simple, - GVariant *parameter, - gpointer user_data); +void show_qr_cb (GtkMenuItem *menu_item, + gpointer user_data); -void show_qr_cb_shortcut (GtkWidget *w, - gpointer user_data); +void show_qr_cb_shortcut (GtkWidget *w, + gpointer user_data); G_END_DECLS diff --git a/src/gui/treeview.c b/src/gui/treeview.c index a07d0c38..09a0b29a 100644 --- a/src/gui/treeview.c +++ b/src/gui/treeview.c @@ -23,6 +23,8 @@ static void add_data_to_model (DatabaseData *db_data, static void add_columns (GtkTreeView *tree_view); +static void delete_row (AppData *app_data); + static void hide_all_otps_cb (GtkTreeView *tree_view, gpointer user_data); @@ -81,73 +83,6 @@ update_model (AppData *app_data) } -void -delete_row (AppData *app_data) -{ - g_return_if_fail (app_data->tree_view != NULL); - - GtkTreeModel *model = gtk_tree_view_get_model (app_data->tree_view); - GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(app_data->tree_view)); - GtkTreeIter iter; - if (!gtk_tree_selection_get_selected (selection, &model, &iter)) { - // selection is empty, we don't have the iter - return; - } - - gboolean delete_entry = FALSE; - GtkWidget *del_diag = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "del_diag_id")); - gtk_window_set_transient_for (GTK_WINDOW(del_diag), GTK_WINDOW(app_data->main_window)); - gint res = gtk_dialog_run (GTK_DIALOG(del_diag)); - switch (res) { - case GTK_RESPONSE_YES: - delete_entry = TRUE; - break; - case GTK_RESPONSE_NO: - default: - delete_entry = FALSE; - break; - } - gtk_widget_hide (del_diag); - - if (delete_entry == FALSE) { - return; - } - - gint db_item_position_to_delete; - gtk_tree_model_get (model, &iter, COLUMN_POSITION_IN_DB, &db_item_position_to_delete, -1); - - json_array_remove (app_data->db_data->json_data, db_item_position_to_delete); - gtk_list_store_remove (GTK_LIST_STORE(model), &iter); - - // json_array_remove shifts all items, so we have to take care of updating the real item's position in the database - gint row_db_pos; - gboolean valid = gtk_tree_model_get_iter_first(model, &iter); - while (valid) { - gtk_tree_model_get (model, &iter, COLUMN_POSITION_IN_DB, &row_db_pos, -1); - if (row_db_pos > db_item_position_to_delete) { - gint shifted_position = row_db_pos - 1; - gtk_list_store_set (GTK_LIST_STORE(model), &iter, COLUMN_POSITION_IN_DB, shifted_position, -1); - } - valid = gtk_tree_model_iter_next(model, &iter); - } - - GError *err = NULL; - update_db (app_data->db_data, &err); - if (err != NULL) { - gchar *msg = g_strconcat ("The database update FAILED. The error message is:\n", err->message, NULL); - show_message_dialog (app_data->main_window, msg, GTK_MESSAGE_ERROR); - g_free (msg); - } else { - reload_db (app_data->db_data, &err); - if (err != NULL) { - gchar *msg = g_strconcat ("The database update FAILED. The error message is:\n", err->message, NULL); - show_message_dialog (app_data->main_window, msg, GTK_MESSAGE_ERROR); - g_free (msg); - } - } -} - - void row_selected_cb (GtkTreeView *tree_view, GtkTreePath *path, @@ -275,16 +210,81 @@ regenerate_model (AppData *app_data) } +static void +delete_row (AppData *app_data) +{ + g_return_if_fail (app_data->tree_view != NULL); + + GtkTreeModel *model = gtk_tree_view_get_model (app_data->tree_view); + GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(app_data->tree_view)); + GtkTreeIter iter; + if (!gtk_tree_selection_get_selected (selection, &model, &iter)) { + show_message_dialog (app_data->main_window, "No row has been selected. Nothing will be deleted.", GTK_MESSAGE_ERROR); + return; + } + + gboolean delete_entry = FALSE; + GtkWidget *del_diag = GTK_WIDGET(gtk_builder_get_object (app_data->builder, "del_diag_id")); + gtk_window_set_transient_for (GTK_WINDOW(del_diag), GTK_WINDOW(app_data->main_window)); + gint res = gtk_dialog_run (GTK_DIALOG(del_diag)); + switch (res) { + case GTK_RESPONSE_YES: + delete_entry = TRUE; + break; + case GTK_RESPONSE_NO: + default: + delete_entry = FALSE; + break; + } + gtk_widget_hide (del_diag); + + if (delete_entry == FALSE) { + return; + } + + gint db_item_position_to_delete; + gtk_tree_model_get (model, &iter, COLUMN_POSITION_IN_DB, &db_item_position_to_delete, -1); + + json_array_remove (app_data->db_data->json_data, db_item_position_to_delete); + gtk_list_store_remove (GTK_LIST_STORE(model), &iter); + + // json_array_remove shifts all items, so we have to take care of updating the real item's position in the database + gint row_db_pos; + gboolean valid = gtk_tree_model_get_iter_first(model, &iter); + while (valid) { + gtk_tree_model_get (model, &iter, COLUMN_POSITION_IN_DB, &row_db_pos, -1); + if (row_db_pos > db_item_position_to_delete) { + gint shifted_position = row_db_pos - 1; + gtk_list_store_set (GTK_LIST_STORE(model), &iter, COLUMN_POSITION_IN_DB, shifted_position, -1); + } + valid = gtk_tree_model_iter_next(model, &iter); + } + + GError *err = NULL; + update_db (app_data->db_data, &err); + if (err != NULL) { + gchar *msg = g_strconcat ("The database update FAILED. The error message is:\n", err->message, NULL); + show_message_dialog (app_data->main_window, msg, GTK_MESSAGE_ERROR); + g_free (msg); + } else { + reload_db (app_data->db_data, &err); + if (err != NULL) { + gchar *msg = g_strconcat ("The database update FAILED. The error message is:\n", err->message, NULL); + show_message_dialog (app_data->main_window, msg, GTK_MESSAGE_ERROR); + g_free (msg); + } + } +} + + static void on_delete_activate (GtkMenuItem *menuitem UNUSED, gpointer user_data) { CAST_USER_DATA(AppData, app_data, user_data); - GtkTreeSelection *tree_selection = gtk_tree_view_get_selection (app_data->tree_view); g_signal_handlers_disconnect_by_func (app_data->tree_view, row_selected_cb, app_data); - // the following function emits the "changed" signal - gtk_tree_selection_unselect_all (tree_selection); + // clear all active otps before proceeding to the deletion phase g_signal_emit_by_name (app_data->tree_view, "hide-all-otps"); diff --git a/src/gui/treeview.h b/src/gui/treeview.h index 61957563..133cf1c9 100644 --- a/src/gui/treeview.h +++ b/src/gui/treeview.h @@ -21,11 +21,6 @@ void create_treeview (AppData *app_data); void update_model (AppData *app_data); -void delete_rows_cb (GtkTreeView *tree_view, - GtkTreePath *path, - GtkTreeViewColumn *column, - gpointer user_data); - void row_selected_cb (GtkTreeView *tree_view, GtkTreePath *path, GtkTreeViewColumn *column,