diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-14 23:01:03 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-14 23:01:03 +0100 |
commit | 1c66a5e7ba39f6dde7afad4bdc4be067bc6a7da3 (patch) | |
tree | f0a6235cb6f5a6e21aee9b96c5fe20a005c885ce | |
parent | 1049e99951c69db162f66eb6288450c2eacbbe15 (diff) | |
download | messenger-gtk-1c66a5e7ba39f6dde7afad4bdc4be067bc6a7da3.tar.gz messenger-gtk-1c66a5e7ba39f6dde7afad4bdc4be067bc6a7da3.zip |
Implemented file downloading, opening and updating visual status or preview
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | src/event.c | 20 | ||||
-rw-r--r-- | src/file.c | 36 | ||||
-rw-r--r-- | src/file.h | 6 | ||||
-rw-r--r-- | src/ui/message.c | 135 | ||||
-rw-r--r-- | src/ui/message.h | 1 |
5 files changed, 185 insertions, 13 deletions
diff --git a/src/event.c b/src/event.c index e1e2601..d80e563 100644 --- a/src/event.c +++ b/src/event.c | |||
@@ -25,6 +25,7 @@ | |||
25 | #include "event.h" | 25 | #include "event.h" |
26 | 26 | ||
27 | #include "contact.h" | 27 | #include "contact.h" |
28 | #include "file.h" | ||
28 | 29 | ||
29 | #include "ui/chat_entry.h" | 30 | #include "ui/chat_entry.h" |
30 | #include "ui/contact_entry.h" | 31 | #include "ui/contact_entry.h" |
@@ -236,7 +237,7 @@ event_joining_contact(MESSENGER_Application *app, | |||
236 | return; | 237 | return; |
237 | 238 | ||
238 | UI_MESSAGE_Handle *message = ui_message_new(app, UI_MESSAGE_STATUS); | 239 | UI_MESSAGE_Handle *message = ui_message_new(app, UI_MESSAGE_STATUS); |
239 | ui_message_update(message, msg); | 240 | ui_message_update(message, app, msg); |
240 | 241 | ||
241 | struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( | 242 | struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( |
242 | msg | 243 | msg |
@@ -304,7 +305,7 @@ _event_invitation_accept_click(UNUSED GtkButton *button, | |||
304 | } | 305 | } |
305 | 306 | ||
306 | void | 307 | void |
307 | event_invitation(UNUSED MESSENGER_Application *app, | 308 | event_invitation(MESSENGER_Application *app, |
308 | struct GNUNET_CHAT_Context *context, | 309 | struct GNUNET_CHAT_Context *context, |
309 | const struct GNUNET_CHAT_Message *msg) | 310 | const struct GNUNET_CHAT_Message *msg) |
310 | { | 311 | { |
@@ -320,7 +321,7 @@ event_invitation(UNUSED MESSENGER_Application *app, | |||
320 | return; | 321 | return; |
321 | 322 | ||
322 | UI_MESSAGE_Handle *message = ui_message_new(app, UI_MESSAGE_STATUS); | 323 | UI_MESSAGE_Handle *message = ui_message_new(app, UI_MESSAGE_STATUS); |
323 | ui_message_update(message, msg); | 324 | ui_message_update(message, app, msg); |
324 | 325 | ||
325 | const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( | 326 | const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( |
326 | msg | 327 | msg |
@@ -357,7 +358,7 @@ event_invitation(UNUSED MESSENGER_Application *app, | |||
357 | } | 358 | } |
358 | 359 | ||
359 | void | 360 | void |
360 | event_receive_message(UNUSED MESSENGER_Application *app, | 361 | event_receive_message(MESSENGER_Application *app, |
361 | struct GNUNET_CHAT_Context *context, | 362 | struct GNUNET_CHAT_Context *context, |
362 | const struct GNUNET_CHAT_Message *msg) | 363 | const struct GNUNET_CHAT_Message *msg) |
363 | { | 364 | { |
@@ -373,7 +374,16 @@ event_receive_message(UNUSED MESSENGER_Application *app, | |||
373 | ); | 374 | ); |
374 | 375 | ||
375 | UI_MESSAGE_Handle *message = ui_message_new(app, type); | 376 | UI_MESSAGE_Handle *message = ui_message_new(app, type); |
376 | ui_message_update(message, msg); | 377 | |
378 | struct GNUNET_CHAT_File *file = GNUNET_CHAT_message_get_file(msg); | ||
379 | |||
380 | if (file) | ||
381 | { | ||
382 | file_create_info(file); | ||
383 | file_add_ui_message_to_info(file, message); | ||
384 | } | ||
385 | |||
386 | ui_message_update(message, app, msg); | ||
377 | 387 | ||
378 | const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( | 388 | const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender( |
379 | msg | 389 | msg |
@@ -70,5 +70,39 @@ file_update_upload_info(const struct GNUNET_CHAT_File *file, | |||
70 | uint64_t completed, | 70 | uint64_t completed, |
71 | uint64_t size) | 71 | uint64_t size) |
72 | { | 72 | { |
73 | // | 73 | MESSENGER_FileInfo* info = GNUNET_CHAT_file_get_user_pointer(file); |
74 | |||
75 | if (!info) | ||
76 | return; | ||
77 | |||
78 | // TODO | ||
79 | } | ||
80 | |||
81 | void | ||
82 | file_update_download_info(const struct GNUNET_CHAT_File *file, | ||
83 | MESSENGER_Application *app, | ||
84 | uint64_t completed, | ||
85 | uint64_t size) | ||
86 | { | ||
87 | MESSENGER_FileInfo* info = GNUNET_CHAT_file_get_user_pointer(file); | ||
88 | |||
89 | if (!info) | ||
90 | return; | ||
91 | |||
92 | GList *list = info->file_messages; | ||
93 | |||
94 | while (list) | ||
95 | { | ||
96 | UI_MESSAGE_Handle *message = (UI_MESSAGE_Handle*) list->data; | ||
97 | |||
98 | gtk_progress_bar_set_fraction( | ||
99 | message->file_progress_bar, | ||
100 | 1.0 * completed / size | ||
101 | ); | ||
102 | |||
103 | if (completed >= size) | ||
104 | ui_message_update(message, app, NULL); | ||
105 | |||
106 | list = list->next; | ||
107 | } | ||
74 | } | 108 | } |
@@ -48,4 +48,10 @@ file_update_upload_info(const struct GNUNET_CHAT_File *file, | |||
48 | uint64_t completed, | 48 | uint64_t completed, |
49 | uint64_t size); | 49 | uint64_t size); |
50 | 50 | ||
51 | void | ||
52 | file_update_download_info(const struct GNUNET_CHAT_File *file, | ||
53 | MESSENGER_Application *app, | ||
54 | uint64_t completed, | ||
55 | uint64_t size); | ||
56 | |||
51 | #endif /* FILE_H_ */ | 57 | #endif /* FILE_H_ */ |
diff --git a/src/ui/message.c b/src/ui/message.c index 4aa7a85..9c93502 100644 --- a/src/ui/message.c +++ b/src/ui/message.c | |||
@@ -27,6 +27,85 @@ | |||
27 | #include <gnunet/gnunet_chat_lib.h> | 27 | #include <gnunet/gnunet_chat_lib.h> |
28 | 28 | ||
29 | #include "../application.h" | 29 | #include "../application.h" |
30 | #include "../file.h" | ||
31 | |||
32 | static void | ||
33 | handle_downloading_file(void *cls, | ||
34 | const struct GNUNET_CHAT_File *file, | ||
35 | uint64_t completed, | ||
36 | uint64_t size) | ||
37 | { | ||
38 | MESSENGER_Application *app = (MESSENGER_Application*) cls; | ||
39 | |||
40 | if (!app) | ||
41 | return; | ||
42 | |||
43 | file_update_download_info(file, app, completed, size); | ||
44 | } | ||
45 | |||
46 | static void | ||
47 | handle_file_button_click(GtkButton *button, | ||
48 | gpointer user_data) | ||
49 | { | ||
50 | MESSENGER_Application *app = (MESSENGER_Application*) user_data; | ||
51 | |||
52 | UI_MESSAGE_Handle* handle = g_hash_table_lookup( | ||
53 | app->ui.bindings, button | ||
54 | ); | ||
55 | |||
56 | if (!handle) | ||
57 | return; | ||
58 | |||
59 | struct GNUNET_CHAT_File *file = g_hash_table_lookup( | ||
60 | app->ui.bindings, handle->file_progress_bar | ||
61 | ); | ||
62 | |||
63 | if (!file) | ||
64 | return; | ||
65 | |||
66 | uint64_t size = GNUNET_CHAT_file_get_size(file); | ||
67 | |||
68 | if (size <= 0) | ||
69 | return; | ||
70 | |||
71 | uint64_t local_size = GNUNET_CHAT_file_get_local_size(file); | ||
72 | |||
73 | if (GNUNET_YES == GNUNET_CHAT_file_is_downloading(file)) | ||
74 | { | ||
75 | GNUNET_CHAT_file_stop_download(file); | ||
76 | |||
77 | gtk_image_set_from_icon_name( | ||
78 | handle->file_status_image, | ||
79 | "folder-download-symbolic", | ||
80 | GTK_ICON_SIZE_BUTTON | ||
81 | ); | ||
82 | } | ||
83 | else if (local_size < size) | ||
84 | { | ||
85 | GNUNET_CHAT_file_start_download(file, handle_downloading_file, app); | ||
86 | |||
87 | gtk_image_set_from_icon_name( | ||
88 | handle->file_status_image, | ||
89 | "process-stop-symbolic", | ||
90 | GTK_ICON_SIZE_BUTTON | ||
91 | ); | ||
92 | } | ||
93 | else if (size > 0) | ||
94 | { | ||
95 | const gchar *preview = GNUNET_CHAT_file_open_preview(file); | ||
96 | |||
97 | if (!preview) | ||
98 | return; | ||
99 | |||
100 | GString* uri = g_string_new("file://"); | ||
101 | g_string_append(uri, preview); | ||
102 | |||
103 | if (!g_app_info_launch_default_for_uri(uri->str, NULL, NULL)) | ||
104 | GNUNET_CHAT_file_close_preview(file); | ||
105 | |||
106 | g_string_free(uri, TRUE); | ||
107 | } | ||
108 | } | ||
30 | 109 | ||
31 | static int | 110 | static int |
32 | handle_message_redraw_animation(gpointer user_data) | 111 | handle_message_redraw_animation(gpointer user_data) |
@@ -267,10 +346,19 @@ ui_message_new(MESSENGER_Application *app, | |||
267 | gtk_builder_get_object(builder, "file_button") | 346 | gtk_builder_get_object(builder, "file_button") |
268 | ); | 347 | ); |
269 | 348 | ||
349 | g_signal_connect( | ||
350 | handle->file_button, | ||
351 | "clicked", | ||
352 | G_CALLBACK(handle_file_button_click), | ||
353 | app | ||
354 | ); | ||
355 | |||
270 | handle->file_status_image = GTK_IMAGE( | 356 | handle->file_status_image = GTK_IMAGE( |
271 | gtk_builder_get_object(builder, "file_status_image") | 357 | gtk_builder_get_object(builder, "file_status_image") |
272 | ); | 358 | ); |
273 | 359 | ||
360 | g_hash_table_insert(app->ui.bindings, handle->file_button, handle); | ||
361 | |||
274 | handle->preview_drawing_area = GTK_DRAWING_AREA( | 362 | handle->preview_drawing_area = GTK_DRAWING_AREA( |
275 | gtk_builder_get_object(builder, "preview_drawing_area") | 363 | gtk_builder_get_object(builder, "preview_drawing_area") |
276 | ); | 364 | ); |
@@ -308,15 +396,37 @@ ui_message_new(MESSENGER_Application *app, | |||
308 | 396 | ||
309 | void | 397 | void |
310 | ui_message_update(UI_MESSAGE_Handle *handle, | 398 | ui_message_update(UI_MESSAGE_Handle *handle, |
399 | MESSENGER_Application *app, | ||
311 | const struct GNUNET_CHAT_Message *msg) | 400 | const struct GNUNET_CHAT_Message *msg) |
312 | { | 401 | { |
313 | struct GNUNET_CHAT_File *file = GNUNET_CHAT_message_get_file(msg); | 402 | struct GNUNET_CHAT_File *file = NULL; |
403 | |||
404 | if (msg) | ||
405 | file = GNUNET_CHAT_message_get_file(msg); | ||
406 | else | ||
407 | file = g_hash_table_lookup(app->ui.bindings, handle->message_box); | ||
314 | 408 | ||
315 | if (!file) | 409 | if (!file) |
316 | return; | 410 | return; |
317 | 411 | ||
318 | if (GNUNET_YES != GNUNET_CHAT_file_is_local(file)) | 412 | if (g_hash_table_contains(app->ui.bindings, handle->message_box)) |
413 | g_hash_table_replace(app->ui.bindings, handle->message_box, file); | ||
414 | else | ||
415 | g_hash_table_insert(app->ui.bindings, handle->message_box, file); | ||
416 | |||
417 | uint64_t size = GNUNET_CHAT_file_get_size(file); | ||
418 | uint64_t local_size = GNUNET_CHAT_file_get_local_size(file); | ||
419 | |||
420 | if ((size <= 0) || (size > local_size)) | ||
421 | { | ||
422 | gtk_image_set_from_icon_name( | ||
423 | handle->file_status_image, | ||
424 | "folder-download-symbolic", | ||
425 | GTK_ICON_SIZE_BUTTON | ||
426 | ); | ||
427 | |||
319 | goto file_content; | 428 | goto file_content; |
429 | } | ||
320 | 430 | ||
321 | if (!(handle->preview_drawing_area)) | 431 | if (!(handle->preview_drawing_area)) |
322 | goto file_progress; | 432 | goto file_progress; |
@@ -336,14 +446,14 @@ ui_message_update(UI_MESSAGE_Handle *handle, | |||
336 | if ((handle->preview_animation) || (handle->preview_animation)) | 446 | if ((handle->preview_animation) || (handle->preview_animation)) |
337 | { | 447 | { |
338 | gtk_widget_set_size_request( | 448 | gtk_widget_set_size_request( |
339 | GTK_WIDGET(handle->preview_drawing_area), | 449 | GTK_WIDGET(handle->preview_drawing_area), |
340 | 250, | 450 | 250, |
341 | -1 | 451 | -1 |
342 | ); | 452 | ); |
343 | 453 | ||
344 | gtk_stack_set_visible_child( | 454 | gtk_stack_set_visible_child( |
345 | handle->content_stack, | 455 | handle->content_stack, |
346 | GTK_WIDGET(handle->preview_drawing_area) | 456 | GTK_WIDGET(handle->preview_drawing_area) |
347 | ); | 457 | ); |
348 | 458 | ||
349 | gtk_widget_queue_draw(GTK_WIDGET(handle->preview_drawing_area)); | 459 | gtk_widget_queue_draw(GTK_WIDGET(handle->preview_drawing_area)); |
@@ -355,6 +465,12 @@ ui_message_update(UI_MESSAGE_Handle *handle, | |||
355 | file_progress: | 465 | file_progress: |
356 | gtk_progress_bar_set_fraction(handle->file_progress_bar, 1.0); | 466 | gtk_progress_bar_set_fraction(handle->file_progress_bar, 1.0); |
357 | 467 | ||
468 | gtk_image_set_from_icon_name( | ||
469 | handle->file_status_image, | ||
470 | "document-open-symbolic", | ||
471 | GTK_ICON_SIZE_BUTTON | ||
472 | ); | ||
473 | |||
358 | file_content: | 474 | file_content: |
359 | gtk_label_set_text(handle->filename_label, GNUNET_CHAT_file_get_name(file)); | 475 | gtk_label_set_text(handle->filename_label, GNUNET_CHAT_file_get_name(file)); |
360 | 476 | ||
@@ -364,6 +480,11 @@ file_content: | |||
364 | ); | 480 | ); |
365 | 481 | ||
366 | gtk_revealer_set_reveal_child(handle->file_revealer, TRUE); | 482 | gtk_revealer_set_reveal_child(handle->file_revealer, TRUE); |
483 | |||
484 | if (g_hash_table_contains(app->ui.bindings, handle->file_progress_bar)) | ||
485 | g_hash_table_replace(app->ui.bindings, handle->file_progress_bar, file); | ||
486 | else | ||
487 | g_hash_table_insert(app->ui.bindings, handle->file_progress_bar, file); | ||
367 | } | 488 | } |
368 | 489 | ||
369 | void | 490 | void |
diff --git a/src/ui/message.h b/src/ui/message.h index 0463732..b3b7353 100644 --- a/src/ui/message.h +++ b/src/ui/message.h | |||
@@ -87,6 +87,7 @@ ui_message_new(MESSENGER_Application *app, | |||
87 | 87 | ||
88 | void | 88 | void |
89 | ui_message_update(UI_MESSAGE_Handle *handle, | 89 | ui_message_update(UI_MESSAGE_Handle *handle, |
90 | MESSENGER_Application *app, | ||
90 | const struct GNUNET_CHAT_Message *message); | 91 | const struct GNUNET_CHAT_Message *message); |
91 | 92 | ||
92 | void | 93 | void |