(1) regenerate leaner genfiles, we're not using the object manager functionality...
[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
20#include <glib.h>
21#include <gio/gio.h>
22
23#include "localed.h"
24#include "localed-gen.c"
25
26GPtrArray *localed_freeable;
27GDBusNodeInfo *spect_data;
28localedLocale1 *localed_interf;
29
30/* begin method/property/signal code */
31
32/* TODO make sure these guys only work if called by root */
33
34/* Example:
35static gboolean
36on_handle_set_hostname(localedLocale1 *hn1_passed_interf,
37 GDBusMethodInvocation *invoc,
38 const gchar *greet,
39 gpointer data) {
40 return FALSE;
41} */
42
43/* note: all localed/locale1's properties are read-only,
44 * and do not need set_ functions, gdbus-codegen realized
45 * this from the XML and handled the to-be error of trying
46 * to set a read-only property's value
47 */
48
49/* Example:
50
51const gchar *
52our_get_hostname() {
53
54 return "TODO";
55}*/
56
57/* end method/property/signal code, begin bus/name handlers */
58
59static void localed_on_bus_acquired(GDBusConnection *conn,
60 const gchar *name,
61 gpointer user_data) {
62
63 g_print("got bus, name: %s\n", name);
64
65}
66
67static void localed_on_name_acquired(GDBusConnection *conn,
68 const gchar *name,
69 gpointer user_data) {
70
71 g_print("got '%s' on system bus\n", name);
72
73 localed_interf = localed_locale1_skeleton_new();
74
75 /* attach function pointers to generated struct's method handlers */
76 /*g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL);
77 g_signal_connect(localed_interf, "handle-set-static-hostname", G_CALLBACK(on_handle_set_static_hostname), NULL);
78 g_signal_connect(localed_interf, "handle-set-pretty-hostname", G_CALLBACK(on_handle_set_pretty_hostname), NULL);
79 g_signal_connect(localed_interf, "handle-set-chassis", G_CALLBACK(on_handle_set_chassis), NULL);
80 g_signal_connect(localed_interf, "handle-set-icon-name", G_CALLBACK(on_handle_set_icon_name), NULL); */
81
82 /* set our properties before export */
83 /*localed_locale1_set_hostname(localed_interf, our_get_hostname());
84 localed_locale1_set_static_hostname(localed_interf, our_get_static_hostname());
85 localed_locale1_set_pretty_hostname(localed_interf, our_get_pretty_hostname());
86 localed_locale1_set_chassis(localed_interf, our_get_chassis());
87 localed_locale1_set_icon_name(localed_interf, our_get_icon_name());
88 localed_locale1_set_kernel_name(localed_interf, our_get_kernel_name());
89 localed_locale1_set_kernel_version(localed_interf, our_get_kernel_version());
90 localed_locale1_set_kernel_release(localed_interf, our_get_kernel_release());
91 localed_locale1_set_operating_system_cpename(localed_interf, our_get_os_cpename());
92 localed_locale1_set_operating_system_pretty_name(localed_interf, our_get_os_pretty_name()); */
93
94 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf),
95 conn,
96 "/org/freedesktop/locale1",
97 NULL)) {
98
99 g_printf("failed to export locale1's interface on system bus!");
100 }
101
102}
103
104static void localed_on_name_lost(GDBusConnection *conn,
105 const gchar *name,
106 gpointer user_data) {
107
108 g_print("lost name %s, exiting...", name);
109
110 localed_mem_clean();
111 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf));
112
113 /* TODO exit through g_main_loop properly... */
114}
115
116/* safe call to try and start localed */
117GError *localed_init() {
118
119 guint bus_descriptor;
120 GError *err = NULL;
121 gchar **localed_ispect_xml;
122 gchar *localed_joined_xml;
123
124 localed_freeable = g_ptr_array_new();
125 localed_ispect_xml = g_malloc(3000);
126
127 g_file_get_contents("conf/localed-ispect.xml", localed_ispect_xml, NULL, NULL);
128 localed_joined_xml = g_strjoinv("\n", localed_ispect_xml);
129 spect_data = g_dbus_node_info_new_for_xml(localed_joined_xml, NULL);
130
131 g_free(localed_ispect_xml);
132 g_ptr_array_add(localed_freeable, localed_joined_xml);
133
134 bus_descriptor = g_bus_own_name(G_BUS_TYPE_SYSTEM,
135 "org.freedesktop.locale1",
136 G_BUS_NAME_OWNER_FLAGS_NONE,
137 localed_on_bus_acquired,
138 localed_on_name_acquired,
139 localed_on_name_lost,
140 NULL,
141 NULL);
142
143 /* TODO: malloc and return reference as if a main() closed */
144 return err;
145}
146
147/* free()'s */
148void localed_mem_clean() {
149
150 g_ptr_array_foreach(localed_freeable, (GFunc) g_free, NULL);
151}
152
153/* TODO figure out DMI variables on obsd */
154/*static gchar *guess_icon_name() {
155
156 gchar *filebuf = NULL;
157 gchar *ret = NULL;
158
159 #if defined(__i386__) || defined(__x86_64__)
160
161 Taken with a few minor changes from systemd's localed.c,
162 copyright 2011 Lennart Poettering.
163
164 See the SMBIOS Specification 2.7.1 section 7.4.1 for
165 details about the values listed here:
166
167 http://www.dmtf.org/sites/default/files/standards/documents/DSP0134_2.7.1.pdf
168
169
170 if (g_file_get_contents ("/sys/class/dmi/id/chassis_type", &filebuf, NULL, NULL)) {
171 switch (g_ascii_strtoull (filebuf, NULL, 10)) {
172 case 0x3:
173 case 0x4:
174 case 0x5:
175 case 0x6:
176 case 0x7:
177 ret = g_strdup ("computer-desktop");
178 goto out;
179 case 0x9:
180 case 0xA:
181 case 0xE:
182 ret = g_strdup ("computer-laptop");
183 goto out;
184 case 0x11:
185 case 0x17:
186 case 0x1C:
187 case 0x1D:
188 ret = g_strdup ("computer-server");
189 goto out;
190 }
191 }
192 #endif
193 ret = g_strdup ("computer");
194 out:
195 g_free (filebuf);
196 return ret;
197}*/