1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB
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.
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.
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.
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
21 * Martyn Russell <martyn@imendio.com>
29 #include <glib/gi18n.h>
31 #include <libmissioncontrol/mc-account.h>
32 #include <libmissioncontrol/mc-protocol.h>
34 #include <libempathy-gtk/gossip-ui-utils.h>
36 #include "gossip-account-widget-generic.h"
42 GtkWidget *table_settings;
43 GtkSizeGroup *size_group;
46 } GossipAccountWidgetGeneric;
48 static gboolean account_widget_generic_entry_focus_cb (GtkWidget *widget,
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);
62 account_widget_generic_entry_focus_cb (GtkWidget *widget,
64 GossipAccountWidgetGeneric *settings)
67 const gchar *param_name;
69 str = gtk_entry_get_text (GTK_ENTRY (widget));
70 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
72 mc_account_set_param_string (settings->account, param_name, str);
78 account_widget_generic_int_changed_cb (GtkWidget *widget,
79 GossipAccountWidgetGeneric *settings)
81 const gchar *param_name;
84 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (widget));
85 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
87 mc_account_set_param_int (settings->account, param_name, value);
91 account_widget_generic_checkbutton_toggled_cb (GtkWidget *widget,
92 GossipAccountWidgetGeneric *settings)
95 const gchar *param_name;
97 active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
98 param_name = g_object_get_data (G_OBJECT (widget), "param_name");
100 mc_account_set_param_boolean (settings->account, param_name, active);
104 account_widget_generic_format_param_name (const gchar *param_name)
109 str = g_strdup (param_name);
111 if (str && g_ascii_isalpha (str[0])) {
112 str[0] = g_ascii_toupper (str[0]);
115 while ((p = strchr (str, '-')) != NULL) {
116 if (p[1] != '\0' && g_ascii_isalpha (p[1])) {
118 p[1] = g_ascii_toupper (p[1]);
128 account_widget_generic_setup_foreach (McProtocolParam *param,
129 GossipAccountWidgetGeneric *settings)
132 gchar *param_name_formatted;
134 param_name_formatted = account_widget_generic_format_param_name (param->name);
136 gtk_table_resize (GTK_TABLE (settings->table_settings),
140 if (param->signature[0] == 's') {
143 str = g_strdup_printf (_("%s:"), param_name_formatted);
144 widget = gtk_label_new (str);
147 gtk_size_group_add_widget (settings->size_group, widget);
148 gtk_table_attach (GTK_TABLE (settings->table_settings),
151 settings->n_rows - 1, settings->n_rows,
156 widget = gtk_entry_new ();
157 mc_account_get_param_string (settings->account,
161 gtk_entry_set_text (GTK_ENTRY (widget), str);
165 if (strstr (param->name, "password")) {
166 gtk_entry_set_visibility (GTK_ENTRY (widget), FALSE);
169 g_signal_connect (widget, "focus-out-event",
170 G_CALLBACK (account_widget_generic_entry_focus_cb),
173 gtk_table_attach (GTK_TABLE (settings->table_settings),
176 settings->n_rows - 1, settings->n_rows,
177 GTK_FILL | GTK_EXPAND, 0,
180 else if (param->signature[0] == 'q' ||
181 param->signature[0] == 'n') {
185 str = g_strdup_printf (_("%s:"), param_name_formatted);
186 widget = gtk_label_new (str);
189 gtk_size_group_add_widget (settings->size_group, widget);
190 gtk_table_attach (GTK_TABLE (settings->table_settings),
193 settings->n_rows - 1, settings->n_rows,
197 widget = gtk_spin_button_new_with_range (0, G_MAXINT, 1);
198 mc_account_get_param_int (settings->account,
201 gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value);
203 g_signal_connect (widget, "value-changed",
204 G_CALLBACK (account_widget_generic_int_changed_cb),
207 gtk_table_attach (GTK_TABLE (settings->table_settings),
210 settings->n_rows - 1, settings->n_rows,
211 GTK_FILL | GTK_EXPAND, 0,
214 else if (param->signature[0] == 'b') {
217 mc_account_get_param_boolean (settings->account,
221 widget = gtk_check_button_new_with_label (param_name_formatted);
222 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget), value);
224 g_signal_connect (widget, "toggled",
225 G_CALLBACK (account_widget_generic_checkbutton_toggled_cb),
228 gtk_table_attach (GTK_TABLE (settings->table_settings),
231 settings->n_rows - 1, settings->n_rows,
232 GTK_FILL | GTK_EXPAND, 0,
235 g_assert_not_reached ();
238 g_free (param_name_formatted);
240 g_object_set_data_full (G_OBJECT (widget), "param_name",
241 g_strdup (param->name), g_free);
245 accounts_widget_generic_setup (GossipAccountWidgetGeneric *settings)
247 McProtocol *protocol;
251 profile = mc_account_get_profile (settings->account);
252 protocol = mc_profile_get_protocol (profile);
253 params = mc_protocol_get_params (protocol);
255 g_slist_foreach (params,
256 (GFunc) account_widget_generic_setup_foreach,
259 g_slist_free (params);
263 account_widget_generic_destroy_cb (GtkWidget *widget,
264 GossipAccountWidgetGeneric *settings)
266 g_object_unref (settings->account);
267 g_object_unref (settings->size_group);
273 gossip_account_widget_generic_new (McAccount *account,
274 GtkWidget *label_name)
276 GossipAccountWidgetGeneric *settings;
278 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
279 g_return_val_if_fail (GTK_IS_WIDGET (label_name), NULL);
281 settings = g_new0 (GossipAccountWidgetGeneric, 1);
283 settings->account = g_object_ref (account);
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);
295 settings->size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
297 gtk_size_group_add_widget (settings->size_group, label_name);
300 accounts_widget_generic_setup (settings);
302 g_signal_connect (settings->sw, "destroy",
303 G_CALLBACK (account_widget_generic_destroy_cb),
306 gtk_widget_show_all (settings->sw);