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