summaryrefslogtreecommitdiff
path: root/rcm-client-hardware-list.c
blob: 9f55e82b3e7560df1aaa55b28095cf70929c64fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <gtk/gtk.h>

#include "generated.h"
#include "rcm-client-main.h"
#include "rcm-client-hardware-list.h"
#include "rcm-client-receive.h"

static GtkWidget *status_msg;
static GtkWidget *hw_list_box;
static GtkWidget *stack;
static GList *hw_list = NULL;

struct hwentry {
	GDBusObject *hw;
	GtkWidget *widget;
};

static gint
find_hwentry_by_object(gconstpointer a, gconstpointer user_data)
{
	const struct hwentry *hwe = a;

	return hwe->hw == user_data ? 0 : -1;
}

static gint
find_hwentry_by_widget(gconstpointer a, gconstpointer user_data)
{
	const struct hwentry *hwe = a;

	return hwe->widget == user_data ? 0 : -1;
}

void rcng_client_hardware_list_remove(GDBusObject *hw)
{
	GList *entry;
	struct hwentry *hwe;

	entry = g_list_find_custom(hw_list, hw, find_hwentry_by_object);
	if (!entry)
		return;

	hwe = entry->data;
	hw_list = g_list_remove_all(hw_list, hwe);

	g_print(" - Object removed %s\n", g_dbus_object_get_object_path(hwe->hw));
	gtk_widget_destroy(hwe->widget);
	g_free(hwe);

	if (!hw_list)
		gtk_stack_set_visible_child(GTK_STACK(stack), status_msg);
}

void rcng_client_hardware_list_add(GDBusObject *hw)
{
	GList *interfaces;
	GList *ll;
	GtkWidget *box;
	GtkWidget *icon;
	GtkWidget *label;
	struct hwentry *hwe;
	gchar *labeltxt;

	g_print(" - Object at %s\n", g_dbus_object_get_object_path(hw));

	interfaces = g_dbus_object_get_interfaces(hw);
	for (ll = interfaces; ll != NULL; ll = ll->next) {
		GDBusInterface *interface = G_DBUS_INTERFACE(ll->data);
		g_print ("   - Interface %s\n", g_dbus_interface_get_info(interface)->name);
	}
	g_list_free_full (interfaces, g_object_unref);

	if (!hw_list)
		gtk_stack_set_visible_child(GTK_STACK(stack), hw_list_box);

	box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
	icon = gtk_image_new_from_icon_name("gtk-harddisk", GTK_ICON_SIZE_DIALOG);
	gtk_widget_set_valign(icon, GTK_ALIGN_START);
	gtk_widget_set_halign(icon, GTK_ALIGN_START);
	gtk_widget_set_margin_start(icon, 12);
	gtk_widget_set_margin_end(icon, 12);
	labeltxt = g_strdup_printf("<b>Hardware</b>\n\ntest\ntest\ntest%s", g_dbus_object_get_object_path(hw));
	label = gtk_label_new(NULL);
	gtk_label_set_markup(GTK_LABEL(label), labeltxt);
	g_free(labeltxt);
	gtk_box_pack_start(GTK_BOX(box), icon, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
	gtk_widget_show_all(box);

	gtk_list_box_insert(GTK_LIST_BOX(hw_list_box), box, -1);

	hwe = g_malloc(sizeof(*hwe));
	hwe->hw = hw;
	hwe->widget = box;
	hw_list = g_list_append(hw_list, hwe);
}

static struct hwentry *old_hwe = NULL;

static void
on_hw_selected(GtkListBox *box,
	       GtkListBoxRow *row,
	       gpointer user_data)
{
	GList *entry;
	GtkWidget *child;
	struct hwentry *hwe;
      
	child = row ? gtk_bin_get_child(GTK_BIN(row)) : NULL;
	entry = g_list_find_custom(hw_list, child, find_hwentry_by_widget);
	hwe = entry ? entry->data : NULL;

	if (hwe == old_hwe)
		return;

	old_hwe = hwe;
	rcng_client_receive_destroy_ui();
	rcng_client_receive_init_ui(hwe ? hwe->hw : NULL);
}

void rcng_client_hardware_list_update_status(gchar *status)
{
	gtk_label_set_text(GTK_LABEL(status_msg), status);
}

void rcng_client_hardware_list_init_ui()
{
	stack = gtk_stack_new();
	gtk_widget_set_margin_start(stack, 12);
	gtk_widget_set_margin_end(stack, 12);
	gtk_widget_set_margin_top(stack, 12);
	gtk_widget_set_margin_bottom(stack, 12);
	gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_CROSSFADE);

	status_msg = gtk_label_new("Connecting to server...");
	gtk_stack_add_named(GTK_STACK(stack), status_msg, "nohw");
	gtk_stack_set_visible_child(GTK_STACK(stack), status_msg);

	hw_list_box = gtk_list_box_new();
	gtk_stack_add_named(GTK_STACK(stack), hw_list_box, "yeshw");
	g_signal_connect(hw_list_box, "row-selected", G_CALLBACK(on_hw_selected), NULL);

	gtk_notebook_append_page(global->notebook, stack, gtk_label_new("Hardware"));
}