]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-irc.c
UOA: Do not segfault when "Done" or "Cancel" button clicked but widget is not ready yet
[empathy.git] / libempathy-gtk / empathy-account-widget-irc.c
1 /*
2  * Copyright (C) 2007-2008 Guillaume Desmottes
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Guillaume Desmottes <gdesmott@gnome.org>
19  */
20
21 #include "config.h"
22
23 #include "empathy-account-widget-private.h"
24 #include "empathy-account-widget-irc.h"
25 #include "empathy-ui-utils.h"
26
27 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT | EMPATHY_DEBUG_IRC
28 #include <libempathy/empathy-debug.h>
29
30 typedef struct {
31   EmpathyAccountWidget *self;
32
33   GtkWidget *vbox_settings;
34
35   GtkWidget *network_chooser;
36 } EmpathyAccountWidgetIrc;
37
38 static void
39 account_widget_irc_destroy_cb (GtkWidget *widget,
40                                EmpathyAccountWidgetIrc *settings)
41 {
42   g_slice_free (EmpathyAccountWidgetIrc, settings);
43 }
44
45 static void
46 account_widget_irc_setup (EmpathyAccountWidgetIrc *settings)
47 {
48   gchar *nick = NULL;
49   gchar *fullname = NULL;
50   EmpathyAccountSettings *ac_settings;
51
52   g_object_get (settings->self, "settings", &ac_settings, NULL);
53
54   nick = empathy_account_settings_dup_string (ac_settings, "account");
55   fullname = empathy_account_settings_dup_string (ac_settings,
56       "fullname");
57
58   if (nick == NULL)
59     {
60       nick = g_strdup (g_get_user_name ());
61
62       empathy_account_settings_set (ac_settings,
63         "account", g_variant_new_string (nick));
64     }
65
66   if (fullname == NULL)
67     {
68       fullname = g_strdup (g_get_real_name ());
69
70       if (fullname == NULL)
71           fullname = g_strdup (nick);
72
73       empathy_account_settings_set (ac_settings,
74           "fullname", g_variant_new_string (fullname));
75     }
76
77   g_free (nick);
78   g_free (fullname);
79 }
80
81 static void
82 network_changed_cb (EmpathyIrcNetworkChooser *chooser,
83     EmpathyAccountWidgetIrc *settings)
84 {
85   empathy_account_widget_changed (settings->self);
86 }
87
88 /**
89  * set_password_prompt_if_needed:
90  *
91  * If @password is not empty, this function sets the 'password-prompt' param
92  * on @ac_settings. This will ensure that Idle actually asks for the password
93  * when connecting.
94  *
95  * Return: %TRUE if the password-prompt param has been changed
96  */
97 static gboolean
98 set_password_prompt_if_needed (EmpathyAccountSettings *ac_settings,
99     const gchar *password)
100 {
101   gboolean prompt;
102
103   prompt = !tp_str_empty (password);
104
105   if (prompt == empathy_account_settings_get_boolean (ac_settings,
106         "password-prompt"))
107     return FALSE;
108
109   empathy_account_settings_set (ac_settings, "password-prompt",
110       g_variant_new_boolean (prompt));
111
112   return TRUE;
113 }
114
115 static void
116 entry_password_changed_cb (GtkEntry *entry,
117     EmpathyAccountWidgetIrc *settings)
118 {
119   const gchar *password;
120   EmpathyAccountSettings *ac_settings;
121
122   g_object_get (settings->self, "settings", &ac_settings, NULL);
123
124   password = gtk_entry_get_text (entry);
125
126   set_password_prompt_if_needed (ac_settings, password);
127
128   g_object_unref (ac_settings);
129 }
130
131 EmpathyIrcNetworkChooser *
132 empathy_account_widget_irc_build (EmpathyAccountWidget *self,
133     const char *filename,
134     GtkWidget **table_common_settings,
135     GtkWidget **box)
136 {
137   EmpathyAccountWidgetIrc *settings;
138   EmpathyAccountSettings *ac_settings;
139   GtkWidget *entry_password;
140   gchar *password;
141
142   settings = g_slice_new0 (EmpathyAccountWidgetIrc);
143   settings->self = self;
144
145   self->ui_details->gui = empathy_builder_get_file (filename,
146       "table_irc_settings", table_common_settings,
147       "vbox_irc", box,
148       "table_irc_settings", &settings->vbox_settings,
149       "entry_password", &entry_password,
150       NULL);
151
152   /* Add network chooser button */
153   g_object_get (settings->self, "settings", &ac_settings, NULL);
154
155   settings->network_chooser = empathy_irc_network_chooser_new (ac_settings);
156
157   g_signal_connect (settings->network_chooser, "changed",
158       G_CALLBACK (network_changed_cb), settings);
159
160   gtk_grid_attach (GTK_GRID (*table_common_settings),
161       settings->network_chooser, 1, 0, 1, 1);
162
163   gtk_widget_show (settings->network_chooser);
164
165   account_widget_irc_setup (settings);
166
167   empathy_account_widget_handle_params (self,
168       "entry_nick", "account",
169       "entry_fullname", "fullname",
170       "entry_password", "password",
171       "entry_quit_message", "quit-message",
172       "entry_username", "username",
173       NULL);
174
175   empathy_builder_connect (self->ui_details->gui, settings,
176       "table_irc_settings", "destroy", account_widget_irc_destroy_cb,
177       NULL);
178
179   self->ui_details->default_focus = g_strdup ("entry_nick");
180
181   g_object_unref (ac_settings);
182
183   /* Automatically set password-prompt when needed */
184   password = empathy_account_settings_dup_string (ac_settings, "password");
185
186   if (set_password_prompt_if_needed (ac_settings, password))
187     {
188       /* Apply right now to save password-prompt */
189       empathy_account_settings_apply_async (ac_settings, NULL, NULL);
190     }
191
192   g_free (password);
193
194   g_signal_connect (entry_password, "changed",
195       G_CALLBACK (entry_password_changed_cb), settings);
196
197   return EMPATHY_IRC_NETWORK_CHOOSER (settings->network_chooser);
198 }
199
200 EmpathyIrcNetworkChooser *
201 empathy_account_widget_irc_build_simple (EmpathyAccountWidget *self,
202     const char *filename,
203     GtkWidget **box)
204 {
205   EmpathyAccountWidgetIrc *settings;
206   EmpathyAccountSettings *ac_settings;
207   GtkAlignment *alignment;
208
209   settings = g_slice_new0 (EmpathyAccountWidgetIrc);
210   settings->self = self;
211
212   self->ui_details->gui = empathy_builder_get_file (filename,
213       "vbox_irc_simple", box,
214       "alignment_network_simple", &alignment,
215       NULL);
216
217   /* Add network chooser button */
218   g_object_get (settings->self, "settings", &ac_settings, NULL);
219
220   settings->network_chooser = empathy_irc_network_chooser_new (ac_settings);
221
222   g_signal_connect (settings->network_chooser, "changed",
223       G_CALLBACK (network_changed_cb), settings);
224
225   gtk_container_add (GTK_CONTAINER (alignment), settings->network_chooser);
226
227   gtk_widget_show (settings->network_chooser);
228
229   empathy_account_widget_handle_params (self,
230       "entry_nick_simple", "account",
231       NULL);
232
233   empathy_builder_connect (self->ui_details->gui, settings,
234       "vbox_irc_simple", "destroy", account_widget_irc_destroy_cb,
235       NULL);
236
237   self->ui_details->default_focus = g_strdup ("entry_nick_simple");
238
239   g_object_unref (ac_settings);
240
241   return EMPATHY_IRC_NETWORK_CHOOSER (settings->network_chooser);
242 }