589bfcc0def351ccf0e248a5fc28dbef1906e185
[empathy.git] / libempathy-gtk / gossip-account-widget-jabber.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2005-2007 Imendio AB
4  *
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.
9  *
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.
14  *
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.
19  * 
20  * Authors: Martyn Russell <martyn@imendio.com>
21  */
22
23 #include "config.h"
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32
33 #include <libempathy/gossip-utils.h>
34
35 #include "gossip-account-widget-jabber.h"
36 #include "gossip-ui-utils.h"
37
38 #define PORT_WITHOUT_SSL 5222
39 #define PORT_WITH_SSL 5223
40
41 typedef struct {
42         McAccount *account;
43
44         GtkWidget *vbox_settings;
45         GtkWidget *button_forget;
46         GtkWidget *entry_id;
47         GtkWidget *entry_password;
48         GtkWidget *entry_resource;
49         GtkWidget *entry_server;
50         GtkWidget *spinbutton_port;
51         GtkWidget *checkbutton_ssl;
52 } GossipAccountWidgetJabber;
53
54 static gboolean account_widget_jabber_entry_focus_cb           (GtkWidget                 *widget,
55                                                                 GdkEventFocus             *event,
56                                                                 GossipAccountWidgetJabber *settings);
57 static void     account_widget_jabber_entry_changed_cb         (GtkWidget                 *widget,
58                                                                 GossipAccountWidgetJabber *settings);
59 static void     account_widget_jabber_checkbutton_toggled_cb   (GtkWidget                 *widget,
60                                                                 GossipAccountWidgetJabber *settings);
61 static void     account_widget_jabber_value_changed_cb         (GtkWidget                 *spinbutton,
62                                                                 GossipAccountWidgetJabber *settings);
63 static void     account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
64                                                                 GossipAccountWidgetJabber *settings);
65 static void     account_widget_jabber_destroy_cb               (GtkWidget                 *widget,
66                                                                 GossipAccountWidgetJabber *settings);
67 static void     account_widget_jabber_setup                    (GossipAccountWidgetJabber *settings);
68
69 static gboolean
70 account_widget_jabber_entry_focus_cb (GtkWidget                 *widget,
71                                       GdkEventFocus             *event,
72                                       GossipAccountWidgetJabber *settings)
73 {
74         const gchar *param;
75         const gchar *str;
76
77         if (widget == settings->entry_password) {
78                 param = "password";
79         }
80         else if (widget == settings->entry_resource) {
81                 param = "resource";
82         }
83         else if (widget == settings->entry_server) {
84                 param = "server";
85         }
86         else if (widget == settings->entry_id) {
87                 param = "account";
88         } else {
89                 return FALSE;
90         }
91
92         str = gtk_entry_get_text (GTK_ENTRY (widget));
93         if (G_STR_EMPTY (str)) {
94                 gchar *value;
95
96                 mc_account_get_param_string (settings->account, param, &value);
97                 gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
98                 g_free (value);
99         } else {
100                 mc_account_set_param_string (settings->account, param, str);
101         }
102
103         return FALSE;
104 }
105
106 static void
107 account_widget_jabber_entry_changed_cb (GtkWidget                 *widget,
108                                         GossipAccountWidgetJabber *settings)
109 {
110         if (widget == settings->entry_password) {
111                 const gchar *str;
112
113                 str = gtk_entry_get_text (GTK_ENTRY (widget));
114                 gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
115         }
116 }
117
118 static void  
119 account_widget_jabber_checkbutton_toggled_cb (GtkWidget                 *widget,
120                                               GossipAccountWidgetJabber *settings)
121 {
122         if (widget == settings->checkbutton_ssl) {
123                 gint     port;
124                 gboolean old_ssl;
125
126                 mc_account_get_param_int (settings->account, "port", &port);
127                 old_ssl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
128
129                 if (old_ssl) {
130                         if (port == PORT_WITHOUT_SSL) {
131                                 port = PORT_WITH_SSL;
132                         }
133                 } else {
134                         if (port == PORT_WITH_SSL) {
135                                 port = PORT_WITHOUT_SSL;
136                         }
137                 }
138                 
139                 mc_account_set_param_int (settings->account, "port", port);
140                 mc_account_set_param_boolean (settings->account, "old-ssl", old_ssl);
141                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
142         }
143 }
144
145 static void
146 account_widget_jabber_value_changed_cb (GtkWidget                 *spinbutton,
147                                         GossipAccountWidgetJabber *settings)
148 {
149         if (spinbutton == settings->spinbutton_port) {
150                 gdouble value;
151
152                 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
153                 mc_account_set_param_int (settings->account, "port", (gint) value);
154         }
155 }
156
157 static void
158 account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
159                                                 GossipAccountWidgetJabber *settings)
160 {
161         mc_account_set_param_string (settings->account, "password", "");
162         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
163 }
164
165 static void
166 account_widget_jabber_destroy_cb (GtkWidget                 *widget,
167                                   GossipAccountWidgetJabber *settings)
168 {
169         g_object_unref (settings->account);
170         g_free (settings);
171 }
172
173 static void
174 account_widget_jabber_setup (GossipAccountWidgetJabber *settings)
175 {
176         gint      port;
177         gchar    *id;
178         gchar    *resource;
179         gchar    *server;
180         gchar    *password;
181         gboolean  old_ssl;
182
183         mc_account_get_param_int (settings->account, "port", &port);
184         mc_account_get_param_string (settings->account, "account", &id);
185         mc_account_get_param_string (settings->account, "resource", &resource);
186         mc_account_get_param_string (settings->account, "server", &server);
187         mc_account_get_param_string (settings->account, "password", &password);
188         mc_account_get_param_boolean (settings->account, "old-ssl", &old_ssl);
189
190         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ssl), old_ssl);
191         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
192         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
193         gtk_entry_set_text (GTK_ENTRY (settings->entry_resource), resource ? resource : "");
194         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
195         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
196
197         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
198
199         g_free (id);
200         g_free (resource);
201         g_free (server);
202         g_free (password);
203 }
204
205 GtkWidget *
206 gossip_account_widget_jabber_new (McAccount *account)
207 {
208         GossipAccountWidgetJabber *settings;
209         GladeXML                  *glade;
210         GtkSizeGroup              *size_group;
211         GtkWidget                 *label_id, *label_password;
212         GtkWidget                 *label_server, *label_resource, *label_port; 
213
214         settings = g_new0 (GossipAccountWidgetJabber, 1);
215         settings->account = g_object_ref (account);
216
217         glade = gossip_glade_get_file ("gossip-account-widget-jabber.glade",
218                                        "vbox_jabber_settings",
219                                        NULL,
220                                        "vbox_jabber_settings", &settings->vbox_settings,
221                                        "button_forget", &settings->button_forget,
222                                        "label_id", &label_id,
223                                        "label_password", &label_password,
224                                        "label_resource", &label_resource,
225                                        "label_server", &label_server,
226                                        "label_port", &label_port,
227                                        "entry_id", &settings->entry_id,
228                                        "entry_password", &settings->entry_password,
229                                        "entry_resource", &settings->entry_resource,
230                                        "entry_server", &settings->entry_server,
231                                        "spinbutton_port", &settings->spinbutton_port,
232                                        "checkbutton_ssl", &settings->checkbutton_ssl,
233                                        NULL);
234
235         account_widget_jabber_setup (settings);
236
237         gossip_glade_connect (glade, 
238                               settings,
239                               "vbox_jabber_settings", "destroy", account_widget_jabber_destroy_cb,
240                               "button_forget", "clicked", account_widget_jabber_button_forget_clicked_cb,
241                               "entry_password", "changed", account_widget_jabber_entry_changed_cb,
242                               "spinbutton_port", "value-changed", account_widget_jabber_value_changed_cb,
243                               "entry_id", "focus-out-event", account_widget_jabber_entry_focus_cb,
244                               "entry_password", "focus-out-event", account_widget_jabber_entry_focus_cb,
245                               "entry_resource", "focus-out-event", account_widget_jabber_entry_focus_cb,
246                               "entry_server", "focus-out-event", account_widget_jabber_entry_focus_cb,
247                               "checkbutton_ssl", "toggled", account_widget_jabber_checkbutton_toggled_cb,
248                               NULL);
249
250         g_object_unref (glade);
251
252         /* Set up remaining widgets */
253         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
254
255         gtk_size_group_add_widget (size_group, label_id);
256         gtk_size_group_add_widget (size_group, label_password);
257         gtk_size_group_add_widget (size_group, label_resource);
258         gtk_size_group_add_widget (size_group, label_server);
259         gtk_size_group_add_widget (size_group, label_port);
260
261         g_object_unref (size_group);
262
263         return settings->vbox_settings;
264 }
265