]> git.0d.be Git - empathy.git/blob - libempathy-gtk/gossip-account-widget-generic.c
[darcs-to-svn @ Adding salut profile]
[empathy.git] / libempathy-gtk / gossip-account-widget-generic.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (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 GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  * 
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  *          Martyn Russell <martyn@imendio.com>
22  */
23
24 #include <config.h>
25
26 #include <string.h>
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 #include <libmissioncontrol/mc-account.h>
32 #include <libmissioncontrol/mc-protocol.h>
33
34 #include <libempathy-gtk/gossip-ui-utils.h>
35
36 #include "gossip-account-widget-generic.h"
37
38 typedef struct {
39         McAccount     *account;
40
41         GtkWidget     *sw;
42         GtkWidget     *table_settings;
43         GtkSizeGroup  *size_group;
44
45         guint          n_rows;
46 } GossipAccountWidgetGeneric;
47
48 static gboolean account_widget_generic_entry_focus_cb         (GtkWidget                  *widget,
49                                                                GdkEventFocus              *event,
50                                                                GossipAccountWidgetGeneric *settings);
51 static void     account_widget_generic_int_changed_cb         (GtkWidget                  *widget,
52                                                                GossipAccountWidgetGeneric *settings);
53 static void     account_widget_generic_checkbutton_toggled_cb (GtkWidget                  *widget,
54                                                                GossipAccountWidgetGeneric *settings);
55 static gchar *  account_widget_generic_format_param_name      (const gchar                *param_name);
56 static void     account_widget_generic_setup_foreach          (McProtocolParam            *param,
57                                                                GossipAccountWidgetGeneric *settings);
58 static void     account_widget_generic_destroy_cb             (GtkWidget                  *widget,
59                                                                GossipAccountWidgetGeneric *settings);
60
61 static gboolean 
62 account_widget_generic_entry_focus_cb (GtkWidget                  *widget,
63                                        GdkEventFocus              *event,
64                                        GossipAccountWidgetGeneric *settings)
65 {
66         const gchar *str;
67         const gchar *param_name;
68
69         str = gtk_entry_get_text (GTK_ENTRY (widget));
70         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
71
72         mc_account_set_param_string (settings->account, param_name, str);
73
74         return FALSE;
75 }
76
77 static void
78 account_widget_generic_int_changed_cb (GtkWidget                  *widget,
79                                        GossipAccountWidgetGeneric *settings)
80 {
81         const gchar *param_name;
82         gint         value;
83
84         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
85         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
86
87         mc_account_set_param_int (settings->account, param_name, value);
88 }
89
90 static void  
91 account_widget_generic_checkbutton_toggled_cb (GtkWidget                  *widget,
92                                                GossipAccountWidgetGeneric *settings)
93 {
94         gboolean     active;
95         const gchar *param_name;
96
97         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
98         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
99
100         mc_account_set_param_boolean (settings->account, param_name, active);
101 }
102
103 static gchar *
104 account_widget_generic_format_param_name (const gchar *param_name)
105 {
106         gchar *str;
107         gchar *p;
108
109         str = g_strdup (param_name);
110         
111         if (str && g_ascii_isalpha (str[0])) {
112                 str[0] = g_ascii_toupper (str[0]);
113         }
114         
115         while ((p = strchr (str, '-')) != NULL) {
116                 if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
117                         p[0] = ' ';
118                         p[1] = g_ascii_toupper (p[1]);
119                 }
120
121                 p++;
122         }
123         
124         return str;
125 }
126
127 static void
128 account_widget_generic_setup_foreach (McProtocolParam            *param,
129                                       GossipAccountWidgetGeneric *settings)
130 {
131         GtkWidget *widget;
132         gchar     *param_name_formatted;
133
134         param_name_formatted = account_widget_generic_format_param_name (param->name);
135
136         gtk_table_resize (GTK_TABLE (settings->table_settings),
137                           ++settings->n_rows,
138                           2);
139
140         if (param->signature[0] == 's') {
141                 gchar *str = NULL;
142
143                 str = g_strdup_printf (_("%s:"), param_name_formatted);
144                 widget = gtk_label_new (str);
145                 g_free (str);
146
147                 gtk_size_group_add_widget (settings->size_group, widget);
148                 gtk_table_attach (GTK_TABLE (settings->table_settings),
149                                   widget,
150                                   0, 1,
151                                   settings->n_rows - 1, settings->n_rows,
152                                   GTK_FILL, 0,
153                                   0, 0);
154
155                 str = NULL;
156                 widget = gtk_entry_new ();
157                 mc_account_get_param_string (settings->account,
158                                              param->name,
159                                              &str);
160                 if (str) {
161                         gtk_entry_set_text (GTK_ENTRY (widget), str);
162                         g_free (str);
163                 }
164
165                 if (strstr (param->name, "password")) {
166                         gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
167                 }
168
169                 g_signal_connect (widget, "focus-out-event",
170                                   G_CALLBACK (account_widget_generic_entry_focus_cb),
171                                   settings);
172
173                 gtk_table_attach (GTK_TABLE (settings->table_settings),
174                                   widget,
175                                   1, 2,
176                                   settings->n_rows - 1, settings->n_rows,
177                                   GTK_FILL | GTK_EXPAND, 0,
178                                   0, 0);
179         }
180         else if (param->signature[0] == 'q' ||
181                  param->signature[0] == 'n') {
182                 gchar *str = NULL;
183                 gint   value = 0;
184
185                 str = g_strdup_printf (_("%s:"), param_name_formatted);
186                 widget = gtk_label_new (str);
187                 g_free (str);
188
189                 gtk_size_group_add_widget (settings->size_group, widget);
190                 gtk_table_attach (GTK_TABLE (settings->table_settings),
191                                   widget,
192                                   0, 1,
193                                   settings->n_rows - 1, settings->n_rows,
194                                   GTK_FILL, 0,
195                                   0, 0);
196
197                 widget = gtk_spin_button_new_with_range (0, G_MAXINT, 1);
198                 mc_account_get_param_int (settings->account,
199                                           param->name,
200                                           &value);
201                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
202
203                 g_signal_connect (widget, "value-changed",
204                                   G_CALLBACK (account_widget_generic_int_changed_cb),
205                                   settings);
206
207                 gtk_table_attach (GTK_TABLE (settings->table_settings),
208                                   widget,
209                                   1, 2,
210                                   settings->n_rows - 1, settings->n_rows,
211                                   GTK_FILL | GTK_EXPAND, 0,
212                                   0, 0);
213         }
214         else if (param->signature[0] == 'b') {
215                 gboolean value;
216
217                 mc_account_get_param_boolean (settings->account,
218                                               param->name,
219                                               &value);
220
221                 widget = gtk_check_button_new_with_label (param_name_formatted);
222                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
223
224                 g_signal_connect (widget, "toggled",
225                                   G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
226                                   settings);
227
228                 gtk_table_attach (GTK_TABLE (settings->table_settings),
229                                   widget,
230                                   0, 2,
231                                   settings->n_rows - 1, settings->n_rows,
232                                   GTK_FILL | GTK_EXPAND, 0,
233                                   0, 0);
234         } else {
235                 g_assert_not_reached ();
236         }
237
238         g_free (param_name_formatted);
239
240         g_object_set_data_full (G_OBJECT (widget), "param_name", 
241                                 g_strdup (param->name), g_free);
242 }
243
244 static void
245 accounts_widget_generic_setup (GossipAccountWidgetGeneric *settings)
246 {
247         McProtocol *protocol;
248         McProfile  *profile;
249         GSList     *params;
250
251         profile = mc_account_get_profile (settings->account);
252         protocol = mc_profile_get_protocol (profile);
253         params = mc_protocol_get_params (protocol);
254
255         g_slist_foreach (params,
256                          (GFunc) account_widget_generic_setup_foreach,
257                          settings);
258
259         g_slist_free (params);
260 }
261
262 static void
263 account_widget_generic_destroy_cb (GtkWidget                  *widget,
264                                    GossipAccountWidgetGeneric *settings)
265 {
266         g_object_unref (settings->account);
267         g_object_unref (settings->size_group);
268
269         g_free (settings);
270 }
271
272 GtkWidget *
273 gossip_account_widget_generic_new (McAccount *account,
274                                    GtkWidget *label_name)
275 {
276         GossipAccountWidgetGeneric *settings;
277
278         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
279         g_return_val_if_fail (GTK_IS_WIDGET (label_name), NULL);
280
281         settings = g_new0 (GossipAccountWidgetGeneric, 1);
282
283         settings->account = g_object_ref (account);
284
285         settings->table_settings = gtk_table_new (0, 2, FALSE);
286         gtk_table_set_row_spacings (GTK_TABLE (settings->table_settings), 6);
287         gtk_table_set_col_spacings (GTK_TABLE (settings->table_settings), 6);
288         settings->sw = gtk_scrolled_window_new (NULL, NULL);
289         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (settings->sw),
290                                         GTK_POLICY_AUTOMATIC,
291                                         GTK_POLICY_AUTOMATIC);
292         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (settings->sw),
293                                                settings->table_settings);
294
295         settings->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
296         if (label_name) {
297                 gtk_size_group_add_widget (settings->size_group, label_name);
298         }
299         
300         accounts_widget_generic_setup (settings);
301
302         g_signal_connect (settings->sw, "destroy",
303                           G_CALLBACK (account_widget_generic_destroy_cb),
304                           settings);
305
306         gtk_widget_show_all (settings->sw);
307
308         return settings->sw;
309 }