9e58843a53a425253d37812d5e302a87736148d0
[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 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: 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
37 #include "empathy-account-widget-msn.h"
38 #include "empathy-ui-utils.h"
39
40 typedef struct {
41         McAccount *account;
42
43         GtkWidget *vbox_settings;
44         GtkWidget *button_forget;
45         GtkWidget *entry_id;
46         GtkWidget *entry_password;
47         GtkWidget *entry_server;
48         GtkWidget *spinbutton_port;
49 } EmpathyAccountWidgetMSN;
50
51 static gboolean account_widget_msn_entry_focus_cb            (GtkWidget               *widget,
52                                                               GdkEventFocus           *event,
53                                                               EmpathyAccountWidgetMSN *settings);
54 static void     account_widget_msn_entry_changed_cb          (GtkWidget               *widget,
55                                                               EmpathyAccountWidgetMSN *settings);
56 static void     account_widget_msn_value_changed_cb          (GtkWidget               *spinbutton,
57                                                               EmpathyAccountWidgetMSN *settings);
58 static void     account_widget_msn_button_forget_clicked_cb  (GtkWidget               *button,
59                                                               EmpathyAccountWidgetMSN *settings);
60 static void     account_widget_msn_destroy_cb                (GtkWidget               *widget,
61                                                               EmpathyAccountWidgetMSN *settings);
62 static void     account_widget_msn_setup                     (EmpathyAccountWidgetMSN *settings);
63
64 static gboolean
65 account_widget_msn_entry_focus_cb (GtkWidget               *widget,
66                                    GdkEventFocus           *event,
67                                    EmpathyAccountWidgetMSN *settings)
68 {
69         const gchar *param;
70         const gchar *str;
71         
72         if (widget == settings->entry_password) {
73                 param = "password";
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         
86         if (G_STR_EMPTY (str)) {
87                 gchar *value = NULL;
88
89                 mc_account_get_param_string (settings->account, param, &value);
90                 gtk_entry_set_text (GTK_ENTRY (widget), value ? value : "");
91                 g_free (value);
92         } else {
93                 mc_account_set_param_string (settings->account, param, str);
94         }
95
96         return FALSE;
97 }
98
99 static void
100 account_widget_msn_entry_changed_cb (GtkWidget               *widget,
101                                      EmpathyAccountWidgetMSN *settings)
102 {
103         if (widget == settings->entry_password) {
104                 const gchar *str;
105
106                 str = gtk_entry_get_text (GTK_ENTRY (widget));
107                 gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (str));
108         }
109 }
110
111 static void
112 account_widget_msn_value_changed_cb (GtkWidget                  *spinbutton,
113                                      EmpathyAccountWidgetMSN    *settings)
114 {
115         if (spinbutton == settings->spinbutton_port) {
116                 gdouble value;
117
118                 value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (spinbutton));
119                 mc_account_set_param_int (settings->account, "port", (gint) value);
120         }
121 }
122
123 static void
124 account_widget_msn_button_forget_clicked_cb (GtkWidget               *button,
125                                              EmpathyAccountWidgetMSN *settings)
126 {
127         mc_account_set_param_string (settings->account, "password", "");
128         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), "");
129 }
130
131 static void
132 account_widget_msn_destroy_cb (GtkWidget               *widget,
133                                EmpathyAccountWidgetMSN *settings)
134 {
135         g_object_unref (settings->account);
136         g_free (settings);
137 }
138
139 static void
140 account_widget_msn_setup (EmpathyAccountWidgetMSN *settings)
141 {
142         guint  port = 0;
143         gchar *id = NULL;
144         gchar *server = NULL;
145         gchar *password = NULL;
146
147         mc_account_get_param_int (settings->account, "port", &port);
148         mc_account_get_param_string (settings->account, "account", &id);
149         mc_account_get_param_string (settings->account, "server", &server);
150         mc_account_get_param_string (settings->account, "password", &password);
151
152         gtk_entry_set_text (GTK_ENTRY (settings->entry_id), id ? id : "");
153         gtk_entry_set_text (GTK_ENTRY (settings->entry_password), password ? password : "");
154         gtk_entry_set_text (GTK_ENTRY (settings->entry_server), server ? server : "");
155         gtk_spin_button_set_value (GTK_SPIN_BUTTON (settings->spinbutton_port), port);
156
157         gtk_widget_set_sensitive (settings->button_forget, !G_STR_EMPTY (password));
158
159         g_free (id);
160         g_free (server);
161         g_free (password);
162 }
163
164 GtkWidget *
165 empathy_account_widget_msn_new (McAccount *account)
166 {
167         EmpathyAccountWidgetMSN *settings;
168         GladeXML                *glade;
169         GtkSizeGroup            *size_group;
170         GtkWidget               *label_id;
171         GtkWidget               *label_password;
172         GtkWidget               *label_server;
173         GtkWidget               *label_port; 
174
175         settings = g_new0 (EmpathyAccountWidgetMSN, 1);
176         settings->account = g_object_ref (account);
177
178         glade = empathy_glade_get_file ("empathy-account-widget-msn.glade",
179                                        "vbox_msn_settings",
180                                        NULL,
181                                        "vbox_msn_settings", &settings->vbox_settings,
182                                        "button_forget", &settings->button_forget,
183                                        "label_id", &label_id,
184                                        "label_password", &label_password,
185                                        "label_server", &label_server,
186                                        "label_port", &label_port,
187                                        "entry_id", &settings->entry_id,
188                                        "entry_password", &settings->entry_password,
189                                        "entry_server", &settings->entry_server,
190                                        "spinbutton_port", &settings->spinbutton_port,
191                                        NULL);
192
193         account_widget_msn_setup (settings);
194
195         empathy_glade_connect (glade, 
196                               settings,
197                               "vbox_msn_settings", "destroy", account_widget_msn_destroy_cb,
198                               "button_forget", "clicked", account_widget_msn_button_forget_clicked_cb,
199                               "entry_id", "changed", account_widget_msn_entry_changed_cb,
200                               "entry_password", "changed", account_widget_msn_entry_changed_cb,
201                               "entry_server", "changed", account_widget_msn_entry_changed_cb,
202                               "entry_id", "focus-out-event", account_widget_msn_entry_focus_cb,
203                               "entry_password", "focus-out-event", account_widget_msn_entry_focus_cb,
204                               "entry_server", "focus-out-event", account_widget_msn_entry_focus_cb,
205                               "spinbutton_port", "value-changed", account_widget_msn_value_changed_cb,
206                               NULL);
207
208         g_object_unref (glade);
209
210         /* Set up remaining widgets */
211         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
212
213         gtk_size_group_add_widget (size_group, label_id);
214         gtk_size_group_add_widget (size_group, label_password);
215         gtk_size_group_add_widget (size_group, label_server);
216         gtk_size_group_add_widget (size_group, label_port);
217
218         g_object_unref (size_group);
219
220         gtk_widget_show (settings->vbox_settings);
221
222         return settings->vbox_settings;
223 }