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