[feat] show user notification when somebody sends a file

This commit is contained in:
Julian Sparber
2017-09-04 17:56:47 +02:00
parent 73d577d4a6
commit cdb4786579
9 changed files with 162 additions and 9 deletions

View File

@@ -13,6 +13,7 @@
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include "get.h" #include "get.h"
#include "teleportapp.h"
static int port; static int port;
static SoupServer *server; static SoupServer *server;
@@ -229,10 +230,12 @@ do_get_response_json (SoupServer *server, SoupMessage *msg, const char *path)
soup_message_set_status (msg, SOUP_STATUS_OK); soup_message_set_status (msg, SOUP_STATUS_OK);
} }
static void handle_incoming_file(const char * hash, const char * name, const char * size, const char * origin) { 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:%s with title: %s\n", origin, size, name); g_print("Got a new file form %s with size:%d with title: %s\n", origin, size, filename);
create_user_notification(filename, size, origin);
//If the user accepts the file //If the user accepts the file
do_downloading(g_strdup_printf("http://%s:%d/transfer/%s", origin, port, hash), name); do_downloading(g_strdup_printf("http://%s:%d/transfer/%s", origin, port, hash), filename);
} }
static void static void
@@ -270,7 +273,7 @@ server_callback (SoupServer *server, SoupMessage *msg,
if (token != NULL && size != NULL && file_name != NULL) { if (token != NULL && size != NULL && file_name != NULL) {
g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name); g_print("Token: %s, Size: %s, Name: %s\n", token, size, file_name);
response = g_string_new("{\"error\": false, \"message\": \"Success\"}"); response = g_string_new("{\"error\": false, \"message\": \"Success\"}");
handle_incoming_file(token, file_name, size, origin_addr); handle_incoming_file(token, file_name, g_ascii_strtoull (size, NULL, 0), origin_addr);
} }
else else
response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}"); response = g_string_new("{\"error\": true, \"message\": \"query malformed\"}");
@@ -279,7 +282,6 @@ server_callback (SoupServer *server, SoupMessage *msg,
g_print("No query passed"); g_print("No query passed");
response = g_string_new("{\"error\": true, \"message\": \"No query passed\"}"); response = g_string_new("{\"error\": true, \"message\": \"No query passed\"}");
} }
} }
if (g_strcmp0(path, "/") == 0) { if (g_strcmp0(path, "/") == 0) {

View File

@@ -22,8 +22,7 @@ struct _TeleportApp {
G_DEFINE_TYPE (TeleportApp, teleport_app, GTK_TYPE_APPLICATION); G_DEFINE_TYPE (TeleportApp, teleport_app, GTK_TYPE_APPLICATION);
static void create_user_notification (const char *file_name, const int file_size, const char *origin_device) { void create_user_notification (const char *file_name, const int file_size, const char *origin_device) {
g_print("Create Notification");
GNotification *notification = g_notification_new ("Teleport"); GNotification *notification = g_notification_new ("Teleport");
g_notification_set_body (notification, g_strdup_printf("%s is sending %s (%d Byte)", origin_device, file_name, file_size)); g_notification_set_body (notification, g_strdup_printf("%s is sending %s (%d Byte)", origin_device, file_name, file_size));
GIcon *icon = g_themed_icon_new ("dialog-information"); GIcon *icon = g_themed_icon_new ("dialog-information");
@@ -137,8 +136,6 @@ teleport_app_class_init (TeleportAppClass *class)
G_TYPE_NONE /* return_type */, G_TYPE_NONE /* return_type */,
1, 1,
G_TYPE_STRING); G_TYPE_STRING);
} }
TeleportApp * TeleportApp *

View File

@@ -9,5 +9,8 @@ G_DECLARE_FINAL_TYPE (TeleportApp, teleport_app, TELEPORT, APP, GtkApplication)
TeleportApp *teleport_app_new (void); TeleportApp *teleport_app_new (void);
extern void create_user_notification (const char *,
const int,
const char *);
#endif /* __TELEPORTAPP_H */ #endif /* __TELEPORTAPP_H */

BIN
src/test_download/email.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 529 KiB

BIN
src/test_download/file Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -0,0 +1,151 @@
#include <gtk/gtk.h>
#include "teleportapp.h"
#include "teleportpeer.h"
#include "teleportappwin.h"
#include "browser.h"
#include "publish.h"
#include "server.h"
enum {
NOTIFY_USER, NOTIFY_FINISED, N_SIGNALS
};
static TeleportAppWindow *win;
static GApplication *application;
static gint signalIds [N_SIGNALS];
struct _TeleportApp {
GtkApplication parent;
};
G_DEFINE_TYPE (TeleportApp, teleport_app, GTK_TYPE_APPLICATION);
static void create_user_notification (const char *file_name, const int file_size, const char *origin_device) {
g_print("Create Notification");
GNotification *notification = g_notification_new ("Teleport");
g_notification_set_body (notification, g_strdup_printf("%s is sending %s (%d Byte)", origin_device, file_name, file_size));
GIcon *icon = g_themed_icon_new ("dialog-information");
g_notification_set_icon (notification, icon);
g_notification_add_button (notification, "Decline", "app.reply-5-minutes");
g_notification_add_button (notification, "Save", "app.reply-5-minutes");
g_application_send_notification (application, NULL, notification);
g_object_unref (icon);
g_object_unref (notification);
}
static void create_finished_notification (const char *file_name, const int file_size, const char *origin_device) {
GNotification *notification = g_notification_new ("Teleport");
g_notification_set_body (notification, g_strdup_printf("Transfer %s from %s is complete)", file_name, origin_device));
GIcon *icon = g_themed_icon_new ("dialog-information");
g_notification_set_icon (notification, icon);
g_notification_add_button (notification, "Open", "app.reply-5-minutes");
g_application_send_notification (application, NULL, notification);
g_object_unref (icon);
g_object_unref (notification);
}
gboolean mainLoopAddPeerCallback (gpointer peer) {
//g_print("new New device name is %p\n", ((Peer *)peer));
//g_print("new New device name is %s\n", ((Peer *)peer)->name);
update_remote_device_list(win, (Peer *) peer);
return G_SOURCE_REMOVE;
}
gboolean mainLoopRemovePeerCallback (gpointer peer) {
update_remote_device_list_remove(win, (Peer *) peer);
return G_SOURCE_REMOVE;
}
void callback_add_peer (GObject *instance, Peer *peer, TeleportAppWindow *win ) {
g_idle_add(mainLoopAddPeerCallback, peer);
}
void callback_remove_peer (GObject *instance, Peer *peer, TeleportAppWindow *win ) {
g_idle_add(mainLoopRemovePeerCallback, peer);
}
void callback_notify_user (GObject *instance, char *name, TeleportAppWindow *win ) {
create_user_notification("icon.png", 2000, "Mark's laptop");
}
static void
teleport_app_init (TeleportApp *app) {
}
static void
teleport_app_activate (GApplication *app) {
//TeleportAppWindow *win;
application = app;
TeleportPeer *peerList = g_object_new (TELEPORT_TYPE_PEER, NULL);
win = teleport_app_window_new (TELEPORT_APP (app));
gtk_window_present (GTK_WINDOW (win));
g_signal_connect (peerList, "addpeer", (GCallback)callback_add_peer, win);
g_signal_connect (peerList, "removepeer", (GCallback)callback_remove_peer, win);
g_signal_connect (app, "notify_user", (GCallback)callback_notify_user, win);
/*teleport_peer_add_peer(peerList, "julian", "192.168.0.1", 3000);
g_print("Data: %s\n", teleport_peer_get_name(peerList, 0, NULL));
g_print("Data: %s\n", teleport_peer_get_ip(peerList, 0, NULL));
g_print("Data: %d\n", teleport_peer_get_port(peerList, 0, NULL));
*/
create_user_notification ("sdfdsff", 2000, "sdfdsfdsf");
run_http_server();
run_avahi_publish_service("Angela's (self)");
run_avahi_service(peerList);
}
static void
teleport_app_open (GApplication *app,
GFile **files,
gint n_files,
const gchar *hint)
{
GList *windows;
int i;
windows = gtk_application_get_windows (GTK_APPLICATION (app));
if (windows)
win = TELEPORT_APP_WINDOW (windows->data);
else
win = teleport_app_window_new (TELEPORT_APP (app));
for (i = 0; i < n_files; i++)
teleport_app_window_open (win, files[i]);
gtk_window_present (GTK_WINDOW (win));
}
static void
teleport_app_class_init (TeleportAppClass *class)
{
G_APPLICATION_CLASS (class)->activate = teleport_app_activate;
G_APPLICATION_CLASS (class)->open = teleport_app_open;
signalIds[NOTIFY_USER] = g_signal_new ("notify_user",
G_TYPE_OBJECT,
G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
0,
NULL /* accumulator */,
NULL /* accumulator data */,
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
}
TeleportApp *
teleport_app_new (void)
{
return g_object_new (TELEPORT_APP_TYPE,
"application-id", "org.gtk.teleportapp",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
}