Confirm closing windows containing chat rooms.
[empathy-mirror.git] / libempathy / empathy-irc-network.c
blobec0cfffb1e0abd8c05b44b40afc8e8e3e94bd26b
1 /*
2 * Copyright (C) 2007 Guillaume Desmottes
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.
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.
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
19 * Authors: Guillaume Desmottes <gdesmott@gnome.org>
22 #include <config.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <glib.h>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-irc-network.h"
31 #include "empathy-utils.h"
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetwork)
34 typedef struct
36 gchar *name;
37 gchar *charset;
38 GSList *servers;
39 } EmpathyIrcNetworkPriv;
41 /* properties */
42 enum
44 PROP_NAME = 1,
45 PROP_CHARSET,
46 LAST_PROPERTY
49 /* signals */
50 enum
52 MODIFIED,
53 LAST_SIGNAL
56 static guint signals[LAST_SIGNAL] = {0};
58 G_DEFINE_TYPE (EmpathyIrcNetwork, empathy_irc_network, G_TYPE_OBJECT);
60 static void
61 server_modified_cb (EmpathyIrcServer *server,
62 EmpathyIrcNetwork *self)
64 g_signal_emit (self, signals[MODIFIED], 0);
67 static void
68 empathy_irc_network_get_property (GObject *object,
69 guint property_id,
70 GValue *value,
71 GParamSpec *pspec)
73 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
74 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
76 switch (property_id)
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;
90 static void
91 empathy_irc_network_set_property (GObject *object,
92 guint property_id,
93 const GValue *value,
94 GParamSpec *pspec)
96 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
97 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
99 switch (property_id)
101 case PROP_NAME:
102 if (tp_strdiff (priv->name, g_value_get_string (value)))
104 g_free (priv->name);
105 priv->name = g_value_dup_string (value);
106 g_signal_emit (object, signals[MODIFIED], 0);
108 break;
109 case PROP_CHARSET:
110 if (tp_strdiff (priv->charset, g_value_get_string (value)))
112 g_free (priv->charset);
113 priv->charset = g_value_dup_string (value);
114 g_signal_emit (object, signals[MODIFIED], 0);
116 break;
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 break;
123 static void
124 empathy_irc_network_dispose (GObject *object)
126 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
127 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
128 GSList *l;
130 for (l = priv->servers; l != NULL; l = g_slist_next (l))
132 g_signal_handlers_disconnect_by_func (l->data,
133 G_CALLBACK (server_modified_cb), self);
134 g_object_unref (l->data);
137 G_OBJECT_CLASS (empathy_irc_network_parent_class)->dispose (object);
140 static void
141 empathy_irc_network_finalize (GObject *object)
143 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
144 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
146 g_slist_free (priv->servers);
147 g_free (priv->name);
148 g_free (priv->charset);
150 G_OBJECT_CLASS (empathy_irc_network_parent_class)->finalize (object);
153 static void
154 empathy_irc_network_init (EmpathyIrcNetwork *self)
156 EmpathyIrcNetworkPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
157 EMPATHY_TYPE_IRC_NETWORK, EmpathyIrcNetworkPriv);
159 self->priv = priv;
161 priv->servers = NULL;
163 self->user_defined = TRUE;
164 self->dropped = FALSE;
167 static void
168 empathy_irc_network_class_init (EmpathyIrcNetworkClass *klass)
170 GObjectClass *object_class = G_OBJECT_CLASS (klass);
171 GParamSpec *param_spec;
173 object_class->get_property = empathy_irc_network_get_property;
174 object_class->set_property = empathy_irc_network_set_property;
176 g_type_class_add_private (object_class, sizeof (EmpathyIrcNetworkPriv));
178 object_class->dispose = empathy_irc_network_dispose;
179 object_class->finalize = empathy_irc_network_finalize;
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);
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);
205 * EmpathyIrcNetwork::modified:
206 * @network: the object that received the signal
208 * Emitted when either a property or a server of the network is modified or
209 * when a network is activated.
212 signals[MODIFIED] = g_signal_new (
213 "modified",
214 G_OBJECT_CLASS_TYPE (object_class),
215 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
217 NULL, NULL,
218 g_cclosure_marshal_generic,
219 G_TYPE_NONE, 0);
223 * empathy_irc_network_activate:
224 * @self: the name of the network
226 * Activates a #EmpathyIrcNetwork.
229 void
230 empathy_irc_network_activate (EmpathyIrcNetwork *self)
232 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
233 g_return_if_fail (self->dropped);
235 self->dropped = FALSE;
237 g_signal_emit (self, signals[MODIFIED], 0);
241 * empathy_irc_network_new:
242 * @name: the name of the network
244 * Creates a new #EmpathyIrcNetwork.
246 * Returns: a new #EmpathyIrcNetwork
248 EmpathyIrcNetwork *
249 empathy_irc_network_new (const gchar *name)
251 return g_object_new (EMPATHY_TYPE_IRC_NETWORK,
252 "name", name,
253 NULL);
257 * empathy_irc_network_get_servers:
258 * @network: an #EmpathyIrcNetwork
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.
265 * Returns: a new #GSList of refed #EmpathyIrcServer.
267 GSList *
268 empathy_irc_network_get_servers (EmpathyIrcNetwork *self)
270 EmpathyIrcNetworkPriv *priv;
271 GSList *servers = NULL, *l;
273 g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK (self), NULL);
274 priv = GET_PRIV (self);
276 for (l = priv->servers; l != NULL; l = g_slist_next (l))
278 servers = g_slist_prepend (servers, g_object_ref (l->data));
281 return g_slist_reverse (servers);
285 * empathy_irc_network_append_server:
286 * @network: an #EmpathyIrcNetwork
287 * @server: the #EmpathyIrcServer to add
289 * Add an #EmpathyIrcServer to the given #EmpathyIrcNetwork. The server
290 * is added at the last position in network's servers list.
293 void
294 empathy_irc_network_append_server (EmpathyIrcNetwork *self,
295 EmpathyIrcServer *server)
297 EmpathyIrcNetworkPriv *priv;
299 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
300 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
302 priv = GET_PRIV (self);
304 g_return_if_fail (g_slist_find (priv->servers, server) == NULL);
306 priv->servers = g_slist_append (priv->servers, g_object_ref (server));
308 g_signal_connect (server, "modified", G_CALLBACK (server_modified_cb), self);
310 g_signal_emit (self, signals[MODIFIED], 0);
314 * empathy_irc_network_remove_server:
315 * @network: an #EmpathyIrcNetwork
316 * @server: the #EmpathyIrcServer to remove
318 * Remove an #EmpathyIrcServer from the servers list of the
319 * given #EmpathyIrcNetwork.
322 void
323 empathy_irc_network_remove_server (EmpathyIrcNetwork *self,
324 EmpathyIrcServer *server)
326 EmpathyIrcNetworkPriv *priv;
327 GSList *l;
329 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
330 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
332 priv = GET_PRIV (self);
334 l = g_slist_find (priv->servers, server);
335 if (l == NULL)
336 return;
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);
343 g_signal_emit (self, signals[MODIFIED], 0);
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.
354 * Move an #EmpathyIrcServer in the servers list of the given
355 * #EmpathyIrcNetwork.
358 void
359 empathy_irc_network_set_server_position (EmpathyIrcNetwork *self,
360 EmpathyIrcServer *server,
361 gint pos)
363 EmpathyIrcNetworkPriv *priv;
364 GSList *l;
366 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
367 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
369 priv = GET_PRIV (self);
371 l = g_slist_find (priv->servers, server);
372 if (l == NULL)
373 return;
375 priv->servers = g_slist_delete_link (priv->servers, l);
376 priv->servers = g_slist_insert (priv->servers, server, pos);
378 g_signal_emit (self, signals[MODIFIED], 0);
381 const gchar *
382 empathy_irc_network_get_name (EmpathyIrcNetwork *self)
384 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
386 return priv->name;
389 const gchar *
390 empathy_irc_network_get_charset (EmpathyIrcNetwork *self)
392 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
394 return priv->charset;