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