divorce XML literal to hostnamed.h, update types to be glib-appropriate, comments
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 #include <gio/gio.h>
2 #include "hostnamed.h"
3
4 GDBusNodeInfo *spect_data;
5 GMainLoop *loop;
6
7 static void handle_method_call(GDBusConnection *conn,
8 const gchar *sender,
9 const gchar *obj_path,
10 const gchar *interf_name,
11 const gchar *method_name,
12 GVariant *params,
13 GDBusMethodInvocation *invc,
14 gpointer usrdat) {
15
16 g_printf("%s wants to call %s, at %s with interface %s\n", sender, method_name, obj_path, interf_name);
17
18 GString *xml_ret;
19 GVariant *xml_ret_gvar;
20
21 g_dbus_interface_info_generate_xml(spect_data->interfaces[0], (guint)0, xml_ret);
22 xml_ret_gvar = g_variant_new_string(xml_ret->str);
23 g_dbus_method_invocation_return_value(invc, xml_ret_gvar);
24
25 }
26
27 static GVariant * handle_get_property(GDBusConnection *conn,
28 const gchar *sender,
29 const gchar *obj_path,
30 const gchar *interf_name,
31 const gchar *prop_name,
32 GError **err,
33 gpointer usr_data) {
34
35 GVariant *ret;
36
37 return ret;
38 }
39
40 static gboolean handle_set_property(GDBusConnection *conn,
41 const gchar *sender,
42 const gchar *obj_path,
43 const gchar *interf_name,
44 const gchar *prop_name,
45 GVariant *val,
46 GError **err,
47 gpointer usr_data) {
48 g_dbus_connection_emit_signal(conn,
49 NULL,
50 obj_path,
51 "org.freedesktop.DBus.Properties",
52 "PropertiesChanged",
53 NULL, /* incorrect */
54 NULL);
55
56 return TRUE;
57 }
58
59 /* "hot" functions initially passed to gdbus */
60 static const GDBusInterfaceVTable interface_vtable =
61 {
62 handle_method_call,
63 handle_get_property,
64 handle_set_property
65 };
66
67 /* end method/property functions, begin bus name handlers
68 * TODO: these should be intertwined as to handle edge cases
69 * for when the system cannot immediately grab the name, as
70 * well as cases where the system unintendedly loses the name
71 */
72 static void on_bus_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) {
73 g_print("got bus, name: %s\n", name);
74
75 guint reg_id;
76
77 reg_id = g_dbus_connection_register_object(conn,
78 "/org/freedesktop/hostname1",
79 spect_data->interfaces[0],
80 &interface_vtable,
81 NULL,
82 NULL,
83 NULL );
84 g_assert(reg_id > 0);
85 }
86
87 static void on_name_acquired(GDBusConnection *conn, const gchar *name, gpointer user_data) {
88 g_print("got name %s\n", name);
89 }
90
91 static void on_name_lost(GDBusConnection *conn, const gchar *name, gpointer user_data) {
92 g_print("lost name %s, exiting...\n", name);
93 g_main_loop_quit(loop);
94 }
95
96 /* safe call to try and start hostnamed */
97 GError hostnamed_init() {
98
99 guint bus_descriptor;
100 GError *err = NULL;
101
102 spect_data = g_dbus_node_info_new_for_xml(SYSTEMD_HOSTNAMED_XML, &err);
103
104 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SESSION,
105 (gchar *)"org.freedesktop.hostname1",
106 G_BUS_NAME_OWNER_FLAGS_NONE,
107 on_bus_acquired,
108 on_name_acquired,
109 on_name_lost,
110 NULL,
111 NULL);
112
113 loop = g_main_loop_new(NULL, FALSE);
114 g_main_loop_run(loop);
115
116 /* i am not sure what the system state is once g_main_loop exits */
117
118 g_bus_unown_name(bus_descriptor);
119 g_dbus_node_info_unref(spect_data);
120 }
121