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;
52 account_widget_generic_entry_focus_cb (GtkWidget *widget,
54 EmpathyAccountWidgetGeneric *settings)
57 const gchar *param_name;
59 str = gtk_entry_get_text (GTK_ENTRY (widget));
60 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
62 mc_account_set_param_string (settings->account, param_name, str);
68 account_widget_generic_int_changed_cb (GtkWidget *widget,
69 EmpathyAccountWidgetGeneric *settings)
71 const gchar *param_name;
74 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
75 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
77 mc_account_set_param_int (settings->account, param_name, value);
81 account_widget_generic_checkbutton_toggled_cb (GtkWidget *widget,
82 EmpathyAccountWidgetGeneric *settings)
85 const gchar *param_name;
87 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
88 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
90 mc_account_set_param_boolean (settings->account, param_name, active);
94 account_widget_generic_format_param_name (const gchar *param_name)
99 str = g_strdup (param_name);
101 if (str && g_ascii_isalpha (str[0])) {
102 str[0] = g_ascii_toupper (str[0]);
105 while ((p = strchr (str, '-')) != NULL) {
106 if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
108 p[1] = g_ascii_toupper (p[1]);
118 account_widget_generic_setup_foreach (McProtocolParam *param,
119 EmpathyAccountWidgetGeneric *settings)
121 GtkWidget *widget = NULL;
122 gchar *param_name_formatted;
124 param_name_formatted = account_widget_generic_format_param_name (param->name);
126 gtk_table_resize (GTK_TABLE (settings->table_settings),
130 if (param->signature[0] == 's') {
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);
138 gtk_table_attach (GTK_TABLE (settings->table_settings),
141 settings->n_rows - 1, settings->n_rows,
146 widget = gtk_entry_new ();
147 mc_account_get_param_string (settings->account,
151 gtk_entry_set_text (GTK_ENTRY (widget), str);
155 if (strstr (param->name, "password")) {
156 gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
159 g_signal_connect (widget, "focus-out-event",
160 G_CALLBACK (account_widget_generic_entry_focus_cb),
163 gtk_table_attach (GTK_TABLE (settings->table_settings),
166 settings->n_rows - 1, settings->n_rows,
167 GTK_FILL | GTK_EXPAND, 0,
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') {
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;
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);
200 gtk_table_attach (GTK_TABLE (settings->table_settings),
203 settings->n_rows - 1, settings->n_rows,
207 widget = gtk_spin_button_new_with_range (minint, maxint, step);
208 mc_account_get_param_int (settings->account,
211 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
213 g_signal_connect (widget, "value-changed",
214 G_CALLBACK (account_widget_generic_int_changed_cb),
217 gtk_table_attach (GTK_TABLE (settings->table_settings),
220 settings->n_rows - 1, settings->n_rows,
221 GTK_FILL | GTK_EXPAND, 0,
224 else if (param->signature[0] == 'b') {
225 gboolean value = FALSE;
227 mc_account_get_param_boolean (settings->account,
231 widget = gtk_check_button_new_with_label (param_name_formatted);
232 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
234 g_signal_connect (widget, "toggled",
235 G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
238 gtk_table_attach (GTK_TABLE (settings->table_settings),
241 settings->n_rows - 1, settings->n_rows,
242 GTK_FILL | GTK_EXPAND, 0,
245 empathy_debug (DEBUG_DOMAIN,
246 "Unknown signature for param %s: %s\n",
247 param_name_formatted, param->signature);
251 g_object_set_data_full (G_OBJECT (widget), "param_name",
252 g_strdup (param->name), g_free);
255 g_free (param_name_formatted);
259 accounts_widget_generic_setup (EmpathyAccountWidgetGeneric *settings)
261 McProtocol *protocol;
265 profile = mc_account_get_profile (settings->account);
266 protocol = mc_profile_get_protocol (profile);
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
274 g_object_unref (profile);
278 params = mc_protocol_get_params (protocol);
280 g_slist_foreach (params,
281 (GFunc) account_widget_generic_setup_foreach,
284 g_slist_free (params);
285 g_object_unref (profile);
286 g_object_unref (protocol);
290 account_widget_generic_destroy_cb (GtkWidget *widget,
291 EmpathyAccountWidgetGeneric *settings)
293 g_object_unref (settings->account);
298 empathy_account_widget_generic_new (McAccount *account)
300 EmpathyAccountWidgetGeneric *settings;
302 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
304 settings = g_new0 (EmpathyAccountWidgetGeneric, 1);
306 settings->account = g_object_ref (account);
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);
318 accounts_widget_generic_setup (settings);
320 g_signal_connect (settings->sw, "destroy",
321 G_CALLBACK (account_widget_generic_destroy_cb),
324 gtk_widget_show_all (settings->sw);