diff --git a/src/server.c b/src/server.c index 54c247e..7098287 100644 --- a/src/server.c +++ b/src/server.c @@ -13,11 +13,12 @@ #include #include "get.h" +#include "server.h" #include "teleportapp.h" static int port; -static SoupServer *server; -static const char *tls_cert_file, *tls_key_file; +static SoupServer *glob_server; +//static const char *tls_cert_file, *tls_key_file; static int compare_strings (gconstpointer a, gconstpointer b) @@ -151,91 +152,11 @@ do_get (SoupServer *server, SoupMessage *msg, const char *path) soup_message_set_status (msg, SOUP_STATUS_OK); } - static void -do_get_response_json (SoupServer *server, SoupMessage *msg, const char *path) -{ - char *slash; - GStatBuf st; - - printf("paths: %s", path); - if (g_stat (path, &st) == -1) { - if (errno == EPERM) - soup_message_set_status (msg, SOUP_STATUS_FORBIDDEN); - else if (errno == ENOENT) - soup_message_set_status (msg, SOUP_STATUS_NOT_FOUND); - else - soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); - return; - } - - if (g_file_test (path, G_FILE_TEST_IS_DIR)) { - GString *listing; - char *index_path; - - slash = strrchr (path, '/'); - if (!slash || slash[1]) { - char *redir_uri; - - redir_uri = g_strdup_printf ("%s/", soup_message_get_uri (msg)->path); - soup_message_set_redirect (msg, SOUP_STATUS_MOVED_PERMANENTLY, - redir_uri); - g_free (redir_uri); - return; - } - - index_path = g_strdup_printf ("%s/index.html", path); - if (g_stat (path, &st) != -1) { - do_get (server, msg, index_path); - g_free (index_path); - return; - } - g_free (index_path); - - listing = get_directory_listing (path); - soup_message_set_response (msg, "text/html", - SOUP_MEMORY_TAKE, - listing->str, listing->len); - soup_message_set_status (msg, SOUP_STATUS_OK); - g_string_free (listing, FALSE); - return; - } - - if (msg->method == SOUP_METHOD_GET) { - GMappedFile *mapping; - SoupBuffer *buffer; - - mapping = g_mapped_file_new (path, FALSE, NULL); - if (!mapping) { - soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); - return; - } - - buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping), - g_mapped_file_get_length (mapping), - mapping, (GDestroyNotify)g_mapped_file_unref); - soup_message_body_append_buffer (msg->response_body, buffer); - soup_buffer_free (buffer); - } else /* msg->method == SOUP_METHOD_HEAD */ { - char *length; - - /* We could just use the same code for both GET and - * HEAD (soup-message-server-io.c will fix things up). - * But we'll optimize and avoid the extra I/O. - */ - length = g_strdup_printf ("%lu", (gulong)st.st_size); - soup_message_headers_append (msg->response_headers, - "Content-Length", length); - g_free (length); - } - - soup_message_set_status (msg, SOUP_STATUS_OK); -} 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); - GVariantBuilder *builder; GVariant *value; + g_print("Got a new file form %s with size:%d with title: %s\n", origin, size, filename); builder = g_variant_builder_new (G_VARIANT_TYPE ("as")); g_variant_builder_add (builder, "s", origin); g_variant_builder_add (builder, @@ -274,7 +195,7 @@ server_callback (SoupServer *server, SoupMessage *msg, g_print ("%s\n", msg->request_body->data); if (data != NULL) { - g_print("File to share %s\n", data); + g_print("File to share %s\n", (char *)data); file_path = g_strdup(data); response = g_string_new("{\"error\": false, \"message\": \"Success\"}"); } @@ -325,22 +246,14 @@ server_callback (SoupServer *server, SoupMessage *msg, g_print (" -> %d %s\n\n", msg->status_code, msg->reason_phrase); } - static void -quit (int sig) -{ - /* Exit cleanly on ^C in case we're valgrinding. */ - exit (0); -} - - int addRouteToServer(char *name, char *file_to_send, char *destination) { - soup_server_add_handler (server, g_strdup_printf("/transfer/%s", name), + GFile *file; + GFileInfo *fileInfo; + soup_server_add_handler (glob_server, g_strdup_printf("/transfer/%s", name), server_callback, g_strdup(file_to_send), NULL); //send notification of available file to the client //For getting file size //https://developer.gnome.org/gio/stable/GFile.html#g-file-query-info - GFile *file; - GFileInfo *fileInfo; file = g_file_new_for_path(file_to_send); //G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_ATTRIBUTE_STANDARD_SIZE fileInfo = g_file_query_info(file, "standard::display-name,standard::size", G_FILE_QUERY_INFO_NONE, NULL, NULL); @@ -356,21 +269,20 @@ int addRouteToServer(char *name, char *file_to_send, char *destination) { } extern int run_http_server(void) { - //GMainLoop *loop; GSList *uris, *u; char *str; - GTlsCertificate *cert; + //GTlsCertificate *cert; GError *error = NULL; port = 3000; - server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ", + glob_server = soup_server_new (SOUP_SERVER_SERVER_HEADER, "teleport-httpd ", NULL); - soup_server_listen_all (server, port, 0, &error); + soup_server_listen_all (glob_server, port, 0, &error); - soup_server_add_handler (server, NULL, + soup_server_add_handler (glob_server, NULL, server_callback, NULL, NULL); - uris = soup_server_get_uris (server); + uris = soup_server_get_uris (glob_server); for (u = uris; u; u = u->next) { str = soup_uri_to_string (u->data, FALSE); g_print ("Listening on %s\n", str); @@ -381,17 +293,5 @@ extern int run_http_server(void) { g_print ("\nWaiting for requests...\n"); - //loop = g_main_loop_new (NULL, TRUE); - //g_main_loop_run (loop); - return 0; } - -/*int - main (int argc, char **argv) - { - char *file_to_send = "/home/julian/teleport/docs/flow-diagram.svg"; - createServer(file_to_send); - return 0; - } - */ diff --git a/src/teleportapp.c b/src/teleportapp.c index 905720b..99d4907 100644 --- a/src/teleportapp.c +++ b/src/teleportapp.c @@ -71,13 +71,14 @@ void open_file_callback (GSimpleAction *simple, } void create_user_notification (const char *file_name, const int file_size, const char *origin_device, GVariant *target) { - GNotification *notification = g_notification_new ("Teleport"); + GIcon *icon; + 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))); - GIcon *icon = g_themed_icon_new ("dialog-information"); + icon = g_themed_icon_new ("dialog-information"); g_notification_set_icon (notification, icon); g_notification_set_default_action_and_target_value (notification, "app.do-nothing", target); g_notification_add_button_with_target_value (notification, "Decline", "app.decline", target); @@ -91,9 +92,10 @@ void create_user_notification (const char *file_name, const int file_size, const } void create_finished_notification (const char *origin, const int filesize, const char *filename, GVariant *target) { + GIcon *icon; GNotification *notification = g_notification_new ("Teleport"); g_notification_set_body (notification, g_strdup_printf("Transfer of %s from %s is complete", filename, origin)); - GIcon *icon = g_themed_icon_new ("dialog-information"); + icon = g_themed_icon_new ("dialog-information"); g_notification_set_icon (notification, icon); g_notification_set_default_action_and_target_value (notification, "app.do-nothing", target); g_notification_add_button_with_target_value (notification, "Open", "app.open-file", target); @@ -115,15 +117,15 @@ gboolean mainLoopRemovePeerCallback (gpointer peer) { return G_SOURCE_REMOVE; } -void callback_add_peer (GObject *instance, Peer *peer, TeleportAppWindow *win ) { +void callback_add_peer (GObject *instance, Peer *peer, gpointer window) { g_idle_add(mainLoopAddPeerCallback, peer); } -void callback_remove_peer (GObject *instance, Peer *peer, TeleportAppWindow *win ) { +void callback_remove_peer (GObject *instance, Peer *peer, gpointer window) { g_idle_add(mainLoopRemovePeerCallback, peer); } -void callback_notify_user (GObject *instance, char *name, TeleportAppWindow *win ) { +void callback_notify_user (GObject *instance, gchar *name, gpointer window) { //create_user_notification("icon.png", 2000, "Mark's laptop"); } @@ -135,8 +137,8 @@ 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); + application = app; win = teleport_app_window_new (TELEPORT_APP (app)); gtk_window_present (GTK_WINDOW (win)); diff --git a/src/teleportapp.h b/src/teleportapp.h index 3f079a5..3db6763 100644 --- a/src/teleportapp.h +++ b/src/teleportapp.h @@ -2,7 +2,7 @@ #define __TELEPORTAPP_H #include - +#include "teleportpeer.h" #define TELEPORT_APP_TYPE (teleport_app_get_type ()) G_DECLARE_FINAL_TYPE (TeleportApp, teleport_app, TELEPORT, APP, GtkApplication) @@ -19,4 +19,18 @@ extern void create_finished_notification (const char *, const char *, GVariant *); +gboolean mainLoopAddPeerCallback (gpointer); +gboolean mainLoopRemovePeerCallback (gpointer); + +void callback_add_peer (GObject *, + Peer *, + gpointer); +void callback_remove_peer (GObject *, + Peer *peer, + gpointer); +void callback_notify_user (GObject *, + gchar *, + gpointer); + + #endif /* __TELEPORTAPP_H */ diff --git a/src/teleportappwin.c b/src/teleportappwin.c index 48a3fa3..579e8dd 100644 --- a/src/teleportappwin.c +++ b/src/teleportappwin.c @@ -60,7 +60,7 @@ open_file_picker(GtkButton *btn, Peer *device) { gint res; g_print("Open file chooser for submitting a file to %s with Address %s\n", device->name, device->ip); - dialog = ("Open File", + dialog = gtk_file_chooser_dialog_new ("Open File", GTK_WINDOW(mainWin), action, ("_Cancel"), @@ -116,7 +116,6 @@ void update_remote_device_list_remove(TeleportAppWindow *win, Peer *device) { GtkWidget *box; GtkListBoxRow *remote_row; GtkLabel *name_label; - GtkWidget *line; gint i = 0; priv = teleport_app_window_get_instance_private (win); @@ -163,11 +162,11 @@ find_child(GtkWidget *parent, const gchar *name) static void teleport_app_window_dispose (GObject *object) { - TeleportAppWindow *win; - TeleportAppWindowPrivate *priv; + //TeleportAppWindow *win; + //TeleportAppWindowPrivate *priv; - win = TELEPORT_APP_WINDOW (object); - priv = teleport_app_window_get_instance_private (win); + //win = TELEPORT_APP_WINDOW (object); + //priv = teleport_app_window_get_instance_private (win); //g_clear_object (&priv->settings); @@ -197,6 +196,6 @@ teleport_app_window_new (TeleportApp *app) teleport_app_window_open (TeleportAppWindow *win, GFile *file) { - TeleportAppWindowPrivate *priv; - priv = teleport_app_window_get_instance_private (win); + //TeleportAppWindowPrivate *priv; + //priv = teleport_app_window_get_instance_private (win); } diff --git a/src/teleportpeer.c b/src/teleportpeer.c index 99b1256..f20e6a4 100644 --- a/src/teleportpeer.c +++ b/src/teleportpeer.c @@ -60,11 +60,12 @@ teleport_peer_init (TeleportPeer *self) gchar * teleport_peer_get_name (TeleportPeer *self, gint index, GError **error) { + Peer *element; //g_return_if_fail (TELEPORT_IS_PEER (self)); //g_return_if_fail (error == NULL || *error == NULL); if (index > self->list->len-1) return NULL; - Peer *element = g_array_index(self->list, Peer *, index); + element = g_array_index(self->list, Peer *, index); return element->name; } @@ -113,10 +114,9 @@ void teleport_peer_remove_peer (TeleportPeer *self, Peer *device) void teleport_peer_remove_peer_by_name (TeleportPeer *self, gchar *name) { - - g_print("Remove this device %s", name); - Peer *element; + Peer *element = NULL; gboolean found = FALSE; + g_print("Remove this device %s", name); for (int i = 0; i < self->list->len && !found; i++) { element = g_array_index(self->list, Peer *, i); if (g_strcmp0(element->name, name) == 0) {