~ubuntu-branches/ubuntu/natty/empathy/natty-updates

« back to all changes in this revision

Viewing changes to libempathy-gtk/empathy-account-widget-jabber.c

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2008-03-10 16:39:07 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20080310163907-tv41g2zmf0qqgi85
Tags: 0.22.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include <libempathy/empathy-debug.h>
36
 
 
37
 
#include "empathy-account-widget-jabber.h"
38
 
#include "empathy-ui-utils.h"
39
 
 
40
 
#define DEBUG_DOMAIN "AccountWidgetJabber"
41
 
 
42
 
#define PORT_WITHOUT_SSL 5222
43
 
#define PORT_WITH_SSL 5223
44
 
 
45
 
typedef struct {
46
 
        McAccount *account;
47
 
 
48
 
        GtkWidget *vbox_settings;
49
 
        GtkWidget *button_forget;
50
 
        GtkWidget *entry_id;
51
 
        GtkWidget *entry_password;
52
 
        GtkWidget *entry_resource;
53
 
        GtkWidget *entry_server;
54
 
        GtkWidget *spinbutton_port;
55
 
        GtkWidget *spinbutton_priority;
56
 
        GtkWidget *checkbutton_ssl;
57
 
        GtkWidget *checkbutton_ignore_ssl_errors;
58
 
        GtkWidget *checkbutton_encryption;
59
 
} EmpathyAccountWidgetJabber;
60
 
 
61
 
static gboolean
62
 
account_widget_jabber_entry_focus_cb (GtkWidget                 *widget,
63
 
                                      GdkEventFocus             *event,
64
 
                                      EmpathyAccountWidgetJabber *settings)
65
 
{
66
 
        const gchar *param;
67
 
        const gchar *str;
68
 
 
69
 
        if (widget == settings->entry_password) {
70
 
                param = "password";
71
 
        }
72
 
        else if (widget == settings->entry_resource) {
73
 
                param = "resource";
74
 
        }
75
 
        else if (widget == settings->entry_server) {
76
 
                param = "server";
77
 
        }
78
 
        else if (widget == settings->entry_id) {
79
 
                param = "account";
80
 
        } else {
81
 
                return FALSE;
82
 
        }
83
 
 
84
 
        str = gtk_entry_get_text (GTK_ENTRY (widget));
85
 
        if (G_STR_EMPTY (str)) {
86
 
                gchar *value = NULL;
87
 
 
88
 
                mc_account_unset_param (settings->account, param);
89
 
                mc_account_get_param_string (settings->account, param, &value);
90
 
                empathy_debug (DEBUG_DOMAIN, "Unset %s and restore to %s", param, value);
91
 
                gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
92
 
                g_free (value);
93
 
        } else {
94
 
                empathy_debug (DEBUG_DOMAIN, "Setting %s to %s", param, str);
95
 
                mc_account_set_param_string (settings->account, param, str);
96
 
        }
97
 
 
98
 
        return FALSE;
99
 
}
100
 
 
101
 
static void
102
 
account_widget_jabber_entry_changed_cb (GtkWidget                 *widget,
103
 
                                        EmpathyAccountWidgetJabber *settings)
104
 
{
105
 
        if (widget == settings->entry_password) {
106
 
                const gchar *str;
107
 
 
108
 
                str = gtk_entry_get_text (GTK_ENTRY (widget));
109
 
                gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
110
 
        }
111
 
}
112
 
 
113
 
static void  
114
 
account_widget_jabber_checkbutton_toggled_cb (GtkWidget                 *widget,
115
 
                                              EmpathyAccountWidgetJabber *settings)
116
 
