diff options
Diffstat (limited to 'src/plugins/chat/chat.c')
-rw-r--r-- | src/plugins/chat/chat.c | 197 |
1 files changed, 88 insertions, 109 deletions
diff --git a/src/plugins/chat/chat.c b/src/plugins/chat/chat.c index fb3b609a..b9fceec8 100644 --- a/src/plugins/chat/chat.c +++ b/src/plugins/chat/chat.c | |||
@@ -32,7 +32,7 @@ | |||
32 | 32 | ||
33 | struct GNUNET_CHAT_safe_write_struct | 33 | struct GNUNET_CHAT_safe_write_struct |
34 | { | 34 | { |
35 | GtkWidget * text_view; | 35 | GtkWidget *text_view; |
36 | const char *message; | 36 | const char *message; |
37 | const char *sender; | 37 | const char *sender; |
38 | }; | 38 | }; |
@@ -40,7 +40,7 @@ struct GNUNET_CHAT_safe_write_struct | |||
40 | struct GNUNET_CHAT_safe_nick_write_struct | 40 | struct GNUNET_CHAT_safe_nick_write_struct |
41 | { | 41 | { |
42 | GtkListStore *model; | 42 | GtkListStore *model; |
43 | GdkPixbuf * icon; | 43 | GdkPixbuf *icon; |
44 | const struct GNUNET_ECRS_MetaData *meta; | 44 | const struct GNUNET_ECRS_MetaData *meta; |
45 | const char *nick; | 45 | const char *nick; |
46 | GNUNET_HashCode pid; | 46 | GNUNET_HashCode pid; |
@@ -49,9 +49,9 @@ struct GNUNET_CHAT_safe_nick_write_struct | |||
49 | struct GNUNET_CHAT_gui_chat_client | 49 | struct GNUNET_CHAT_gui_chat_client |
50 | { | 50 | { |
51 | struct GNUNET_CHAT_gui_chat_client *next; | 51 | struct GNUNET_CHAT_gui_chat_client *next; |
52 | GtkWidget * send_button; | 52 | GtkWidget *send_button; |
53 | GtkWidget * text_view; | 53 | GtkWidget *text_view; |
54 | GtkListStore * nick_model; | 54 | GtkListStore *nick_model; |
55 | GtkWidget *chatFrame; | 55 | GtkWidget *chatFrame; |
56 | GtkWidget *chat_entry; | 56 | GtkWidget *chat_entry; |
57 | GladeXML *chatXML; | 57 | GladeXML *chatXML; |
@@ -90,59 +90,51 @@ write_save_call (void *arg) | |||
90 | 90 | ||
91 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (cls->text_view)); | 91 | buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (cls->text_view)); |
92 | message_buf_size = | 92 | message_buf_size = |
93 | snprintf (NULL, 0, | 93 | snprintf (NULL, 0, "<%s>: %s\n", cls->sender, cls->message); |
94 | "<%s>: %s\n", | ||
95 | cls->sender, | ||
96 | cls->message); | ||
97 | message_buf = GNUNET_malloc (message_buf_size + 1); | 94 | message_buf = GNUNET_malloc (message_buf_size + 1); |
98 | GNUNET_snprintf (message_buf, | 95 | GNUNET_snprintf (message_buf, |
99 | message_buf_size + 1, | 96 | message_buf_size + 1, |
100 | "<%s>: %s\n", | 97 | "<%s>: %s\n", cls->sender, cls->message); |
101 | cls->sender, | 98 | gtk_text_buffer_insert_at_cursor (buffer, message_buf, message_buf_size); |
102 | cls->message); | 99 | GNUNET_free (message_buf); |
103 | gtk_text_buffer_insert_at_cursor (buffer, | ||
104 | message_buf, | ||
105 | message_buf_size); | ||
106 | GNUNET_free(message_buf); | ||
107 | return NULL; | 100 | return NULL; |
108 | } | 101 | } |
109 | 102 | ||
110 | /** | 103 | /** |
111 | * Safe call to add a nick to the chat room view | 104 | * Safe call to add a nick to the chat room view |
112 | */ | 105 | */ |
113 | static void * | 106 | static void * |
114 | add_nick_save_call (void *arg) | 107 | add_nick_save_call (void *arg) |
115 | { | 108 | { |
116 | struct GNUNET_CHAT_safe_nick_write_struct *cls = arg; | 109 | struct GNUNET_CHAT_safe_nick_write_struct *cls = arg; |
117 | GtkListStore * model; | 110 | GtkListStore *model; |
118 | GtkTreeIter iter; | 111 | GtkTreeIter iter; |
119 | GNUNET_HashCode * pid; | 112 | GNUNET_HashCode *pid; |
120 | 113 | ||
121 | model = GTK_LIST_STORE (cls->model); | 114 | model = GTK_LIST_STORE (cls->model); |
122 | gtk_list_store_append (model, &iter); | 115 | gtk_list_store_append (model, &iter); |
123 | pid = GNUNET_malloc(sizeof(GNUNET_HashCode)); | 116 | pid = GNUNET_malloc (sizeof (GNUNET_HashCode)); |
124 | *pid = cls->pid; | 117 | *pid = cls->pid; |
125 | gtk_list_store_set(model, | 118 | gtk_list_store_set (model, |
126 | &iter, | 119 | &iter, |
127 | CHAT_PID, pid, | 120 | CHAT_PID, pid, |
128 | CHAT_METADATA, GNUNET_ECRS_meta_data_duplicate(cls->meta), | 121 | CHAT_METADATA, |
129 | CHAT_ICON, cls->icon, | 122 | GNUNET_ECRS_meta_data_duplicate (cls->meta), CHAT_ICON, |
130 | CHAT_NICKNAME, cls->nick, | 123 | cls->icon, CHAT_NICKNAME, cls->nick, -1); |
131 | -1); | ||
132 | return NULL; | 124 | return NULL; |
133 | } | 125 | } |
134 | 126 | ||
135 | /** | 127 | /** |
136 | * Safe call to remove a nick from the chat room view | 128 | * Safe call to remove a nick from the chat room view |
137 | */ | 129 | */ |
138 | static void * | 130 | static void * |
139 | remove_nick_save_call (void *arg) | 131 | remove_nick_save_call (void *arg) |
140 | { | 132 | { |
141 | struct GNUNET_CHAT_safe_nick_write_struct *cls = arg; | 133 | struct GNUNET_CHAT_safe_nick_write_struct *cls = arg; |
142 | GtkListStore * model; | 134 | GtkListStore *model; |
143 | GtkTreeIter iter; | 135 | GtkTreeIter iter; |
144 | GNUNET_HashCode * pid; | 136 | GNUNET_HashCode *pid; |
145 | struct GNUNET_ECRS_MetaData * meta; | 137 | struct GNUNET_ECRS_MetaData *meta; |
146 | 138 | ||
147 | model = GTK_LIST_STORE (cls->model); | 139 | model = GTK_LIST_STORE (cls->model); |
148 | /* find and remove existing entry */ | 140 | /* find and remove existing entry */ |
@@ -153,19 +145,15 @@ remove_nick_save_call (void *arg) | |||
153 | pid = NULL; | 145 | pid = NULL; |
154 | gtk_tree_model_get (GTK_TREE_MODEL (model), | 146 | gtk_tree_model_get (GTK_TREE_MODEL (model), |
155 | &iter, | 147 | &iter, |
156 | CHAT_PID, &pid, | 148 | CHAT_PID, &pid, CHAT_METADATA, &meta, -1); |
157 | CHAT_METADATA, &meta, | 149 | if ((pid != NULL) && |
158 | -1); | 150 | (0 == memcmp (pid, &cls->pid, sizeof (GNUNET_HashCode)))) |
159 | if ( (pid != NULL) && | 151 | { |
160 | (0 == memcmp(pid, | 152 | GNUNET_ECRS_meta_data_destroy (meta); |
161 | &cls->pid, | 153 | GNUNET_free (pid); |
162 | sizeof(GNUNET_HashCode))) ) | 154 | gtk_list_store_remove (model, &iter); |
163 | { | 155 | return NULL; /* done! */ |
164 | GNUNET_ECRS_meta_data_destroy(meta); | 156 | } |
165 | GNUNET_free(pid); | ||
166 | gtk_list_store_remove(model, &iter); | ||
167 | return NULL; /* done! */ | ||
168 | } | ||
169 | } | 157 | } |
170 | while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter)); | 158 | while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter)); |
171 | } | 159 | } |
@@ -189,40 +177,38 @@ remove_nick_save_call (void *arg) | |||
189 | static int | 177 | static int |
190 | receive_callback (void *cls, | 178 | receive_callback (void *cls, |
191 | struct GNUNET_CHAT_Room *room, | 179 | struct GNUNET_CHAT_Room *room, |
192 | const GNUNET_HashCode *sender, | 180 | const GNUNET_HashCode * sender, |
193 | const struct GNUNET_ECRS_MetaData * member_info, | 181 | const struct GNUNET_ECRS_MetaData *member_info, |
194 | const char *message, | 182 | const char *message, GNUNET_CHAT_MSG_OPTIONS options) |
195 | GNUNET_CHAT_MSG_OPTIONS options) | ||
196 | { | 183 | { |
197 | struct GNUNET_CHAT_gui_chat_client *client = cls; | 184 | struct GNUNET_CHAT_gui_chat_client *client = cls; |
198 | struct GNUNET_CHAT_safe_write_struct writearg; | 185 | struct GNUNET_CHAT_safe_write_struct writearg; |
199 | char * sndr; | 186 | char *sndr; |
200 | 187 | ||
201 | sndr = sender == NULL ? _("anonymous") : GNUNET_PSEUDO_id_to_name(chat_ectx, chat_cfg, sender); | 188 | sndr = |
189 | sender == NULL ? _("anonymous") : GNUNET_PSEUDO_id_to_name (chat_ectx, | ||
190 | chat_cfg, | ||
191 | sender); | ||
202 | writearg.text_view = client->text_view; | 192 | writearg.text_view = client->text_view; |
203 | writearg.message = message; | 193 | writearg.message = message; |
204 | writearg.sender = sndr; | 194 | writearg.sender = sndr; |
205 | GNUNET_GTK_save_call (&write_save_call, &writearg); | 195 | GNUNET_GTK_save_call (&write_save_call, &writearg); |
206 | GNUNET_free_non_null(sndr); | 196 | GNUNET_free_non_null (sndr); |
207 | return GNUNET_OK; | 197 | return GNUNET_OK; |
208 | } | 198 | } |
209 | 199 | ||
210 | static int | 200 | static int |
211 | member_list_callback (void *cls, | 201 | member_list_callback (void *cls, |
212 | const struct GNUNET_ECRS_MetaData * member_info, | 202 | const struct GNUNET_ECRS_MetaData *member_info, |
213 | const GNUNET_RSA_PublicKey * pkey, | 203 | const GNUNET_RSA_PublicKey * pkey, |
214 | GNUNET_CHAT_MSG_OPTIONS opt) | 204 | GNUNET_CHAT_MSG_OPTIONS opt) |
215 | { | 205 | { |
216 | struct GNUNET_CHAT_gui_chat_client *client = cls; | 206 | struct GNUNET_CHAT_gui_chat_client *client = cls; |
217 | struct GNUNET_CHAT_safe_nick_write_struct writearg; | 207 | struct GNUNET_CHAT_safe_nick_write_struct writearg; |
218 | char *nick; | 208 | char *nick; |
219 | 209 | ||
220 | GNUNET_hash(pkey, | 210 | GNUNET_hash (pkey, sizeof (GNUNET_RSA_PublicKey), &writearg.pid); |
221 | sizeof(GNUNET_RSA_PublicKey), | 211 | nick = GNUNET_PSEUDO_id_to_name (chat_ectx, chat_cfg, &writearg.pid); |
222 | &writearg.pid); | ||
223 | nick = GNUNET_PSEUDO_id_to_name(chat_ectx, | ||
224 | chat_cfg, | ||
225 | &writearg.pid); | ||
226 | writearg.model = client->nick_model; | 212 | writearg.model = client->nick_model; |
227 | /* FIXME: set icon if pid == ours */ | 213 | /* FIXME: set icon if pid == ours */ |
228 | writearg.icon = NULL; | 214 | writearg.icon = NULL; |
@@ -232,7 +218,7 @@ member_list_callback (void *cls, | |||
232 | GNUNET_GTK_save_call (&add_nick_save_call, &writearg); | 218 | GNUNET_GTK_save_call (&add_nick_save_call, &writearg); |
233 | else | 219 | else |
234 | GNUNET_GTK_save_call (&remove_nick_save_call, &writearg); | 220 | GNUNET_GTK_save_call (&remove_nick_save_call, &writearg); |
235 | GNUNET_free(nick); | 221 | GNUNET_free (nick); |
236 | return GNUNET_OK; | 222 | return GNUNET_OK; |
237 | } | 223 | } |
238 | 224 | ||
@@ -243,20 +229,17 @@ on_chat_frame_send_button_click_event_chat (GtkWidget * widget, gpointer data) | |||
243 | const char *message; | 229 | const char *message; |
244 | struct GNUNET_CHAT_gui_chat_client *pos; | 230 | struct GNUNET_CHAT_gui_chat_client *pos; |
245 | 231 | ||
246 | GNUNET_mutex_lock(lock); | 232 | GNUNET_mutex_lock (lock); |
247 | pos = list; | 233 | pos = list; |
248 | while ( (pos != NULL) && | 234 | while ((pos != NULL) && (pos->send_button != widget)) |
249 | (pos->send_button != widget) ) | ||
250 | pos = pos->next; | 235 | pos = pos->next; |
251 | GNUNET_GE_ASSERT(NULL, pos != NULL); | 236 | GNUNET_GE_ASSERT (NULL, pos != NULL); |
252 | 237 | ||
253 | message = (const char *) gtk_entry_get_text (GTK_ENTRY(pos->chat_entry)); | 238 | message = (const char *) gtk_entry_get_text (GTK_ENTRY (pos->chat_entry)); |
254 | GNUNET_CHAT_send_message (pos->room, | 239 | GNUNET_CHAT_send_message (pos->room, |
255 | message, | 240 | message, GNUNET_CHAT_MSG_OPTION_NONE, NULL, &seq); |
256 | GNUNET_CHAT_MSG_OPTION_NONE, | 241 | gtk_entry_set_text (GTK_ENTRY (pos->chat_entry), ""); |
257 | NULL, &seq); | 242 | GNUNET_mutex_unlock (lock); |
258 | gtk_entry_set_text (GTK_ENTRY(pos->chat_entry), ""); | ||
259 | GNUNET_mutex_unlock(lock); | ||
260 | } | 243 | } |
261 | 244 | ||
262 | /** FIXME: move to gtkcommon! */ | 245 | /** FIXME: move to gtkcommon! */ |
@@ -275,44 +258,44 @@ extractMainWidgetFromWindow (GladeXML * xml, const char *windowName) | |||
275 | } | 258 | } |
276 | 259 | ||
277 | static void | 260 | static void |
278 | create_chat_room_tab (const char *room_name, | 261 | create_chat_room_tab (const char *room_name, const char *nick) |
279 | const char *nick) | ||
280 | { | 262 | { |
281 | GtkWidget *chatLabel; | 263 | GtkWidget *chatLabel; |
282 | GtkWidget *chatnotebook; | 264 | GtkWidget *chatnotebook; |
283 | GtkTreeView *treeview; | 265 | GtkTreeView *treeview; |
284 | struct GNUNET_CHAT_gui_chat_client *client; | 266 | struct GNUNET_CHAT_gui_chat_client *client; |
285 | struct GNUNET_ECRS_MetaData * meta; | 267 | struct GNUNET_ECRS_MetaData *meta; |
286 | GtkCellRenderer *renderer; | 268 | GtkCellRenderer *renderer; |
287 | GtkTreeViewColumn *column; | 269 | GtkTreeViewColumn *column; |
288 | int col; | 270 | int col; |
289 | 271 | ||
290 | chatnotebook = | 272 | chatnotebook = |
291 | glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "chatnotebook"); | 273 | glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "chatnotebook"); |
292 | 274 | ||
293 | client = GNUNET_malloc (sizeof (struct GNUNET_CHAT_gui_chat_client)); | 275 | client = GNUNET_malloc (sizeof (struct GNUNET_CHAT_gui_chat_client)); |
294 | client->chatXML = | 276 | client->chatXML = |
295 | glade_xml_new (GNUNET_GTK_get_glade_filename (), | 277 | glade_xml_new (GNUNET_GTK_get_glade_filename (), |
296 | "chatFrame", | 278 | "chatFrame", PACKAGE_NAME); |
297 | PACKAGE_NAME); | ||
298 | GNUNET_GTK_connect_glade_with_plugins (client->chatXML); | 279 | GNUNET_GTK_connect_glade_with_plugins (client->chatXML); |
299 | client->chatFrame = extractMainWidgetFromWindow (client->chatXML, | 280 | client->chatFrame = extractMainWidgetFromWindow (client->chatXML, |
300 | "chatFrame"); | 281 | "chatFrame"); |
301 | chatLabel = gtk_label_new (room_name); | 282 | chatLabel = gtk_label_new (room_name); |
302 | gtk_notebook_append_page (GTK_NOTEBOOK (chatnotebook), | 283 | gtk_notebook_append_page (GTK_NOTEBOOK (chatnotebook), |
303 | client->chatFrame, | 284 | client->chatFrame, chatLabel); |
304 | chatLabel); | ||
305 | gtk_widget_show (chatnotebook); | 285 | gtk_widget_show (chatnotebook); |
306 | 286 | ||
307 | client->nick_model = gtk_list_store_new (CHAT_NUM, | 287 | client->nick_model = gtk_list_store_new (CHAT_NUM, G_TYPE_STRING, /* nickname */ |
308 | G_TYPE_STRING, /* nickname */ | 288 | G_TYPE_POINTER, /* metadata */ |
309 | G_TYPE_POINTER, /* metadata */ | 289 | GDK_TYPE_PIXBUF, /* icon */ |
310 | GDK_TYPE_PIXBUF, /* icon */ | 290 | G_TYPE_POINTER); /* pid */ |
311 | G_TYPE_POINTER); /* pid */ | 291 | client->send_button = |
312 | client->send_button = glade_xml_get_widget (client->chatXML, "chatSendButton"); | 292 | glade_xml_get_widget (client->chatXML, "chatSendButton"); |
313 | client->text_view = glade_xml_get_widget (client->chatXML, "chatLogViewer"); | 293 | client->text_view = glade_xml_get_widget (client->chatXML, "chatLogViewer"); |
314 | client->chat_entry = glade_xml_get_widget (client->chatXML, "chatLineTextEntry"); | 294 | client->chat_entry = |
315 | treeview = GTK_TREE_VIEW(glade_xml_get_widget (client->chatXML, "roomMembersTreeView")); | 295 | glade_xml_get_widget (client->chatXML, "chatLineTextEntry"); |
296 | treeview = | ||
297 | GTK_TREE_VIEW (glade_xml_get_widget | ||
298 | (client->chatXML, "roomMembersTreeView")); | ||
316 | gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (client->nick_model)); | 299 | gtk_tree_view_set_model (treeview, GTK_TREE_MODEL (client->nick_model)); |
317 | 300 | ||
318 | renderer = gtk_cell_renderer_text_new (); | 301 | renderer = gtk_cell_renderer_text_new (); |
@@ -328,7 +311,7 @@ create_chat_room_tab (const char *room_name, | |||
328 | gtk_tree_view_column_set_reorderable (column, TRUE); | 311 | gtk_tree_view_column_set_reorderable (column, TRUE); |
329 | gtk_tree_view_column_set_sort_column_id (column, CHAT_NICKNAME); | 312 | gtk_tree_view_column_set_sort_column_id (column, CHAT_NICKNAME); |
330 | gtk_tree_view_column_set_min_width (column, 0); | 313 | gtk_tree_view_column_set_min_width (column, 0); |
331 | 314 | ||
332 | renderer = gtk_cell_renderer_pixbuf_new (); | 315 | renderer = gtk_cell_renderer_pixbuf_new (); |
333 | col = gtk_tree_view_insert_column_with_attributes (treeview, | 316 | col = gtk_tree_view_insert_column_with_attributes (treeview, |
334 | -1, | 317 | -1, |
@@ -341,25 +324,21 @@ create_chat_room_tab (const char *room_name, | |||
341 | gtk_tree_view_column_set_clickable (column, FALSE); | 324 | gtk_tree_view_column_set_clickable (column, FALSE); |
342 | gtk_tree_view_column_set_reorderable (column, FALSE); | 325 | gtk_tree_view_column_set_reorderable (column, FALSE); |
343 | 326 | ||
344 | GNUNET_mutex_lock(lock); | 327 | GNUNET_mutex_lock (lock); |
345 | client->next = list; | 328 | client->next = list; |
346 | list = client; | 329 | list = client; |
347 | GNUNET_mutex_unlock(lock); | 330 | GNUNET_mutex_unlock (lock); |
348 | 331 | ||
349 | meta = GNUNET_ECRS_meta_data_create(); | 332 | meta = GNUNET_ECRS_meta_data_create (); |
350 | GNUNET_ECRS_meta_data_insert(meta, | 333 | GNUNET_ECRS_meta_data_insert (meta, EXTRACTOR_TITLE, nick); |
351 | EXTRACTOR_TITLE, | ||
352 | nick); | ||
353 | client->room = | 334 | client->room = |
354 | GNUNET_CHAT_join_room (chat_ectx, chat_cfg, nick, | 335 | GNUNET_CHAT_join_room (chat_ectx, chat_cfg, nick, |
355 | meta, | 336 | meta, |
356 | room_name, | 337 | room_name, |
357 | -1, | 338 | -1, |
358 | &receive_callback, client, &member_list_callback, | 339 | &receive_callback, client, &member_list_callback, |
359 | client, | 340 | client, NULL, NULL, &client->mypid); |
360 | NULL, NULL, | 341 | GNUNET_ECRS_meta_data_destroy (meta); |
361 | &client->mypid); | ||
362 | GNUNET_ECRS_meta_data_destroy(meta); | ||
363 | } | 342 | } |
364 | 343 | ||
365 | void | 344 | void |