1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
2 | * |
---|
3 | * Copyright (C) 2008 Red Hat, Inc. |
---|
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 | * Written by: Ray Strode <rstrode@redhat.com> |
---|
20 | */ |
---|
21 | |
---|
22 | #include "config.h" |
---|
23 | |
---|
24 | #include <stdlib.h> |
---|
25 | #include <stdio.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <string.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <dirent.h> |
---|
30 | #include <sys/stat.h> |
---|
31 | |
---|
32 | #include <glib.h> |
---|
33 | #include <glib/gi18n.h> |
---|
34 | #include <glib/gstdio.h> |
---|
35 | #include <gtk/gtk.h> |
---|
36 | |
---|
37 | #include <gconf/gconf-client.h> |
---|
38 | |
---|
39 | #include "gdm-recent-option-widget.h" |
---|
40 | |
---|
41 | #define GDM_RECENT_OPTION_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_RECENT_OPTION_WIDGET, GdmRecentOptionWidgetPrivate)) |
---|
42 | |
---|
43 | struct GdmRecentOptionWidgetPrivate |
---|
44 | { |
---|
45 | GConfClient *gconf_client; |
---|
46 | char *gconf_key; |
---|
47 | guint notification_id; |
---|
48 | GdmRecentOptionLookupItemFunc lookup_item_func; |
---|
49 | |
---|
50 | int max_item_count; |
---|
51 | }; |
---|
52 | |
---|
53 | enum { |
---|
54 | PROP_0, |
---|
55 | PROP_MAX_ITEM_COUNT, |
---|
56 | }; |
---|
57 | |
---|
58 | enum { |
---|
59 | NUMBER_OF_SIGNALS |
---|
60 | }; |
---|
61 | |
---|
62 | static void gdm_recent_option_widget_class_init (GdmRecentOptionWidgetClass *klass); |
---|
63 | static void gdm_recent_option_widget_init (GdmRecentOptionWidget *option_widget); |
---|
64 | static void gdm_recent_option_widget_finalize (GObject *object); |
---|
65 | |
---|
66 | G_DEFINE_TYPE (GdmRecentOptionWidget, gdm_recent_option_widget, GDM_TYPE_OPTION_WIDGET) |
---|
67 | |
---|
68 | static GSList * |
---|
69 | gdm_recent_option_widget_get_list_from_gconf (GdmRecentOptionWidget *widget) |
---|
70 | { |
---|
71 | GConfValue *value; |
---|
72 | GSList *value_list; |
---|
73 | GSList *list; |
---|
74 | GSList *tmp; |
---|
75 | GError *lookup_error; |
---|
76 | |
---|
77 | lookup_error = NULL; |
---|
78 | value = gconf_client_get (widget->priv->gconf_client, |
---|
79 | widget->priv->gconf_key, |
---|
80 | &lookup_error); |
---|
81 | |
---|
82 | if (lookup_error != NULL) { |
---|
83 | g_warning ("could not get gconf key '%s': %s", |
---|
84 | widget->priv->gconf_key, |
---|
85 | lookup_error->message); |
---|
86 | g_error_free (lookup_error); |
---|
87 | return NULL; |
---|
88 | } |
---|
89 | |
---|
90 | if (value == NULL) { |
---|
91 | g_warning ("gconf key '%s' is unset", |
---|
92 | widget->priv->gconf_key); |
---|
93 | return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | if (value->type != GCONF_VALUE_LIST || |
---|
97 | gconf_value_get_list_type (value) != GCONF_VALUE_STRING) { |
---|
98 | g_warning ("gconf key is not a list of strings"); |
---|
99 | return NULL; |
---|
100 | } |
---|
101 | |
---|
102 | value_list = gconf_value_get_list (value); |
---|
103 | |
---|
104 | list = NULL; |
---|
105 | for (tmp = value_list; tmp != NULL; tmp = tmp->next) { |
---|
106 | const char *id; |
---|
107 | |
---|
108 | id = gconf_value_get_string ((GConfValue *)tmp->data); |
---|
109 | |
---|
110 | list = g_slist_prepend (list, g_strdup (id)); |
---|
111 | } |
---|
112 | list = g_slist_reverse (list); |
---|
113 | |
---|
114 | gconf_value_free (value); |
---|
115 | |
---|
116 | return list; |
---|
117 | } |
---|
118 | |
---|
119 | static GSList * |
---|
120 | truncate_list (GSList *list, |
---|
121 | int size) |
---|
122 | { |
---|
123 | GSList *tmp; |
---|
124 | GSList *next_node; |
---|
125 | |
---|
126 | tmp = g_slist_nth (list, size); |
---|
127 | do { |
---|
128 | next_node = tmp->next; |
---|
129 | |
---|
130 | g_free (tmp->data); |
---|
131 | list = g_slist_delete_link (list, tmp); |
---|
132 | tmp = next_node; |
---|
133 | } while (tmp != NULL); |
---|
134 | |
---|
135 | return list; |
---|
136 | } |
---|
137 | |
---|
138 | static void |
---|
139 | gdm_recent_option_widget_sync_items_from_gconf (GdmRecentOptionWidget *widget) |
---|
140 | { |
---|
141 | GSList *list; |
---|
142 | GSList *tmp; |
---|
143 | gboolean default_is_set; |
---|
144 | |
---|
145 | list = gdm_recent_option_widget_get_list_from_gconf (widget); |
---|
146 | |
---|
147 | if (widget->priv->max_item_count > 0 && g_slist_length (list) > widget->priv->max_item_count) { |
---|
148 | list = truncate_list (list, widget->priv->max_item_count); |
---|
149 | gconf_client_set_list (widget->priv->gconf_client, |
---|
150 | widget->priv->gconf_key, |
---|
151 | GCONF_VALUE_STRING, list, NULL); |
---|
152 | g_slist_foreach (list, (GFunc) g_free, NULL); |
---|
153 | g_slist_free (list); |
---|
154 | |
---|
155 | list = gdm_recent_option_widget_get_list_from_gconf (widget); |
---|
156 | } |
---|
157 | |
---|
158 | gdm_option_widget_remove_all_items (GDM_OPTION_WIDGET (widget)); |
---|
159 | default_is_set = FALSE; |
---|
160 | |
---|
161 | for (tmp = list; tmp != NULL; tmp = tmp->next) { |
---|
162 | const char *key; |
---|
163 | char *id; |
---|
164 | char *name; |
---|
165 | char *comment; |
---|
166 | |
---|
167 | key = (char *) tmp->data; |
---|
168 | |
---|
169 | id = widget->priv->lookup_item_func (widget, key, &name, &comment); |
---|
170 | |
---|
171 | if (id != NULL) { |
---|
172 | gboolean item_exists; |
---|
173 | |
---|
174 | item_exists = gdm_option_widget_lookup_item (GDM_OPTION_WIDGET (widget), id, NULL, NULL, NULL); |
---|
175 | |
---|
176 | if (item_exists) { |
---|
177 | continue; |
---|
178 | } |
---|
179 | |
---|
180 | gdm_option_widget_add_item (GDM_OPTION_WIDGET (widget), |
---|
181 | id, name, comment, |
---|
182 | GDM_OPTION_WIDGET_POSITION_MIDDLE); |
---|
183 | if (!default_is_set) { |
---|
184 | gdm_option_widget_set_active_item (GDM_OPTION_WIDGET (widget), |
---|
185 | id); |
---|
186 | default_is_set = TRUE; |
---|
187 | } |
---|
188 | |
---|
189 | g_free (name); |
---|
190 | g_free (comment); |
---|
191 | g_free (id); |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | g_slist_foreach (list, (GFunc) g_free, NULL); |
---|
196 | g_slist_free (list); |
---|
197 | } |
---|
198 | |
---|
199 | static void |
---|
200 | gdm_recent_option_widget_notify_func (GConfClient *client, |
---|
201 | guint connection_id, |
---|
202 | GConfEntry *entry, |
---|
203 | gpointer user_data) |
---|
204 | { |
---|
205 | gdm_recent_option_widget_sync_items_from_gconf (GDM_RECENT_OPTION_WIDGET (user_data)); |
---|
206 | } |
---|
207 | |
---|
208 | gboolean |
---|
209 | gdm_recent_option_widget_set_gconf_key (GdmRecentOptionWidget *widget, |
---|
210 | const char *gconf_key, |
---|
211 | GdmRecentOptionLookupItemFunc lookup_item_func, |
---|
212 | GError **error) |
---|
213 | { |
---|
214 | GError *notify_error; |
---|
215 | |
---|
216 | if (widget->priv->gconf_key != NULL && |
---|
217 | strcmp (widget->priv->gconf_key, gconf_key) == 0) { |
---|
218 | return TRUE; |
---|
219 | } |
---|
220 | |
---|
221 | if (widget->priv->notification_id != 0) { |
---|
222 | gconf_client_notify_remove (widget->priv->gconf_client, |
---|
223 | widget->priv->notification_id); |
---|
224 | |
---|
225 | widget->priv->notification_id = 0; |
---|
226 | } |
---|
227 | |
---|
228 | g_free (widget->priv->gconf_key); |
---|
229 | widget->priv->gconf_key = g_strdup (gconf_key); |
---|
230 | |
---|
231 | widget->priv->lookup_item_func = lookup_item_func; |
---|
232 | |
---|
233 | gdm_recent_option_widget_sync_items_from_gconf (widget); |
---|
234 | |
---|
235 | notify_error = NULL; |
---|
236 | widget->priv->notification_id = gconf_client_notify_add (widget->priv->gconf_client, |
---|
237 | gconf_key, |
---|
238 | (GConfClientNotifyFunc) |
---|
239 | gdm_recent_option_widget_notify_func, |
---|
240 | widget, NULL, ¬ify_error); |
---|
241 | if (notify_error != NULL) { |
---|
242 | g_propagate_error (error, notify_error); |
---|
243 | return FALSE; |
---|
244 | } |
---|
245 | |
---|
246 | return TRUE; |
---|
247 | } |
---|
248 | |
---|
249 | static void |
---|
250 | gdm_recent_option_widget_dispose (GObject *object) |
---|
251 | { |
---|
252 | G_OBJECT_CLASS (gdm_recent_option_widget_parent_class)->dispose (object); |
---|
253 | } |
---|
254 | |
---|
255 | static void |
---|
256 | gdm_recent_option_widget_set_property (GObject *object, |
---|
257 | guint prop_id, |
---|
258 | const GValue *value, |
---|
259 | GParamSpec *pspec) |
---|
260 | { |
---|
261 | GdmRecentOptionWidget *self; |
---|
262 | |
---|
263 | self = GDM_RECENT_OPTION_WIDGET (object); |
---|
264 | |
---|
265 | switch (prop_id) { |
---|
266 | case PROP_MAX_ITEM_COUNT: |
---|
267 | self->priv->max_item_count = g_value_get_int (value); |
---|
268 | break; |
---|
269 | |
---|
270 | default: |
---|
271 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
---|
272 | break; |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | static void |
---|
277 | gdm_recent_option_widget_get_property (GObject *object, |
---|
278 | guint prop_id, |
---|
279 | GValue *value, |
---|
280 | GParamSpec *pspec) |
---|
281 | { |
---|
282 | GdmRecentOptionWidget *self; |
---|
283 | |
---|
284 | self = GDM_RECENT_OPTION_WIDGET (object); |
---|
285 | |
---|
286 | switch (prop_id) { |
---|
287 | case PROP_MAX_ITEM_COUNT: |
---|
288 | g_value_set_int (value, |
---|
289 | self->priv->max_item_count); |
---|
290 | break; |
---|
291 | default: |
---|
292 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
---|
293 | break; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | static void |
---|
298 | gdm_recent_option_widget_class_init (GdmRecentOptionWidgetClass *klass) |
---|
299 | { |
---|
300 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
---|
301 | |
---|
302 | object_class->get_property = gdm_recent_option_widget_get_property; |
---|
303 | object_class->set_property = gdm_recent_option_widget_set_property; |
---|
304 | object_class->dispose = gdm_recent_option_widget_dispose; |
---|
305 | object_class->finalize = gdm_recent_option_widget_finalize; |
---|
306 | |
---|
307 | g_object_class_install_property (object_class, |
---|
308 | PROP_MAX_ITEM_COUNT, |
---|
309 | g_param_spec_int ("max-item-count", |
---|
310 | _("Max Item Count"), |
---|
311 | _("The maximum number of items to keep around in the list"), |
---|
312 | 0, G_MAXINT, 5, |
---|
313 | (G_PARAM_READWRITE | |
---|
314 | G_PARAM_CONSTRUCT))); |
---|
315 | |
---|
316 | |
---|
317 | g_type_class_add_private (klass, sizeof (GdmRecentOptionWidgetPrivate)); |
---|
318 | } |
---|
319 | |
---|
320 | static void |
---|
321 | gdm_recent_option_widget_init (GdmRecentOptionWidget *widget) |
---|
322 | { |
---|
323 | widget->priv = GDM_RECENT_OPTION_WIDGET_GET_PRIVATE (widget); |
---|
324 | |
---|
325 | widget->priv->gconf_client = gconf_client_get_default (); |
---|
326 | widget->priv->max_item_count = 5; |
---|
327 | } |
---|
328 | |
---|
329 | static void |
---|
330 | gdm_recent_option_widget_finalize (GObject *object) |
---|
331 | { |
---|
332 | GdmRecentOptionWidget *widget; |
---|
333 | |
---|
334 | g_return_if_fail (object != NULL); |
---|
335 | g_return_if_fail (GDM_IS_OPTION_WIDGET (object)); |
---|
336 | |
---|
337 | widget = GDM_RECENT_OPTION_WIDGET (object); |
---|
338 | |
---|
339 | g_return_if_fail (widget->priv != NULL); |
---|
340 | |
---|
341 | g_object_unref (widget->priv->gconf_client); |
---|
342 | |
---|
343 | G_OBJECT_CLASS (gdm_recent_option_widget_parent_class)->finalize (object); |
---|
344 | } |
---|
345 | |
---|
346 | GtkWidget * |
---|
347 | gdm_recent_option_widget_new (const char *label_text, |
---|
348 | int max_item_count) |
---|
349 | { |
---|
350 | GObject *object; |
---|
351 | |
---|
352 | object = g_object_new (GDM_TYPE_RECENT_OPTION_WIDGET, |
---|
353 | "label-text", label_text, |
---|
354 | "max-item-count", max_item_count, |
---|
355 | NULL); |
---|
356 | |
---|
357 | return GTK_WIDGET (object); |
---|
358 | } |
---|
359 | |
---|
360 | void |
---|
361 | gdm_recent_option_widget_add_item (GdmRecentOptionWidget *widget, |
---|
362 | const char *id) |
---|
363 | { |
---|
364 | GSList *list; |
---|
365 | |
---|
366 | list = gdm_recent_option_widget_get_list_from_gconf (widget); |
---|
367 | |
---|
368 | list = g_slist_prepend (list, g_strdup (id)); |
---|
369 | |
---|
370 | gconf_client_set_list (widget->priv->gconf_client, |
---|
371 | widget->priv->gconf_key, |
---|
372 | GCONF_VALUE_STRING, list, NULL); |
---|
373 | |
---|
374 | g_slist_foreach (list, (GFunc) g_free, NULL); |
---|
375 | g_slist_free (list); |
---|
376 | |
---|
377 | gdm_recent_option_widget_sync_items_from_gconf (widget); |
---|
378 | } |
---|