include gprintf.h in hostnamed.c/localed.c to allow strict compilation
[systembsd.git] / src / interfaces / localed / localed.c
CommitLineData
3b82e3c1 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
5b005882 17#include <unistd.h>
18#include <limits.h>
19
4c04b514 20#include <glib/gprintf.h>
21
1e8c7c88 22#include "localed-gen.h"
5b005882 23
24GPtrArray *localed_freeable;
25GDBusNodeInfo *spect_data;
26localedLocale1 *localed_interf;
27
28/* begin method/property/signal code */
29
30/* TODO make sure these guys only work if called by root */
31
32/* Example:
33static gboolean
34on_handle_set_hostname(localedLocale1 *hn1_passed_interf,
35 GDBusMethodInvocation *invoc,
36 const gchar *greet,
37 gpointer data) {
38 return FALSE;
39} */
40
41/* note: all localed/locale1's properties are read-only,
42 * and do not need set_ functions, gdbus-codegen realized
43 * this from the XML and handled the to-be error of trying
44 * to set a read-only property's value
45 */
46
47/* Example:
48
49const gchar *
50our_get_hostname() {
51
52 return "TODO";
53}*/
54
55/* end method/property/signal code, begin bus/name handlers */
56
57static void localed_on_bus_acquired(GDBusConnection *conn,
58 const gchar *name,
59 gpointer user_data) {
60
61 g_print("got bus, name: %s\n", name);
62
63}
64
65static void localed_on_name_acquired(GDBusConnection *conn,
66 const gchar *name,
67 gpointer user_data) {
68
69 g_print("got '%s' on system bus\n", name);
70
71 localed_interf = localed_locale1_skeleton_new();
72
73 /* attach function pointers to generated struct's method handlers */
74 /*g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
75 g_signal_connect(localed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
76 g_signal_connect(localed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
77 g_signal_connect(localed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
78 g_signal_connect(localed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); */
79
80 /* set our properties before export */
81 /*localed_locale1_set_hostname(localed_interf, our_get_hostname());
82 localed_locale1_set_static_hostname(localed_interf, our_get_static_hostname());
83 localed_locale1_set_pretty_hostname(localed_interf, our_get_pretty_hostname());
84 localed_locale1_set_chassis(localed_interf, our_get_chassis());
85 localed_locale1_set_icon_name(localed_interf, our_get_icon_name());
86 localed_locale1_set_kernel_name(localed_interf, our_get_kernel_name());
87 localed_locale1_set_kernel_version(localed_interf, our_get_kernel_version());
88 localed_locale1_set_kernel_release(localed_interf, our_get_kernel_release());
89 localed_locale1_set_operating_system_cpename(localed_interf, our_get_os_cpename());
90 localed_locale1_set_operating_system_pretty_name(localed_interf, our_get_os_pretty_name()); */
91
92 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf),
93 conn,
94 "/org/freedesktop/locale1",
95 NULL)) {
96
97 g_printf("failed to export locale1's interface on system bus!");
98 }
99
100}
101
1e8c7c88 102/* free()'s */
103void localed_mem_clean() {
104
105 g_ptr_array_foreach(localed_freeable, (GFunc) g_free, NULL);
106}
107
5b005882 108static void localed_on_name_lost(GDBusConnection *conn,
109 const gchar *name,
110 gpointer user_data) {
111
112 g_print("lost name %s, exiting...", name);
113
114 localed_mem_clean();
115 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf));
116
117 /* TODO exit through g_main_loop properly... */
118}
119
120/* safe call to try and start localed */
7f0a0212 121void localed_init() {
5b005882 122
123 guint bus_descriptor;
124 GError *err = NULL;
125 gchar **localed_ispect_xml;
126 gchar *localed_joined_xml;
127
128 localed_freeable = g_ptr_array_new();
129 localed_ispect_xml = g_malloc(3000);
130
131 g_file_get_contents("conf/localed-ispect.xml", localed_ispect_xml, NULL, NULL);
132 localed_joined_xml = g_strjoinv("\n", localed_ispect_xml);
133 spect_data = g_dbus_node_info_new_for_xml(localed_joined_xml, NULL);
134
135 g_free(localed_ispect_xml);
136 g_ptr_array_add(localed_freeable, localed_joined_xml);
137
138 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
139 "org.freedesktop.locale1",
140 G_BUS_NAME_OWNER_FLAGS_NONE,
141 localed_on_bus_acquired,
142 localed_on_name_acquired,
143 localed_on_name_lost,
144 NULL,
145 NULL);
146
147 /* TODO: malloc and return reference as if a main() closed */
5b005882 148}
149
1e8c7c88 150int main() {
151
152 GMainLoop *localed_loop;
153 localed_loop = g_main_loop_new(NULL, TRUE);
5b005882 154
1e8c7c88 155 /* config stuff here */
156
157 localed_init();
158
159 g_main_loop_run(localed_loop);
160 g_main_loop_unref(localed_loop);
161
162 return 0;
5b005882 163}
164