From 62773a7c965b220c3f83acfe2e915f08ddb3f474 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 26 Dec 2012 20:29:28 +0100 Subject: [PATCH] Use of g_idle_add_block() where possible --- src/gui/gtk/JubGtkChatUI.h | 4 - src/gui/gtk/JubGtkChatUI.m | 106 +++++++--------- src/gui/gtk/JubGtkRosterUI.m | 235 ++++++++++++++--------------------- 3 files changed, 135 insertions(+), 210 deletions(-) diff --git a/src/gui/gtk/JubGtkChatUI.h b/src/gui/gtk/JubGtkChatUI.h index 1c35681..1b27ca1 100644 --- a/src/gui/gtk/JubGtkChatUI.h +++ b/src/gui/gtk/JubGtkChatUI.h @@ -13,10 +13,6 @@ typedef void (^jub_close_block_t)(void); jub_close_block_t closeBlock; BOOL bufferEmpty; } -@property (assign) GtkWidget *chat_window; -@property (assign) GtkTextBuffer * chat_buffer; -@property (readonly) jub_send_block_t sendBlock; -@property (readonly) jub_close_block_t closeBlock; - initWithTitle: (OFString*)title closeBlock: (jub_close_block_t)closeBlock diff --git a/src/gui/gtk/JubGtkChatUI.m b/src/gui/gtk/JubGtkChatUI.m index 4a45bcf..79872f8 100644 --- a/src/gui/gtk/JubGtkChatUI.m +++ b/src/gui/gtk/JubGtkChatUI.m @@ -32,86 +32,68 @@ static gboolean call_close_block(GtkWidget *object, gpointer data) return FALSE; } -struct init_params { - OFString *title; - JubGtkChatUI *object; - volatile BOOL *initialized; -}; - -static gboolean init(gpointer data) +@implementation JubGtkChatUI +- initWithTitle: (OFString*)title + closeBlock: (jub_close_block_t)closeBlock_ + sendBlock: (jub_send_block_t)sendBlock_ { - struct init_params *params = data; - - GtkTextView *chat_view; - GtkEntry *chat_entry; - - GtkBuilder *builder = gtk_builder_new(); - gtk_builder_add_from_file(builder, "data/gtk/chat.ui", NULL); + self = [super init]; - params->object.chat_window = GTK_WIDGET( - gtk_builder_get_object(builder, "ChatWindow")); - g_signal_connect(params->object.chat_window, "destroy", - G_CALLBACK(call_close_block), params->object.closeBlock); + @try { + __block volatile BOOL initialized = NO; + __block GtkWidget *chat_window_; + __block GtkTextBuffer *chat_buffer_; + closeBlock = [closeBlock_ copy]; + sendBlock = [sendBlock_ copy]; + bufferEmpty = YES; - chat_view = GTK_TEXT_VIEW( - gtk_builder_get_object(builder, "ChatTextView")); - params->object.chat_buffer = gtk_text_view_get_buffer(chat_view); - gtk_text_buffer_create_tag(params->object.chat_buffer, "bold", - "weight", PANGO_WEIGHT_BOLD, NULL); + g_idle_add_block(^{ + GtkTextView *chat_view; + GtkEntry *chat_entry; - chat_entry = GTK_ENTRY( - gtk_builder_get_object(builder, "ChatEntry")); - struct call_send_block_params *send_params = - [params->object allocMemoryWithSize: sizeof(*send_params)]; - send_params->block = params->object.sendBlock; - send_params->chat = params->object; - g_signal_connect(chat_entry, "key_release_event", - G_CALLBACK(call_send_block), send_params); + GtkBuilder *builder = gtk_builder_new(); + gtk_builder_add_from_file(builder, "data/gtk/chat.ui", + NULL); - gtk_window_set_title(GTK_WINDOW(params->object.chat_window), - [params->title UTF8String]); - gtk_widget_show(params->object.chat_window); + chat_window_ = GTK_WIDGET( + gtk_builder_get_object(builder, "ChatWindow")); + g_signal_connect(chat_window_, "destroy", + G_CALLBACK(call_close_block), closeBlock); - *params->initialized = YES; + chat_view = GTK_TEXT_VIEW( + gtk_builder_get_object(builder, "ChatTextView")); + chat_buffer_ = gtk_text_view_get_buffer(chat_view); + gtk_text_buffer_create_tag(chat_buffer_, "bold", + "weight", PANGO_WEIGHT_BOLD, NULL); - g_object_unref(builder); - [params->title release]; - free(params); + chat_entry = GTK_ENTRY( + gtk_builder_get_object(builder, "ChatEntry")); + struct call_send_block_params *send_params = + [self allocMemoryWithSize: sizeof(*send_params)]; + send_params->block = sendBlock; + send_params->chat = self; + g_signal_connect(chat_entry, "key_release_event", + G_CALLBACK(call_send_block), send_params); - return FALSE; -} + gtk_window_set_title(GTK_WINDOW(chat_window_), + [title UTF8String]); + gtk_widget_show(chat_window_); -@implementation JubGtkChatUI -@synthesize chat_window; -@synthesize chat_buffer; -@synthesize sendBlock; -@synthesize closeBlock; + initialized = YES; -- initWithTitle: (OFString*)title - closeBlock: (jub_close_block_t)closeBlock_ - sendBlock: (jub_send_block_t)sendBlock_ -{ - volatile BOOL initialized = NO; - self = [super init]; + g_object_unref(builder); + }); - @try { - closeBlock = [closeBlock_ copy]; - sendBlock = [sendBlock_ copy]; - bufferEmpty = YES; + while (!initialized); - struct init_params *params = malloc(sizeof(*params)); - params->initialized = &initialized; - params->title = [title retain]; - params->object = self; - g_idle_add(init, params); + chat_buffer = chat_buffer_; + chat_window = chat_window_; } @catch (id e) { [self release]; @throw e; } - while (!initialized); - return self; } diff --git a/src/gui/gtk/JubGtkRosterUI.m b/src/gui/gtk/JubGtkRosterUI.m index 06ffd28..6fb8af0 100644 --- a/src/gui/gtk/JubGtkRosterUI.m +++ b/src/gui/gtk/JubGtkRosterUI.m @@ -1,6 +1,7 @@ #import "JubGtkRosterUI.h" #import "JubGObjectMap.h" #import "JubGtkChatUI.h" +#import "JubGtkHelper.h" static gboolean filter_roster_by_presence(GtkTreeModel *model, GtkTreeIter *iter, gpointer data) @@ -78,13 +79,6 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model, } /* Presence handling */ -static gboolean refilter_roster(gpointer data) -{ - gtk_tree_model_filter_refilter(data); - - return FALSE; -} - - (void)connection: (XMPPConnection*)connection didReceivePresence: (XMPPPresence*)presence { @@ -93,7 +87,9 @@ static gboolean refilter_roster(gpointer data) else if ([presence.type isEqual: @"unavailable"]) [presences removeObject: [presence.from bareJID]]; - g_idle_add(refilter_roster, roster_filter); + g_idle_add_block(^{ + gtk_tree_model_filter_refilter(roster_filter); + }); } // FIXME: This needs to move somewhere else @@ -130,121 +126,95 @@ static gboolean refilter_roster(gpointer data) } /* Roster Delegate methods */ -struct add_roster_item_param { - OFString *group; - OFString *name; - XMPPJID *jid; - OFMapTable *groupMap; - OFMapTable *contactRows; - GtkTreeStore *roster_model; -}; - -static gboolean add_roster_item(gpointer user_data) +- (void)Jub_addRosterItem: (XMPPRosterItem*)item + group: (OFString*)group { - struct add_roster_item_param *params = user_data; - GtkTreeIter group_iter, contact_iter; - GtkTreeRowReference *group_ref, *contact_ref; - GtkTreePath *group_path, *contact_path; - - group_ref = [params->groupMap valueForKey: params->group]; - - if (!group_ref) { - // Create new group row - gtk_tree_store_append(params->roster_model, &group_iter, NULL); - gtk_tree_store_set(params->roster_model, &group_iter, - 0, [params->group UTF8String], -1); + g_idle_add_block(^{ + GtkTreeIter group_iter, contact_iter; + GtkTreeRowReference *group_ref, *contact_ref; + GtkTreePath *group_path, *contact_path; + OFMapTable *contactRows = + [contactMap objectForKey: [item.JID bareJID]]; + + group_ref = [groupMap valueForKey: group]; + + if (!group_ref) { + // Create new group row + gtk_tree_store_append(roster_model, &group_iter, NULL); + gtk_tree_store_set(roster_model, &group_iter, + 0, [group UTF8String], -1); + + group_path = gtk_tree_model_get_path(GTK_TREE_MODEL( + roster_model), &group_iter); + + group_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL( + roster_model), group_path); + + [groupMap setValue: group_ref + forKey: group]; + } else { + // Get iter for existing group row + group_path = gtk_tree_row_reference_get_path(group_ref); + + gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model), + &group_iter, group_path); + } + gtk_tree_path_free(group_path); + + // Create new contact row + gtk_tree_store_append(roster_model, &contact_iter, &group_iter); + if (item.name) + gtk_tree_store_set(roster_model, &contact_iter, + 0, [item.name UTF8String], + 1, [[item.JID bareJID] UTF8String], -1); + else + gtk_tree_store_set(roster_model, &contact_iter, + 0, [item.JID.node UTF8String], + 1, [[item.JID bareJID] UTF8String], -1); - group_path = gtk_tree_model_get_path(GTK_TREE_MODEL( - params->roster_model), &group_iter); + contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL( + roster_model), &contact_iter); - group_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL( - params->roster_model), group_path); + contact_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL( + roster_model), contact_path); - [params->groupMap setValue: group_ref - forKey: params->group]; - } else { - // Get iter for existing group row - group_path = gtk_tree_row_reference_get_path(group_ref); + gtk_tree_path_free(contact_path); - gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model), - &group_iter, group_path); - } - gtk_tree_path_free(group_path); - - // Create new contact row - gtk_tree_store_append(params->roster_model, &contact_iter, &group_iter); - if (params->name) - gtk_tree_store_set(params->roster_model, &contact_iter, - 0, [params->name UTF8String], - 1, [[params->jid bareJID] UTF8String], -1); - else - gtk_tree_store_set(params->roster_model, &contact_iter, - 0, [params->jid.node UTF8String], - 1, [[params->jid bareJID] UTF8String], -1); - - contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL( - params->roster_model), &contact_iter); - - contact_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL( - params->roster_model), contact_path); - - gtk_tree_path_free(contact_path); - - [params->contactRows setValue: contact_ref - forKey: params->group]; - - [params->group release]; - [params->name release]; - [params->jid release]; - [params->groupMap release]; - [params->contactRows release]; - g_object_unref(params->roster_model); - free(params); - - return FALSE; + [contactRows setValue: contact_ref + forKey: group]; + }); } -struct remove_roster_item_param { - OFString *group; - OFMapTable *groupMap; - OFMapTable *contactRows; - GtkTreeStore *roster_model; -}; - -static gboolean remove_roster_item(gpointer user_data) +- (void)Jub_removeRosterItem: (XMPPRosterItem*)item + group: (OFString*)group { - struct remove_roster_item_param *params = user_data; - GtkTreeIter contact_iter, group_iter; - GtkTreePath *contact_path, *group_path; - GtkTreeRowReference *contact_ref, *group_ref; - - contact_ref = [params->contactRows valueForKey: params->group]; - contact_path = gtk_tree_row_reference_get_path(contact_ref); - gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model), - &contact_iter, contact_path); - - gtk_tree_store_remove(params->roster_model, &contact_iter); - - group_ref = [params->groupMap valueForKey: params->group]; - group_path = gtk_tree_row_reference_get_path(group_ref); - gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model), - &group_iter, group_path); - - if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(params->roster_model), - &group_iter)) { - gtk_tree_store_remove(params->roster_model, &group_iter); - [params->groupMap removeValueForKey: params->group]; - } + g_idle_add_block(^{ + GtkTreeIter contact_iter, group_iter; + GtkTreePath *contact_path, *group_path; + GtkTreeRowReference *contact_ref, *group_ref; + OFMapTable *contactRows = + [contactMap objectForKey: [item.JID bareJID]]; - gtk_tree_path_free(group_path); + contact_ref = [contactRows valueForKey: group]; + contact_path = gtk_tree_row_reference_get_path(contact_ref); + gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model), + &contact_iter, contact_path); - [params->group release]; - [params->groupMap release]; - [params->contactRows release]; - g_object_unref(params->roster_model); - free(params); + gtk_tree_store_remove(roster_model, &contact_iter); + + group_ref = [groupMap valueForKey: group]; + group_path = gtk_tree_row_reference_get_path(group_ref); + gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model), + &group_iter, group_path); - return FALSE; + if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(roster_model), + &group_iter)) { + gtk_tree_store_remove(roster_model, &group_iter); + [groupMap removeValueForKey: group]; + } + + gtk_tree_path_free(group_path); + }); } - (void)rosterWasReceived: (XMPPRoster*)roster_ @@ -265,17 +235,9 @@ static gboolean remove_roster_item(gpointer user_data) else groups = @[@"General"]; - for (OFString *group in groups) { - struct add_roster_item_param *params = - malloc(sizeof(*params)); - params->group = [group retain]; - params->name = [item.name retain]; - params->jid = [item.JID retain]; - params->groupMap = [groupMap retain]; - params->contactRows = [contactRows retain]; - params->roster_model = g_object_ref(roster_model); - g_idle_add(add_roster_item, params); - } + for (OFString *group in groups) + [self Jub_addRosterItem: item + group: group]; }]; } @@ -292,16 +254,9 @@ static gboolean remove_roster_item(gpointer user_data) else groups = @[@"General"]; - for (OFString *group in groups) { - struct remove_roster_item_param *params = - malloc(sizeof(*params)); - params->group = [group retain]; - params->contactRows = [[contactMap objectForKey: - [oldItem.JID bareJID]] retain]; - params->groupMap = [groupMap retain]; - params->roster_model = g_object_ref(roster_model); - g_idle_add(remove_roster_item, params); - } + for (OFString *group in groups) + [self Jub_removeRosterItem: oldItem + group: group]; [contactMap removeObjectForKey: [item.JID bareJID]]; } @@ -319,17 +274,9 @@ static gboolean remove_roster_item(gpointer user_data) else groups = @[@"General"]; - for (OFString *group in groups) { - struct add_roster_item_param *params = - malloc(sizeof(*params)); - params->group = [group retain]; - params->name = [item.name retain]; - params->jid = [item.JID retain]; - params->groupMap = [groupMap retain]; - params->contactRows = [contactRows retain]; - params->roster_model = g_object_ref(roster_model); - g_idle_add(add_roster_item, params); - } + for (OFString *group in groups) + [self Jub_addRosterItem: item + group: group]; } } -- 2.39.5