1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation; either version 2 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | * |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <string.h> |
---|
28 | #include <signal.h> |
---|
29 | #include <sys/stat.h> |
---|
30 | #include <sys/types.h> |
---|
31 | |
---|
32 | #include <glib.h> |
---|
33 | #include <glib/gi18n.h> |
---|
34 | #include <glib/gstdio.h> |
---|
35 | #include <glib-object.h> |
---|
36 | #define DBUS_API_SUBJECT_TO_CHANGE |
---|
37 | #include <dbus/dbus-glib.h> |
---|
38 | #include <dbus/dbus-glib-lowlevel.h> |
---|
39 | |
---|
40 | #include "gdm-settings.h" |
---|
41 | #include "gdm-settings-glue.h" |
---|
42 | |
---|
43 | #include "gdm-settings-desktop-backend.h" |
---|
44 | |
---|
45 | #include "gdm-marshal.h" |
---|
46 | |
---|
47 | #define GDM_DBUS_PATH "/org/gnome/DisplayManager" |
---|
48 | #define GDM_SETTINGS_DBUS_PATH GDM_DBUS_PATH "/Settings" |
---|
49 | #define GDM_SETTINGS_DBUS_NAME "org.gnome.DisplayManager.Settings" |
---|
50 | |
---|
51 | #define GDM_SETTINGS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_SETTINGS, GdmSettingsPrivate)) |
---|
52 | |
---|
53 | struct GdmSettingsPrivate |
---|
54 | { |
---|
55 | DBusGConnection *connection; |
---|
56 | GdmSettingsBackend *backend; |
---|
57 | }; |
---|
58 | |
---|
59 | enum { |
---|
60 | VALUE_CHANGED, |
---|
61 | LAST_SIGNAL |
---|
62 | }; |
---|
63 | |
---|
64 | static guint signals [LAST_SIGNAL] = { 0, }; |
---|
65 | |
---|
66 | static void gdm_settings_class_init (GdmSettingsClass *klass); |
---|
67 | static void gdm_settings_init (GdmSettings *settings); |
---|
68 | static void gdm_settings_finalize (GObject *object); |
---|
69 | |
---|
70 | static gpointer settings_object = NULL; |
---|
71 | |
---|
72 | G_DEFINE_TYPE (GdmSettings, gdm_settings, G_TYPE_OBJECT) |
---|
73 | |
---|
74 | GQuark |
---|
75 | gdm_settings_error_quark (void) |
---|
76 | { |
---|
77 | static GQuark ret = 0; |
---|
78 | if (ret == 0) { |
---|
79 | ret = g_quark_from_static_string ("gdm_settings_error"); |
---|
80 | } |
---|
81 | |
---|
82 | return ret; |
---|
83 | } |
---|
84 | |
---|
85 | /* |
---|
86 | dbus-send --system --print-reply --dest=org.gnome.DisplayManager /org/gnome/DisplayManager/Settings org.gnome.DisplayManager.Settings.GetValue string:"xdmcp/Enable" |
---|
87 | */ |
---|
88 | |
---|
89 | gboolean |
---|
90 | gdm_settings_get_value (GdmSettings *settings, |
---|
91 | const char *key, |
---|
92 | char **value, |
---|
93 | GError **error) |
---|
94 | { |
---|
95 | GError *local_error; |
---|
96 | gboolean res; |
---|
97 | |
---|
98 | g_return_val_if_fail (GDM_IS_SETTINGS (settings), FALSE); |
---|
99 | g_return_val_if_fail (key != NULL, FALSE); |
---|
100 | |
---|
101 | local_error = NULL; |
---|
102 | res = gdm_settings_backend_get_value (settings->priv->backend, |
---|
103 | key, |
---|
104 | value, |
---|
105 | &local_error); |
---|
106 | if (! res) { |
---|
107 | g_propagate_error (error, local_error); |
---|
108 | } |
---|
109 | |
---|
110 | return res; |
---|
111 | } |
---|
112 | |
---|
113 | /* |
---|
114 | dbus-send --system --print-reply --dest=org.gnome.DisplayManager /org/gnome/DisplayManager/Settings org.gnome.DisplayManager.Settings.SetValue string:"xdmcp/Enable" string:"false" |
---|
115 | */ |
---|
116 | |
---|
117 | gboolean |
---|
118 | gdm_settings_set_value (GdmSettings *settings, |
---|
119 | const char *key, |
---|
120 | const char *value, |
---|
121 | GError **error) |
---|
122 | { |
---|
123 | GError *local_error; |
---|
124 | gboolean res; |
---|
125 | |
---|
126 | g_return_val_if_fail (GDM_IS_SETTINGS (settings), FALSE); |
---|
127 | g_return_val_if_fail (key != NULL, FALSE); |
---|
128 | |
---|
129 | g_debug ("Setting value %s", key); |
---|
130 | |
---|
131 | local_error = NULL; |
---|
132 | res = gdm_settings_backend_set_value (settings->priv->backend, |
---|
133 | key, |
---|
134 | value, |
---|
135 | &local_error); |
---|
136 | if (! res) { |
---|
137 | g_propagate_error (error, local_error); |
---|
138 | } |
---|
139 | |
---|
140 | return res; |
---|
141 | } |
---|
142 | |
---|
143 | static gboolean |
---|
144 | register_settings (GdmSettings *settings) |
---|
145 | { |
---|
146 | GError *error = NULL; |
---|
147 | |
---|
148 | error = NULL; |
---|
149 | settings->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); |
---|
150 | if (settings->priv->connection == NULL) { |
---|
151 | if (error != NULL) { |
---|
152 | g_critical ("error getting system bus: %s", error->message); |
---|
153 | g_error_free (error); |
---|
154 | } |
---|
155 | exit (1); |
---|
156 | } |
---|
157 | |
---|
158 | dbus_g_connection_register_g_object (settings->priv->connection, GDM_SETTINGS_DBUS_PATH, G_OBJECT (settings)); |
---|
159 | |
---|
160 | return TRUE; |
---|
161 | } |
---|
162 | |
---|
163 | /* |
---|
164 | dbus-send --system --print-reply --dest=org.gnome.DisplayManager /org/gnome/DisplayManager/Settings org.freedesktop.DBus.Introspectable.Introspect |
---|
165 | */ |
---|
166 | |
---|
167 | static void |
---|
168 | gdm_settings_class_init (GdmSettingsClass *klass) |
---|
169 | { |
---|
170 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
---|
171 | |
---|
172 | object_class->finalize = gdm_settings_finalize; |
---|
173 | |
---|
174 | signals [VALUE_CHANGED] = |
---|
175 | g_signal_new ("value-changed", |
---|
176 | G_TYPE_FROM_CLASS (object_class), |
---|
177 | G_SIGNAL_RUN_LAST, |
---|
178 | G_STRUCT_OFFSET (GdmSettingsClass, value_changed), |
---|
179 | NULL, |
---|
180 | NULL, |
---|
181 | gdm_marshal_VOID__STRING_STRING_STRING, |
---|
182 | G_TYPE_NONE, |
---|
183 | 3, |
---|
184 | G_TYPE_STRING, |
---|
185 | G_TYPE_STRING, |
---|
186 | G_TYPE_STRING); |
---|
187 | |
---|
188 | g_type_class_add_private (klass, sizeof (GdmSettingsPrivate)); |
---|
189 | |
---|
190 | dbus_g_object_type_install_info (GDM_TYPE_SETTINGS, &dbus_glib_gdm_settings_object_info); |
---|
191 | } |
---|
192 | |
---|
193 | static void |
---|
194 | backend_value_changed (GdmSettingsBackend *backend, |
---|
195 | const char *key, |
---|
196 | const char *old_value, |
---|
197 | const char *new_value, |
---|
198 | GdmSettings *settings) |
---|
199 | { |
---|
200 | g_debug ("Emitting value-changed %s %s %s", key, old_value, new_value); |
---|
201 | /* just proxy it */ |
---|
202 | g_signal_emit (settings, |
---|
203 | signals [VALUE_CHANGED], |
---|
204 | 0, |
---|
205 | key, |
---|
206 | old_value, |
---|
207 | new_value); |
---|
208 | } |
---|
209 | |
---|
210 | static void |
---|
211 | gdm_settings_init (GdmSettings *settings) |
---|
212 | { |
---|
213 | settings->priv = GDM_SETTINGS_GET_PRIVATE (settings); |
---|
214 | |
---|
215 | settings->priv->backend = gdm_settings_desktop_backend_new (); |
---|
216 | g_signal_connect (settings->priv->backend, |
---|
217 | "value-changed", |
---|
218 | G_CALLBACK (backend_value_changed), |
---|
219 | settings); |
---|
220 | } |
---|
221 | |
---|
222 | static void |
---|
223 | gdm_settings_finalize (GObject *object) |
---|
224 | { |
---|
225 | GdmSettings *settings; |
---|
226 | |
---|
227 | g_return_if_fail (object != NULL); |
---|
228 | g_return_if_fail (GDM_IS_SETTINGS (object)); |
---|
229 | |
---|
230 | settings = GDM_SETTINGS (object); |
---|
231 | |
---|
232 | g_return_if_fail (settings->priv != NULL); |
---|
233 | |
---|
234 | if (settings->priv->backend != NULL) { |
---|
235 | g_object_unref (settings->priv->backend); |
---|
236 | } |
---|
237 | |
---|
238 | G_OBJECT_CLASS (gdm_settings_parent_class)->finalize (object); |
---|
239 | } |
---|
240 | |
---|
241 | GdmSettings * |
---|
242 | gdm_settings_new (void) |
---|
243 | { |
---|
244 | if (settings_object != NULL) { |
---|
245 | g_object_ref (settings_object); |
---|
246 | } else { |
---|
247 | gboolean res; |
---|
248 | |
---|
249 | settings_object = g_object_new (GDM_TYPE_SETTINGS, NULL); |
---|
250 | g_object_add_weak_pointer (settings_object, |
---|
251 | (gpointer *) &settings_object); |
---|
252 | res = register_settings (settings_object); |
---|
253 | if (! res) { |
---|
254 | g_warning ("Unable to register settings"); |
---|
255 | g_object_unref (settings_object); |
---|
256 | return NULL; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | return GDM_SETTINGS (settings_object); |
---|
261 | } |
---|