Commit 6fe0756c authored by Julian Sparber's avatar Julian Sparber

[feat] use a pointer to the peer struct as referenc to the device instate of...

[feat] use a pointer to the peer struct as referenc to the device instate of just the name of the device
parent 1530816f
......@@ -49,22 +49,24 @@ static void create_finished_notification (const char *file_name, const int file_
}
gboolean mainLoopAddPeerCallback (gpointer name) {
update_remote_device_list(win, (char *)name);
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 name) {
update_remote_device_list_remove(win, (char *)name);
gboolean mainLoopRemovePeerCallback (gpointer peer) {
update_remote_device_list_remove(win, (Peer *) peer);
return G_SOURCE_REMOVE;
}
void callback_add_peer(GObject *instance, char *name, TeleportAppWindow *win ) {
g_idle_add(mainLoopAddPeerCallback, g_strdup(name));
void callback_add_peer(GObject *instance, Peer *peer, TeleportAppWindow *win ) {
g_idle_add(mainLoopAddPeerCallback, peer);
}
void callback_remove_peer(GObject *instance, char *name, TeleportAppWindow *win ) {
g_idle_add(mainLoopRemovePeerCallback, g_strdup(name));
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 ) {
......
......@@ -3,7 +3,7 @@
#include "teleportapp.h"
#include "teleportappwin.h"
#include "server.h"
#include "teleportpeer.h"
GtkWidget *find_child(GtkWidget *, const gchar *);
TeleportAppWindow *mainWin;
......@@ -45,11 +45,11 @@ teleport_app_window_init (TeleportAppWindow *win)
}
static void
open_file_picker(GtkButton *btn, GString *deviceName) {
open_file_picker(GtkButton *btn, Peer *device) {
GtkWidget *dialog;
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
gint res;
g_print("Open file chooser for submitting a file to %s\n", deviceName);
g_print("Open file chooser for submitting a file to %s with Address %s\n", device->name, device->ip);
dialog = gtk_file_chooser_dialog_new ("Open File",
GTK_WINDOW(mainWin),
......@@ -67,15 +67,14 @@ open_file_picker(GtkButton *btn, GString *deviceName) {
GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
filename = gtk_file_chooser_get_filename (chooser);
g_print("Choosen file is %s\n", filename);
addRouteToServer(g_compute_checksum_for_string (G_CHECKSUM_SHA256, filename, -1), filename, "localhost");
//open_file (filename);
addRouteToServer(g_compute_checksum_for_string (G_CHECKSUM_SHA256, filename, -1), filename, device->ip);
g_free (filename);
}
gtk_widget_destroy (dialog);
}
void update_remote_device_list(TeleportAppWindow *win, char *name) {
void update_remote_device_list(TeleportAppWindow *win, Peer *device) {
TeleportAppWindowPrivate *priv;
GtkBuilder *builder_remote_list;
GtkWidget *row;
......@@ -89,10 +88,10 @@ void update_remote_device_list(TeleportAppWindow *win, char *name) {
row = GTK_WIDGET (gtk_builder_get_object (builder_remote_list, "remote_device_row"));
name_label = GTK_LABEL (gtk_builder_get_object (builder_remote_list, "device_name"));
gtk_label_set_text(name_label, name);
gtk_label_set_text(name_label, device->name);
gtk_list_box_insert(GTK_LIST_BOX(priv->remote_devices_list), row, -1);
send_btn = GTK_BUTTON (gtk_builder_get_object (builder_remote_list, "send_btn"));
g_signal_connect (send_btn, "clicked", G_CALLBACK (open_file_picker), name);
g_signal_connect (send_btn, "clicked", G_CALLBACK (open_file_picker), device);
//line = GTK_WIDGET (gtk_builder_get_object (builder_remote_list, "remote_space_row"));
//gtk_list_box_insert(GTK_LIST_BOX(priv->remote_devices_list), line, -1);
......@@ -100,7 +99,7 @@ void update_remote_device_list(TeleportAppWindow *win, char *name) {
}
void update_remote_device_list_remove(TeleportAppWindow *win, char *name) {
void update_remote_device_list_remove(TeleportAppWindow *win, Peer *device) {
TeleportAppWindowPrivate *priv;
GtkWidget *box;
GtkListBoxRow *remote_row;
......@@ -115,7 +114,7 @@ void update_remote_device_list_remove(TeleportAppWindow *win, char *name) {
while(remote_row != NULL) {
name_label = GTK_LABEL(find_child(GTK_WIDGET(remote_row), "GtkLabel"));
if (name_label != NULL && g_strcmp0(name, gtk_label_get_text(name_label)) == 0) {
if (name_label != NULL && g_strcmp0(device->name, gtk_label_get_text(name_label)) == 0) {
gtk_container_remove (GTK_CONTAINER(box), GTK_WIDGET(remote_row));
}
i++;
......
......@@ -3,6 +3,7 @@
#include <gtk/gtk.h>
#include "teleportapp.h"
#include "teleportpeer.h"
#define TELEPORT_APP_WINDOW_TYPE (teleport_app_window_get_type ())
......@@ -11,8 +12,8 @@ G_DECLARE_FINAL_TYPE (TeleportAppWindow, teleport_app_window, TELEPORT, APP_WIND
TeleportAppWindow *teleport_app_window_new (TeleportApp *app);
void teleport_app_window_open (TeleportAppWindow *win,
GFile *file);
extern void update_remote_device_list(TeleportAppWindow *, char *);
extern void update_remote_device_list_remove(TeleportAppWindow *, char *);
extern void update_remote_device_list(TeleportAppWindow *, Peer *);
extern void update_remote_device_list_remove(TeleportAppWindow *, Peer *);
#endif /* __TELEPORTAPPWIN_H */
......@@ -5,19 +5,12 @@ enum {
ADD, REMOVE, N_SIGNALS
};
typedef struct Peers {
char *name;
char *ip;
gint port;
} Peer;
static gint signalIds [N_SIGNALS];
struct _TeleportPeer
{
GObject parent;
GArray *list;
/* instance members */
};
......@@ -44,7 +37,7 @@ teleport_peer_class_init (TeleportPeerClass *klass)
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
G_TYPE_POINTER);
signalIds[REMOVE] = g_signal_new ("removepeer",
G_TYPE_OBJECT,
......@@ -55,14 +48,14 @@ teleport_peer_class_init (TeleportPeerClass *klass)
NULL /* C marshaller */,
G_TYPE_NONE /* return_type */,
1,
G_TYPE_STRING);
G_TYPE_POINTER);
}
static void
teleport_peer_init (TeleportPeer *self)
{
self->list = g_array_new (FALSE, FALSE, sizeof(Peer));
self->list = g_array_new (FALSE, FALSE, sizeof(Peer *));
}
gchar * teleport_peer_get_name (TeleportPeer *self, gint index, GError **error)
......@@ -71,47 +64,50 @@ gchar * teleport_peer_get_name (TeleportPeer *self, gint index, GError **error)
//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);
return element.name;
Peer *element = g_array_index(self->list, Peer *, index);
return element->name;
}
gchar * teleport_peer_get_ip (TeleportPeer *self, gint index, GError **error)
{
//g_return_if_fail (TELEPORT_IS_PEER (self));
//g_return_if_fail (error == NULL || *error == NULL);
Peer element = g_array_index(self->list, Peer, index);
Peer *element = g_array_index(self->list, Peer *, index);
if (index > self->list->len-1)
return NULL;
return element.ip;
return element->ip;
}
gint teleport_peer_get_port (TeleportPeer *self, gint index, GError **error)
{
Peer element = g_array_index(self->list, Peer, index);
Peer *element = g_array_index(self->list, Peer*, index);
if (index > self->list->len-1)
return 0;
return element.port;
return element->port;
}
void teleport_peer_add_peer (TeleportPeer *self, gchar * name, gchar * ip, gint port)
{
Peer new;
new.ip = ip;
new.port = port;
new.name = name;
Peer *new = g_new(Peer, 1);
new->ip = ip;
new->port = port;
new->name = name;
g_array_append_val(self->list, new);
g_signal_emit (self, signalIds[ADD], 0, name);
g_signal_emit (self, signalIds[ADD], 0, new);
}
void teleport_peer_remove_peer (TeleportPeer *self, gchar * name)
void teleport_peer_remove_peer (TeleportPeer *self, Peer *device)
{
Peer element;
gboolean found = FALSE;
//Maybe I could just compare the addresses
for (int i = 0; i < self->list->len && !found; i++) {
Peer element = g_array_index(self->list, Peer, i);
if (g_strcmp0(element.name, name) == 0) {
Peer *element = g_array_index(self->list, Peer *, i);
if (g_strcmp0(element->name, device->name) == 0) {
found = TRUE;
g_array_remove_index(self->list, i);
}
}
g_signal_emit (self, signalIds[REMOVE], 0, name);
g_signal_emit (self, signalIds[REMOVE], 0, device);
}
......@@ -6,10 +6,17 @@
#define TELEPORT_TYPE_PEER teleport_peer_get_type ()
G_DECLARE_FINAL_TYPE (TeleportPeer, teleport_peer, TELEPORT, PEER, GObject)
typedef struct Peers {
char *name;
char *ip;
gint port;
} Peer;
gchar* teleport_peer_get_name (TeleportPeer *self, gint index, GError **error);
gchar* teleport_peer_get_ip (TeleportPeer *self, gint index, GError **error);
gint teleport_peer_get_port (TeleportPeer *self, gint index, GError **error);
void teleport_peer_add_peer (TeleportPeer *self, gchar * name, gchar * ip, gint port);
void teleport_peer_remove_peer (TeleportPeer *self, gchar * name);
void teleport_peer_remove_peer (TeleportPeer *, Peer *);
#endif /* __TELEPORTPEER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment