]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-msn.c
edcd185ad413b7a67f906acfcccfd67b5138179f
[empathy.git] / libempathy-gtk / empathy-account-widget-msn.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  *          Cosimo Cecchi <anarki@lilik.it>
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 #include <libempathy/empathy-debug.h>
37
38 #include "empathy-account-widget-msn.h"
39 #include "empathy-ui-utils.h"
40
41 #define DEBUG_DOMAIN "AccountWidgetMSN"
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_server;
51         GtkWidget *spinbutton_port;
52 } EmpathyAccountWidgetMSN;
53
54 static gboolean
55 account_widget_msn_entry_focus_cb (GtkWidget               *widget,
56                                    GdkEventFocus           *event,
57                                    EmpathyAccountWidgetMSN *settings)
58 {
59         const gchar *param;
60         const gchar *str;
61         
62         if (widget == settings->entry_password) {
63                 param = "password";
64         }
65         else if (widget == settings->entry_server) {
66                 param = "server";
67         }
68         else if (widget == settings->entry_id) {
69                 param = "account";
70         } else {
71                 return FALSE;
72         }
73         
74         str = gtk_entry_get_text (GTK_ENTRY (widget));
75         if (G_STR_EMPTY (str)) {
76                 gchar *value = NULL;
77
78                 mc_account_unset_param (settings->account, param);
79                 mc_account_get_param_string (settings->account, param, &value);
80                 empathy_debug (DEBUG_DOMAIN, "Unset %s and restore to %s", param, value);
81                 gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
82                 g_free (value);
83         } else {
84                 empathy_debug (DEBUG_DOMAIN, "Setting %s to %s", param, str);
85                 mc_account_set_param_string (settings->account, param, str);
86         }
87
88         return FALSE;
89 }
90
91 static void
92 account_widget_msn_entry_changed_cb (GtkWidget               *widget,
93                                      EmpathyAccountWidgetMSN *settings)
94 {
95         if (widget == settings->entry_password) {
96                 const gchar *str;
97
98                 str = gtk_entry_get_text (GTK_ENTRY (widget));
99                 gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
100         }
101 }
102
103 static void
104 account_widget_msn_value_changed_cb (GtkWidget                  *spinbutton,
105                                      EmpathyAccountWidgetMSN    *settings)
106 {
107         gdouble      value;
108         const gchar *param;
109
110         value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
111
112         if (spinbutton == settings->spinbutton_port) {
113                 param = "port";
114         } else {
115                 return;
116         }
117
118         if (value != 0) {
119                 empathy_debug (DEBUG_DOMAIN, "Setting %s to %d", param, (gint) value);
120                 mc_account_set_param_int (settings->account, param, (gint) value);
121         } else {
122                 gint val;
123
124                 mc_account_unset_param (settings->account, param);
125                 mc_account_get_param_int (settings->account, param, &val);
126                 empathy_debug (DEBUG_DOMAIN, "Unset %s and restore to %d", param, val);
127
128                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), val);
129         }
130 }
131
132 static void
133 account_widget_msn_button_forget_clicked_cb (GtkWidget               *button,
134                                              EmpathyAccountWidgetMSN *settings)
135 {
136         empathy_debug (DEBUG_DOMAIN, "Unset password");
137         mc_account_unset_param (settings->account, "password");
138         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
139 }
140
141 static void
142 account_widget_msn_destroy_cb (GtkWidget               *widget,
143                                EmpathyAccountWidgetMSN *settings)
144 {
145         g_object_unref (settings->account);
146         g_free (settings);
147 }
148
149 static void
150 account_widget_msn_setup (EmpathyAccountWidgetMSN *settings)
151 {
152         guint  port = 0;
153         gchar *id = NULL;
154         gchar *server = NULL;
155         gchar *password = NULL;
156
157         mc_account_get_param_int (settings->account, "port", &port);
158         mc_account_get_param_string (settings->account, "account", &id);
159         mc_account_get_param_string (settings->account, "server", &server);
160         mc_account_get_param_string (settings->account, "password", &password);
161
162         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
163         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
164         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
165         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
166
167         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
168
169         g_free (id);
170         g_free (server);
171         g_free (password);
172 }
173
174 GtkWidget *
175 empathy_account_widget_msn_new (McAccount *account)
176 {
177         EmpathyAccountWidgetMSN *settings;
178         GladeXML                *glade;
179
180         settings = g_new0 (EmpathyAccountWidgetMSN, 1);
181         settings->account = g_object_ref (account);
182
183         glade = empathy_glade_get_file ("empathy-account-widget-msn.glade",
184                                        "vbox_msn_settings",
185                                        NULL,
186                                        "vbox_msn_settings", &settings->vbox_settings,
187                                        "button_forget", &settings->button_forget,
188                                        "entry_id", &settings->entry_id,
189                                        "entry_password", &settings->entry_password,
190                                        "entry_server", &settings->entry_server,
191                                        "spinbutton_port", &settings->spinbutton_port,
192                                        NULL);
193
194         account_widget_msn_setup (settings);
195
196         empathy_glade_connect (glade, 
197                               settings,
198                               "vbox_msn_settings", "destroy", account_widget_msn_destroy_cb,
199                               "button_forget", "clicked", account_widget_msn_button_forget_clicked_cb,
200                               "entry_id", "changed", account_widget_msn_entry_changed_cb,
201                               "entry_password", "changed", account_widget_msn_entry_changed_cb,
202                               "entry_server", "changed", account_widget_msn_entry_changed_cb,
203                               "entry_id", "focus-out-event", account_widget_msn_entry_focus_cb,
204                               "entry_password", "focus-out-event", account_widget_msn_entry_focus_cb,
205                               "entry_server", "focus-out-event", account_widget_msn_entry_focus_cb,
206                               "spinbutton_port", "value-changed", account_widget_msn_value_changed_cb,
207                               NULL);
208
209         g_object_unref (glade);
210
211         gtk_widget_show (settings->vbox_settings);
212
213         return settings->vbox_settings;
214 }