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>
26 #include <glib/gi18n-lib.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-marshal.h"
31 #include "empathy-irc-network.h"
32 #include "empathy-utils.h"
34 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetwork)
40 } EmpathyIrcNetworkPriv;
57 static guint signals[LAST_SIGNAL] = {0};
59 G_DEFINE_TYPE (EmpathyIrcNetwork, empathy_irc_network, G_TYPE_OBJECT);
62 server_modified_cb (EmpathyIrcServer *server,
63 EmpathyIrcNetwork *self)
65 g_signal_emit (self, signals[MODIFIED], 0);
69 empathy_irc_network_get_property (GObject *object,
74 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
75 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
80 g_value_set_string (value, priv->name);
83 g_value_set_string (value, priv->charset);
86 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
92 empathy_irc_network_set_property (GObject *object,
97 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
98 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
103 if (tp_strdiff (priv->name, g_value_get_string (value)))
106 priv->name = g_value_dup_string (value);
107 g_signal_emit (object, signals[MODIFIED], 0);
111 if (tp_strdiff (priv->charset, g_value_get_string (value)))
113 g_free (priv->charset);
114 priv->charset = g_value_dup_string (value);
115 g_signal_emit (object, signals[MODIFIED], 0);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
125 empathy_irc_network_dispose (GObject *object)
127 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
128 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
131 for (l = priv->servers; l != NULL; l = g_slist_next (l))
133 g_signal_handlers_disconnect_by_func (l->data,
134 G_CALLBACK (server_modified_cb), self);
135 g_object_unref (l->data);
138 G_OBJECT_CLASS (empathy_irc_network_parent_class)->dispose (object);
142 empathy_irc_network_finalize (GObject *object)
144 EmpathyIrcNetwork *self = EMPATHY_IRC_NETWORK (object);
145 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
147 g_slist_free (priv->servers);
149 g_free (priv->charset);
151 G_OBJECT_CLASS (empathy_irc_network_parent_class)->finalize (object);
155 empathy_irc_network_init (EmpathyIrcNetwork *self)
157 EmpathyIrcNetworkPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
158 EMPATHY_TYPE_IRC_NETWORK, EmpathyIrcNetworkPriv);
162 priv->servers = NULL;
164 self->user_defined = TRUE;
165 self->dropped = FALSE;
169 empathy_irc_network_class_init (EmpathyIrcNetworkClass *klass)
171 GObjectClass *object_class = G_OBJECT_CLASS (klass);
172 GParamSpec *param_spec;
174 object_class->get_property = empathy_irc_network_get_property;
175 object_class->set_property = empathy_irc_network_set_property;
177 g_type_class_add_private (object_class, sizeof (EmpathyIrcNetworkPriv));
179 object_class->dispose = empathy_irc_network_dispose;
180 object_class->finalize = empathy_irc_network_finalize;
182 param_spec = g_param_spec_string (
185 "The displayed name of this network",
188 G_PARAM_STATIC_NAME |
189 G_PARAM_STATIC_NICK |
190 G_PARAM_STATIC_BLURB);
191 g_object_class_install_property (object_class, PROP_NAME, param_spec);
193 param_spec = g_param_spec_string (
196 "The charset to use on this network",
200 G_PARAM_STATIC_NAME |
201 G_PARAM_STATIC_NICK |
202 G_PARAM_STATIC_BLURB);
203 g_object_class_install_property (object_class, PROP_CHARSET, param_spec);
206 * EmpathyIrcNetwork::modified:
207 * @network: the object that received the signal
209 * Emitted when either a property or a server of the network is modified or
210 * when a network is activated.
213 signals[MODIFIED] = g_signal_new (
215 G_OBJECT_CLASS_TYPE (object_class),
216 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
219 g_cclosure_marshal_VOID__VOID,
224 * empathy_irc_network_activate:
225 * @self: the name of the network
227 * Activates a #EmpathyIrcNetwork.
231 empathy_irc_network_activate (EmpathyIrcNetwork *self)
233 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
234 g_return_if_fail (self->dropped);
236 self->dropped = FALSE;
238 g_signal_emit (self, signals[MODIFIED], 0);
242 * empathy_irc_network_new:
243 * @name: the name of the network
245 * Creates a new #EmpathyIrcNetwork.
247 * Returns: a new #EmpathyIrcNetwork
250 empathy_irc_network_new (const gchar *name)
252 return g_object_new (EMPATHY_TYPE_IRC_NETWORK,
258 * empathy_irc_network_get_servers:
259 * @network: an #EmpathyIrcNetwork
261 * Get the list of #EmpathyIrcServer that belongs to this network.
262 * These servers are sorted according their priority.
263 * So the first one will be the first used when trying to connect to
266 * Returns: a new #GSList of refed #EmpathyIrcServer.
269 empathy_irc_network_get_servers (EmpathyIrcNetwork *self)
271 EmpathyIrcNetworkPriv *priv;
272 GSList *servers = NULL, *l;
274 g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK (self), NULL);
275 priv = GET_PRIV (self);
277 for (l = priv->servers; l != NULL; l = g_slist_next (l))
279 servers = g_slist_prepend (servers, g_object_ref (l->data));
282 return g_slist_reverse (servers);
286 * empathy_irc_network_append_server:
287 * @network: an #EmpathyIrcNetwork
288 * @server: the #EmpathyIrcServer to add
290 * Add an #EmpathyIrcServer to the given #EmpathyIrcNetwork. The server
291 * is added at the last position in network's servers list.
295 empathy_irc_network_append_server (EmpathyIrcNetwork *self,
296 EmpathyIrcServer *server)
298 EmpathyIrcNetworkPriv *priv;
300 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
301 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
303 priv = GET_PRIV (self);
305 g_return_if_fail (g_slist_find (priv->servers, server) == NULL);
307 priv->servers = g_slist_append (priv->servers, g_object_ref (server));
309 g_signal_connect (server, "modified", G_CALLBACK (server_modified_cb), self);
311 g_signal_emit (self, signals[MODIFIED], 0);
315 * empathy_irc_network_remove_server:
316 * @network: an #EmpathyIrcNetwork
317 * @server: the #EmpathyIrcServer to remove
319 * Remove an #EmpathyIrcServer from the servers list of the
320 * given #EmpathyIrcNetwork.
324 empathy_irc_network_remove_server (EmpathyIrcNetwork *self,
325 EmpathyIrcServer *server)
327 EmpathyIrcNetworkPriv *priv;
330 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
331 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
333 priv = GET_PRIV (self);
335 l = g_slist_find (priv->servers, server);
339 g_object_unref (l->data);
340 priv->servers = g_slist_delete_link (priv->servers, l);
341 g_signal_handlers_disconnect_by_func (server, G_CALLBACK (server_modified_cb),
344 g_signal_emit (self, signals[MODIFIED], 0);
348 * empathy_irc_network_set_server_position:
349 * @network: an #EmpathyIrcNetwork
350 * @server: the #EmpathyIrcServer to move
351 * @pos: the position to move the server. If this is negative, or is larger than
352 * the number of servers in the list, the server is moved to the end of the
355 * Move an #EmpathyIrcServer in the servers list of the given
356 * #EmpathyIrcNetwork.
360 empathy_irc_network_set_server_position (EmpathyIrcNetwork *self,
361 EmpathyIrcServer *server,
364 EmpathyIrcNetworkPriv *priv;
367 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
368 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
370 priv = GET_PRIV (self);
372 l = g_slist_find (priv->servers, server);
376 priv->servers = g_slist_delete_link (priv->servers, l);
377 priv->servers = g_slist_insert (priv->servers, server, pos);
379 g_signal_emit (self, signals[MODIFIED], 0);
383 empathy_irc_network_get_name (EmpathyIrcNetwork *self)
385 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
391 empathy_irc_network_get_charset (EmpathyIrcNetwork *self)
393 EmpathyIrcNetworkPriv *priv = GET_PRIV (self);
395 return priv->charset;