1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* e-shell-settings-dialog.c |
---|
3 | * |
---|
4 | * Copyright (C) 2002 Ximian, Inc. |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the GNU General Public License as |
---|
8 | * published by the Free Software Foundation; either version 2 of the |
---|
9 | * License, or (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | * General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public |
---|
17 | * License along with this program; if not, write to the |
---|
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | * Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * Author: Ettore Perazzoli <ettore@ximian.com> |
---|
22 | */ |
---|
23 | |
---|
24 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include "e-shell-settings-dialog.h" |
---|
29 | |
---|
30 | #include "e-corba-config-page.h" |
---|
31 | |
---|
32 | #include "e-util/e-lang-utils.h" |
---|
33 | |
---|
34 | #include <gal/util/e-util.h> |
---|
35 | #include <gal/util/e-unicode-i18n.h> |
---|
36 | |
---|
37 | #include <bonobo/bonobo-widget.h> |
---|
38 | #include <bonobo/bonobo-exception.h> |
---|
39 | #include <liboaf/liboaf.h> |
---|
40 | |
---|
41 | #include <string.h> |
---|
42 | |
---|
43 | |
---|
44 | #define PARENT_TYPE e_multi_config_dialog_get_type () |
---|
45 | static EMultiConfigDialogClass *parent_class = NULL; |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | struct _EShellSettingsDialogPrivate { |
---|
50 | GHashTable *types; |
---|
51 | }; |
---|
52 | |
---|
53 | |
---|
54 | /* FIXME ugly hack to work around that sizing of invisible widgets is broken |
---|
55 | with Bonobo. */ |
---|
56 | |
---|
57 | static void |
---|
58 | set_dialog_size (EShellSettingsDialog *dialog) |
---|
59 | { |
---|
60 | GdkFont *font; |
---|
61 | int width, height; |
---|
62 | |
---|
63 | font = GTK_WIDGET (dialog)->style->font; |
---|
64 | width = gdk_string_width (font, "M") * 72; |
---|
65 | height = (font->ascent + font->descent) * 35; |
---|
66 | |
---|
67 | gtk_widget_set_usize (GTK_WIDGET (dialog), width, height); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /* Page handling. */ |
---|
72 | |
---|
73 | struct _Page { |
---|
74 | char *title; |
---|
75 | char *description; |
---|
76 | GdkPixbuf *icon; |
---|
77 | OAF_Property *type; |
---|
78 | int priority; |
---|
79 | EConfigPage *page_widget; |
---|
80 | }; |
---|
81 | typedef struct _Page Page; |
---|
82 | |
---|
83 | static Page * |
---|
84 | page_new (const char *title, |
---|
85 | const char *description, |
---|
86 | GdkPixbuf *icon, |
---|
87 | OAF_Property *type, |
---|
88 | int priority, |
---|
89 | EConfigPage *page_widget) |
---|
90 | { |
---|
91 | Page *page; |
---|
92 | |
---|
93 | if (icon != NULL) |
---|
94 | gdk_pixbuf_ref (icon); |
---|
95 | |
---|
96 | page = g_new (Page, 1); |
---|
97 | page->title = g_strdup (title); |
---|
98 | page->description = g_strdup (description); |
---|
99 | page->icon = icon; |
---|
100 | page->type = type; |
---|
101 | page->priority = priority; |
---|
102 | page->page_widget = page_widget; |
---|
103 | |
---|
104 | return page; |
---|
105 | } |
---|
106 | |
---|
107 | static void |
---|
108 | page_free (Page *page) |
---|
109 | { |
---|
110 | g_free (page->title); |
---|
111 | g_free (page->description); |
---|
112 | |
---|
113 | if (page->icon != NULL) |
---|
114 | gdk_pixbuf_unref (page->icon); |
---|
115 | |
---|
116 | g_free (page); |
---|
117 | } |
---|
118 | |
---|
119 | static int |
---|
120 | compare_page_func (const void *a, |
---|
121 | const void *b) |
---|
122 | { |
---|
123 | const Page *page_a; |
---|
124 | const Page *page_b; |
---|
125 | |
---|
126 | page_a = (const Page *) a; |
---|
127 | page_b = (const Page *) b; |
---|
128 | |
---|
129 | if (page_a->priority == page_b->priority) |
---|
130 | return strcmp (page_a->title, page_b->title); |
---|
131 | |
---|
132 | return page_a->priority - page_b->priority; |
---|
133 | } |
---|
134 | |
---|
135 | static GList * |
---|
136 | sort_page_list (GList *list) |
---|
137 | { |
---|
138 | return g_list_sort (list, compare_page_func); |
---|
139 | } |
---|
140 | |
---|
141 | static void |
---|
142 | load_pages (EShellSettingsDialog *dialog) |
---|
143 | { |
---|
144 | EShellSettingsDialogPrivate *priv; |
---|
145 | OAF_ServerInfoList *control_list; |
---|
146 | CORBA_Environment ev; |
---|
147 | GSList *language_list; |
---|
148 | GList *page_list; |
---|
149 | GList *p; |
---|
150 | int i, j; |
---|
151 | |
---|
152 | priv = dialog->priv; |
---|
153 | |
---|
154 | CORBA_exception_init (&ev); |
---|
155 | |
---|
156 | control_list = oaf_query ("defined(evolution:config_item:title)", NULL, &ev); |
---|
157 | if (ev._major != CORBA_NO_EXCEPTION || control_list == NULL) { |
---|
158 | g_warning ("Cannot load configuration pages -- %s", ev._repo_id); |
---|
159 | CORBA_exception_free (&ev); |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | language_list = e_get_language_list (); |
---|
164 | |
---|
165 | page_list = NULL; |
---|
166 | for (i = 0; i < control_list->_length; i ++) { |
---|
167 | CORBA_Object corba_object; |
---|
168 | OAF_ServerInfo *info; |
---|
169 | const char *title; |
---|
170 | const char *description; |
---|
171 | const char *icon_path; |
---|
172 | const char *priority_string; |
---|
173 | OAF_Property *type; |
---|
174 | int priority; |
---|
175 | GdkPixbuf *icon; |
---|
176 | |
---|
177 | info = & control_list->_buffer[i]; |
---|
178 | |
---|
179 | title = oaf_server_info_prop_lookup (info, "evolution:config_item:title", language_list); |
---|
180 | description = oaf_server_info_prop_lookup (info, "evolution:config_item:description", language_list); |
---|
181 | icon_path = oaf_server_info_prop_lookup (info, "evolution:config_item:icon_name", NULL); |
---|
182 | type = oaf_server_info_prop_find (info, "evolution:config_item:type"); |
---|
183 | priority_string = oaf_server_info_prop_lookup (info, "evolution:config_item:priority", NULL); |
---|
184 | |
---|
185 | if (icon_path == NULL) { |
---|
186 | icon = NULL; |
---|
187 | } else { |
---|
188 | if (g_path_is_absolute (icon_path)) { |
---|
189 | icon = gdk_pixbuf_new_from_file (icon_path); |
---|
190 | } else { |
---|
191 | char *real_icon_path; |
---|
192 | |
---|
193 | real_icon_path = g_concat_dir_and_file (EVOLUTION_IMAGES, icon_path); |
---|
194 | icon = gdk_pixbuf_new_from_file (real_icon_path); |
---|
195 | g_free (real_icon_path); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if (type != NULL && type->v._d != OAF_P_STRINGV) |
---|
200 | type = NULL; |
---|
201 | if (priority_string == NULL) |
---|
202 | priority = 0xffff; |
---|
203 | else |
---|
204 | priority = atoi (priority_string); |
---|
205 | |
---|
206 | corba_object = oaf_activate_from_id ((char *) info->iid, 0, NULL, &ev); |
---|
207 | |
---|
208 | if (! BONOBO_EX (&ev)) { |
---|
209 | Page *page; |
---|
210 | |
---|
211 | page = page_new (title, description, icon, type, priority, |
---|
212 | E_CONFIG_PAGE (e_corba_config_page_new_from_objref (corba_object))); |
---|
213 | |
---|
214 | page_list = g_list_prepend (page_list, page); |
---|
215 | } else { |
---|
216 | g_warning ("Cannot activate %s -- %s", info->iid, BONOBO_EX_ID (&ev)); |
---|
217 | } |
---|
218 | |
---|
219 | if (icon != NULL) |
---|
220 | gdk_pixbuf_unref (icon); |
---|
221 | } |
---|
222 | |
---|
223 | page_list = sort_page_list (page_list); |
---|
224 | for (p = page_list, i = 0; p != NULL; p = p->next, i++) { |
---|
225 | Page *page; |
---|
226 | |
---|
227 | page = (Page *) p->data; |
---|
228 | |
---|
229 | e_multi_config_dialog_add_page (E_MULTI_CONFIG_DIALOG (dialog), |
---|
230 | page->title, |
---|
231 | page->description, |
---|
232 | page->icon, |
---|
233 | page->page_widget); |
---|
234 | |
---|
235 | if (page->type != NULL) { |
---|
236 | GNOME_stringlist list = page->type->v._u.value_stringv; |
---|
237 | |
---|
238 | for (j = 0; j < list._length; j++) { |
---|
239 | if (g_hash_table_lookup (priv->types, list._buffer[j]) == NULL) |
---|
240 | g_hash_table_insert (priv->types, g_strdup (list._buffer[j]), |
---|
241 | GINT_TO_POINTER (i)); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | page_free (page); |
---|
247 | } |
---|
248 | |
---|
249 | g_list_free (page_list); |
---|
250 | e_free_language_list (language_list); |
---|
251 | CORBA_free (control_list); |
---|
252 | |
---|
253 | CORBA_exception_free (&ev); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | /* GtkWidget methods. */ |
---|
258 | |
---|
259 | static void |
---|
260 | impl_realize (GtkWidget *widget) |
---|
261 | { |
---|
262 | EShellSettingsDialog *dialog; |
---|
263 | |
---|
264 | dialog = E_SHELL_SETTINGS_DIALOG (widget); |
---|
265 | |
---|
266 | set_dialog_size (dialog); |
---|
267 | |
---|
268 | (* GTK_WIDGET_CLASS (parent_class)->realize) (widget); |
---|
269 | } |
---|
270 | |
---|
271 | |
---|
272 | /* GtkObject methods. */ |
---|
273 | |
---|
274 | static gboolean |
---|
275 | destroy_type_entry (gpointer key, gpointer value, gpointer data) |
---|
276 | { |
---|
277 | g_free (key); |
---|
278 | |
---|
279 | return TRUE; |
---|
280 | } |
---|
281 | |
---|
282 | static void |
---|
283 | impl_destroy (GtkObject *object) |
---|
284 | { |
---|
285 | EShellSettingsDialog *dialog; |
---|
286 | EShellSettingsDialogPrivate *priv; |
---|
287 | |
---|
288 | dialog = E_SHELL_SETTINGS_DIALOG (object); |
---|
289 | priv = dialog->priv; |
---|
290 | |
---|
291 | g_hash_table_foreach_remove (priv->types, destroy_type_entry, NULL); |
---|
292 | g_hash_table_destroy (priv->types); |
---|
293 | |
---|
294 | g_free (priv); |
---|
295 | |
---|
296 | (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); |
---|
297 | } |
---|
298 | |
---|
299 | |
---|
300 | static void |
---|
301 | class_init (EShellSettingsDialog *class) |
---|
302 | { |
---|
303 | GtkObjectClass *object_class; |
---|
304 | GtkWidgetClass *widget_class; |
---|
305 | |
---|
306 | object_class = GTK_OBJECT_CLASS (class); |
---|
307 | object_class->destroy = impl_destroy; |
---|
308 | |
---|
309 | widget_class = GTK_WIDGET_CLASS (class); |
---|
310 | widget_class->realize = impl_realize; |
---|
311 | |
---|
312 | parent_class = gtk_type_class (PARENT_TYPE); |
---|
313 | } |
---|
314 | |
---|
315 | static void |
---|
316 | init (EShellSettingsDialog *dialog) |
---|
317 | { |
---|
318 | EShellSettingsDialogPrivate *priv; |
---|
319 | |
---|
320 | priv = g_new (EShellSettingsDialogPrivate, 1); |
---|
321 | priv->types = g_hash_table_new (g_str_hash, g_str_equal); |
---|
322 | |
---|
323 | dialog->priv = priv; |
---|
324 | |
---|
325 | load_pages (dialog); |
---|
326 | |
---|
327 | gtk_window_set_title (GTK_WINDOW (dialog), _("Evolution Settings")); |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | GtkWidget * |
---|
332 | e_shell_settings_dialog_new () |
---|
333 | { |
---|
334 | EShellSettingsDialog *new; |
---|
335 | |
---|
336 | new = gtk_type_new (e_shell_settings_dialog_get_type ()); |
---|
337 | |
---|
338 | return GTK_WIDGET (new); |
---|
339 | } |
---|
340 | |
---|
341 | void |
---|
342 | e_shell_settings_dialog_show_type (EShellSettingsDialog *dialog, const char *type) |
---|
343 | { |
---|
344 | EShellSettingsDialogPrivate *priv; |
---|
345 | gpointer key, value; |
---|
346 | int page; |
---|
347 | |
---|
348 | g_return_if_fail (dialog != NULL); |
---|
349 | g_return_if_fail (E_IS_SHELL_SETTINGS_DIALOG (dialog)); |
---|
350 | g_return_if_fail (type != NULL); |
---|
351 | |
---|
352 | priv = dialog->priv; |
---|
353 | |
---|
354 | if (!g_hash_table_lookup_extended (priv->types, type, &key, &value)) { |
---|
355 | char *slash, *supertype; |
---|
356 | |
---|
357 | slash = strchr (type, '/'); |
---|
358 | if (slash) { |
---|
359 | supertype = g_strndup (type, slash - type); |
---|
360 | value = g_hash_table_lookup (priv->types, type); |
---|
361 | g_free (supertype); |
---|
362 | } else |
---|
363 | value = NULL; |
---|
364 | } |
---|
365 | page = GPOINTER_TO_INT (value); |
---|
366 | |
---|
367 | e_multi_config_dialog_show_page (E_MULTI_CONFIG_DIALOG (dialog), page); |
---|
368 | } |
---|
369 | |
---|
370 | |
---|
371 | E_MAKE_TYPE (e_shell_settings_dialog, "EShellSettingsDialog", EShellSettingsDialog, |
---|
372 | class_init, init, PARENT_TYPE) |
---|
373 | |
---|