1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
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 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
30 #include <glib/gi18n.h>
32 #include <libmissioncontrol/mc-account.h>
33 #include <libmissioncontrol/mc-protocol.h>
35 #include <libempathy/empathy-debug.h>
37 #include "empathy-account-widget-generic.h"
38 #include "empathy-ui-utils.h"
40 #define DEBUG_DOMAIN "AccountWidgetGeneric"
46 GtkWidget *table_settings;
49 } EmpathyAccountWidgetGeneric;
51 static gboolean account_widget_generic_entry_focus_cb (GtkWidget *widget,
53 EmpathyAccountWidgetGeneric *settings);
54 static void account_widget_generic_int_changed_cb (GtkWidget *widget,
55 EmpathyAccountWidgetGeneric *settings);
56 static void account_widget_generic_checkbutton_toggled_cb (GtkWidget *widget,
57 EmpathyAccountWidgetGeneric *settings);
58 static gchar * account_widget_generic_format_param_name (const gchar *param_name);
59 static void account_widget_generic_setup_foreach (McProtocolParam *param,
60 EmpathyAccountWidgetGeneric *settings);
61 static void account_widget_generic_destroy_cb (GtkWidget *widget,
62 EmpathyAccountWidgetGeneric *settings);
65 account_widget_generic_entry_focus_cb (GtkWidget *widget,
67 EmpathyAccountWidgetGeneric *settings)
70 const gchar *param_name;
72 str = gtk_entry_get_text (GTK_ENTRY (widget));
73 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
75 mc_account_set_param_string (settings->account, param_name, str);
81 account_widget_generic_int_changed_cb (GtkWidget *widget,
82 EmpathyAccountWidgetGeneric *settings)
84 const gchar *param_name;
87 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
88 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
90 mc_account_set_param_int (settings->account, param_name, value);
94 account_widget_generic_checkbutton_toggled_cb (GtkWidget *widget,
95 EmpathyAccountWidgetGeneric *settings)
98 const gchar *param_name;
100 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
101 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
103 mc_account_set_param_boolean (settings->account, param_name, active);
107 account_widget_generic_format_param_name (const gchar *param_name)
112 str = g_strdup (param_name);
114 if (str && g_ascii_isalpha (str[0])) {
115 str[0] = g_ascii_toupper (str[0]);
118 while ((p = strchr (str, '-')) != NULL) {
119 if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
121 p[1] = g_ascii_toupper (p[1]);
131 account_widget_generic_setup_foreach (McProtocolParam *param,
132 EmpathyAccountWidgetGeneric *settings)
135 gchar *param_name_formatted;
137 param_name_formatted = account_widget_generic_format_param_name (param->name);
139 gtk_table_resize (GTK_TABLE (settings->table_settings),
143 if (param->signature[0] == 's') {
146 str = g_strdup_printf (_("%s:"), param_name_formatted);
147 widget = gtk_label_new (str);
148 gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
151 gtk_table_attach (GTK_TABLE (settings->table_settings),
154 settings->n_rows - 1, settings->n_rows,
159 widget = gtk_entry_new ();
160 mc_account_get_param_string (settings->account,
164 gtk_entry_set_text (GTK_ENTRY (widget), str);
168 if (strstr (param->name, "password")) {
169 gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
172 g_signal_connect (widget, "focus-out-event",
173 G_CALLBACK (account_widget_generic_entry_focus_cb),
176 gtk_table_attach (GTK_TABLE (settings->table_settings),
179 settings->n_rows - 1, settings->n_rows,
180 GTK_FILL | GTK_EXPAND, 0,
183 /* int types: ynqiuxt. double type is 'd' */
184 else if (param->signature[0] == 'y' ||
185 param->signature[0] == 'n' ||
186 param->signature[0] == 'q' ||
187 param->signature[0] == 'i' ||
188 param->signature[0] == 'u' ||
189 param->signature[0] == 'x' ||
190 param->signature[0] == 't' ||
191 param->signature[0] == 'd') {
197 switch (param->signature[0]) {
198 case 'y': minint = G_MININT8; maxint = G_MAXINT8; break;
199 case 'n': minint = G_MININT16; maxint = G_MAXINT16; break;
200 case 'q': minint = 0; maxint = G_MAXUINT16; break;
201 case 'i': minint = G_MININT32; maxint = G_MAXINT32; break;
202 case 'u': minint = 0; maxint = G_MAXUINT32; break;
203 case 'x': minint = G_MININT64; maxint = G_MAXINT64; break;
204 case 't': minint = 0; maxint = G_MAXUINT64; break;
205 case 'd': minint = G_MININT32; maxint = G_MAXINT32; step = 0.1; break;
208 str = g_strdup_printf (_("%s:"), param_name_formatted);
209 widget = gtk_label_new (str);
210 gtk_misc_set_alignment (GTK_MISC (widget), 0, 0.5);
213 gtk_table_attach (GTK_TABLE (settings->table_settings),
216 settings->n_rows - 1, settings->n_rows,
220 widget = gtk_spin_button_new_with_range (minint, maxint, step);
221 mc_account_get_param_int (settings->account,
224 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
226 g_signal_connect (widget, "value-changed",
227 G_CALLBACK (account_widget_generic_int_changed_cb),
230 gtk_table_attach (GTK_TABLE (settings->table_settings),
233 settings->n_rows - 1, settings->n_rows,
234 GTK_FILL | GTK_EXPAND, 0,
237 else if (param->signature[0] == 'b') {
238 gboolean value = FALSE;
240 mc_account_get_param_boolean (settings->account,
244 widget = gtk_check_button_new_with_label (param_name_formatted);
245 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
247 g_signal_connect (widget, "toggled",
248 G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
251 gtk_table_attach (GTK_TABLE (settings->table_settings),
254 settings->n_rows - 1, settings->n_rows,
255 GTK_FILL | GTK_EXPAND, 0,
258 empathy_debug (DEBUG_DOMAIN,
259 "Unknown signature for param %s: %s\n",
260 param_name_formatted, param->signature);
261 g_assert_not_reached ();
264 g_free (param_name_formatted);
266 g_object_set_data_full (G_OBJECT (widget), "param_name",
267 g_strdup (param->name), g_free);
271 accounts_widget_generic_setup (EmpathyAccountWidgetGeneric *settings)
273 McProtocol *protocol;
277 profile = mc_account_get_profile (settings->account);
278 protocol = mc_profile_get_protocol (profile);
281 /* The CM is not installed, MC shouldn't list them
282 * see SF bug #1688779
283 * FIXME: We should display something asking the user to
286 g_object_unref (profile);
290 params = mc_protocol_get_params (protocol);
292 g_slist_foreach (params,
293 (GFunc) account_widget_generic_setup_foreach,
296 g_slist_free (params);
297 g_object_unref (profile);
298 g_object_unref (protocol);
302 account_widget_generic_destroy_cb (GtkWidget *widget,
303 EmpathyAccountWidgetGeneric *settings)
305 g_object_unref (settings->account);
310 empathy_account_widget_generic_new (McAccount *account)
312 EmpathyAccountWidgetGeneric *settings;
314 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
316 settings = g_new0 (EmpathyAccountWidgetGeneric, 1);
318 settings->account = g_object_ref (account);
320 settings->table_settings = gtk_table_new (0, 2, FALSE);
321 gtk_table_set_row_spacings (GTK_TABLE (settings->table_settings), 6);
322 gtk_table_set_col_spacings (GTK_TABLE (settings->table_settings), 6);
323 settings->sw = gtk_scrolled_window_new (NULL, NULL);
324 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (settings->sw),
325 GTK_POLICY_AUTOMATIC,
326 GTK_POLICY_AUTOMATIC);
327 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (settings->sw),
328 settings->table_settings);
330 accounts_widget_generic_setup (settings);
332 g_signal_connect (settings->sw, "destroy",
333 G_CALLBACK (account_widget_generic_destroy_cb),
336 gtk_widget_show_all (settings->sw);