include glib-unix.h in interface files
[systembsd.git] / src / interfaces / timedated / timedated.c
1 /*
2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <unistd.h>
18 #include <limits.h>
19
20 #include <sys/param.h>
21 #include <string.h>
22
23 #include <glib/gprintf.h>
24 #include <glib-unix.h>
25
26 #include "timedated-gen.h"
27
28 GPtrArray *timedated_freeable;
29 Timedate1 *timedated_interf;
30
31 /* --- begin method/property/signal code --- */
32
33 /*static gboolean
34 on_handle_set_hostname(Timedate1 *hn1_passed_interf,
35 GDBusMethodInvocation *invoc,
36 const gchar *greet,
37 gpointer data) {
38 return FALSE;
39 }*/
40
41 /*const gchar *
42 our_get_hostname() {
43
44 gchar *hostname_buf, *ret;
45 size_t hostname_divider;
46
47 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
48 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
49 g_ptr_array_add(timedated_freeable, hostname_buf);
50 g_ptr_array_add(timedated_freeable, ret);
51
52 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
53 return "";
54
55 hostname_divider = strcspn(hostname_buf, ".");
56
57 return strncpy(ret, hostname_buf, hostname_divider);
58 }*/
59
60 /* --- end method/property/signal code, begin bus/name handlers --- */
61
62 static void timedated_on_bus_acquired(GDBusConnection *conn,
63 const gchar *name,
64 gpointer user_data) {
65
66 g_print("got bus, name: %s\n", name);
67
68 }
69
70 static void timedated_on_name_acquired(GDBusConnection *conn,
71 const gchar *name,
72 gpointer user_data) {
73
74 g_print("got '%s' on system bus\n", name);
75
76 timedated_interf = timedate1_skeleton_new();
77
78 /* attach function pointers to generated struct's method handlers
79 g_signal_connect(timedated_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);*/
80
81 /* set our properties before export
82 timedate1_set_hostname(timedated_interf, our_get_hostname()); */
83
84 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(timedated_interf),
85 conn,
86 "/org/freedesktop/timedate1",
87 NULL)) {
88
89 g_printf("Failed to export Timedate1's interface!");
90 }
91
92 }
93
94 /* --- end bus/name handlers, begin misc functions --- */
95
96 /* free()'s */
97 void timedated_mem_clean() {
98
99 g_ptr_array_foreach(timedated_freeable, (GFunc) g_free, NULL);
100 g_ptr_array_free(timedated_freeable, TRUE);
101 }
102
103 static void timedated_on_name_lost(GDBusConnection *conn,
104 const gchar *name,
105 gpointer user_data) {
106
107 g_print("lost name %s, exiting...", name);
108
109 timedated_mem_clean();
110 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(timedated_interf));
111
112 }
113
114 int main() {
115
116 guint bus_descriptor;
117 GMainLoop *timedated_loop;
118
119 timedated_loop = g_main_loop_new(NULL, TRUE);
120 timedated_freeable = g_ptr_array_new();
121
122 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
123 "org.freedesktop.timedate1",
124 G_BUS_NAME_OWNER_FLAGS_NONE,
125 timedated_on_bus_acquired,
126 timedated_on_name_acquired,
127 timedated_on_name_lost,
128 NULL,
129 NULL);
130
131 g_main_loop_run(timedated_loop);
132 g_main_loop_unref(timedated_loop);
133
134 g_bus_unown_name(bus_descriptor);
135
136 timedated_mem_clean();
137
138 return 0;
139 }