0ad9ef1439c025afff3de92a344131c9a147a221
[systembsd.git] / src / interfaces / hostnamed / hostnamed.c
1 #include <gio/gio.h>
2
3 GDBusNodeInfo *spect_data;
4
5 static gchar *hostname;
6 static gchar *pretty_hostname;
7 static gchar *static_hostname;
8 static gchar *icon_name;
9
10 static void handle_method_call(GDBusConnection *conn,
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) {
18
19 if(g_strcmp0(interf_name, "org.freedesktop.DBus.Introspectable") == 0) {
20
21 GVariant *xml_ret_gvar;
22 GString *xml_ret;
23
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 }
28
29 }
30
31 static GVariant * handle_get_property(GDBusConnection *conn,
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) {
38
39 GVariant *ret;
40
41 return ret;
42 }
43
44 static gboolean handle_set_property(GDBusConnection *conn,
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;
62 }
63
64 /* "hot" functions initially passed to gdbus */
65 static const GDBusInterfaceVTable interface_vtable =
66 {
67 handle_method_call,
68 handle_get_property,
69 handle_set_property
70 };
71
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 */
77 static void on_bus_acquired(GDBusConnection *conn,
78 const gchar *name,
79 gpointer user_data) {
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);
92 }
93
94 static void on_name_acquired(GDBusConnection *conn,
95 const gchar *name,
96 gpointer user_data) {
97
98 g_print("got name %s\n", name);
99 }
100
101 static void on_name_lost(GDBusConnection *conn,
102 const gchar *name,
103 gpointer user_data) {
104
105 g_print("lost name %s, exiting...", name);
106 //TODO exit through g_main_loop properly...
107 exit(0);
108 }
109
110 /* safe call to try and start hostnamed */
111 GError * hostnamed_init() {
112
113 guint bus_descriptor;
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);
121
122 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
123 "org.freedesktop.hostname1",
124 G_BUS_NAME_OWNER_FLAGS_NONE,
125 on_bus_acquired,
126 on_name_acquired,
127 on_name_lost,
128 NULL,
129 NULL);
130
131 //TODO: malloc and return reference as if a main() closed
132 return err;
133 }