]> git.0d.be Git - empathy.git/blob - libempathy/empathy-irc-network.c
account-settings: allow to change the service
[empathy.git] / libempathy / empathy-irc-network.c
1 /*
2  * Copyright (C) 2007 Guillaume Desmottes
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Guillaume Desmottes <gdesmott@gnome.org>
20  */
21
22 #include <config.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <glib/gi18n-lib.h>
27
28 #include <telepathy-glib/util.h>
29
30 #include "empathy-irc-network.h"
31 #include "empathy-utils.h"
32
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetwork)
34 typedef struct
35 {
36   gchar *name;
37   gchar *charset;
38   GSList *servers;
39 } EmpathyIrcNetworkPriv;
40
41 /* properties */
42 enum
43 {
44   PROP_NAME = 1,
45   PROP_CHARSET,
46   LAST_PROPERTY
47 };
48
49 /* signals */
50 enum
51 {
52   MODIFIED,
53   LAST_SIGNAL
54 };
55
56 static guint signals[LAST_SIGNAL] = {0};
57
58 G_DEFINE_TYPE (EmpathyIrcNetwork, empathy_irc_network, G_TYPE_OBJECT);
59
60 static void
61 server_modified_cb (EmpathyIrcServer *server,
62                     EmpathyIrcNetwork *self)
63 {
64   g_signal_emit (self, signals[MODIFIED], 0);
65 }
66
67 static void
68 empathy_irc_network_get_property (GObject *object,
69                                   guint property_id,
70                                   GValue *value,
71                                   GParamSpec *pspec)
72 {
73   EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
74   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
75
76   switch (property_id)
77     {
78       case PROP_NAME:
79         g_value_set_string (value, priv->name);
80         break;
81       case PROP_CHARSET:
82         g_value_set_string (value, priv->charset);
83         break;
84       default:
85         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86         break;
87     }
88 }
89
90 static void
91 empathy_irc_network_set_property (GObject *object,
92                                   guint property_id,
93                                   const GValue *value,
94                                   GParamSpec *pspec)
95 {
96   EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
97   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
98
99   switch (property_id)
100     {
101       case PROP_NAME:
102         if (tp_strdiff (priv->name, g_value_get_string (value)))
103           {
104             g_free (priv->name);
105             priv->name = g_value_dup_string (value);
106             g_signal_emit (object, signals[MODIFIED], 0);
107           }
108         break;
109       case PROP_CHARSET:
110         if (tp_strdiff (priv->charset, g_value_get_string (value)))
111           {
112             g_free (priv->charset);
113             priv->charset = g_value_dup_string (value);
114             g_signal_emit (object, signals[MODIFIED], 0);
115           }
116         break;
117       default:
118         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119         break;
120     }
121 }
122
123 static void
124 empathy_irc_network_dispose (GObject *object)
125 {
126   EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
127   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
128   GSList *l;
129
130   for (l = priv->servers; l != NULL; l = g_slist_next (l))
131     {
132       g_signal_handlers_disconnect_by_func (l->data,
133           G_CALLBACK (server_modified_cb), self);
134       g_object_unref (l->data);
135     }
136
137   G_OBJECT_CLASS (empathy_irc_network_parent_class)->dispose (object);
138 }
139
140 static void
141 empathy_irc_network_finalize (GObject *object)
142 {
143   EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
144   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
145
146   g_slist_free (priv->servers);
147   g_free (priv->name);
148   g_free (priv->charset);
149
150   G_OBJECT_CLASS (empathy_irc_network_parent_class)->finalize (object);
151 }
152
153 static void
154 empathy_irc_network_init (EmpathyIrcNetwork *self)
155 {
156   EmpathyIrcNetworkPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
157       EMPATHY_TYPE_IRC_NETWORK, EmpathyIrcNetworkPriv);
158
159   self->priv = priv;
160
161   priv->servers = NULL;
162
163   self->user_defined = TRUE;
164   self->dropped = FALSE;
165 }
166
167 static void
168 empathy_irc_network_class_init (EmpathyIrcNetworkClass *klass)
169 {
170   GObjectClass *object_class = G_OBJECT_CLASS (klass);
171   GParamSpec *param_spec;
172
173   object_class->get_property = empathy_irc_network_get_property;
174   object_class->set_property = empathy_irc_network_set_property;
175
176   g_type_class_add_private (object_class, sizeof (EmpathyIrcNetworkPriv));
177
178   object_class->dispose = empathy_irc_network_dispose;
179   object_class->finalize = empathy_irc_network_finalize;
180
181   param_spec = g_param_spec_string (
182       "name",
183       "Network name",
184       "The displayed name of this network",
185       NULL,
186       G_PARAM_READWRITE |
187       G_PARAM_STATIC_NAME |
188       G_PARAM_STATIC_NICK |
189       G_PARAM_STATIC_BLURB);
190   g_object_class_install_property (object_class, PROP_NAME, param_spec);
191
192   param_spec = g_param_spec_string (
193       "charset",
194       "Charset",
195       "The charset to use on this network",
196       "UTF-8",
197       G_PARAM_CONSTRUCT |
198       G_PARAM_READWRITE |
199       G_PARAM_STATIC_NAME |
200       G_PARAM_STATIC_NICK |
201       G_PARAM_STATIC_BLURB);
202   g_object_class_install_property (object_class, PROP_CHARSET, param_spec);
203
204   /**
205    * EmpathyIrcNetwork::modified:
206    * @network: the object that received the signal
207    *
208    * Emitted when either a property or a server of the network is modified or
209    * when a network is activated.
210    *
211    */
212   signals[MODIFIED] = g_signal_new (
213       "modified",
214       G_OBJECT_CLASS_TYPE (object_class),
215       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
216       0,
217       NULL, NULL,
218       g_cclosure_marshal_generic,
219       G_TYPE_NONE, 0);
220 }
221
222 /**
223  * empathy_irc_network_activate:
224  * @self: the name of the network
225  *
226  * Activates a #EmpathyIrcNetwork.
227  *
228  */
229 void
230 empathy_irc_network_activate (EmpathyIrcNetwork *self)
231 {
232   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
233   g_return_if_fail (self->dropped);
234
235   self->dropped = FALSE;
236
237   g_signal_emit (self, signals[MODIFIED], 0);
238 }
239
240 /**
241  * empathy_irc_network_new:
242  * @name: the name of the network
243  *
244  * Creates a new #EmpathyIrcNetwork.
245  *
246  * Returns: a new #EmpathyIrcNetwork
247  */
248 EmpathyIrcNetwork *
249 empathy_irc_network_new (const gchar *name)
250 {
251   return g_object_new (EMPATHY_TYPE_IRC_NETWORK,
252       "name", name,
253       NULL);
254 }
255
256 /**
257  * empathy_irc_network_get_servers:
258  * @network: an #EmpathyIrcNetwork
259  *
260  * Get the list of #EmpathyIrcServer that belongs to this network.
261  * These servers are sorted according their priority.
262  * So the first one will be the first used when trying to connect to
263  * the network.
264  *
265  * Returns: a new #GSList of refed #EmpathyIrcServer.
266  */
267 GSList *
268 empathy_irc_network_get_servers (EmpathyIrcNetwork *self)
269 {
270   EmpathyIrcNetworkPriv *priv;
271   GSList *servers = NULL, *l;
272
273   g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK (self), NULL);
274   priv = GET_PRIV (self);
275
276   for (l = priv->servers; l != NULL; l = g_slist_next (l))
277     {
278       servers = g_slist_prepend (servers, g_object_ref (l->data));
279     }
280
281   return g_slist_reverse (servers);
282 }
283
284 /**
285  * empathy_irc_network_append_server:
286  * @network: an #EmpathyIrcNetwork
287  * @server: the #EmpathyIrcServer to add
288  *
289  * Add an #EmpathyIrcServer to the given #EmpathyIrcNetwork. The server
290  * is added at the last position in network's servers list.
291  *
292  */
293 void
294 empathy_irc_network_append_server (EmpathyIrcNetwork *self,
295                                    EmpathyIrcServer *server)
296 {
297   EmpathyIrcNetworkPriv *priv;
298
299   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
300   g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
301
302   priv = GET_PRIV (self);
303
304   g_return_if_fail (g_slist_find (priv->servers, server) == NULL);
305
306   priv->servers = g_slist_append (priv->servers, g_object_ref (server));
307
308   g_signal_connect (server, "modified", G_CALLBACK (server_modified_cb), self);
309
310   g_signal_emit (self, signals[MODIFIED], 0);
311 }
312
313 /**
314  * empathy_irc_network_remove_server:
315  * @network: an #EmpathyIrcNetwork
316  * @server: the #EmpathyIrcServer to remove
317  *
318  * Remove an #EmpathyIrcServer from the servers list of the
319  * given #EmpathyIrcNetwork.
320  *
321  */
322 void
323 empathy_irc_network_remove_server (EmpathyIrcNetwork *self,
324                                    EmpathyIrcServer *server)
325 {
326   EmpathyIrcNetworkPriv *priv;
327   GSList *l;
328
329   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
330   g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
331
332   priv = GET_PRIV (self);
333
334   l = g_slist_find (priv->servers, server);
335   if (l == NULL)
336     return;
337
338   g_object_unref (l->data);
339   priv->servers = g_slist_delete_link (priv->servers, l);
340   g_signal_handlers_disconnect_by_func (server, G_CALLBACK (server_modified_cb),
341       self);
342
343   g_signal_emit (self, signals[MODIFIED], 0);
344 }
345
346 /**
347  * empathy_irc_network_set_server_position:
348  * @network: an #EmpathyIrcNetwork
349  * @server: the #EmpathyIrcServer to move
350  * @pos: the position to move the server. If this is negative, or is larger than
351  * the number of servers in the list, the server is moved to the end of the
352  * list.
353  *
354  * Move an #EmpathyIrcServer in the servers list of the given
355  * #EmpathyIrcNetwork.
356  *
357  */
358 void
359 empathy_irc_network_set_server_position (EmpathyIrcNetwork *self,
360                                          EmpathyIrcServer *server,
361                                          gint pos)
362 {
363   EmpathyIrcNetworkPriv *priv;
364   GSList *l;
365
366   g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
367   g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
368
369   priv = GET_PRIV (self);
370
371   l = g_slist_find (priv->servers, server);
372   if (l == NULL)
373     return;
374
375   priv->servers = g_slist_delete_link (priv->servers, l);
376   priv->servers = g_slist_insert (priv->servers, server, pos);
377
378   g_signal_emit (self, signals[MODIFIED], 0);
379 }
380
381 const gchar *
382 empathy_irc_network_get_name (EmpathyIrcNetwork *self)
383 {
384   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
385
386   return priv->name;
387 }
388
389 const gchar *
390 empathy_irc_network_get_charset (EmpathyIrcNetwork *self)
391 {
392   EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
393
394   return priv->charset;
395 }