]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-jabber.c
Adding new empathy_strdiff API stolen from telepathy-glib. It check if
[empathy.git] / libempathy-gtk / empathy-account-widget-jabber.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
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: Xavier Claessens <xclaesse@gmail.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 <libmissioncontrol/mc-profile.h>
34
35 #include <libempathy/empathy-utils.h>
36
37 #include "empathy-account-widget-jabber.h"
38 #include "empathy-ui-utils.h"
39
40 #define PORT_WITHOUT_SSL 5222
41 #define PORT_WITH_SSL 5223
42
43 typedef struct {
44         McAccount *account;
45
46         GtkWidget *vbox_settings;
47         GtkWidget *button_forget;
48         GtkWidget *entry_id;
49         GtkWidget *entry_password;
50         GtkWidget *entry_resource;
51         GtkWidget *entry_server;
52         GtkWidget *spinbutton_port;
53         GtkWidget *checkbutton_ssl;
54 } EmpathyAccountWidgetJabber;
55
56 static gboolean account_widget_jabber_entry_focus_cb           (GtkWidget                 *widget,
57                                                                 GdkEventFocus             *event,
58                                                                 EmpathyAccountWidgetJabber *settings);
59 static void     account_widget_jabber_entry_changed_cb         (GtkWidget                 *widget,
60                                                                 EmpathyAccountWidgetJabber *settings);
61 static void     account_widget_jabber_checkbutton_toggled_cb   (GtkWidget                 *widget,
62                                                                 EmpathyAccountWidgetJabber *settings);
63 static void     account_widget_jabber_value_changed_cb         (GtkWidget                 *spinbutton,
64                                                                 EmpathyAccountWidgetJabber *settings);
65 static void     account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
66                                                                 EmpathyAccountWidgetJabber *settings);
67 static void     account_widget_jabber_destroy_cb               (GtkWidget                 *widget,
68                                                                 EmpathyAccountWidgetJabber *settings);
69 static void     account_widget_jabber_setup                    (EmpathyAccountWidgetJabber *settings);
70
71 static gboolean
72 account_widget_jabber_entry_focus_cb (GtkWidget                 *widget,
73                                       GdkEventFocus             *event,
74                                       EmpathyAccountWidgetJabber *settings)
75 {
76         const gchar *param;
77         const gchar *str;
78
79         if (widget == settings->entry_password) {
80                 param = "password";
81         }
82         else if (widget == settings->entry_resource) {
83                 param = "resource";
84         }
85         else if (widget == settings->entry_server) {
86                 param = "server";
87         }
88         else if (widget == settings->entry_id) {
89                 param = "account";
90         } else {
91                 return FALSE;
92         }
93
94         str = gtk_entry_get_text (GTK_ENTRY (widget));
95         if (G_STR_EMPTY (str)) {
96                 gchar *value = NULL;
97
98                 mc_account_get_param_string (settings->account, param, &value);
99                 gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
100                 g_free (value);
101         } else {
102                 mc_account_set_param_string (settings->account, param, str);
103
104                 if (widget == settings->entry_id) {
105                         McProfile   *profile;
106                         const gchar *profile_name;
107
108                         /* Try to guess the server if we are using the vanilla
109                          * jabber profile. We don't have to do that with
110                          * gtalk profile. */
111                         profile = mc_account_get_profile (settings->account);
112                         profile_name = mc_profile_get_unique_name (profile);
113                         if (strcmp (profile_name, "jabber") == 0) {
114                                 gchar *server;
115
116                                 server = strstr (str, "@");
117                                 if (server != NULL) {
118                                         /* skip the leading @ */
119                                         server++;
120                                         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server);
121                                         mc_account_set_param_string (settings->account, "server", server);
122                                 }
123                         }
124                         g_object_unref (profile);
125                 }
126         }
127
128         return FALSE;
129 }
130
131 static void
132 account_widget_jabber_entry_changed_cb (GtkWidget                 *widget,
133                                         EmpathyAccountWidgetJabber *settings)
134 {
135         if (widget == settings->entry_password) {
136                 const gchar *str;
137
138                 str = gtk_entry_get_text (GTK_ENTRY (widget));
139                 gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
140         }
141 }
142
143 static void  
144 account_widget_jabber_checkbutton_toggled_cb (GtkWidget                 *widget,
145                                               EmpathyAccountWidgetJabber *settings)
146 {
147         if (widget == settings->checkbutton_ssl) {
148                 gint     port = 0;
149                 gboolean old_ssl;
150
151                 mc_account_get_param_int (settings->account, "port", &port);
152                 old_ssl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
153
154                 if (old_ssl) {
155                         if (port == PORT_WITHOUT_SSL) {
156                                 port = PORT_WITH_SSL;
157                         }
158                 } else {
159                         if (port == PORT_WITH_SSL) {
160                                 port = PORT_WITHOUT_SSL;
161                         }
162                 }
163                 
164                 mc_account_set_param_int (settings->account, "port", port);
165                 mc_account_set_param_boolean (settings->account, "old-ssl", old_ssl);
166                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
167         }
168 }
169
170 static void
171 account_widget_jabber_value_changed_cb (GtkWidget                 *spinbutton,
172                                         EmpathyAccountWidgetJabber *settings)
173 {
174         if (spinbutton == settings->spinbutton_port) {
175                 gdouble value;
176
177                 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
178                 mc_account_set_param_int (settings->account, "port", (gint) value);
179         }
180 }
181
182 static void
183 account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
184                                                 EmpathyAccountWidgetJabber *settings)
185 {
186         mc_account_set_param_string (settings->account, "password", "");
187         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
188 }
189
190 static void
191 account_widget_jabber_destroy_cb (GtkWidget                 *widget,
192                                   EmpathyAccountWidgetJabber *settings)
193 {
194         g_object_unref (settings->account);
195         g_free (settings);
196 }
197
198 static void
199 account_widget_jabber_setup (EmpathyAccountWidgetJabber *settings)
200 {
201         gint      port = 0;
202         gchar    *id = NULL;
203         gchar    *resource = NULL;
204         gchar    *server = NULL;
205         gchar    *password = NULL;
206         gboolean  old_ssl = FALSE;
207
208         mc_account_get_param_int (settings->account, "port", &port);
209         mc_account_get_param_string (settings->account, "account", &id);
210         mc_account_get_param_string (settings->account, "resource", &resource);
211         mc_account_get_param_string (settings->account, "server", &server);
212         mc_account_get_param_string (settings->account, "password", &password);
213         mc_account_get_param_boolean (settings->account, "old-ssl", &old_ssl);
214
215         if (!id) {
216                 McProfile   *profile;
217                 const gchar *server;
218
219                 profile = mc_account_get_profile (settings->account);
220                 server = mc_profile_get_default_account_domain (profile);
221                 if (server) {
222                         id = g_strconcat ("user@", server, NULL);
223                 }
224                 g_object_unref (profile);
225         }
226
227         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ssl), old_ssl);
228         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
229         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
230         gtk_entry_set_text (GTK_ENTRY (settings->entry_resource), resource ? resource : "");
231         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
232         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
233
234         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
235
236         g_free (id);
237         g_free (resource);
238         g_free (server);
239         g_free (password);
240 }
241
242 GtkWidget *
243 empathy_account_widget_jabber_new (McAccount *account)
244 {
245         EmpathyAccountWidgetJabber *settings;
246         GladeXML                  *glade;
247         GtkSizeGroup              *size_group;
248         GtkWidget                 *label_id, *label_password;
249         GtkWidget                 *label_server, *label_resource, *label_port; 
250
251         settings = g_new0 (EmpathyAccountWidgetJabber, 1);
252         settings->account = g_object_ref (account);
253
254         glade = empathy_glade_get_file ("empathy-account-widget-jabber.glade",
255                                        "vbox_jabber_settings",
256                                        NULL,
257                                        "vbox_jabber_settings", &settings->vbox_settings,
258                                        "button_forget", &settings->button_forget,
259                                        "label_id", &label_id,
260                                        "label_password", &label_password,
261                                        "label_resource", &label_resource,
262                                        "label_server", &label_server,
263                                        "label_port", &label_port,
264                                        "entry_id", &settings->entry_id,
265                                        "entry_password", &settings->entry_password,
266                                        "entry_resource", &settings->entry_resource,
267                                        "entry_server", &settings->entry_server,
268                                        "spinbutton_port", &settings->spinbutton_port,
269                                        "checkbutton_ssl", &settings->checkbutton_ssl,
270                                        NULL);
271
272         account_widget_jabber_setup (settings);
273
274         empathy_glade_connect (glade, 
275                               settings,
276                               "vbox_jabber_settings", "destroy", account_widget_jabber_destroy_cb,
277                               "button_forget", "clicked", account_widget_jabber_button_forget_clicked_cb,
278                               "entry_password", "changed", account_widget_jabber_entry_changed_cb,
279                               "spinbutton_port", "value-changed", account_widget_jabber_value_changed_cb,
280                               "entry_id", "focus-out-event", account_widget_jabber_entry_focus_cb,
281                               "entry_password", "focus-out-event", account_widget_jabber_entry_focus_cb,
282                               "entry_resource", "focus-out-event", account_widget_jabber_entry_focus_cb,
283                               "entry_server", "focus-out-event", account_widget_jabber_entry_focus_cb,
284                               "checkbutton_ssl", "toggled", account_widget_jabber_checkbutton_toggled_cb,
285                               NULL);
286
287         g_object_unref (glade);
288
289         /* Set up remaining widgets */
290         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
291
292         gtk_size_group_add_widget (size_group, label_id);
293         gtk_size_group_add_widget (size_group, label_password);
294         gtk_size_group_add_widget (size_group, label_resource);
295         gtk_size_group_add_widget (size_group, label_server);
296         gtk_size_group_add_widget (size_group, label_port);
297
298         g_object_unref (size_group);
299
300   gtk_editable_select_region (GTK_EDITABLE (settings->entry_id), 0, -1);
301   gtk_widget_grab_focus (settings->entry_id);
302
303         gtk_widget_show (settings->vbox_settings);
304
305         return settings->vbox_settings;
306 }