{
117
 
        gboolean     value;
118
 
        gboolean     default_value;
119
 
        const gchar *param;
120
 
 
121
 
        value = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
122
 
 
123
 
        if (widget == settings->checkbutton_ssl) {
124
 
                gint port = 0;
125
 
 
126
 
                mc_account_get_param_int (settings->account, "port", &port);
127
 
 
128
 
                if (value) {
129
 
                        if (port == PORT_WITHOUT_SSL || port == 0) {
130
 
                                port = PORT_WITH_SSL;
131
 
                        }
132
 
                } else {
133
 
                        if (port == PORT_WITH_SSL || port == 0) {
134
 
                                port = PORT_WITHOUT_SSL;
135
 
                        }
136
 
                }
137
 
 
138
 
                gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
139
 
                param = "old-ssl";
140
 
        }
141
 
        else if (widget == settings->checkbutton_ignore_ssl_errors) {
142
 
                param = "ignore-ssl-errors";
143
 
        }
144
 
        else if (widget == settings->checkbutton_encryption) {
145
 
                param = "require-encryption";
146
 
        } else {
147
 
                return;
148
 
        }
149
 
 
150
 
        /* FIXME: This is ugly! checkbox don't have a "not-set" value so we
151
 
         * always unset the param and set the value if different from the
152
 
         * default value. */
153
 
        mc_account_unset_param (settings->account, param);
154
 
        mc_account_get_param_boolean (settings->account, param, &default_value);
155
 
 
156
 
        if (default_value != value) {
157
 
                empathy_debug (DEBUG_DOMAIN, "Setting %s to %d", param, value);
158
 
                mc_account_set_param_boolean (settings->account, param, value);
159
 
        } else {
160
 
                empathy_debug (DEBUG_DOMAIN, "Unset %s", param, value);
161
 
        }
162
 
}
163
 
 
164
 
static void
165
 
account_widget_jabber_value_changed_cb (GtkWidget                 *spinbutton,
166
 
                                        EmpathyAccountWidgetJabber *settings)
167
 
{
168
 
        gdouble      value;
169
 
        const gchar *param;
170
 
 
171
 
        value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
172
 
 
173
 
        if (spinbutton == settings->spinbutton_port) {
174
 
                param = "port";
175
 
        }
176
 
        else if (spinbutton == settings->spinbutton_priority) {
177
 
                param = "priority";
178
 
        } else {
179
 
                return;
180
 
        }
181
 
 
182
 
        if (value != 0) {
183
 
                empathy_debug (DEBUG_DOMAIN, "Setting %s to %d", param, (gint) value);
184
 
                mc_account_set_param_int (settings->account, param, (gint) value);
185
 
        } else {
186
 
                gint val;
187
 
 
188
 
                mc_account_unset_param (settings->account, param);
189
 
                mc_account_get_param_int (settings->account, param, &val);
190
 
                empathy_debug (DEBUG_DOMAIN, "Unset %s and restore to %d", param, val);
191
 
                gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), val);
192
 
        }
193
 
}
194
 
 
195
 
static void
196
 
account_widget_jabber_button_forget_clicked_cb (GtkWidget                 *button,
197
 
                                                EmpathyAccountWidgetJabber *settings)
198
 
{
199
 
        empathy_debug (DEBUG_DOMAIN, "Unset password");
200
 
        mc_account_unset_param (settings->account, "password");
201
 
        gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
202
 
}
203
 
 
204
 
static void
205
 
account_widget_jabber_destroy_cb (GtkWidget                 *widget,
206
 
                                  EmpathyAccountWidgetJabber *settings)
207
 
{
208
 
        g_object_unref (settings->account);
209
 
        g_free (settings);
210
 
}
211
 
 
212
 
static void
213
 
account_widget_jabber_setup (EmpathyAccountWidgetJabber *settings)
214
 
{
215
 
        gint      port = 0;
216
 
        gint      priority = 0;
217
 
        gchar    *id = NULL;
218
 
        gchar    *resource = NULL;
219
 
        gchar    *server = NULL;
220
 
        gchar    *password = NULL;
221
 
        gboolean  old_ssl = FALSE;
222
 
        gboolean  ignore_ssl_errors = FALSE;
223
 
        gboolean  encryption = FALSE;
224
 
 
225
 
        mc_account_get_param_int (settings->account, "port", &port);
226
 
        mc_account_get_param_int (settings->account, "priority", &priority);
227
 
        mc_account_get_param_string (settings->account, "account", &id);
228
 
        mc_account_get_param_string (settings->account, "resource", &resource);
229
 
        mc_account_get_param_string (settings->account, "server", &server);
230
 
        mc_account_get_param_string (settings->account, "password", &password);
231
 
        mc_account_get_param_boolean (settings->account, "old-ssl", &old_ssl);
232
 
        mc_account_get_param_boolean (settings->account, "ignore-ssl-errors", &ignore_ssl_errors);
233
 
        mc_account_get_param_boolean (settings->account, "require-encryption", &encryption);
234
 
 
235
 
        if (!id) {
236
 
                McProfile   *profile;
237
 
                const gchar *server;
238
 
 
239
 
                profile = mc_account_get_profile (settings->account);
240
 
                server = mc_profile_get_default_account_domain (profile);
241
 
                if (server) {
242
 
                        id = g_strconcat ("user@", server, NULL);
243
 
                }
244
 
                g_object_unref (profile);
245
 
        }
246
 
 
247
 
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
248
 
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_priority), priority);
249
 
        gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
