]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-irc-network-chooser.c
account-settings: allow to change the service
[empathy.git] / libempathy-gtk / empathy-irc-network-chooser.c
1 /*
2  * Copyright (C) 2007-2008 Guillaume Desmottes
3  * Copyright (C) 2010 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: Guillaume Desmottes <gdesmott@gnome.org>
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27
28 #include <glib/gi18n-lib.h>
29 #include <gtk/gtk.h>
30
31 #include <libempathy/empathy-utils.h>
32 #include <libempathy/empathy-irc-network-manager.h>
33
34 #include "empathy-irc-network-dialog.h"
35 #include "empathy-ui-utils.h"
36 #include "empathy-irc-network-chooser-dialog.h"
37
38 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT | EMPATHY_DEBUG_IRC
39 #include <libempathy/empathy-debug.h>
40
41 #include "empathy-irc-network-chooser.h"
42
43 #define DEFAULT_IRC_NETWORK "irc.gimp.org"
44 #define DEFAULT_IRC_PORT 6667
45 #define DEFAULT_IRC_SSL FALSE
46
47 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetworkChooser)
48
49 enum {
50     PROP_SETTINGS = 1
51 };
52
53 enum {
54     SIG_CHANGED,
55     LAST_SIGNAL
56 };
57
58 static guint signals[LAST_SIGNAL] = { 0 };
59
60 typedef struct {
61     EmpathyAccountSettings *settings;
62
63     EmpathyIrcNetworkManager *network_manager;
64     GtkWidget *dialog;
65     /* Displayed network */
66     EmpathyIrcNetwork *network;
67 } EmpathyIrcNetworkChooserPriv;
68
69 G_DEFINE_TYPE (EmpathyIrcNetworkChooser, empathy_irc_network_chooser,
70     GTK_TYPE_BUTTON);
71
72 static void
73 empathy_irc_network_chooser_set_property (GObject *object,
74     guint prop_id,
75     const GValue *value,
76     GParamSpec *pspec)
77 {
78   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (object);
79
80   switch (prop_id)
81     {
82       case PROP_SETTINGS:
83         priv->settings = g_value_dup_object (value);
84         break;
85       default:
86         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87         break;
88     }
89 }
90
91 static void
92 empathy_irc_network_chooser_get_property (GObject *object,
93     guint prop_id,
94     GValue *value,
95     GParamSpec *pspec)
96 {
97   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (object);
98
99   switch (prop_id)
100     {
101       case PROP_SETTINGS:
102         g_value_set_object (value, priv->settings);
103         break;
104       default:
105         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106         break;
107     }
108 }
109
110 static void
111 unset_server_params (EmpathyIrcNetworkChooser *self)
112 {
113   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
114
115   DEBUG ("Unset server, port and use-ssl");
116   empathy_account_settings_unset (priv->settings, "server");
117   empathy_account_settings_unset (priv->settings, "port");
118   empathy_account_settings_unset (priv->settings, "use-ssl");
119 }
120
121 static void
122 update_server_params (EmpathyIrcNetworkChooser *self)
123 {
124   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
125   GSList *servers;
126   const gchar *charset;
127
128   g_assert (priv->network != NULL);
129
130   charset = empathy_irc_network_get_charset (priv->network);
131   DEBUG ("Setting charset to %s", charset);
132   empathy_account_settings_set_string (priv->settings, "charset", charset);
133
134   servers = empathy_irc_network_get_servers (priv->network);
135   if (g_slist_length (servers) > 0)
136     {
137       /* set the first server as CM server */
138       EmpathyIrcServer *server = servers->data;
139       gchar *address;
140       guint port;
141       gboolean ssl;
142
143       g_object_get (server,
144           "address", &address,
145           "port", &port,
146           "ssl", &ssl,
147           NULL);
148
149       DEBUG ("Setting server to %s", address);
150       empathy_account_settings_set_string (priv->settings, "server", address);
151       DEBUG ("Setting port to %u", port);
152       empathy_account_settings_set_uint32 (priv->settings, "port", port);
153       DEBUG ("Setting use-ssl to %s", ssl ? "TRUE": "FALSE" );
154       empathy_account_settings_set_boolean (priv->settings, "use-ssl", ssl);
155
156       g_free (address);
157     }
158   else
159     {
160       /* No server. Unset values */
161       unset_server_params (self);
162     }
163
164   g_slist_foreach (servers, (GFunc) g_object_unref, NULL);
165   g_slist_free (servers);
166 }
167
168 static void
169 set_label (EmpathyIrcNetworkChooser *self)
170 {
171   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
172
173   g_assert (priv->network != NULL);
174
175   gtk_button_set_label (GTK_BUTTON (self),
176       empathy_irc_network_get_name (priv->network));
177 }
178
179 static void
180 set_label_from_settings (EmpathyIrcNetworkChooser *self)
181 {
182   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
183   const gchar *server;
184
185   tp_clear_object (&priv->network);
186
187   server = empathy_account_settings_get_string (priv->settings, "server");
188
189   if (server != NULL)
190     {
191       EmpathyIrcServer *srv;
192       gint port;
193       gboolean ssl;
194
195       priv->network = empathy_irc_network_manager_find_network_by_address (
196           priv->network_manager, server);
197
198       if (priv->network != NULL)
199         {
200           /* The network is known */
201           g_object_ref (priv->network);
202           set_label (self);
203           return;
204         }
205
206       /* We don't have this network. Let's create it */
207       port = empathy_account_settings_get_uint32 (priv->settings, "port");
208       ssl = empathy_account_settings_get_boolean (priv->settings,
209           "use-ssl");
210
211       DEBUG ("Create a network %s", server);
212       priv->network = empathy_irc_network_new (server);
213       srv = empathy_irc_server_new (server, port, ssl);
214
215       empathy_irc_network_append_server (priv->network, srv);
216       empathy_irc_network_manager_add (priv->network_manager, priv->network);
217
218       set_label (self);
219
220       g_object_unref (srv);
221       return;
222     }
223
224   /* Set default network */
225   priv->network = empathy_irc_network_manager_find_network_by_address (
226           priv->network_manager, DEFAULT_IRC_NETWORK);
227
228   if (priv->network == NULL)
229     {
230       /* Default network is not known, recreate it */
231       EmpathyIrcServer *srv;
232
233       priv->network = empathy_irc_network_new (DEFAULT_IRC_NETWORK);
234
235       srv = empathy_irc_server_new (DEFAULT_IRC_NETWORK, DEFAULT_IRC_PORT,
236           DEFAULT_IRC_SSL);
237
238       empathy_irc_network_append_server (priv->network, srv);
239       empathy_irc_network_manager_add (priv->network_manager, priv->network);
240
241       g_object_unref (srv);
242     }
243
244   set_label (self);
245   update_server_params (self);
246   g_object_ref (priv->network);
247 }
248
249 static void
250 dialog_response_cb (GtkDialog *dialog,
251     gint response,
252     EmpathyIrcNetworkChooser *self)
253 {
254   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
255   EmpathyIrcNetworkChooserDialog *chooser =
256     EMPATHY_IRC_NETWORK_CHOOSER_DIALOG (priv->dialog);
257
258   if (response != GTK_RESPONSE_CLOSE &&
259       response != GTK_RESPONSE_DELETE_EVENT)
260     return;
261
262   if (empathy_irc_network_chooser_dialog_get_changed (chooser))
263     {
264       tp_clear_object (&priv->network);
265
266       priv->network = g_object_ref (
267           empathy_irc_network_chooser_dialog_get_network (chooser));
268
269       update_server_params (self);
270       set_label (self);
271
272       g_signal_emit (self, signals[SIG_CHANGED], 0);
273     }
274
275   gtk_widget_destroy (priv->dialog);
276   priv->dialog = NULL;
277 }
278
279 static void
280 clicked_cb (GtkButton *button,
281     gpointer user_data)
282 {
283   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (button);
284   GtkWindow *window;
285
286   if (priv->dialog != NULL)
287     goto out;
288
289   window = empathy_get_toplevel_window (GTK_WIDGET (button));
290
291   priv->dialog = empathy_irc_network_chooser_dialog_new (priv->settings,
292       priv->network, window);
293   gtk_widget_show_all (priv->dialog);
294
295   tp_g_signal_connect_object (priv->dialog, "response",
296       G_CALLBACK (dialog_response_cb), button, 0);
297
298 out:
299   empathy_window_present (GTK_WINDOW (priv->dialog));
300 }
301
302 static void
303 empathy_irc_network_chooser_constructed (GObject *object)
304 {
305   EmpathyIrcNetworkChooser *self = (EmpathyIrcNetworkChooser *) object;
306   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
307
308   g_assert (priv->settings != NULL);
309
310   set_label_from_settings (self);
311
312   g_signal_connect (self, "clicked", G_CALLBACK (clicked_cb), self);
313 }
314
315 static void
316 empathy_irc_network_chooser_dispose (GObject *object)
317 {
318   EmpathyIrcNetworkManager *self = (EmpathyIrcNetworkManager *) object;
319   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
320
321   tp_clear_object (&priv->settings);
322   tp_clear_object (&priv->network_manager);
323   tp_clear_object (&priv->network);
324
325   if (G_OBJECT_CLASS (empathy_irc_network_chooser_parent_class)->dispose)
326     G_OBJECT_CLASS (empathy_irc_network_chooser_parent_class)->dispose (object);
327 }
328
329 static void
330 empathy_irc_network_chooser_class_init (EmpathyIrcNetworkChooserClass *klass)
331 {
332   GObjectClass *object_class = G_OBJECT_CLASS (klass);
333
334   object_class->get_property = empathy_irc_network_chooser_get_property;
335   object_class->set_property = empathy_irc_network_chooser_set_property;
336   object_class->constructed = empathy_irc_network_chooser_constructed;
337   object_class->dispose = empathy_irc_network_chooser_dispose;
338
339   g_object_class_install_property (object_class, PROP_SETTINGS,
340     g_param_spec_object ("settings",
341       "Settings",
342       "The EmpathyAccountSettings to show and edit",
343       EMPATHY_TYPE_ACCOUNT_SETTINGS,
344       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
345
346   signals[SIG_CHANGED] = g_signal_new ("changed",
347       G_OBJECT_CLASS_TYPE (object_class),
348       G_SIGNAL_RUN_LAST,
349       0,
350       NULL, NULL,
351       g_cclosure_marshal_generic,
352       G_TYPE_NONE,
353       0);
354
355   g_type_class_add_private (object_class,
356       sizeof (EmpathyIrcNetworkChooserPriv));
357 }
358
359 static void
360 empathy_irc_network_chooser_init (EmpathyIrcNetworkChooser *self)
361 {
362   EmpathyIrcNetworkChooserPriv *priv;
363
364   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
365       EMPATHY_TYPE_IRC_NETWORK_CHOOSER, EmpathyIrcNetworkChooserPriv);
366   self->priv = priv;
367
368   priv->network_manager = empathy_irc_network_manager_dup_default ();
369 }
370
371 GtkWidget *
372 empathy_irc_network_chooser_new (EmpathyAccountSettings *settings)
373 {
374   return g_object_new (EMPATHY_TYPE_IRC_NETWORK_CHOOSER,
375       "settings", settings,
376       NULL);
377 }
378
379 EmpathyIrcNetwork *
380 empathy_irc_network_chooser_get_network (EmpathyIrcNetworkChooser *self)
381 {
382   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
383
384   return priv->network;
385 }