messenger-gtk

Gtk+3 graphical user interfaces for GNUnet Messenger
Log | Files | Refs | Submodules | README | LICENSE

commit ccea1126148c68d34d671daab6f4af60d457ef7f
parent 40e11dc56e320eba25dec0036733d4409d37ff47
Author: Jacki <jacki@thejackimonster.de>
Date:   Tue, 23 Apr 2024 16:19:40 +0200

Adjust code to be more reliable

Signed-off-by: Jacki <jacki@thejackimonster.de>

Diffstat:
Msrc/application.c | 6+++---
Msrc/event.c | 24++----------------------
Msrc/request.c | 2+-
Msrc/ui/chat.c | 94++++++++++++++++++++++---------------------------------------------------------
Msrc/ui/chat.h | 9+++++----
Msrc/ui/chat_entry.c | 35+++++++++++++++++------------------
Msrc/ui/chat_entry.h | 11+++++++----
Msrc/util.c | 35++++++++++++++++++++++++++++++++++-
Msrc/util.h | 4++++
9 files changed, 99 insertions(+), 121 deletions(-)

diff --git a/src/application.c b/src/application.c @@ -639,7 +639,7 @@ application_call_event(MESSENGER_Application *app, call->app = app; call->event = event; - util_idle_add(G_SOURCE_FUNC(_application_event_call), call); + util_immediate_add(G_SOURCE_FUNC(_application_event_call), call); } static gboolean @@ -676,7 +676,7 @@ application_call_sync_event(MESSENGER_Application *app, call->event = event; util_scheduler_cleanup(); - util_idle_add(G_SOURCE_FUNC(_application_sync_event_call), call); + util_immediate_add(G_SOURCE_FUNC(_application_sync_event_call), call); // Locking the mutex for synchronization pthread_mutex_lock(&(app->chat.mutex)); @@ -729,7 +729,7 @@ application_call_message_event(MESSENGER_Application *app, call->context = context; call->message = message; - util_idle_add(G_SOURCE_FUNC(_application_message_event_call), call); + util_immediate_add(G_SOURCE_FUNC(_application_message_event_call), call); } void diff --git a/src/event.c b/src/event.c @@ -197,14 +197,7 @@ _idle_chat_entry_update(gpointer user_data) (!(entry->chat->send_text_view))) goto update_exit; - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata( - G_OBJECT(entry->chat->send_text_view), - entry->chat->app->quarks.data - ) - ); - - ui_chat_entry_update(entry, entry->chat->app, context); + ui_chat_entry_update(entry, entry->chat->app); update_exit: entry->update = 0; @@ -232,24 +225,17 @@ _add_new_chat_entry(MESSENGER_Application *app, g_assert((app) && (context)); UI_MESSENGER_Handle *ui = &(app->ui.messenger); - UI_CHAT_ENTRY_Handle *entry = ui_chat_entry_new(app); + UI_CHAT_ENTRY_Handle *entry = ui_chat_entry_new(app, context); enqueue_chat_entry_update(entry); gtk_container_add(GTK_CONTAINER(ui->chats_listbox), entry->entry_box); - GNUNET_CHAT_context_set_user_pointer(context, entry); gtk_container_add( GTK_CONTAINER(ui->chats_stack), entry->chat->chat_box ); - g_object_set_qdata( - G_OBJECT(entry->chat->send_text_view), - app->quarks.data, - context - ); - ui->chat_entries = g_list_append(ui->chat_entries, entry); GtkWidget *row = gtk_widget_get_parent(entry->entry_box); @@ -353,12 +339,6 @@ _clear_chat_entry(GtkWidget *widget, ui->chat_entries = g_list_remove(ui->chat_entries, entry); - g_object_set_qdata( - G_OBJECT(entry->chat->send_text_view), - app->quarks.data, - NULL - ); - ui_chat_entry_dispose(entry, app); } diff --git a/src/request.c b/src/request.c @@ -64,7 +64,7 @@ request_new(MESSENGER_Application *application, request->user_data = user_data; #ifdef MESSENGER_APPLICATION_NO_PORTAL - request->timeout = util_idle_add( + request->timeout = util_immediate_add( G_SOURCE_FUNC(_request_timeout_call), request ); diff --git a/src/ui/chat.c b/src/ui/chat.c @@ -251,25 +251,15 @@ handle_leave_chat_button_click(UNUSED GtkButton *button, UI_CHAT_Handle *handle = (UI_CHAT_Handle*) user_data; - if ((!handle) || (!(handle->send_text_view))) - return; - - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata( - G_OBJECT(handle->send_text_view), - handle->app->quarks.data - ) - ); - - if (!context) + if ((!handle) || (!(handle->context))) return; struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_context_get_contact( - context + handle->context ); struct GNUNET_CHAT_Group *group = GNUNET_CHAT_context_get_group( - context + handle->context ); if (contact) @@ -277,7 +267,9 @@ handle_leave_chat_button_click(UNUSED GtkButton *button, else if (group) GNUNET_CHAT_group_leave(group); - UI_CHAT_ENTRY_Handle *entry = GNUNET_CHAT_context_get_user_pointer(context); + UI_CHAT_ENTRY_Handle *entry = GNUNET_CHAT_context_get_user_pointer( + handle->context + ); if ((!entry) || (!(handle->app))) return; @@ -461,26 +453,12 @@ _new_tag_callback(MESSENGER_Application *app, { g_assert((app) && (user_data)); - GtkListBox *listbox = GTK_LIST_BOX(user_data); - GtkTextView *text_view = GTK_TEXT_VIEW(g_object_get_qdata( - G_OBJECT(app->ui.new_tag.dialog), - app->quarks.widget - )); + UI_CHAT_Handle *handle = (UI_CHAT_Handle*) user_data; + UI_MESSAGE_Handle *message; - if (!text_view) + if ((!(handle->context)) || (!tag)) goto unselect; - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata( - G_OBJECT(text_view), - app->quarks.data - ) - ); - - UI_MESSAGE_Handle *message; - if ((!context) || (!tag)) - goto cleanup; - while (selected) { GtkListBoxRow *row = GTK_LIST_BOX_ROW(selected->data); @@ -497,7 +475,7 @@ _new_tag_callback(MESSENGER_Application *app, goto skip_row; GNUNET_CHAT_context_send_tag( - context, + handle->context, message->msg, tag ); @@ -506,15 +484,8 @@ _new_tag_callback(MESSENGER_Application *app, selected = selected->next; } -cleanup: - g_object_set_qdata( - G_OBJECT(app->ui.new_tag.dialog), - app->quarks.widget, - NULL - ); - unselect: - gtk_list_box_unselect_all(listbox); + gtk_list_box_unselect_all(handle->messages_listbox); } static void @@ -531,17 +502,11 @@ handle_chat_selection_tag_button_click(UNUSED GtkButton *button, ui_new_tag_dialog_init(app, &(app->ui.new_tag)); - g_object_set_qdata( - G_OBJECT(app->ui.new_tag.dialog), - app->quarks.widget, - handle->send_text_view - ); - ui_new_tag_dialog_link( &(app->ui.new_tag), _new_tag_callback, selected, - handle->messages_listbox + handle ); gtk_widget_show(GTK_WIDGET(app->ui.new_tag.dialog)); @@ -737,12 +702,8 @@ _send_text_from_view(MESSENGER_Application *app, return FALSE; } - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata(G_OBJECT(text_view), app->quarks.data) - ); - - if (context) - GNUNET_CHAT_context_send_text(context, text); + if (handle->context) + GNUNET_CHAT_context_send_text(handle->context, text); g_free(text); gtk_text_buffer_delete(buffer, &start, &end); @@ -808,11 +769,7 @@ handle_send_record_button_click(GtkButton *button, g_object_get_qdata(G_OBJECT(button), app->quarks.ui) ); - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata(G_OBJECT(handle->send_text_view), app->quarks.data) - ); - - if ((handle->recorded) && (context) && + if ((handle->recorded) && (handle->context) && (handle->recording_filename[0]) && (!gtk_revealer_get_child_revealed(handle->picker_revealer))) { @@ -822,7 +779,7 @@ handle_send_record_button_click(GtkButton *button, gtk_progress_bar_set_fraction(file_load->load_progress_bar, 0.0); struct GNUNET_CHAT_File *file = GNUNET_CHAT_context_send_file( - context, + handle->context, handle->recording_filename, handle_sending_recording_upload_file, file_load @@ -1347,7 +1304,8 @@ _setup_gst_pipelines(UI_CHAT_Handle *handle) } UI_CHAT_Handle* -ui_chat_new(MESSENGER_Application *app) +ui_chat_new(MESSENGER_Application *app, + struct GNUNET_CHAT_Context *context) { g_assert(app); @@ -1359,6 +1317,7 @@ ui_chat_new(MESSENGER_Application *app) _setup_gst_pipelines(handle); handle->app = app; + handle->context = context; handle->loads = NULL; @@ -2133,16 +2092,15 @@ _chat_update_contact(UI_CHAT_Handle *handle, void ui_chat_update(UI_CHAT_Handle *handle, - MESSENGER_Application *app, - struct GNUNET_CHAT_Context* context) + MESSENGER_Application *app) { - g_assert((handle) && (app) && (context)); + g_assert((handle) && (app)); struct GNUNET_CHAT_Contact* contact; struct GNUNET_CHAT_Group* group; - contact = GNUNET_CHAT_context_get_contact(context); - group = GNUNET_CHAT_context_get_group(context); + contact = GNUNET_CHAT_context_get_contact(handle->context); + group = GNUNET_CHAT_context_get_group(handle->context); const char *icon = "action-unavailable-symbolic"; @@ -2183,8 +2141,8 @@ ui_chat_update(UI_CHAT_Handle *handle, g_string_free(subtitle, TRUE); _chat_update_contacts(handle, app, group); - _chat_update_files(handle, app, context); - _chat_update_media(handle, app, context); + _chat_update_files(handle, app, handle->context); + _chat_update_media(handle, app, handle->context); g_object_set_qdata( G_OBJECT(handle->reveal_identity_button), @@ -2218,7 +2176,7 @@ ui_chat_update(UI_CHAT_Handle *handle, (contact) || (group)? TRUE : FALSE ); - const int status = GNUNET_CHAT_context_get_status(context); + const int status = GNUNET_CHAT_context_get_status(handle->context); const gboolean activated = (GNUNET_OK == status? TRUE : FALSE); gtk_text_view_set_editable(handle->send_text_view, activated); diff --git a/src/ui/chat.h b/src/ui/chat.h @@ -65,6 +65,7 @@ typedef struct UI_CHAT_Handle guint play_watch; MESSENGER_Application *app; + struct GNUNET_CHAT_Context *context; gdouble edge_value; GList *loads; @@ -152,10 +153,12 @@ typedef struct UI_CHAT_Handle * application. * * @param app Messenger application + * @param context Chat context * @return New chat handle */ UI_CHAT_Handle* -ui_chat_new(MESSENGER_Application *app); +ui_chat_new(MESSENGER_Application *app, + struct GNUNET_CHAT_Context *context); /** * Updates a given chat handle with the current @@ -164,12 +167,10 @@ ui_chat_new(MESSENGER_Application *app); * * @param handle Chat handle * @param app Messenger application - * @param context Chat context */ void ui_chat_update(UI_CHAT_Handle *handle, - MESSENGER_Application *app, - struct GNUNET_CHAT_Context* context); + MESSENGER_Application *app); /** * Frees its resources and destroys a given diff --git a/src/ui/chat_entry.c b/src/ui/chat_entry.c @@ -34,17 +34,19 @@ #include <gnunet/gnunet_time_lib.h> UI_CHAT_ENTRY_Handle* -ui_chat_entry_new(MESSENGER_Application *app) +ui_chat_entry_new(MESSENGER_Application *app, + struct GNUNET_CHAT_Context *context) { - g_assert(app); + g_assert((app) && (context)); UI_CHAT_ENTRY_Handle* handle = g_malloc(sizeof(UI_CHAT_ENTRY_Handle)); memset(handle, 0, sizeof(*handle)); handle->timestamp = GNUNET_TIME_absolute_get_zero_(); + handle->context = context; - handle->chat = ui_chat_new(app); + handle->chat = ui_chat_new(app, handle->context); handle->builder = ui_builder_from_resource( application_get_resource_path(app, "ui/chat_entry.ui") ); @@ -73,6 +75,11 @@ ui_chat_entry_new(MESSENGER_Application *app) gtk_builder_get_object(handle->builder, "read_receipt_image") ); + GNUNET_CHAT_context_set_user_pointer( + handle->context, + handle + ); + return handle; } @@ -109,16 +116,15 @@ _chat_entry_update_contact(UI_CHAT_ENTRY_Handle *handle, void ui_chat_entry_update(UI_CHAT_ENTRY_Handle *handle, - MESSENGER_Application *app, - struct GNUNET_CHAT_Context *context) + MESSENGER_Application *app) { g_assert((handle) && (app)); struct GNUNET_CHAT_Contact* contact; struct GNUNET_CHAT_Group* group; - contact = GNUNET_CHAT_context_get_contact(context); - group = GNUNET_CHAT_context_get_group(context); + contact = GNUNET_CHAT_context_get_contact(handle->context); + group = GNUNET_CHAT_context_get_group(handle->context); const char *icon = "action-unavailable-symbolic"; @@ -144,7 +150,7 @@ ui_chat_entry_update(UI_CHAT_ENTRY_Handle *handle, if (!(handle->chat)) return; - ui_chat_update(handle->chat, app, context); + ui_chat_update(handle->chat, app); GList *rows = gtk_container_get_children( GTK_CONTAINER(handle->chat->messages_listbox) @@ -244,6 +250,9 @@ ui_chat_entry_delete(UI_CHAT_ENTRY_Handle *handle) if (handle->update) util_source_remove(handle->update); + if (handle->context) + GNUNET_CHAT_context_set_user_pointer(handle->context, NULL); + g_free(handle); } @@ -264,16 +273,6 @@ ui_chat_entry_dispose(UI_CHAT_ENTRY_Handle *handle, gtk_widget_get_parent(handle->entry_box) ); - struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( - g_object_get_qdata( - G_OBJECT(handle->chat->send_text_view), - app->quarks.data - ) - ); - - if (context) - GNUNET_CHAT_context_set_user_pointer(context, NULL); - _chat_entry_update_contact(handle, app, NULL); gtk_container_remove( diff --git a/src/ui/chat_entry.h b/src/ui/chat_entry.h @@ -27,11 +27,14 @@ #include "chat.h" +#include <gnunet/gnunet_chat_lib.h> + typedef struct UI_CHAT_ENTRY_Handle { guint update; struct GNUNET_TIME_Absolute timestamp; + struct GNUNET_CHAT_Context *context; UI_CHAT_Handle *chat; GtkBuilder *builder; @@ -53,10 +56,12 @@ typedef struct UI_CHAT_ENTRY_Handle * a given messenger application. * * @param app Messenger application + * @param context Chat context * @return New chat entry handle */ UI_CHAT_ENTRY_Handle* -ui_chat_entry_new(MESSENGER_Application *app); +ui_chat_entry_new(MESSENGER_Application *app, + struct GNUNET_CHAT_Context *context); /** * Updates a given chat entry handle with the @@ -66,12 +71,10 @@ ui_chat_entry_new(MESSENGER_Application *app); * * @param handle Chat entry handle * @param app Messenger application - * @param context Chat context */ void ui_chat_entry_update(UI_CHAT_ENTRY_Handle *handle, - MESSENGER_Application *app, - struct GNUNET_CHAT_Context *context); + MESSENGER_Application *app); /** * Frees its resources and destroys a given diff --git a/src/util.c b/src/util.c @@ -24,9 +24,11 @@ #include "util.h" +#include <pthread.h> #include <stdio.h> static GList *tasks = NULL; +static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; struct UTIL_CompleteTask { @@ -63,6 +65,11 @@ util_complete_task(gpointer task_data) g_assert(task_data); struct UTIL_CompleteTask *task = (struct UTIL_CompleteTask*) task_data; + gboolean result = FALSE; + + pthread_mutex_lock(&mutex); + if (!tasks) + goto unlock_mutex; const GSourceFunc function = task->function; gpointer data = task->data; @@ -72,7 +79,11 @@ util_complete_task(gpointer task_data) g_free(task); - return function(data); + result = function(data); + +unlock_mutex: + pthread_mutex_unlock(&mutex); + return result; } guint @@ -97,6 +108,28 @@ util_idle_add(GSourceFunc function, } guint +util_immediate_add(GSourceFunc function, + gpointer data) +{ + struct UTIL_CompleteTask *task = g_malloc(sizeof(struct UTIL_CompleteTask)); + + task->function = function; + task->data = data; + task->id = g_timeout_add( + 0, + G_SOURCE_FUNC(util_complete_task), + task + ); + + tasks = g_list_append( + tasks, + task + ); + + return task->id; +} + +guint util_timeout_add(guint interval, GSourceFunc function, gpointer data) diff --git a/src/util.h b/src/util.h @@ -50,6 +50,10 @@ guint util_idle_add(GSourceFunc function, gpointer data); +guint +util_immediate_add(GSourceFunc function, + gpointer data); + /** * Abstraction of `g_timeout_add()` task * to be cancelled externally.