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