Updated Traditional Chinese translation.
[evolution.git] / shell / e-shell-settings-dialog.c
blob0b96b2cf63ca7e4671bad3fe8434c0dfd8188c0d
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* e-shell-settings-dialog.c
4 * Copyright (C) 2002 Ximian, Inc.
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.
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.
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.
21 * Author: Ettore Perazzoli <ettore@ximian.com>
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include "e-shell-settings-dialog.h"
30 #include "e-corba-config-page.h"
31 #include <e-util/e-icon-factory.h>
33 #include <bonobo/bonobo-widget.h>
34 #include <bonobo/bonobo-exception.h>
36 #include <bonobo-activation/bonobo-activation.h>
38 #include <string.h>
40 struct _EShellSettingsDialogPrivate {
41 GHashTable *types;
44 G_DEFINE_TYPE (EShellSettingsDialog, e_shell_settings_dialog, E_TYPE_MULTI_CONFIG_DIALOG)
47 /* FIXME ugly hack to work around that sizing of invisible widgets is broken
48 with Bonobo. */
50 static void
51 set_dialog_size (EShellSettingsDialog *dialog)
53 PangoLayout *layout;
54 PangoContext *context;
55 PangoFontMetrics *metrics;
56 int width, height;
58 layout = gtk_widget_create_pango_layout (GTK_WIDGET (dialog), "M");
59 context = pango_layout_get_context (layout);
60 metrics = pango_context_get_metrics (context,
61 gtk_widget_get_style (GTK_WIDGET (dialog))->font_desc,
62 pango_context_get_language (context));
64 pango_layout_get_pixel_size (layout, &width, NULL);
66 width *= 60;
67 height = PANGO_PIXELS (pango_font_metrics_get_ascent (metrics)
68 + pango_font_metrics_get_descent (metrics)) * 30;
70 gtk_window_set_default_size((GtkWindow *)dialog, width, height);
71 g_object_unref (layout);
72 pango_font_metrics_unref (metrics);
76 /* Page handling. */
78 struct _Page {
79 char *title;
80 char *description;
81 GdkPixbuf *icon;
82 Bonobo_ActivationProperty *type;
83 int priority;
84 EConfigPage *page_widget;
86 typedef struct _Page Page;
88 static Page *
89 page_new (const char *title,
90 const char *description,
91 GdkPixbuf *icon,
92 Bonobo_ActivationProperty *type,
93 int priority,
94 EConfigPage *page_widget)
96 Page *page;
98 if (icon != NULL)
99 g_object_ref (icon);
101 page = g_new (Page, 1);
102 page->title = g_strdup (title);
103 page->description = g_strdup (description);
104 page->icon = icon;
105 page->type = type;
106 page->priority = priority;
107 page->page_widget = page_widget;
109 return page;
112 static void
113 page_free (Page *page)
115 g_free (page->title);
116 g_free (page->description);
118 if (page->icon != NULL)
119 g_object_unref (page->icon);
121 g_free (page);
124 static int
125 compare_page_func (const void *a,
126 const void *b)
128 const Page *page_a;
129 const Page *page_b;
131 page_a = (const Page *) a;
132 page_b = (const Page *) b;
134 if (page_a->priority == page_b->priority)
135 return strcmp (page_a->title, page_b->title);
137 return page_a->priority - page_b->priority;
140 static GList *
141 sort_page_list (GList *list)
143 return g_list_sort (list, compare_page_func);
146 static void
147 load_pages (EShellSettingsDialog *dialog)
149 EShellSettingsDialogPrivate *priv;
150 Bonobo_ServerInfoList *control_list;
151 CORBA_Environment ev;
152 const GList *l;
153 GSList *language_list;
154 GList *page_list;
155 GList *p;
156 int i, j;
158 priv = dialog->priv;
160 CORBA_exception_init (&ev);
162 control_list = bonobo_activation_query ("repo_ids.has('IDL:GNOME/Evolution/ConfigControl:" BASE_VERSION "')", NULL, &ev);
163 if (ev._major != CORBA_NO_EXCEPTION || control_list == NULL) {
164 g_warning ("Cannot load configuration pages -- %s", BONOBO_EX_REPOID (&ev));
165 CORBA_exception_free (&ev);
166 return;
169 CORBA_exception_free (&ev);
171 /* Great, one uses GList the other GSList (!) */
172 l = gnome_i18n_get_language_list("LC_MESSAGES");
173 for (language_list=NULL;l;l=l->next)
174 language_list = g_slist_append(language_list, l->data);
176 page_list = NULL;
177 for (i = 0; i < control_list->_length; i ++) {
178 CORBA_Object corba_object;
179 Bonobo_ServerInfo *info;
180 const char *title;
181 const char *description;
182 const char *icon_path;
183 const char *priority_string;
184 Bonobo_ActivationProperty *type;
185 int priority;
186 GdkPixbuf *icon;
188 CORBA_exception_init (&ev);
190 info = & control_list->_buffer[i];
192 title = bonobo_server_info_prop_lookup (info, "evolution2:config_item:title", language_list);
193 description = bonobo_server_info_prop_lookup (info, "evolution2:config_item:description", language_list);
194 icon_path = bonobo_server_info_prop_lookup (info, "evolution2:config_item:icon_name", NULL);
195 type = bonobo_server_info_prop_find (info, "evolution2:config_item:type");
196 priority_string = bonobo_server_info_prop_lookup (info, "evolution2:config_item:priority", NULL);
198 if (icon_path == NULL) {
199 icon = NULL;
200 } else {
201 if (g_path_is_absolute (icon_path)) {
202 icon = gdk_pixbuf_new_from_file (icon_path, NULL);
203 } else {
204 icon = e_icon_factory_get_icon (icon_path, E_ICON_SIZE_DIALOG);
208 if (type != NULL && type->v._d != Bonobo_ACTIVATION_P_STRINGV)
209 type = NULL;
210 if (priority_string == NULL)
211 priority = 0xffff;
212 else
213 priority = atoi (priority_string);
215 corba_object = bonobo_activation_activate_from_id ((char *) info->iid, 0, NULL, &ev);
217 if (! BONOBO_EX (&ev)) {
218 Page *page;
220 page = page_new (title, description, icon, type, priority,
221 E_CONFIG_PAGE (e_corba_config_page_new_from_objref (corba_object)));
223 page_list = g_list_prepend (page_list, page);
224 } else {
225 char *bonobo_ex_text = bonobo_exception_get_text (&ev);
226 g_warning ("Cannot activate %s -- %s", info->iid, bonobo_ex_text);
227 g_free (bonobo_ex_text);
230 if (icon != NULL)
231 g_object_unref (icon);
233 CORBA_exception_free (&ev);
235 g_slist_free(language_list);
237 page_list = sort_page_list (page_list);
238 for (p = page_list, i = 0; p != NULL; p = p->next, i++) {
239 Page *page;
241 page = (Page *) p->data;
243 e_multi_config_dialog_add_page (E_MULTI_CONFIG_DIALOG (dialog),
244 page->title,
245 page->description,
246 page->icon,
247 page->page_widget);
249 if (page->type != NULL) {
250 Bonobo_StringList list = page->type->v._u.value_stringv;
252 for (j = 0; j < list._length; j++) {
253 if (g_hash_table_lookup (priv->types, list._buffer[j]) == NULL)
254 g_hash_table_insert (priv->types, g_strdup (list._buffer[j]),
255 GINT_TO_POINTER (i));
260 page_free (page);
263 g_list_free (page_list);
264 CORBA_free (control_list);
268 /* GtkObject methods. */
270 static gboolean
271 destroy_type_entry (gpointer key, gpointer value, gpointer data)
273 g_free (key);
275 return TRUE;
278 static void
279 impl_finalize (GObject *object)
281 EShellSettingsDialog *dialog;
282 EShellSettingsDialogPrivate *priv;
284 dialog = E_SHELL_SETTINGS_DIALOG (object);
285 priv = dialog->priv;
287 g_hash_table_foreach_remove (priv->types, destroy_type_entry, NULL);
288 g_hash_table_destroy (priv->types);
290 g_free (priv);
292 (* G_OBJECT_CLASS (e_shell_settings_dialog_parent_class)->finalize) (object);
296 static void
297 e_shell_settings_dialog_class_init (EShellSettingsDialogClass *klass)
299 GObjectClass *object_class;
301 object_class = G_OBJECT_CLASS (klass);
302 object_class->finalize = impl_finalize;
305 static void
306 e_shell_settings_dialog_init (EShellSettingsDialog *dialog)
308 EShellSettingsDialogPrivate *priv;
310 priv = g_new (EShellSettingsDialogPrivate, 1);
311 priv->types = g_hash_table_new (g_str_hash, g_str_equal);
313 dialog->priv = priv;
315 load_pages (dialog);
316 set_dialog_size (dialog);
318 gtk_window_set_title (GTK_WINDOW (dialog), _("Evolution Settings"));
319 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
323 GtkWidget *
324 e_shell_settings_dialog_new ()
326 EShellSettingsDialog *new;
328 new = g_object_new (e_shell_settings_dialog_get_type (), NULL);
330 return GTK_WIDGET (new);
333 void
334 e_shell_settings_dialog_show_type (EShellSettingsDialog *dialog, const char *type)
336 EShellSettingsDialogPrivate *priv;
337 gpointer key, value;
338 int page;
340 g_return_if_fail (dialog != NULL);
341 g_return_if_fail (E_IS_SHELL_SETTINGS_DIALOG (dialog));
342 g_return_if_fail (type != NULL);
344 priv = dialog->priv;
346 if (!g_hash_table_lookup_extended (priv->types, type, &key, &value)) {
347 char *slash, *supertype;
349 slash = strchr (type, '/');
350 if (slash) {
351 supertype = g_strndup (type, slash - type);
352 value = g_hash_table_lookup (priv->types, type);
353 g_free (supertype);
354 } else
355 value = NULL;
357 page = GPOINTER_TO_INT (value);
359 e_multi_config_dialog_show_page (E_MULTI_CONFIG_DIALOG (dialog), page);