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