ea207ed3 |
1 | #include <gio/gio.h> |
2 | |
ff1030d4 |
3 | GDBusNodeInfo *spect_data; |
0df0018d |
4 | |
780e1f19 |
5 | static void handle_method_call(GDBusConnection *conn, |
80043b36 |
6 | const gchar *sender, |
7 | const gchar *obj_path, |
8 | const gchar *interf_name, |
9 | const gchar *method_name, |
10 | GVariant *params, |
11 | GDBusMethodInvocation *invc, |
12 | gpointer usrdat) { |
780e1f19 |
13 | |
e8c57a5d |
14 | //if(g_strcmp0(method_name, "Introspect" |
780e1f19 |
15 | |
ff1030d4 |
16 | GVariant *xml_ret_gvar; |
17 | GString *xml_ret; |
18 | |
80043b36 |
19 | g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret); |
20 | xml_ret_gvar = g_variant_new_string(xml_ret->str); |
21 | g_dbus_method_invocation_return_value(invc, xml_ret_gvar); |
780e1f19 |
22 | |
a622ef09 |
23 | } |
780e1f19 |
24 | |
25 | static GVariant * handle_get_property(GDBusConnection *conn, |
80043b36 |
26 | const gchar *sender, |
27 | const gchar *obj_path, |
28 | const gchar *interf_name, |
29 | const gchar *prop_name, |
30 | GError **err, |
31 | gpointer usr_data) { |
780e1f19 |
32 | |
80043b36 |
33 | GVariant *ret; |
780e1f19 |
34 | |
80043b36 |
35 | return ret; |
780e1f19 |
36 | } |
37 | |
38 | static gboolean handle_set_property(GDBusConnection *conn, |
80043b36 |
39 | const gchar *sender, |
40 | const gchar *obj_path, |
41 | const gchar *interf_name, |
42 | const gchar *prop_name, |
43 | GVariant *val, |
44 | GError **err, |
45 | gpointer usr_data) { |
46 | |
47 | g_dbus_connection_emit_signal(conn, |
48 | NULL, |
49 | obj_path, |
50 | "org.freedesktop.DBus.Properties", |
51 | "PropertiesChanged", |
52 | NULL, /* incorrect */ |
53 | NULL); |
54 | |
55 | return TRUE; |
780e1f19 |
56 | } |
387173cb |
57 | |
a622ef09 |
58 | /* "hot" functions initially passed to gdbus */ |
59 | static const GDBusInterfaceVTable interface_vtable = |
60 | { |
80043b36 |
61 | handle_method_call, |
62 | handle_get_property, |
63 | handle_set_property |
a622ef09 |
64 | }; |
65 | |
0df0018d |
66 | /* end method/property functions, begin bus name handlers |
67 | * TODO: these should be intertwined as to handle edge cases |
68 | * for when the system cannot immediately grab the name, as |
69 | * well as cases where the system unintendedly loses the name |
70 | */ |
80043b36 |
71 | static void on_bus_acquired(GDBusConnection *conn, |
72 | const gchar *name, |
73 | gpointer user_data) { |
ea207ed3 |
74 | g_print("got bus, name: %s\n", name); |
387173cb |
75 | |
387173cb |
76 | guint reg_id; |
ea207ed3 |
77 | |
a622ef09 |
78 | reg_id = g_dbus_connection_register_object(conn, |
79 | "/org/freedesktop/hostname1", |
80 | spect_data->interfaces[0], |
81 | &interface_vtable, |
82 | NULL, |
83 | NULL, |
80043b36 |
84 | NULL); |
ea207ed3 |
85 | } |
86 | |
80043b36 |
87 | static void on_name_acquired(GDBusConnection *conn, |
88 | const gchar *name, |
89 | gpointer user_data) { |
90 | |
ea207ed3 |
91 | g_print("got name %s\n", name); |
ea207ed3 |
92 | } |
93 | |
80043b36 |
94 | static void on_name_lost(GDBusConnection *conn, |
95 | const gchar *name, |
96 | gpointer user_data) { |
97 | |
f4d34761 |
98 | g_print("lost name %s, exiting...", name); |
e8c57a5d |
99 | //TODO exit through g_main_loop properly... |
100 | exit(0); |
ea207ed3 |
101 | } |
102 | |
0df0018d |
103 | /* safe call to try and start hostnamed */ |
6de394d4 |
104 | GError * hostnamed_init() { |
387173cb |
105 | |
ea207ed3 |
106 | guint bus_descriptor; |
a622ef09 |
107 | GError *err = NULL; |
e8c57a5d |
108 | |
6de394d4 |
109 | bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM, |
ea207ed3 |
110 | (gchar *)"org.freedesktop.hostname1", |
111 | G_BUS_NAME_OWNER_FLAGS_NONE, |
112 | on_bus_acquired, |
113 | on_name_acquired, |
114 | on_name_lost, |
115 | NULL, |
116 | NULL); |
496f5d66 |
117 | |
6de394d4 |
118 | //TODO: malloc and return reference as if a main() closed |
119 | return err; |
780e1f19 |
120 | } |