]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-jabber.c
81e7d35132f0e6cd587d0c6fbfe298badac6af29
[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 library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include "config.h"
23
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <glade/glade.h>
31
32 #include <libmissioncontrol/mc-profile.h>
33
34 #include <libempathy/empathy-utils.h>
35
36 #include "empathy-account-widget-jabber.h"
37 #include "empathy-ui-utils.h"
38
39 #define PORT_WITHOUT_SSL 5222
40 #define PORT_WITH_SSL 5223
41
42 typedef struct {
43         McAccount *account;
44
45         GtkWidget *vbox_settings;
46         GtkWidget *button_forget;
47         GtkWidget *entry_id;
48         GtkWidget *entry_password;
49         GtkWidget *entry_resource;
50         GtkWidget *entry_server;
51         GtkWidget *spinbutton_port;
52         GtkWidget *spinbutton_priority;
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
105         return FALSE;
106 }
107
108 static void
109 account_widget_jabber_entry_changed_cb (GtkWidget                 *widget,
110                                         EmpathyAccountWidgetJabber *settings)
111 {
112         if (widget == settings->entry_password) {
113                 const gchar *str;
114
115                 str = gtk_entry_get_text (GTK_ENTRY (widget));
116                 gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
117         }
118 }
119
120 static void  
121 account_widget_jabber_checkbutton_toggled_cb (GtkWidget                 *widget,
122                                               EmpathyAccountWidgetJabber *settings)
123 {
124         if (widget == settings->checkbutton_ssl) {
125                 gint     port = 0;
126                 gboolean old_ssl;
127
128                 mc_account_get_param_int (settings->account, "port", &port);
129                 old_ssl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
130
131                 if (old_ssl) {
132                         if (port == PORT_WITHOUT_SSL) {
133                                 port = PORT_WITH_SSL;
134                         }
135                 } else {
136                         if (port == PORT_WITH_SSL) {
137                                 port = PORT_WITHOUT_SSL;
138                         }
139                 }
140                 
141                 mc_account_set_param_int (settings->account, "port", port);
142                 mc_account_set_param_boolean (settings->account, "old-ssl", old_ssl);
143                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
144         }
145 }
146
147 static void
148 account_widget_jabber_value_changed_cb (GtkWidget                 *spinbutton,
149                                         EmpathyAccountWidgetJabber *settings)
150 {
151         gdouble value;
152
153         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
154
155         if (spinbutton == settings->spinbutton_port) {
156                 mc_account_set_param_int (settings->account, "port", (gint) value);
157         }
158         else if (spinbutton == settings->spinbutton_priority) {
159                 mc_account_set_param_int (settings->account, "priority", (gint) value);
160         }
161 }
162
163 static void
164 account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
165                                                 EmpathyAccountWidgetJabber *settings)
166 {
167         mc_account_set_param_string (settings->account, "password", "");
168         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
169 }
170
171 static void
172 account_widget_jabber_destroy_cb (GtkWidget                 *widget,
173                                   EmpathyAccountWidgetJabber *settings)
174 {
175         g_object_unref (settings->account);
176         g_free (settings);
177 }
178
179 static void
180 account_widget_jabber_setup (EmpathyAccountWidgetJabber *settings)
181 {
182         gint      port = 0;
183         gint      priority = 0;
184         gchar    *id = NULL;
185         gchar    *resource = NULL;
186         gchar    *server = NULL;
187         gchar    *password = NULL;
188         gboolean  old_ssl = FALSE;
189
190         mc_account_get_param_int (settings->account, "port", &port);
191         mc_account_get_param_int (settings->account, "priority", &priority);
192         mc_account_get_param_string (settings->account, "account", &id);
193         mc_account_get_param_string (settings->account, "resource", &resource);
194         mc_account_get_param_string (settings->account, "server", &server);
195         mc_account_get_param_string (settings->account, "password", &password);
196         mc_account_get_param_boolean (settings->account, "old-ssl", &old_ssl);
197
198         if (!id) {
199                 McProfile   *profile;
200                 const gchar *server;
201
202                 profile = mc_account_get_profile (settings->account);
203                 server = mc_profile_get_default_account_domain (profile);
204                 if (server) {
205                         id = g_strconcat ("user@", server, NULL);
206                 }
207                 g_object_unref (profile);
208         }
209
210         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ssl), old_ssl);
211         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
212         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
213         gtk_entry_set_text (GTK_ENTRY (settings->entry_resource), resource ? resource : "");
214         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
215         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
216         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_priority), priority);
217
218         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
219
220         g_free (id);
221         g_free (resource);
222         g_free (server);
223         g_free (password);
224 }
225
226 GtkWidget *
227 empathy_account_widget_jabber_new (McAccount *account)
228 {
229         EmpathyAccountWidgetJabber *settings;
230         GladeXML                  *glade;
231         GtkSizeGroup              *size_group;
232         GtkWidget                 *label_id, *label_password;
233         GtkWidget                 *label_server, *label_resource;
234         GtkWidget                 *label_port, *label_priority; 
235
236         settings = g_new0 (EmpathyAccountWidgetJabber, 1);
237         settings->account = g_object_ref (account);
238
239         glade = empathy_glade_get_file ("empathy-account-widget-jabber.glade",
240                                        "vbox_jabber_settings",
241                                        NULL,
242                                        "vbox_jabber_settings", &settings->vbox_settings,
243                                        "button_forget", &settings->button_forget,
244                                        "label_id", &label_id,
245                                        "label_password", &label_password,
246                                        "label_resource", &label_resource,
247                                        "label_priority", &label_priority,
248                                        "label_server", &label_server,
249                                        "label_port", &label_port,
250                                        "entry_id", &settings->entry_id,
251                                        "entry_password", &settings->entry_password,
252                                        "entry_resource", &settings->entry_resource,
253                                        "entry_server", &settings->entry_server,
254                                        "spinbutton_port", &settings->spinbutton_port,
255                                        "spinbutton_priority", &settings->spinbutton_priority,
256                                        "checkbutton_ssl", &settings->checkbutton_ssl,
257                                        NULL);
258
259         account_widget_jabber_setup (settings);
260
261         empathy_glade_connect (glade, 
262                               settings,
263                               "vbox_jabber_settings", "destroy", account_widget_jabber_destroy_cb,
264                               "button_forget", "clicked", account_widget_jabber_button_forget_clicked_cb,
265                               "entry_password", "changed", account_widget_jabber_entry_changed_cb,
266                               "spinbutton_port", "value-changed", account_widget_jabber_value_changed_cb,
267                               "spinbutton_priority", "value-changed", account_widget_jabber_value_changed_cb,
268                               "entry_id", "focus-out-event", account_widget_jabber_entry_focus_cb,
269                               "entry_password", "focus-out-event", account_widget_jabber_entry_focus_cb,
270                               "entry_resource", "focus-out-event", account_widget_jabber_entry_focus_cb,
271                               "entry_server", "focus-out-event", account_widget_jabber_entry_focus_cb,
272                               "checkbutton_ssl", "toggled", account_widget_jabber_checkbutton_toggled_cb,
273                               NULL);
274
275         g_object_unref (glade);
276
277         /* Set up remaining widgets */
278         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
279
280         gtk_size_group_add_widget (size_group, label_id);
281         gtk_size_group_add_widget (size_group, label_password);
282         gtk_size_group_add_widget (size_group, label_resource);
283         gtk_size_group_add_widget (size_group, label_server);
284         gtk_size_group_add_widget (size_group, label_port);
285         gtk_size_group_add_widget (size_group, label_priority);
286
287         g_object_unref (size_group);
288
289         gtk_widget_show (settings->vbox_settings);
290
291         return settings->vbox_settings;
292 }