]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-irc-network-chooser.c
add empathy-irc-network-chooser
[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
37 #define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT | EMPATHY_DEBUG_IRC
38 #include <libempathy/empathy-debug.h>
39
40 #include "empathy-irc-network-chooser.h"
41
42 #define DEFAULT_IRC_NETWORK "irc.gimp.org"
43
44 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetworkChooser)
45
46 enum {
47     PROP_SETTINGS = 1
48 };
49
50 typedef struct {
51     EmpathyAccountSettings *settings;
52
53     EmpathyIrcNetworkManager *network_manager;
54     /* Displayed network */
55     EmpathyIrcNetwork *network;
56 } EmpathyIrcNetworkChooserPriv;
57
58 G_DEFINE_TYPE (EmpathyIrcNetworkChooser, empathy_irc_network_chooser,
59     GTK_TYPE_BUTTON);
60
61 static void
62 empathy_irc_network_chooser_set_property (GObject *object,
63     guint prop_id,
64     const GValue *value,
65     GParamSpec *pspec)
66 {
67   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (object);
68
69   switch (prop_id)
70     {
71       case PROP_SETTINGS:
72         priv->settings = g_value_dup_object (value);
73         break;
74       default:
75         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
76         break;
77     }
78 }
79
80 static void
81 empathy_irc_network_chooser_get_property (GObject *object,
82     guint prop_id,
83     GValue *value,
84     GParamSpec *pspec)
85 {
86   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (object);
87
88   switch (prop_id)
89     {
90       case PROP_SETTINGS:
91         g_value_set_object (value, priv->settings);
92         break;
93       default:
94         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95         break;
96     }
97 }
98
99 static void
100 unset_server_params (EmpathyIrcNetworkChooser *self)
101 {
102   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
103
104   DEBUG ("Unset server, port and use-ssl");
105   empathy_account_settings_unset (priv->settings, "server");
106   empathy_account_settings_unset (priv->settings, "port");
107   empathy_account_settings_unset (priv->settings, "use-ssl");
108 }
109
110 static void
111 update_server_params (EmpathyIrcNetworkChooser *self)
112 {
113   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
114   GSList *servers;
115   gchar *charset;
116
117   g_assert (priv->network != NULL);
118
119   g_object_get (priv->network, "charset", &charset, NULL);
120   DEBUG ("Setting charset to %s", charset);
121   empathy_account_settings_set_string (priv->settings, "charset", charset);
122   g_free (charset);
123
124   servers = empathy_irc_network_get_servers (priv->network);
125   if (g_slist_length (servers) > 0)
126     {
127       /* set the first server as CM server */
128       EmpathyIrcServer *server = servers->data;
129       gchar *address;
130       guint port;
131       gboolean ssl;
132
133       g_object_get (server,
134           "address", &address,
135           "port", &port,
136           "ssl", &ssl,
137           NULL);
138
139       DEBUG ("Setting server to %s", address);
140       empathy_account_settings_set_string (priv->settings, "server", address);
141       DEBUG ("Setting port to %u", port);
142       empathy_account_settings_set_uint32 (priv->settings, "port", port);
143       DEBUG ("Setting use-ssl to %s", ssl ? "TRUE": "FALSE" );
144       empathy_account_settings_set_boolean (priv->settings, "use-ssl", ssl);
145
146       g_free (address);
147     }
148   else
149     {
150       /* No server. Unset values */
151       unset_server_params (self);
152     }
153
154   g_slist_foreach (servers, (GFunc) g_object_unref, NULL);
155   g_slist_free (servers);
156 }
157
158 static void
159 set_label (EmpathyIrcNetworkChooser *self)
160 {
161   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
162   gchar *name;
163
164   g_assert (priv->network != NULL);
165   g_object_get (priv->network, "name", &name, NULL);
166
167   gtk_button_set_label (GTK_BUTTON (self), name);
168   g_free (name);
169 }
170
171 static void
172 set_label_from_settings (EmpathyIrcNetworkChooser *self)
173 {
174   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
175   const gchar *server;
176
177   tp_clear_object (&priv->network);
178
179   server = empathy_account_settings_get_string (priv->settings, "server");
180
181   if (server != NULL)
182     {
183       EmpathyIrcServer *srv;
184       gint port;
185       gboolean ssl;
186
187       priv->network = empathy_irc_network_manager_find_network_by_address (
188           priv->network_manager, server);
189
190       if (priv->network != NULL)
191         {
192           /* The network is known */
193           g_object_ref (priv->network);
194           set_label (self);
195           return;
196         }
197
198       /* We don't have this network. Let's create it */
199       port = empathy_account_settings_get_uint32 (priv->settings, "port");
200       ssl = empathy_account_settings_get_boolean (priv->settings,
201           "use-ssl");
202
203       DEBUG ("Create a network %s", server);
204       priv->network = empathy_irc_network_new (server);
205       srv = empathy_irc_server_new (server, port, ssl);
206
207       empathy_irc_network_append_server (priv->network, srv);
208       empathy_irc_network_manager_add (priv->network_manager, priv->network);
209
210       set_label (self);
211
212       g_object_unref (srv);
213       return;
214     }
215
216   /* Set default network */
217   priv->network = empathy_irc_network_manager_find_network_by_address (
218           priv->network_manager, DEFAULT_IRC_NETWORK);
219   g_assert (priv->network != NULL);
220
221   set_label (self);
222   update_server_params (self);
223   g_object_ref (priv->network);
224 }
225
226 static void
227 clicked_cb (GtkButton *button,
228     gpointer user_data)
229 {
230   /* TODO: open edit dialog */
231 }
232
233 static void
234 empathy_irc_network_chooser_constructed (GObject *object)
235 {
236   EmpathyIrcNetworkChooser *self = (EmpathyIrcNetworkChooser *) object;
237   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
238
239   g_assert (priv->settings != NULL);
240
241   set_label_from_settings (self);
242
243   g_signal_connect (self, "clicked", G_CALLBACK (clicked_cb), self);
244 }
245
246 static void
247 empathy_irc_network_chooser_dispose (GObject *object)
248 {
249   EmpathyIrcNetworkManager *self = (EmpathyIrcNetworkManager *) object;
250   EmpathyIrcNetworkChooserPriv *priv = GET_PRIV (self);
251
252   tp_clear_object (&priv->settings);
253   tp_clear_object (&priv->network_manager);
254   tp_clear_object (&priv->network);
255
256   if (G_OBJECT_CLASS (empathy_irc_network_chooser_parent_class)->dispose)
257     G_OBJECT_CLASS (empathy_irc_network_chooser_parent_class)->dispose (object);
258 }
259
260 static void
261 empathy_irc_network_chooser_class_init (EmpathyIrcNetworkChooserClass *klass)
262 {
263   GObjectClass *object_class = G_OBJECT_CLASS (klass);
264
265   object_class->get_property = empathy_irc_network_chooser_get_property;
266   object_class->set_property = empathy_irc_network_chooser_set_property;
267   object_class->constructed = empathy_irc_network_chooser_constructed;
268   object_class->dispose = empathy_irc_network_chooser_dispose;
269
270   g_object_class_install_property (object_class, PROP_SETTINGS,
271     g_param_spec_object ("settings",
272       "Settings",
273       "The EmpathyAccountSettings to show and edit",
274       EMPATHY_TYPE_ACCOUNT_SETTINGS,
275       G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
276
277   g_type_class_add_private (object_class,
278       sizeof (EmpathyIrcNetworkChooserPriv));
279 }
280
281 static void
282 empathy_irc_network_chooser_init (EmpathyIrcNetworkChooser *self)
283 {
284   EmpathyIrcNetworkChooserPriv *priv;
285
286   priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
287       EMPATHY_TYPE_IRC_NETWORK_CHOOSER, EmpathyIrcNetworkChooserPriv);
288   self->priv = priv;
289
290   priv->network_manager = empathy_irc_network_manager_dup_default ();
291 }
292
293 GtkWidget *
294 empathy_irc_network_chooser_new (EmpathyAccountSettings *settings)
295 {
296   return g_object_new (EMPATHY_TYPE_IRC_NETWORK_CHOOSER,
297       "settings", settings,
298       NULL);
299 }