From 88283a02cf098db8698d537ba0f020d8e9dadf94 Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Tue, 5 Sep 2017 12:39:41 +0200 Subject: [PATCH] [feat] add notification for accepting a file --- src/get.c | 9 +++--- src/get.h | 2 +- src/server.c | 22 ++++++++++++--- src/teleportapp.c | 71 ++++++++++++++++++++++++++++++++--------------- src/teleportapp.h | 2 +- 5 files changed, 73 insertions(+), 33 deletions(-) diff --git a/src/get.c b/src/get.c index 73a2c71..a392ef8 100644 --- a/src/get.c +++ b/src/get.c @@ -95,17 +95,18 @@ get (char *url, const gchar *output_file_path) return 0; } -int do_client_notify (char * url) +int do_client_notify (char *url) { get (g_strdup(url), NULL); g_print("Offering selected file to other machine.\n"); return 0; } -int do_downloading (char * url, char * file_name) +int +do_downloading (const char *originDevice, const char *url, const char *filename) { - g_print("Downloading %s to %s\n", url, g_uri_escape_string(file_name, NULL, TRUE)); - get (g_strdup(url), g_strdup_printf("./test_download/%s", g_uri_escape_string(file_name, NULL, TRUE))); + g_print("Downloading %s to %s\n", url, g_uri_escape_string(filename, NULL, TRUE)); + get (g_strdup(url), g_strdup_printf("./test_download/%s", g_uri_escape_string(filename, NULL, TRUE))); return 0; } diff --git a/src/get.h b/src/get.h index 9514bb8..598e459 100644 --- a/src/get.h +++ b/src/get.h @@ -2,7 +2,7 @@ #define __GET_H -extern int do_downloading(char *, const char *); +extern int do_downloading(const char *, const char *, const char *); extern int do_client_notify(char *); #endif /* __GET_H */ diff --git a/src/server.c b/src/server.c index 511e534..881d049 100644 --- a/src/server.c +++ b/src/server.c @@ -233,10 +233,24 @@ do_get_response_json (SoupServer *server, SoupMessage *msg, const char *path) static void handle_incoming_file(const char *hash, const char *filename, const int size, const char *origin) { g_print("Got a new file form %s with size:%d with title: %s\n", origin, size, filename); - //char* balance[2] = {"First", "Secound"}; - //create_user_notification(filename, size, origin, balance); - //If the user accepts the file - do_downloading(g_strdup_printf("http://%s:%d/transfer/%s", origin, port, hash), filename); + GVariantBuilder *builder; + GVariant *value; + + builder = g_variant_builder_new (G_VARIANT_TYPE ("as")); + g_variant_builder_add (builder, "s", origin); + g_variant_builder_add (builder, + "s", + g_strdup_printf("http://%s:%d/transfer/%s", + origin, + port, + hash)), + g_variant_builder_add (builder, "s", filename); + value = g_variant_new ("as", builder); + g_variant_builder_unref (builder); + + // create_user_notification startes the download + // if the user wants to save the file + create_user_notification(filename, size, origin, value); } static void diff --git a/src/teleportapp.c b/src/teleportapp.c index 5a22f0e..a498097 100644 --- a/src/teleportapp.c +++ b/src/teleportapp.c @@ -6,11 +6,16 @@ #include "browser.h" #include "publish.h" #include "server.h" +#include "get.h" -void test_callback(GSimpleAction *simple, - GVariant *parameter, - gpointer user_data); +void save_file_callback(GSimpleAction *simple, + GVariant *parameter, + gpointer user_data); + +void do_nothing_callback(GSimpleAction *simple, + GVariant *parameter, + gpointer user_data); enum { NOTIFY_USER, NOTIFY_FINISED, N_SIGNALS @@ -18,8 +23,8 @@ enum { static GActionEntry app_entries[] = { - { "save", test_callback, "s", NULL, NULL }, - { "quit", test_callback, "s", NULL, NULL } + { "save", save_file_callback, "as", NULL, NULL }, + { "decline", do_nothing_callback, "as", NULL, NULL } }; static TeleportAppWindow *win; @@ -33,27 +38,37 @@ struct _TeleportApp { G_DEFINE_TYPE (TeleportApp, teleport_app, GTK_TYPE_APPLICATION); -void test_callback (GSimpleAction *simple, - GVariant *parameter, - gpointer user_data) { - g_print("Working: %s\n", g_variant_get_string (parameter, NULL)); +void save_file_callback (GSimpleAction *simple, + GVariant *parameter, + gpointer user_data) { + do_downloading(g_variant_get_string (g_variant_get_child_value (parameter, 0), NULL), + g_variant_get_string (g_variant_get_child_value (parameter, 1), NULL), + g_variant_get_string (g_variant_get_child_value (parameter, 2), NULL)); } -void create_user_notification (const char *file_name, const int file_size, const char *origin_device, char *target[]) { - GNotification *notification = g_notification_new ("Teleport"); +void do_nothing_callback (GSimpleAction *simple, + GVariant *parameter, + gpointer user_data) { +} + +void create_user_notification (const char *file_name, const int file_size, const char *origin_device, GVariant *target) { + GNotification *notification = g_notification_new ("Teleport"); g_notification_set_body (notification, - g_strdup_printf("%s is sending %s (%s)", - origin_device, - file_name, - g_format_size (file_size))); + g_strdup_printf("%s is sending %s (%s)", + origin_device, + file_name, + g_format_size (file_size))); GIcon *icon = g_themed_icon_new ("dialog-information"); g_notification_set_icon (notification, icon); - g_notification_set_default_action(notification, ""); - g_notification_add_button (notification, "Decline", "app.decline"); - g_notification_add_button_with_target (notification, "Save", "app.save", "s", target); + g_notification_set_default_action_and_target_value (notification, "app.decline", target); + g_notification_add_button_with_target_value (notification, "Decline", "app.decline", target); + g_notification_add_button_with_target_value (notification, "Save", "app.save", target); g_application_send_notification (application, NULL, notification); g_object_unref (icon); g_object_unref (notification); + //the example says I have to unref it but it gives a critival error + //https://developer.gnome.org/glib/stable/gvariant-format-strings.html + //g_variant_unref (value); } static void create_finished_notification (const char *file_name, const int file_size, const char *origin_device) { @@ -107,8 +122,8 @@ teleport_app_activate (GApplication *app) { gtk_window_present (GTK_WINDOW (win)); g_action_map_add_action_entries (G_ACTION_MAP (app), - app_entries, G_N_ELEMENTS (app_entries), - app); + app_entries, G_N_ELEMENTS (app_entries), + app); g_signal_connect (peerList, "addpeer", (GCallback)callback_add_peer, win); g_signal_connect (peerList, "removepeer", (GCallback)callback_remove_peer, win); @@ -119,14 +134,24 @@ teleport_app_activate (GApplication *app) { g_print("Data: %d\n", teleport_peer_get_port(peerList, 0, NULL)); */ - char* balance[2] = {"First", "Secound"}; - create_user_notification ("sdfdsff", 2000, "sdfdsfdsf", balance); + GVariantBuilder *builder; + GVariant *value; + + builder = g_variant_builder_new (G_VARIANT_TYPE ("as")); + g_variant_builder_add (builder, "s", "devicename"); + g_variant_builder_add (builder, "s", "https://downloadlink"); + g_variant_builder_add (builder, "s", "filename"); + value = g_variant_new ("as", builder); + g_variant_builder_unref (builder); + + create_user_notification ("sdfdsff", 2000, "sdfdsfdsf", value); + run_http_server(); run_avahi_publish_service((char *) g_get_host_name()); run_avahi_service(peerList); } -static void + static void teleport_app_open (GApplication *app, GFile **files, gint n_files, diff --git a/src/teleportapp.h b/src/teleportapp.h index 0a34187..a8c2291 100644 --- a/src/teleportapp.h +++ b/src/teleportapp.h @@ -12,6 +12,6 @@ TeleportApp *teleport_app_new (void); extern void create_user_notification (const char *, const int, const char *, - char *[]); + GVariant *); #endif /* __TELEPORTAPP_H */