2 * Copyright (c) 2014 Ian Sutton <ian@kremlin.cc>
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.
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.
20 #include <sys/param.h>
23 #include <glib/gprintf.h>
24 #include <glib-unix.h>
26 #include "localed-gen.h"
28 GPtrArray
*localed_freeable
;
29 Locale1
*localed_interf
;
31 /* --- begin method/property/dbus signal code --- */
34 on_handle_set_hostname(Locale1 *hn1_passed_interf,
35 GDBusMethodInvocation *invoc,
44 gchar *hostname_buf, *ret;
45 size_t hostname_divider;
47 hostname_buf = (gchar*) g_malloc0(MAXHOSTNAMELEN);
48 ret = (gchar*) g_malloc0(MAXHOSTNAMELEN);
49 g_ptr_array_add(localed_freeable, hostname_buf);
50 g_ptr_array_add(localed_freeable, ret);
52 if(gethostname(hostname_buf, MAXHOSTNAMELEN))
55 hostname_divider = strcspn(hostname_buf, ".");
57 return strncpy(ret, hostname_buf, hostname_divider);
60 /* --- end method/property/dbus signal code, begin bus/name handlers --- */
62 static void localed_on_bus_acquired(GDBusConnection
*conn
,
66 g_print("got bus, name: %s\n", name
);
70 static void localed_on_name_acquired(GDBusConnection
*conn
,
74 g_print("got '%s' on system bus\n", name
);
76 localed_interf
= locale1_skeleton_new();
78 /* attach function pointers to generated struct's method handlers
79 g_signal_connect(localed_interf, "handle-set-hostname", G_CALLBACK(on_handle_set_hostname), NULL); */
81 /* set our properties before export
82 locale1_set_hostname(localed_interf, our_get_hostname()); */
84 if(!g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(localed_interf
),
86 "/org/freedesktop/locale1",
89 g_printf("Failed to export Locale1's interface!");
94 /* --- end bus/name handlers, begin misc unix functions --- */
97 void localed_mem_clean() {
99 g_ptr_array_foreach(localed_freeable
, (GFunc
) g_free
, NULL
);
100 g_ptr_array_free(localed_freeable
, TRUE
);
103 static void localed_on_name_lost(GDBusConnection
*conn
,
105 gpointer user_data
) {
107 g_print("lost name %s, exiting...", name
);
110 g_dbus_interface_skeleton_unexport(G_DBUS_INTERFACE_SKELETON(localed_interf
));
116 guint bus_descriptor
;
117 GMainLoop
*localed_loop
;
119 localed_loop
= g_main_loop_new(NULL
, TRUE
);
120 localed_freeable
= g_ptr_array_new();
122 bus_descriptor
= g_bus_own_name(G_BUS_TYPE_SYSTEM
,
123 "org.freedesktop.locale1",
124 G_BUS_NAME_OWNER_FLAGS_NONE
,
125 localed_on_bus_acquired
,
126 localed_on_name_acquired
,
127 localed_on_name_lost
,
131 g_main_loop_run(localed_loop
);
132 g_main_loop_unref(localed_loop
);
134 g_bus_unown_name(bus_descriptor
);