250
 
        gtk_entry_set_text (GTK_ENTRY (settings->entry_resource), resource ? resource : "");
251
 
        gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
252
 
        gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
253
 
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ssl), old_ssl);
254
 
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_ignore_ssl_errors), ignore_ssl_errors);
255
 
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (settings->checkbutton_encryption), encryption);
256
 
 
257
 
        gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
258
 
 
259
 
        g_free (id);
260
 
        g_free (resource);
261
 
        g_free (server);
262
 
        g_free (password);
263
 
}
264
 
 
265
 
GtkWidget *
266
 
empathy_account_widget_jabber_new (McAccount *account)
267
 
{
268
 
        EmpathyAccountWidgetJabber *settings;
269
 
        GladeXML                  *glade;
270
 
        GtkWidget                 *label_id, *label_password;
271
 
        GtkWidget                 *label_server, *label_resource;
272
 
        GtkWidget                 *label_port, *label_priority; 
273
 
 
274
 
        settings = g_new0 (EmpathyAccountWidgetJabber, 1);
275
 
        settings->account = g_object_ref (account);
276
 
 
277
 
        glade = empathy_glade_get_file ("empathy-account-widget-jabber.glade",
278
 
                                       "vbox_jabber_settings",
279
 
                                       NULL,
280
 
                                       "vbox_jabber_settings", &settings->vbox_settings,
281
 
                                       "button_forget", &settings->button_forget,
282
 
                                       "label_id", &label_id,
283
 
                                       "label_password", &label_password,
284
 
                                       "label_resource", &label_resource,
285
 
                                       "label_priority", &label_priority,
286
 
                                       "label_server", &label_server,
287
 
                                       "label_port", &label_port,
288
 
                                       "entry_id", &settings->entry_id,
289
 
                                       "entry_password", &settings->entry_password,
290
 
                                       "entry_resource", &settings->entry_resource,
291
 
                                       "entry_server", &settings->entry_server,
292
 
                                       "spinbutton_port", &settings->spinbutton_port,
293
 
                                       "spinbutton_priority", &settings->spinbutton_priority,
294
 
                                       "checkbutton_ssl", &settings->checkbutton_ssl,
295
 
                                       "checkbutton_ignore_ssl_errors", &settings->checkbutton_ignore_ssl_errors,
296
 
                                       "checkbutton_encryption", &settings->checkbutton_encryption,
297
 
                                       NULL);
298
 
 
299
 
        account_widget_jabber_setup (settings);
300
 
 
301
 
        empathy_glade_connect (glade, 
302
 
                              settings,
303
 
                              "vbox_jabber_settings", "destroy", account_widget_jabber_destroy_cb,
304
 
                              "button_forget", "clicked", account_widget_jabber_button_forget_clicked_cb,
305
 
                              "entry_password", "changed", account_widget_jabber_entry_changed_cb,
306
 
                              "spinbutton_port", "value-changed", account_widget_jabber_value_changed_cb,
307
 
                              "spinbutton_priority", "value-changed", account_widget_jabber_value_changed_cb,
308
 
                              "entry_id", "focus-out-event", account_widget_jabber_entry_focus_cb,
309
 
                              "entry_password", "focus-out-event", account_widget_jabber_entry_focus_cb,
310
 
                              "entry_resource", "focus-out-event", account_widget_jabber_entry_focus_cb,
311
 
                              "entry_server", "focus-out-event", account_widget_jabber_entry_focus_cb,
312
 
                              "checkbutton_ssl", "toggled", account_widget_jabber_checkbutton_toggled_cb,
313
 
                              "checkbutton_ignore_ssl_errors", "toggled", account_widget_jabber_checkbutton_toggled_cb,
314
 
                              "checkbutton_encryption", "toggled", account_widget_jabber_checkbutton_toggled_cb,
315
 
                              NULL);
316
 
 
317
 
        g_object_unref (glade);
318
 
 
319
 
        gtk_widget_show (settings->vbox_settings);
320
 
 
321
 
        return settings->vbox_settings;
322
 
}