]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-generic.c
Remove useless function declarations
[empathy.git] / libempathy-gtk / empathy-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/empathy-debug.h>
36
37 #include "empathy-account-widget-generic.h"
38 #include "empathy-ui-utils.h"
39
40 #define DEBUG_DOMAIN "AccountWidgetGeneric"
41
42 typedef struct {
43         McAccount     *account;
44
45         GtkWidget     *sw;
46         GtkWidget     *table_settings;
47
48         guint          n_rows;
49 } EmpathyAccountWidgetGeneric;
50
51 static gboolean 
52 account_widget_generic_entry_focus_cb (GtkWidget                  *widget,
53                                        GdkEventFocus              *event,
54                                        EmpathyAccountWidgetGeneric *settings)
55 {
56         const gchar *str;
57         const gchar *param_name;
58
59         str = gtk_entry_get_text (GTK_ENTRY (widget));
60         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
61
62         mc_account_set_param_string (settings->account, param_name, str);
63
64         return FALSE;
65 }
66
67 static void
68 account_widget_generic_int_changed_cb (GtkWidget                  *widget,
69                                        EmpathyAccountWidgetGeneric *settings)
70 {
71         const gchar *param_name;
72         gint         value;
73
74         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
75         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
76
77         mc_account_set_param_int (settings->account, param_name, value);
78 }
79
80 static void  
81 account_widget_generic_checkbutton_toggled_cb (GtkWidget                  *widget,
82                                                EmpathyAccountWidgetGeneric *settings)
83 {
84         gboolean     active;
85         const gchar *param_name;
86
87         active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
88         param_name = g_object_get_data (G_OBJECT (widget), "param_name");
89
90         mc_account_set_param_boolean (settings->account, param_name, active);
91 }
92
93 static gchar *
94 account_widget_generic_format_param_name (const gchar *param_name)
95 {
96         gchar *str;
97         gchar *p;
98
99         str = g_strdup (param_name);
100         
101         if (str && g_ascii_isalpha (str[0])) {
102                 str[0] = g_ascii_toupper (str[0]);
103         }
104         
105         while ((p = strchr (str, '-')) != NULL) {
106                 if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
107                         p[0] = ' ';
108                         p[1] = g_ascii_toupper (p[1]);
109                 }
110
111                 p++;
112         }
113         
114         return str;
115 }
116
117 static void
118 account_widget_generic_setup_foreach (McProtocolParam            *param,
119                                       EmpathyAccountWidgetGeneric *settings)
120 {
121         GtkWidget *widget = NULL;
122         gchar     *param_name_formatted;
123
124         param_name_formatted = account_widget_generic_format_param_name (param->name);
125
126         gtk_table_resize (GTK_TABLE (settings->table_settings),
127                           ++settings->n_rows,
128                           2);
129
130         if (param->signature[0] == 's') {
131                 gchar *str = NULL;
132
133                 str = g_strdup_printf (_("%s:"), param_name_formatted);
134                 widget = gtk_label_new (str);
135                 gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
136                 g_free (str);
137
138                 gtk_table_attach (GTK_TABLE (settings->table_settings),
139                                   widget,
140                                   0, 1,
141                                   settings->n_rows - 1, settings->n_rows,
142                                   GTK_FILL, 0,
143                                   0, 0);
144
145                 str = NULL;
146                 widget = gtk_entry_new ();
147                 mc_account_get_param_string (settings->account,
148                                              param->name,
149                                              &str);
150                 if (str) {
151                         gtk_entry_set_text (GTK_ENTRY (widget), str);
152                         g_free (str);
153                 }
154
155                 if (strstr (param->name, "password")) {
156                         gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
157                 }
158
159                 g_signal_connect (widget, "focus-out-event",
160                                   G_CALLBACK (account_widget_generic_entry_focus_cb),
161                                   settings);
162
163                 gtk_table_attach (GTK_TABLE (settings->table_settings),
164                                   widget,
165                                   1, 2,
166                                   settings->n_rows - 1, settings->n_rows,
167                                   GTK_FILL | GTK_EXPAND, 0,
168                                   0, 0);
169         }
170         /* int types: ynqiuxt. double type is 'd' */
171         else if (param->signature[0] == 'y' ||
172                  param->signature[0] == 'n' ||
173                  param->signature[0] == 'q' ||
174                  param->signature[0] == 'i' ||
175                  param->signature[0] == 'u' ||
176                  param->signature[0] == 'x' ||
177                  param->signature[0] == 't' ||
178                  param->signature[0] == 'd') {
179                 gchar   *str = NULL;
180                 gint     value = 0;
181                 gdouble  minint = 0;
182                 gdouble  maxint = 0;
183                 gdouble  step = 1;
184                 switch (param->signature[0]) {
185                 case 'y': minint = G_MININT8;  maxint = G_MAXINT8;   break;
186                 case 'n': minint = G_MININT16; maxint = G_MAXINT16;  break;
187                 case 'q': minint = 0;          maxint = G_MAXUINT16; break;
188                 case 'i': minint = G_MININT32; maxint = G_MAXINT32;  break;
189                 case 'u': minint = 0;          maxint = G_MAXUINT32; break;
190                 case 'x': minint = G_MININT64; maxint = G_MAXINT64;  break;
191                 case 't': minint = 0;          maxint = G_MAXUINT64; break;
192                 case 'd': minint = G_MININT32; maxint = G_MAXINT32; step = 0.1; break;
193                 }
194
195                 str = g_strdup_printf (_("%s:"), param_name_formatted);
196                 widget = gtk_label_new (str);
197                 gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
198                 g_free (str);
199
200                 gtk_table_attach (GTK_TABLE (settings->table_settings),
201                                   widget,
202                                   0, 1,
203                                   settings->n_rows - 1, settings->n_rows,
204                                   GTK_FILL, 0,
205                                   0, 0);
206
207                 widget = gtk_spin_button_new_with_range (minint, maxint, step);
208                 mc_account_get_param_int (settings->account,
209                                           param->name,
210                                           &value);
211                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
212
213                 g_signal_connect (widget, "value-changed",
214                                   G_CALLBACK (account_widget_generic_int_changed_cb),
215                                   settings);
216
217                 gtk_table_attach (GTK_TABLE (settings->table_settings),
218                                   widget,
219                                   1, 2,
220                                   settings->n_rows - 1, settings->n_rows,
221                                   GTK_FILL | GTK_EXPAND, 0,
222                                   0, 0);
223         }
224         else if (param->signature[0] == 'b') {
225                 gboolean value = FALSE;
226
227                 mc_account_get_param_boolean (settings->account,
228                                               param->name,
229                                               &value);
230
231                 widget = gtk_check_button_new_with_label (param_name_formatted);
232                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
233
234                 g_signal_connect (widget, "toggled",
235                                   G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
236                                   settings);
237
238                 gtk_table_attach (GTK_TABLE (settings->table_settings),
239                                   widget,
240                                   0, 2,
241                                   settings->n_rows - 1, settings->n_rows,
242                                   GTK_FILL | GTK_EXPAND, 0,
243                                   0, 0);
244         } else {
245                 empathy_debug (DEBUG_DOMAIN,
246                                "Unknown signature for param %s: %s\n",
247                                param_name_formatted, param->signature);
248         }
249
250         if (widget) {
251                 g_object_set_data_full (G_OBJECT (widget), "param_name", 
252                                         g_strdup (param->name), g_free);
253         }
254
255         g_free (param_name_formatted);
256 }
257
258 static void
259 accounts_widget_generic_setup (EmpathyAccountWidgetGeneric *settings)
260 {
261         McProtocol *protocol;
262         McProfile  *profile;
263         GSList     *params;
264
265         profile = mc_account_get_profile (settings->account);
266         protocol = mc_profile_get_protocol (profile);
267
268         if (!protocol) {
269                 /* The CM is not installed, MC shouldn't list them
270                  * see SF bug #1688779
271                  * FIXME: We should display something asking the user to 
272                  * install the CM
273                  */
274                 g_object_unref (profile);
275                 return;
276         }
277
278         params = mc_protocol_get_params (protocol);
279
280         g_slist_foreach (params,
281                          (GFunc) account_widget_generic_setup_foreach,
282                          settings);
283
284         g_slist_free (params);
285         g_object_unref (profile);
286         g_object_unref (protocol);
287 }
288
289 static void
290 account_widget_generic_destroy_cb (GtkWidget                  *widget,
291                                    EmpathyAccountWidgetGeneric *settings)
292 {
293         g_object_unref (settings->account);
294         g_free (settings);
295 }
296
297 GtkWidget *
298 empathy_account_widget_generic_new (McAccount *account)
299 {
300         EmpathyAccountWidgetGeneric *settings;
301
302         g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
303
304         settings = g_new0 (EmpathyAccountWidgetGeneric, 1);
305
306         settings->account = g_object_ref (account);
307
308         settings->table_settings = gtk_table_new (0, 2, FALSE);
309         gtk_table_set_row_spacings (GTK_TABLE (settings->table_settings), 6);
310         gtk_table_set_col_spacings (GTK_TABLE (settings->table_settings), 6);
311         settings->sw = gtk_scrolled_window_new (NULL, NULL);
312         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (settings->sw),
313                                         GTK_POLICY_AUTOMATIC,
314                                         GTK_POLICY_AUTOMATIC);
315         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (settings->sw),
316                                                settings->table_settings);
317         
318         accounts_widget_generic_setup (settings);
319
320         g_signal_connect (settings->sw, "destroy",
321                           G_CALLBACK (account_widget_generic_destroy_cb),
322                           settings);
323
324         gtk_widget_show_all (settings->sw);
325
326         return settings->sw;
327 }