]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-jabber.c
Rename all filenames starting with "gossip" by "empathy", change namespace
[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
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         if (spinbutton == settings->spinbutton_port) {
152                 gdouble value;
153
154                 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
155                 mc_account_set_param_int (settings->account, "port", (gint) value);
156         }
157 }
158
159 static void
160 account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
161                                                 EmpathyAccountWidgetJabber *settings)
162 {
163         mc_account_set_param_string (settings->account, "password", "");
164         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
165 }
166
167 static void
168 account_widget_jabber_destroy_cb (GtkWidget                 *widget,
169                                   EmpathyAccountWidgetJabber *settings)
170 {
171         g_object_unref (settings->account);
172         g_free (settings);
173 }
174
175 static void
176 account_widget_jabber_setup (EmpathyAccountWidgetJabber *settings)
177 {
178         gint      port = 0;
179         gchar    *id = NULL;
180         gchar    *resource = NULL;
181         gchar    *server = NULL;
182         gchar    *password = NULL;
183         gboolean  old_ssl = FALSE;
184
185         mc_account_get_param_int (settings->account, "port", &port);
186         mc_account_get_param_string (settings->account, "account", &id);
187         mc_account_get_param_string (settings->account, "resource", &resource);
188         mc_account_get_param_string (settings->account, "server", &server);
189         mc_account_get_param_string (settings->account, "password", &password);
190         mc_account_get_param_boolean (settings->account, "old-ssl", &old_ssl);
191
192         if (!id) {
193                 McProfile   *profile;
194                 const gchar *server;
195
196                 profile = mc_account_get_profile (settings->account);
197                 server = mc_profile_get_default_account_domain (profile);
198                 if (server) {
199                         id = g_strconcat ("user@", server, NULL);
200                 }
201                 g_object_unref (profile);
202         }
203
204         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ssl), old_ssl);
205         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
206         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
207         gtk_entry_set_text (GTK_ENTRY (settings->entry_resource), resource ? resource : "");
208         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
209         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
210
211         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
212
213         g_free (id);
214         g_free (resource);
215         g_free (server);
216         g_free (password);
217 }
218
219 GtkWidget *
220 empathy_account_widget_jabber_new (McAccount *account)
221 {
222         EmpathyAccountWidgetJabber *settings;
223         GladeXML                  *glade;
224         GtkSizeGroup              *size_group;
225         GtkWidget                 *label_id, *label_password;
226         GtkWidget                 *label_server, *label_resource, *label_port; 
227
228         settings = g_new0 (EmpathyAccountWidgetJabber, 1);
229         settings->account = g_object_ref (account);
230
231         glade = empathy_glade_get_file ("empathy-account-widget-jabber.glade",
232                                        "vbox_jabber_settings",
233                                        NULL,
234                                        "vbox_jabber_settings", &settings->vbox_settings,
235                                        "button_forget", &settings->button_forget,
236                                        "label_id", &label_id,
237                                        "label_password", &label_password,
238                                        "label_resource", &label_resource,
239                                        "label_server", &label_server,
240                                        "label_port", &label_port,
241                                        "entry_id", &settings->entry_id,
242                                        "entry_password", &settings->entry_password,
243                                        "entry_resource", &settings->entry_resource,
244                                        "entry_server", &settings->entry_server,
245                                        "spinbutton_port", &settings->spinbutton_port,
246                                        "checkbutton_ssl", &settings->checkbutton_ssl,
247                                        NULL);
248
249         account_widget_jabber_setup (settings);
250
251         empathy_glade_connect (glade, 
252                               settings,
253                               "vbox_jabber_settings", "destroy", account_widget_jabber_destroy_cb,
254                               "button_forget", "clicked", account_widget_jabber_button_forget_clicked_cb,
255                               "entry_password", "changed", account_widget_jabber_entry_changed_cb,
256                               "spinbutton_port", "value-changed", account_widget_jabber_value_changed_cb,
257                               "entry_id", "focus-out-event", account_widget_jabber_entry_focus_cb,
258                               "entry_password", "focus-out-event", account_widget_jabber_entry_focus_cb,
259                               "entry_resource", "focus-out-event", account_widget_jabber_entry_focus_cb,
260                               "entry_server", "focus-out-event", account_widget_jabber_entry_focus_cb,
261                               "checkbutton_ssl", "toggled", account_widget_jabber_checkbutton_toggled_cb,
262                               NULL);
263
264         g_object_unref (glade);
265
266         /* Set up remaining widgets */
267         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
268
269         gtk_size_group_add_widget (size_group, label_id);
270         gtk_size_group_add_widget (size_group, label_password);
271         gtk_size_group_add_widget (size_group, label_resource);
272         gtk_size_group_add_widget (size_group, label_server);
273         gtk_size_group_add_widget (size_group, label_port);
274
275         g_object_unref (size_group);
276
277         gtk_widget_show (settings->vbox_settings);
278
279         return settings->vbox_settings;
280 }
281