From af913a09689e1664ec345fe79944d0f6ec68b71c Mon Sep 17 00:00:00 2001 From: Lakindu Akash Date: Sun, 12 Sep 2021 15:29:29 +0530 Subject: [PATCH] Refcator and fix warnings in connecetd devices --- src/ui/ui.c | 113 ++++++++++++++++++++++++++++++---------------------- src/ui/ui.h | 4 ++ 2 files changed, 70 insertions(+), 47 deletions(-) diff --git a/src/ui/ui.c b/src/ui/ui.c index f124720..dc67713 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -60,11 +60,11 @@ GtkButton *button_about; GtkButton *button_refresh; GtkGrid *grid_devices; -GtkWidget *h; -GtkWidget *i; -GtkWidget *m; -GtkWidget *n; -PtrToNode l; +GtkWidget *label_cd_hostname; +GtkWidget *label_cd_ip; +GtkWidget *label_cd_mac; +GtkWidget *label_cd_number; +PtrToNode device_list; GtkEntry *entry_ssd; GtkEntry *entry_pass; @@ -161,48 +161,6 @@ static void on_about_open_click(GtkWidget *widget, gpointer data){ show_info(widget,data); } -static void set_connected_devices_label() -{ - Position tmp; - l = get_connected_devices(running_info[0]); // running_info[0] PID - - // Remove all the children widgets - GList *children, *iter; - - children = gtk_container_get_children(GTK_CONTAINER(grid_devices)); - for (iter = children; iter != NULL; iter = g_list_next(iter)) - { - gtk_widget_destroy(GTK_WIDGET(iter->data)); - } - g_list_free(children); - - while (l->Next != NULL) - { - tmp = l; // Save the last one - l = l->Next; - char number[1]; - sprintf(number, "%d", l->Number); - n = gtk_label_new(number); - h = gtk_label_new(l->HOSTNAME); - i = gtk_label_new(l->IP); - m = gtk_label_new(l->MAC); - - gtk_grid_attach(grid_devices, n, 0, l->Number, 1, 1); - gtk_grid_attach(grid_devices, h, 1, l->Number, 1, 1); - gtk_grid_attach(grid_devices, i, 2, l->Number, 1, 1); - gtk_grid_attach(grid_devices, m, 3, l->Number, 1, 1); - gtk_widget_show_all((GtkWidget *)grid_devices); - free(tmp); // Free the last pointer - } -} - -static void on_refresh_clicked(GtkWidget *widget, gpointer data) -{ - if (running_info[0] != NULL) - { - set_connected_devices_label(); - } -} static void loadStyles(){ provider = gtk_css_provider_new(); @@ -919,4 +877,65 @@ gchar* get_accepted_macs(){ return accepted_macs; +} + +/** + * Clear device list +*/ +static void clear_connecetd_devices_list(){ + + // Remove all the children widgets + GList *children, *iter; + + children = gtk_container_get_children(GTK_CONTAINER(grid_devices)); + for (iter = children; iter != NULL; iter = g_list_next(iter)) + { + gtk_widget_destroy(GTK_WIDGET(iter->data)); + } + g_list_free(children); +} + +/** + * Set connected device list + * +*/ +static void set_connected_devices_label() +{ + Position tmp; + device_list = get_connected_devices(running_info[0]); // running_info[0] PID + + clear_connecetd_devices_list(); + + while (device_list->Next != NULL) + { + tmp = device_list; // Save the last one + device_list = device_list->Next; + char number[2]; + sprintf(number, "%d", device_list->Number); + label_cd_number = gtk_label_new(number); + label_cd_hostname = gtk_label_new(device_list->HOSTNAME); + label_cd_ip = gtk_label_new(device_list->IP); + label_cd_mac = gtk_label_new(device_list->MAC); + + gtk_grid_attach(grid_devices, label_cd_number, 0, device_list->Number, 1, 1); + gtk_grid_attach(grid_devices, label_cd_hostname, 1, device_list->Number, 1, 1); + gtk_grid_attach(grid_devices, label_cd_ip, 2, device_list->Number, 1, 1); + gtk_grid_attach(grid_devices, label_cd_mac, 3, device_list->Number, 1, 1); + gtk_widget_show_all((GtkWidget *)grid_devices); + free(tmp); // Free the last pointer + } +} + +/** + * When conncetd devices refresh button clicked +*/ +static void on_refresh_clicked(GtkWidget *widget, gpointer data) +{ + if (running_info[0] != NULL) + { + set_connected_devices_label(); + } + else { + clear_connecetd_devices_list(); + } } \ No newline at end of file diff --git a/src/ui/ui.h b/src/ui/ui.h index 7216927..a78b61c 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -72,4 +72,8 @@ gchar* get_accepted_macs(); static void set_connected_devices_label(); // new +static void on_refresh_clicked(GtkWidget *widget, gpointer data); + +static void clear_connecetd_devices_list(); + #endif //WIHOTSPOT_UI_H