syncing between computers..
[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
1cd91c9c 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
25static 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
38static 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 */
59static 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 71static 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 87static 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 94static void on_name_lost(GDBusConnection *conn,
95 const gchar *name,
96 gpointer user_data) {
97
780e1f19 98 g_print("lost name %s, exiting...\n", name);
1cd91c9c 99 //TODO exit through g_main_loop properly...
100 exit(0);
ea207ed3 101}
102
0df0018d 103/* safe call to try and start hostnamed */
6de394d4 104GError * hostnamed_init() {
387173cb 105
ea207ed3 106 guint bus_descriptor;
a622ef09 107 GError *err = NULL;
1cd91c9c 108 gchar **hnd_ispect_xml;
109 GDir *cur_dir;
110 gchar *dir;
111
112 cur_dir = g_dir_open("./../", 0, err);
780e1f19 113
1cd91c9c 114 g_sprintf(dir, "%s\n", g_dir_read_name(cur_dir));
115
116 //g_file_get_contents("../../../../conf/hostnamed-ispect.xml", hnd_ispect_xml, NULL, err);
117 //spect_data = g_dbus_node_info_new_for_xml(hnd_ispect_xml, &err);
15c779be 118
6de394d4 119 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
ea207ed3 120 (gchar *)"org.freedesktop.hostname1",
121 G_BUS_NAME_OWNER_FLAGS_NONE,
122 on_bus_acquired,
123 on_name_acquired,
124 on_name_lost,
125 NULL,
126 NULL);
496f5d66 127
6de394d4 128 //TODO: malloc and return reference as if a main() closed
129 return err;
780e1f19 130}