diff options
Diffstat (limited to 'src/ui/messenger.c')
-rw-r--r-- | src/ui/messenger.c | 215 |
1 files changed, 25 insertions, 190 deletions
diff --git a/src/ui/messenger.c b/src/ui/messenger.c index daabd17..2fc8c54 100644 --- a/src/ui/messenger.c +++ b/src/ui/messenger.c | |||
@@ -26,6 +26,7 @@ | |||
26 | 26 | ||
27 | #include <gtk-3.0/gdk/gdkkeys.h> | 27 | #include <gtk-3.0/gdk/gdkkeys.h> |
28 | 28 | ||
29 | #include "chat_entry.h" | ||
29 | #include "message.h" | 30 | #include "message.h" |
30 | #include "new_platform.h" | 31 | #include "new_platform.h" |
31 | #include "../application.h" | 32 | #include "../application.h" |
@@ -80,118 +81,25 @@ handle_new_platform_button_click(UNUSED GtkButton* button, | |||
80 | 81 | ||
81 | static void | 82 | static void |
82 | handle_chats_listbox_row_activated(UNUSED GtkListBox* listbox, | 83 | handle_chats_listbox_row_activated(UNUSED GtkListBox* listbox, |
83 | UNUSED GtkListBoxRow* row, | 84 | GtkListBoxRow* row, |
84 | gpointer user_data) | 85 | gpointer user_data) |
85 | { | 86 | { |
86 | HdyLeaflet* leaflet = HDY_LEAFLET(user_data); | 87 | UI_MESSENGER_Handle *handle = (UI_MESSENGER_Handle*) user_data; |
88 | |||
89 | GtkStack *stack = handle->chats_stack; | ||
90 | HdyLeaflet *leaflet = handle->leaflet_chat; | ||
87 | 91 | ||
88 | GList* children = gtk_container_get_children(GTK_CONTAINER(leaflet)); | 92 | GList *children = gtk_container_get_children(GTK_CONTAINER(leaflet)); |
89 | 93 | ||
90 | if ((children) && (children->next)) { | 94 | if ((children) && (children->next)) { |
91 | hdy_leaflet_set_visible_child(leaflet, GTK_WIDGET(children->next->data)); | 95 | hdy_leaflet_set_visible_child(leaflet, GTK_WIDGET(children->next->data)); |
92 | } | 96 | } |
93 | } | ||
94 | |||
95 | static void | ||
96 | handle_back_button_click(UNUSED GtkButton* button, | ||
97 | gpointer user_data) | ||
98 | { | ||
99 | HdyLeaflet* leaflet = HDY_LEAFLET(user_data); | ||
100 | |||
101 | GList* children = gtk_container_get_children(GTK_CONTAINER(leaflet)); | ||
102 | |||
103 | if (children) { | ||
104 | hdy_leaflet_set_visible_child(leaflet, GTK_WIDGET(children->data)); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | static void | ||
109 | handle_send_text_buffer_changed(GtkTextBuffer *buffer, | ||
110 | gpointer user_data) | ||
111 | { | ||
112 | GtkImage *symbol = GTK_IMAGE(user_data); | ||
113 | |||
114 | GtkTextIter start, end; | ||
115 | gtk_text_buffer_get_start_iter(buffer, &start); | ||
116 | gtk_text_buffer_get_end_iter(buffer, &end); | ||
117 | |||
118 | const gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, TRUE); | ||
119 | |||
120 | gtk_image_set_from_icon_name( | ||
121 | symbol, | ||
122 | 0 < g_utf8_strlen(text, 1)? | ||
123 | "mail-send-symbolic" : | ||
124 | "audio-input-microphone-symbolic", | ||
125 | GTK_ICON_SIZE_BUTTON | ||
126 | ); | ||
127 | } | ||
128 | |||
129 | static void | ||
130 | handle_send_record_button_click(GtkButton *button, | ||
131 | gpointer user_data) | ||
132 | { | ||
133 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
134 | |||
135 | GtkTextBuffer *buffer = gtk_text_view_get_buffer( | ||
136 | app->ui.messenger.send_text_view | ||
137 | ); | ||
138 | |||
139 | GtkTextIter start, end; | ||
140 | gtk_text_buffer_get_start_iter(buffer, &start); | ||
141 | gtk_text_buffer_get_end_iter(buffer, &end); | ||
142 | |||
143 | const gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, TRUE); | ||
144 | |||
145 | if (0 < g_utf8_strlen(text, 1)) | ||
146 | { | ||
147 | struct GNUNET_CHAT_Context *context = g_hash_table_lookup( | ||
148 | app->ui.bindings, button | ||
149 | ); | ||
150 | |||
151 | if (context) | ||
152 | GNUNET_CHAT_context_send_text(context, text); | ||
153 | } | ||
154 | else | ||
155 | { | ||
156 | // TODO: record audio and attach as file? | ||
157 | } | ||
158 | |||
159 | gtk_text_buffer_delete(buffer, &start, &end); | ||
160 | } | ||
161 | 97 | ||
162 | static gboolean | 98 | GtkWidget *entry = GTK_WIDGET( |
163 | handle_send_text_key_press (GtkWidget *widget, | 99 | gtk_container_get_children(GTK_CONTAINER(row))->data |
164 | GdkEventKey *event, | ||
165 | gpointer user_data) | ||
166 | { | ||
167 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
168 | |||
169 | if ((app->ui.mobile) || | ||
170 | (event->state & GDK_SHIFT_MASK) || | ||
171 | ((event->keyval != GDK_KEY_Return) && | ||
172 | (event->keyval != GDK_KEY_KP_Enter))) | ||
173 | return FALSE; | ||
174 | |||
175 | GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (widget)); | ||
176 | |||
177 | GtkTextIter start, end; | ||
178 | gtk_text_buffer_get_start_iter(buffer, &start); | ||
179 | gtk_text_buffer_get_end_iter(buffer, &end); | ||
180 | |||
181 | const gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, TRUE); | ||
182 | |||
183 | if (0 == g_utf8_strlen(text, 1)) | ||
184 | return FALSE; | ||
185 | |||
186 | struct GNUNET_CHAT_Context *context = g_hash_table_lookup( | ||
187 | app->ui.bindings, widget | ||
188 | ); | 100 | ); |
189 | 101 | ||
190 | if (context) | 102 | gtk_stack_set_visible_child_name(stack, gtk_widget_get_name(entry)); |
191 | GNUNET_CHAT_context_send_text(context, text); | ||
192 | |||
193 | gtk_text_buffer_delete(buffer, &start, &end); | ||
194 | return TRUE; | ||
195 | } | 103 | } |
196 | 104 | ||
197 | static void | 105 | static void |
@@ -207,6 +115,8 @@ void | |||
207 | ui_messenger_init(MESSENGER_Application *app, | 115 | ui_messenger_init(MESSENGER_Application *app, |
208 | UI_MESSENGER_Handle *handle) | 116 | UI_MESSENGER_Handle *handle) |
209 | { | 117 | { |
118 | handle->chat_entries = g_list_alloc(); | ||
119 | |||
210 | GtkBuilder* builder = gtk_builder_new_from_file("resources/ui/messenger.ui"); | 120 | GtkBuilder* builder = gtk_builder_new_from_file("resources/ui/messenger.ui"); |
211 | 121 | ||
212 | handle->main_window = GTK_APPLICATION_WINDOW( | 122 | handle->main_window = GTK_APPLICATION_WINDOW( |
@@ -257,25 +167,6 @@ ui_messenger_init(MESSENGER_Application *app, | |||
257 | G_BINDING_INVERT_BOOLEAN | 167 | G_BINDING_INVERT_BOOLEAN |
258 | ); | 168 | ); |
259 | 169 | ||
260 | handle->back_button = GTK_BUTTON( | ||
261 | gtk_builder_get_object(builder, "back_button") | ||
262 | ); | ||
263 | |||
264 | g_object_bind_property( | ||
265 | handle->leaflet_chat, | ||
266 | "folded", | ||
267 | handle->back_button, | ||
268 | "visible", | ||
269 | G_BINDING_SYNC_CREATE | ||
270 | ); | ||
271 | |||
272 | g_signal_connect( | ||
273 | handle->back_button, | ||
274 | "clicked", | ||
275 | G_CALLBACK(handle_back_button_click), | ||
276 | handle->leaflet_chat | ||
277 | ); | ||
278 | |||
279 | handle->profile_avatar = HDY_AVATAR( | 170 | handle->profile_avatar = HDY_AVATAR( |
280 | gtk_builder_get_object(builder, "profile_avatar") | 171 | gtk_builder_get_object(builder, "profile_avatar") |
281 | ); | 172 | ); |
@@ -372,26 +263,11 @@ ui_messenger_init(MESSENGER_Application *app, | |||
372 | handle->chats_listbox, | 263 | handle->chats_listbox, |
373 | "row-activated", | 264 | "row-activated", |
374 | G_CALLBACK(handle_chats_listbox_row_activated), | 265 | G_CALLBACK(handle_chats_listbox_row_activated), |
375 | handle->leaflet_chat | 266 | handle |
376 | ); | ||
377 | |||
378 | handle->chat_title = GTK_LABEL( | ||
379 | gtk_builder_get_object(builder, "chat_title") | ||
380 | ); | ||
381 | |||
382 | handle->chat_subtitle = GTK_LABEL( | ||
383 | gtk_builder_get_object(builder, "chat_subtitle") | ||
384 | ); | ||
385 | |||
386 | handle->chat_details_button = GTK_BUTTON( | ||
387 | gtk_builder_get_object(builder, "chat_details_button") | ||
388 | ); | 267 | ); |
389 | 268 | ||
390 | g_signal_connect( | 269 | handle->chats_stack = GTK_STACK( |
391 | handle->chat_details_button, | 270 | gtk_builder_get_object(builder, "chats_stack") |
392 | "clicked", | ||
393 | G_CALLBACK(handle_flap_via_button_click), | ||
394 | handle->flap_chat_details | ||
395 | ); | 271 | ); |
396 | 272 | ||
397 | handle->hide_chat_details_button = GTK_BUTTON( | 273 | handle->hide_chat_details_button = GTK_BUTTON( |
@@ -405,55 +281,6 @@ ui_messenger_init(MESSENGER_Application *app, | |||
405 | handle->flap_chat_details | 281 | handle->flap_chat_details |
406 | ); | 282 | ); |
407 | 283 | ||
408 | handle->messages_listbox = GTK_LIST_BOX( | ||
409 | gtk_builder_get_object(builder, "messages_listbox") | ||
410 | ); | ||
411 | |||
412 | handle->attach_file_button = GTK_BUTTON( | ||
413 | gtk_builder_get_object(builder, "attach_file_button") | ||
414 | ); | ||
415 | |||
416 | handle->send_text_view = GTK_TEXT_VIEW( | ||
417 | gtk_builder_get_object(builder, "send_text_view") | ||
418 | ); | ||
419 | |||
420 | handle->emoji_button = GTK_BUTTON( | ||
421 | gtk_builder_get_object(builder, "emoji_button") | ||
422 | ); | ||
423 | |||
424 | handle->send_record_button = GTK_BUTTON( | ||
425 | gtk_builder_get_object(builder, "send_record_button") | ||
426 | ); | ||
427 | |||
428 | handle->send_record_symbol = GTK_IMAGE( | ||
429 | gtk_builder_get_object(builder, "send_record_symbol") | ||
430 | ); | ||
431 | |||
432 | GtkTextBuffer *send_text_buffer = gtk_text_view_get_buffer( | ||
433 | handle->send_text_view | ||
434 | ); | ||
435 | |||
436 | g_signal_connect( | ||
437 | send_text_buffer, | ||
438 | "changed", | ||
439 | G_CALLBACK(handle_send_text_buffer_changed), | ||
440 | handle->send_record_symbol | ||
441 | ); | ||
442 | |||
443 | g_signal_connect( | ||
444 | handle->send_record_button, | ||
445 | "clicked", | ||
446 | G_CALLBACK(handle_send_record_button_click), | ||
447 | app | ||
448 | ); | ||
449 | |||
450 | g_signal_connect( | ||
451 | handle->send_text_view, | ||
452 | "key-press-event", | ||
453 | G_CALLBACK(handle_send_text_key_press), | ||
454 | app | ||
455 | ); | ||
456 | |||
457 | gtk_widget_show(GTK_WIDGET(handle->main_window)); | 284 | gtk_widget_show(GTK_WIDGET(handle->main_window)); |
458 | 285 | ||
459 | g_signal_connect( | 286 | g_signal_connect( |
@@ -464,8 +291,16 @@ ui_messenger_init(MESSENGER_Application *app, | |||
464 | ); | 291 | ); |
465 | } | 292 | } |
466 | 293 | ||
294 | static void | ||
295 | _free_ui_chat_entry (gpointer user_data) | ||
296 | { | ||
297 | UI_CHAT_ENTRY_Handle* handle = (UI_CHAT_ENTRY_Handle*) user_data; | ||
298 | |||
299 | ui_chat_entry_delete(handle); | ||
300 | } | ||
301 | |||
467 | void | 302 | void |
468 | ui_messenger_run(MESSENGER_Application *app) | 303 | ui_messenger_cleanup(UI_MESSENGER_Handle *handle) |
469 | { | 304 | { |
470 | ui_messenger_init(app, &(app->ui.messenger)); | 305 | g_list_free_full(handle->chat_entries, _free_ui_chat_entry); |
471 | } | 306 | } |