diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-15 01:35:37 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-15 01:35:37 +0100 |
commit | 5e1a9a66817f14e2d39962b3ebcee7752925aa95 (patch) | |
tree | 0c0d5ae48f648ce28edbc25e8dc958beefe85ed1 | |
parent | 87d68ebf5ba44793de328fde54a6d830f6b6157b (diff) | |
download | messenger-gtk-5e1a9a66817f14e2d39962b3ebcee7752925aa95.tar.gz messenger-gtk-5e1a9a66817f14e2d39962b3ebcee7752925aa95.zip |
Reduce redundant callbacks
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | src/application.c | 5 | ||||
-rw-r--r-- | src/event.c | 119 | ||||
-rw-r--r-- | src/ui/accounts.c | 2 | ||||
-rw-r--r-- | src/ui/chat_entry.c | 5 | ||||
-rw-r--r-- | src/ui/chat_entry.h | 2 | ||||
-rw-r--r-- | src/ui/messenger.c | 38 | ||||
-rw-r--r-- | src/ui/messenger.h | 4 |
7 files changed, 134 insertions, 41 deletions
diff --git a/src/application.c b/src/application.c index 638e6f5..335db11 100644 --- a/src/application.c +++ b/src/application.c | |||
@@ -239,7 +239,10 @@ _application_event_call(gpointer user_data) | |||
239 | MESSENGER_ApplicationEventCall *call; | 239 | MESSENGER_ApplicationEventCall *call; |
240 | 240 | ||
241 | call = (MESSENGER_ApplicationEventCall*) user_data; | 241 | call = (MESSENGER_ApplicationEventCall*) user_data; |
242 | |||
243 | pthread_mutex_lock(&(call->app->chat.mutex)); | ||
242 | call->event(call->app); | 244 | call->event(call->app); |
245 | pthread_mutex_unlock(&(call->app->chat.mutex)); | ||
243 | 246 | ||
244 | GNUNET_free(call); | 247 | GNUNET_free(call); |
245 | return FALSE; | 248 | return FALSE; |
@@ -258,7 +261,7 @@ application_call_event(MESSENGER_Application *app, | |||
258 | call->app = app; | 261 | call->app = app; |
259 | call->event = event; | 262 | call->event = event; |
260 | 263 | ||
261 | g_idle_add(G_SOURCE_FUNC(_application_event_call), call); | 264 | g_timeout_add(0, G_SOURCE_FUNC(_application_event_call), call); |
262 | } | 265 | } |
263 | 266 | ||
264 | typedef struct MESSENGER_ApplicationMessageEventCall | 267 | typedef struct MESSENGER_ApplicationMessageEventCall |
diff --git a/src/event.c b/src/event.c index 2094bdd..5330e5b 100644 --- a/src/event.c +++ b/src/event.c | |||
@@ -103,11 +103,100 @@ event_handle_warning(MESSENGER_Application *app, | |||
103 | ); | 103 | ); |
104 | } | 104 | } |
105 | 105 | ||
106 | static gboolean | ||
107 | _idle_refresh_accounts(gpointer user_data) | ||
108 | { | ||
109 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
110 | |||
111 | if (!(app->ui.messenger.main_window)) | ||
112 | goto refresh_exit; | ||
113 | |||
114 | if (gtk_widget_is_visible(GTK_WIDGET(app->ui.messenger.main_window))) | ||
115 | ui_messenger_refresh(app, &(app->ui.messenger)); | ||
116 | else | ||
117 | ui_accounts_dialog_refresh(app, &(app->ui.accounts)); | ||
118 | |||
119 | refresh_exit: | ||
120 | app->ui.messenger.account_refresh = 0; | ||
121 | return FALSE; | ||
122 | } | ||
123 | |||
106 | void | 124 | void |
107 | event_refresh_accounts(MESSENGER_Application *app) | 125 | event_refresh_accounts(MESSENGER_Application *app) |
108 | { | 126 | { |
109 | ui_accounts_dialog_refresh(app, &(app->ui.accounts)); | 127 | if (app->ui.messenger.account_refresh) |
110 | ui_messenger_refresh(app, &(app->ui.messenger)); | 128 | g_source_remove(app->ui.messenger.account_refresh); |
129 | |||
130 | if (app->ui.messenger.main_window) | ||
131 | app->ui.messenger.account_refresh = g_idle_add( | ||
132 | G_SOURCE_FUNC(_idle_refresh_accounts), | ||
133 | app | ||
134 | ); | ||
135 | else | ||
136 | app->ui.messenger.account_refresh = 0; | ||
137 | } | ||
138 | |||
139 | static gboolean | ||
140 | _select_chat_to_activate(gpointer user_data) | ||
141 | { | ||
142 | UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) user_data; | ||
143 | |||
144 | if (!(entry->chat)) | ||
145 | return FALSE; | ||
146 | |||
147 | MESSENGER_Application *app = entry->chat->app; | ||
148 | |||
149 | if (!app) | ||
150 | return FALSE; | ||
151 | |||
152 | UI_MESSENGER_Handle *ui = &(app->ui.messenger); | ||
153 | |||
154 | GtkListBoxRow *row = GTK_LIST_BOX_ROW( | ||
155 | gtk_widget_get_parent(entry->entry_box) | ||
156 | ); | ||
157 | |||
158 | gtk_list_box_select_row(ui->chats_listbox, row); | ||
159 | gtk_list_box_invalidate_filter(ui->chats_listbox); | ||
160 | |||
161 | gtk_widget_activate(GTK_WIDGET(row)); | ||
162 | |||
163 | ui->chat_selection = 0; | ||
164 | return FALSE; | ||
165 | } | ||
166 | |||
167 | static gboolean | ||
168 | _idle_chat_entry_update(gpointer user_data) | ||
169 | { | ||
170 | UI_CHAT_ENTRY_Handle *entry = (UI_CHAT_ENTRY_Handle*) user_data; | ||
171 | |||
172 | if ((!(entry->chat)) || (!(entry->chat->app)) || | ||
173 | (!(entry->chat->send_text_view))) | ||
174 | goto update_exit; | ||
175 | |||
176 | struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) ( | ||
177 | g_object_get_qdata( | ||
178 | G_OBJECT(entry->chat->send_text_view), | ||
179 | entry->chat->app->quarks.data | ||
180 | ) | ||
181 | ); | ||
182 | |||
183 | ui_chat_entry_update(entry, entry->chat->app, context); | ||
184 | |||
185 | update_exit: | ||
186 | entry->update = 0; | ||
187 | return FALSE; | ||
188 | } | ||
189 | |||
190 | static void | ||
191 | enqueue_chat_entry_update(UI_CHAT_ENTRY_Handle *entry) | ||
192 | { | ||
193 | if (entry->update) | ||
194 | g_source_remove(entry->update); | ||
195 | |||
196 | entry->update = g_idle_add( | ||
197 | G_SOURCE_FUNC(_idle_chat_entry_update), | ||
198 | entry | ||
199 | ); | ||
111 | } | 200 | } |
112 | 201 | ||
113 | static void | 202 | static void |
@@ -117,7 +206,7 @@ _add_new_chat_entry(MESSENGER_Application *app, | |||
117 | UI_MESSENGER_Handle *ui = &(app->ui.messenger); | 206 | UI_MESSENGER_Handle *ui = &(app->ui.messenger); |
118 | UI_CHAT_ENTRY_Handle *entry = ui_chat_entry_new(app); | 207 | UI_CHAT_ENTRY_Handle *entry = ui_chat_entry_new(app); |
119 | 208 | ||
120 | ui_chat_entry_update(entry, app, context); | 209 | enqueue_chat_entry_update(entry); |
121 | 210 | ||
122 | gtk_container_add(GTK_CONTAINER(ui->chats_listbox), entry->entry_box); | 211 | gtk_container_add(GTK_CONTAINER(ui->chats_listbox), entry->entry_box); |
123 | GNUNET_CHAT_context_set_user_pointer(context, entry); | 212 | GNUNET_CHAT_context_set_user_pointer(context, entry); |
@@ -145,10 +234,13 @@ _add_new_chat_entry(MESSENGER_Application *app, | |||
145 | entry | 234 | entry |
146 | ); | 235 | ); |
147 | 236 | ||
148 | gtk_list_box_select_row(ui->chats_listbox, row); | 237 | if (ui->chat_selection) |
149 | gtk_list_box_invalidate_filter(ui->chats_listbox); | 238 | g_source_remove(ui->chat_selection); |
150 | 239 | ||
151 | gtk_widget_activate(GTK_WIDGET(row)); | 240 | ui->chat_selection = g_idle_add( |
241 | G_SOURCE_FUNC(_select_chat_to_activate), | ||
242 | entry | ||
243 | ); | ||
152 | } | 244 | } |
153 | 245 | ||
154 | static int | 246 | static int |
@@ -275,7 +367,7 @@ event_update_chats(MESSENGER_Application *app, | |||
275 | if (!handle) | 367 | if (!handle) |
276 | _add_new_chat_entry(app, context); | 368 | _add_new_chat_entry(app, context); |
277 | else | 369 | else |
278 | ui_chat_entry_update(handle, app, context); | 370 | enqueue_chat_entry_update(handle); |
279 | else if (handle) | 371 | else if (handle) |
280 | _clear_chat_entry(gtk_widget_get_parent(handle->entry_box), app); | 372 | _clear_chat_entry(gtk_widget_get_parent(handle->entry_box), app); |
281 | 373 | ||
@@ -298,7 +390,7 @@ _update_contact_context(MESSENGER_Application *app, | |||
298 | if (!handle) | 390 | if (!handle) |
299 | return; | 391 | return; |
300 | 392 | ||
301 | ui_chat_entry_update(handle, app, context); | 393 | enqueue_chat_entry_update(handle); |
302 | } | 394 | } |
303 | 395 | ||
304 | void | 396 | void |
@@ -365,7 +457,7 @@ event_presence_contact(MESSENGER_Application *app, | |||
365 | 457 | ||
366 | GNUNET_CHAT_member_set_user_pointer(context, contact, message); | 458 | GNUNET_CHAT_member_set_user_pointer(context, contact, message); |
367 | 459 | ||
368 | ui_chat_entry_update(handle, app, context); | 460 | enqueue_chat_entry_update(handle); |
369 | } | 461 | } |
370 | 462 | ||
371 | void | 463 | void |
@@ -388,7 +480,7 @@ event_update_contacts(MESSENGER_Application *app, | |||
388 | if (!handle) | 480 | if (!handle) |
389 | return; | 481 | return; |
390 | 482 | ||
391 | ui_chat_entry_update(handle, app, context); | 483 | enqueue_chat_entry_update(handle); |
392 | } | 484 | } |
393 | 485 | ||
394 | static void | 486 | static void |
@@ -453,7 +545,8 @@ event_invitation(MESSENGER_Application *app, | |||
453 | gtk_widget_show(GTK_WIDGET(message->accept_button)); | 545 | gtk_widget_show(GTK_WIDGET(message->accept_button)); |
454 | 546 | ||
455 | ui_chat_add_message(handle->chat, app, message); | 547 | ui_chat_add_message(handle->chat, app, message); |
456 | ui_chat_entry_update(handle, app, context); | 548 | |
549 | enqueue_chat_entry_update(handle); | ||
457 | } | 550 | } |
458 | 551 | ||
459 | void | 552 | void |
@@ -518,7 +611,7 @@ event_receive_message(MESSENGER_Application *app, | |||
518 | ui_chat_add_message(handle->chat, app, message); | 611 | ui_chat_add_message(handle->chat, app, message); |
519 | 612 | ||
520 | skip_message: | 613 | skip_message: |
521 | ui_chat_entry_update(handle, app, context); | 614 | enqueue_chat_entry_update(handle); |
522 | } | 615 | } |
523 | 616 | ||
524 | void | 617 | void |
@@ -546,5 +639,5 @@ event_delete_message(MESSENGER_Application *app, | |||
546 | messages = messages->next; | 639 | messages = messages->next; |
547 | } | 640 | } |
548 | 641 | ||
549 | ui_chat_entry_update(handle, app, context); | 642 | enqueue_chat_entry_update(handle); |
550 | } | 643 | } |
diff --git a/src/ui/accounts.c b/src/ui/accounts.c index 1a95b7b..16cd621 100644 --- a/src/ui/accounts.c +++ b/src/ui/accounts.c | |||
@@ -51,6 +51,8 @@ _show_messenger_main_window(gpointer user_data) | |||
51 | { | 51 | { |
52 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | 52 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; |
53 | 53 | ||
54 | ui_messenger_refresh(app, &(app->ui.messenger)); | ||
55 | |||
54 | gtk_widget_show(GTK_WIDGET(app->ui.messenger.main_window)); | 56 | gtk_widget_show(GTK_WIDGET(app->ui.messenger.main_window)); |
55 | return FALSE; | 57 | return FALSE; |
56 | } | 58 | } |
diff --git a/src/ui/chat_entry.c b/src/ui/chat_entry.c index 3c9461e..f538978 100644 --- a/src/ui/chat_entry.c +++ b/src/ui/chat_entry.c | |||
@@ -34,6 +34,8 @@ ui_chat_entry_new(MESSENGER_Application *app) | |||
34 | { | 34 | { |
35 | UI_CHAT_ENTRY_Handle* handle = g_malloc(sizeof(UI_CHAT_ENTRY_Handle)); | 35 | UI_CHAT_ENTRY_Handle* handle = g_malloc(sizeof(UI_CHAT_ENTRY_Handle)); |
36 | 36 | ||
37 | memset(handle, 0, sizeof(*handle)); | ||
38 | |||
37 | handle->chat = ui_chat_new(app); | 39 | handle->chat = ui_chat_new(app); |
38 | handle->builder = gtk_builder_new_from_resource( | 40 | handle->builder = gtk_builder_new_from_resource( |
39 | application_get_resource_path(app, "ui/chat_entry.ui") | 41 | application_get_resource_path(app, "ui/chat_entry.ui") |
@@ -153,5 +155,8 @@ ui_chat_entry_delete(UI_CHAT_ENTRY_Handle *handle) | |||
153 | 155 | ||
154 | g_object_unref(handle->builder); | 156 | g_object_unref(handle->builder); |
155 | 157 | ||
158 | if (handle->update) | ||
159 | g_source_remove(handle->update); | ||
160 | |||
156 | g_free(handle); | 161 | g_free(handle); |
157 | } | 162 | } |
diff --git a/src/ui/chat_entry.h b/src/ui/chat_entry.h index 8c90624..3a614c4 100644 --- a/src/ui/chat_entry.h +++ b/src/ui/chat_entry.h | |||
@@ -29,6 +29,8 @@ | |||
29 | 29 | ||
30 | typedef struct UI_CHAT_ENTRY_Handle | 30 | typedef struct UI_CHAT_ENTRY_Handle |
31 | { | 31 | { |
32 | guint update; | ||
33 | |||
32 | UI_CHAT_Handle *chat; | 34 | UI_CHAT_Handle *chat; |
33 | GtkBuilder *builder; | 35 | GtkBuilder *builder; |
34 | 36 | ||
diff --git a/src/ui/messenger.c b/src/ui/messenger.c index 633920c..45da0cd 100644 --- a/src/ui/messenger.c +++ b/src/ui/messenger.c | |||
@@ -280,31 +280,12 @@ handle_main_window_destroy(UNUSED GtkWidget *window, | |||
280 | application_exit(app, MESSENGER_QUIT); | 280 | application_exit(app, MESSENGER_QUIT); |
281 | } | 281 | } |
282 | 282 | ||
283 | static void | ||
284 | _switch_accounts_listbox_connection(MESSENGER_Application *app, | ||
285 | UI_MESSENGER_Handle *handle, | ||
286 | gboolean enabled) | ||
287 | { | ||
288 | if (enabled) | ||
289 | handle->accounts_signal = g_signal_connect( | ||
290 | handle->accounts_listbox, | ||
291 | "row-activated", | ||
292 | G_CALLBACK(handle_accounts_listbox_row_activated), | ||
293 | app | ||
294 | ); | ||
295 | else | ||
296 | g_signal_handler_disconnect( | ||
297 | handle->accounts_listbox, | ||
298 | handle->accounts_signal | ||
299 | ); | ||
300 | } | ||
301 | |||
302 | void | 283 | void |
303 | ui_messenger_init(MESSENGER_Application *app, | 284 | ui_messenger_init(MESSENGER_Application *app, |
304 | UI_MESSENGER_Handle *handle) | 285 | UI_MESSENGER_Handle *handle) |
305 | { | 286 | { |
287 | memset(handle, 0, sizeof(*handle)); | ||
306 | handle->app = app; | 288 | handle->app = app; |
307 | handle->chat_entries = NULL; | ||
308 | 289 | ||
309 | handle->builder = gtk_builder_new_from_resource( | 290 | handle->builder = gtk_builder_new_from_resource( |
310 | application_get_resource_path(app, "ui/messenger.ui") | 291 | application_get_resource_path(app, "ui/messenger.ui") |
@@ -415,7 +396,12 @@ ui_messenger_init(MESSENGER_Application *app, | |||
415 | gtk_builder_get_object(handle->builder, "add_account_listbox_row") | 396 | gtk_builder_get_object(handle->builder, "add_account_listbox_row") |
416 | ); | 397 | ); |
417 | 398 | ||
418 | _switch_accounts_listbox_connection(app, handle, TRUE); | 399 | g_signal_connect( |
400 | handle->accounts_listbox, | ||
401 | "row-activated", | ||
402 | G_CALLBACK(handle_accounts_listbox_row_activated), | ||
403 | app | ||
404 | ); | ||
419 | 405 | ||
420 | handle->new_contact_button = GTK_BUTTON( | 406 | handle->new_contact_button = GTK_BUTTON( |
421 | gtk_builder_get_object(handle->builder, "new_contact_button") | 407 | gtk_builder_get_object(handle->builder, "new_contact_button") |
@@ -582,8 +568,6 @@ ui_messenger_refresh(MESSENGER_Application *app, | |||
582 | if (!(handle->accounts_listbox)) | 568 | if (!(handle->accounts_listbox)) |
583 | return; | 569 | return; |
584 | 570 | ||
585 | _switch_accounts_listbox_connection(app, handle, FALSE); | ||
586 | |||
587 | gtk_container_foreach( | 571 | gtk_container_foreach( |
588 | GTK_CONTAINER(handle->accounts_listbox), | 572 | GTK_CONTAINER(handle->accounts_listbox), |
589 | _clear_accounts_listbox, | 573 | _clear_accounts_listbox, |
@@ -595,8 +579,6 @@ ui_messenger_refresh(MESSENGER_Application *app, | |||
595 | _messenger_iterate_accounts, | 579 | _messenger_iterate_accounts, |
596 | app | 580 | app |
597 | ); | 581 | ); |
598 | |||
599 | _switch_accounts_listbox_connection(app, handle, TRUE); | ||
600 | } | 582 | } |
601 | 583 | ||
602 | gboolean | 584 | gboolean |
@@ -626,5 +608,11 @@ ui_messenger_cleanup(UI_MESSENGER_Handle *handle) | |||
626 | if (handle->chat_entries) | 608 | if (handle->chat_entries) |
627 | g_list_free_full(handle->chat_entries, (GDestroyNotify) ui_chat_entry_delete); | 609 | g_list_free_full(handle->chat_entries, (GDestroyNotify) ui_chat_entry_delete); |
628 | 610 | ||
611 | if (handle->chat_selection) | ||
612 | g_source_remove(handle->chat_selection); | ||
613 | |||
614 | if (handle->account_refresh) | ||
615 | g_source_remove(handle->account_refresh); | ||
616 | |||
629 | memset(handle, 0, sizeof(*handle)); | 617 | memset(handle, 0, sizeof(*handle)); |
630 | } | 618 | } |
diff --git a/src/ui/messenger.h b/src/ui/messenger.h index ef1ae60..54f8dfa 100644 --- a/src/ui/messenger.h +++ b/src/ui/messenger.h | |||
@@ -38,6 +38,8 @@ typedef struct UI_MESSENGER_Handle | |||
38 | MESSENGER_Application *app; | 38 | MESSENGER_Application *app; |
39 | 39 | ||
40 | GList *chat_entries; | 40 | GList *chat_entries; |
41 | guint chat_selection; | ||
42 | guint account_refresh; | ||
41 | 43 | ||
42 | GtkBuilder *builder; | 44 | GtkBuilder *builder; |
43 | GtkApplicationWindow *main_window; | 45 | GtkApplicationWindow *main_window; |
@@ -72,8 +74,6 @@ typedef struct UI_MESSENGER_Handle | |||
72 | 74 | ||
73 | GtkStack *chats_stack; | 75 | GtkStack *chats_stack; |
74 | GtkWidget *no_chat_box; | 76 | GtkWidget *no_chat_box; |
75 | |||
76 | gulong accounts_signal; | ||
77 | } UI_MESSENGER_Handle; | 77 | } UI_MESSENGER_Handle; |
78 | 78 | ||
79 | void | 79 | void |