]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-widget-irc.c
Move should_create_salut_account to local-xmpp-assistant-widget
[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 <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26
27 #include <glib/gi18n-lib.h>
28 #include <gtk/gtk.h>
29
30 #include <libempathy/empathy-utils.h>
31
32 #include "empathy-irc-network-dialog.h"
33 #include "empathy-irc-network-chooser.h"
34 #include "empathy-account-widget.h"
35 #include "empathy-account-widget-private.h"
36 #include "empathy-account-widget-irc.h"
37 #include "empathy-ui-utils.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT | EMPATHY_DEBUG_IRC
40 #include <libempathy/empathy-debug.h>
41
42 typedef struct {
43   EmpathyAccountWidget *self;
44
45   GtkWidget *vbox_settings;
46
47   GtkWidget *network_chooser;
48 } EmpathyAccountWidgetIrc;
49
50 static void
51 account_widget_irc_destroy_cb (GtkWidget *widget,
52                                EmpathyAccountWidgetIrc *settings)
53 {
54   g_slice_free (EmpathyAccountWidgetIrc, settings);
55 }
56
57 static void
58 account_widget_irc_setup (EmpathyAccountWidgetIrc *settings)
59 {
60   const gchar *nick = NULL;
61   const gchar *fullname = NULL;
62   EmpathyAccountSettings *ac_settings;
63
64   g_object_get (settings->self, "settings", &ac_settings, NULL);
65
66   nick = empathy_account_settings_get_string (ac_settings, "account");
67   fullname = empathy_account_settings_get_string (ac_settings,
68       "fullname");
69
70   if (!nick)
71     {
72       nick = g_strdup (g_get_user_name ());
73       empathy_account_settings_set_string (ac_settings,
74         "account", nick);
75     }
76
77   if (!fullname)
78     {
79       fullname = g_strdup (g_get_real_name ());
80       if (!fullname)
81         {
82           fullname = g_strdup (nick);
83         }
84       empathy_account_settings_set_string (ac_settings,
85           "fullname", fullname);
86     }
87 }
88
89 static void
90 network_changed_cb (EmpathyIrcNetworkChooser *chooser,
91     EmpathyAccountWidgetIrc *settings)
92 {
93   empathy_account_widget_changed (settings->self);
94 }
95
96 /**
97  * set_password_prompt_if_needed:
98  *
99  * If @password is not empty, this function sets the 'password-prompt' param
100  * on @ac_settings. This will ensure that Idle actually asks for the password
101  * when connecting.
102  *
103  * Return: %TRUE if the password-prompt param has been changed
104  */
105 static gboolean
106 set_password_prompt_if_needed (EmpathyAccountSettings *ac_settings,
107     const gchar *password)
108 {
109   gboolean prompt;
110
111   prompt = !tp_str_empty (password);
112
113   if (prompt == empathy_account_settings_get_boolean (ac_settings,
114         "password-prompt"))
115     return FALSE;
116
117   empathy_account_settings_set_boolean (ac_settings, "password-prompt",
118       prompt);
119
120   return TRUE;
121 }
122
123 static void
124 entry_password_changed_cb (GtkEntry *entry,
125     EmpathyAccountWidgetIrc *settings)
126 {
127   const gchar *password;
128   EmpathyAccountSettings *ac_settings;
129
130   g_object_get (settings->self, "settings", &ac_settings, NULL);
131
132   password = gtk_entry_get_text (entry);
133
134   set_password_prompt_if_needed (ac_settings, password);
135
136   g_object_unref (ac_settings);
137 }
138
139 EmpathyIrcNetworkChooser *
140 empathy_account_widget_irc_build (EmpathyAccountWidget *self,
141     const char *filename,
142     GtkWidget **table_common_settings)
143 {
144   EmpathyAccountWidgetIrc *settings;
145   EmpathyAccountSettings *ac_settings;
146   GtkWidget *entry_password;
147   const gchar *password;
148
149   settings = g_slice_new0 (EmpathyAccountWidgetIrc);
150   settings->self = self;
151
152   self->ui_details->gui = empathy_builder_get_file (filename,
153       "table_irc_settings", table_common_settings,
154       "vbox_irc", &self->ui_details->widget,
155       "table_irc_settings", &settings->vbox_settings,
156       "entry_password", &entry_password,
157       NULL);
158
159   /* Add network chooser button */
160   g_object_get (settings->self, "settings", &ac_settings, NULL);
161
162   settings->network_chooser = empathy_irc_network_chooser_new (ac_settings);
163
164   g_signal_connect (settings->network_chooser, "changed",
165       G_CALLBACK (network_changed_cb), settings);
166
167   gtk_grid_attach (GTK_GRID (*table_common_settings),
168       settings->network_chooser, 1, 0, 1, 1);
169
170   gtk_widget_show (settings->network_chooser);
171
172   account_widget_irc_setup (settings);
173
174   empathy_account_widget_handle_params (self,
175       "entry_nick", "account",
176       "entry_fullname", "fullname",
177       "entry_password", "password",
178       "entry_quit_message", "quit-message",
179       "entry_username", "username",
180       NULL);
181
182   empathy_builder_connect (self->ui_details->gui, settings,
183       "table_irc_settings", "destroy", account_widget_irc_destroy_cb,
184       NULL);
185
186   self->ui_details->default_focus = g_strdup ("entry_nick");
187
188   g_object_unref (ac_settings);
189
190   /* Automatically set password-prompt when needed */
191   password = empathy_account_settings_get_string (ac_settings, "password");
192
193   if (set_password_prompt_if_needed (ac_settings, password))
194     {
195       /* Apply right now to save password-prompt */
196       empathy_account_settings_apply_async (ac_settings, NULL, NULL);
197     }
198
199   g_signal_connect (entry_password, "changed",
200       G_CALLBACK (entry_password_changed_cb), settings);
201
202   return EMPATHY_IRC_NETWORK_CHOOSER (settings->network_chooser);
203 }
204
205 EmpathyIrcNetworkChooser *
206 empathy_account_widget_irc_build_simple (EmpathyAccountWidget *self,
207     const char *filename)
208 {
209   EmpathyAccountWidgetIrc *settings;
210   EmpathyAccountSettings *ac_settings;
211   GtkAlignment *alignment;
212
213   settings = g_slice_new0 (EmpathyAccountWidgetIrc);
214   settings->self = self;
215
216   self->ui_details->gui = empathy_builder_get_file (filename,
217       "vbox_irc_simple", &self->ui_details->widget,
218       "alignment_network_simple", &alignment,
219       NULL);
220
221   /* Add network chooser button */
222   g_object_get (settings->self, "settings", &ac_settings, NULL);
223
224   settings->network_chooser = empathy_irc_network_chooser_new (ac_settings);
225
226   g_signal_connect (settings->network_chooser, "changed",
227       G_CALLBACK (network_changed_cb), settings);
228
229   gtk_container_add (GTK_CONTAINER (alignment), settings->network_chooser);
230
231   gtk_widget_show (settings->network_chooser);
232
233   empathy_account_widget_handle_params (self,
234       "entry_nick_simple", "account",
235       NULL);
236
237   empathy_builder_connect (self->ui_details->gui, settings,
238       "vbox_irc_simple", "destroy", account_widget_irc_destroy_cb,
239       NULL);
240
241   self->ui_details->default_focus = g_strdup ("entry_nick_simple");
242
243   g_object_unref (ac_settings);
244
245   return EMPATHY_IRC_NETWORK_CHOOSER (settings->network_chooser);
246 }