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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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.
212 signals[MODIFIED] = g_signal_new (
214 G_OBJECT_CLASS_TYPE (object_class),
215 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
218 g_cclosure_marshal_VOID__VOID,
223 * empathy_irc_network_new:
224 * @name: the name of the network
226 * Creates a new #EmpathyIrcNetwork.
228 * Returns: a new #EmpathyIrcNetwork
231 empathy_irc_network_new (const gchar *name)
233 return g_object_new (EMPATHY_TYPE_IRC_NETWORK,
239 * empathy_irc_network_get_servers:
240 * @network: an #EmpathyIrcNetwork
242 * Get the list of #EmpathyIrcServer that belongs to this network.
243 * These servers are sorted according their priority.
244 * So the first one will be the first used when trying to connect to
247 * Returns: a new #GSList of refed #EmpathyIrcServer.
250 empathy_irc_network_get_servers (EmpathyIrcNetwork *self)
252 EmpathyIrcNetworkPriv *priv;
253 GSList *servers = NULL, *l;
255 g_return_val_if_fail (EMPATHY_IS_IRC_NETWORK (self), NULL);
256 priv = GET_PRIV (self);
258 for (l = priv->servers; l != NULL; l = g_slist_next (l))
260 servers = g_slist_prepend (servers, g_object_ref (l->data));
263 return g_slist_reverse (servers);
267 * empathy_irc_network_append_server:
268 * @network: an #EmpathyIrcNetwork
269 * @server: the #EmpathyIrcServer to add
271 * Add an #EmpathyIrcServer to the given #EmpathyIrcNetwork. The server
272 * is added at the last position in network's servers list.
276 empathy_irc_network_append_server (EmpathyIrcNetwork *self,
277 EmpathyIrcServer *server)
279 EmpathyIrcNetworkPriv *priv;
281 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
282 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
284 priv = GET_PRIV (self);
286 g_return_if_fail (g_slist_find (priv->servers, server) == NULL);
288 priv->servers = g_slist_append (priv->servers, g_object_ref (server));
290 g_signal_connect (server, "modified", G_CALLBACK (server_modified_cb), self);
292 g_signal_emit (self, signals[MODIFIED], 0);
296 * empathy_irc_network_remove_server:
297 * @network: an #EmpathyIrcNetwork
298 * @server: the #EmpathyIrcServer to remove
300 * Remove an #EmpathyIrcServer from the servers list of the
301 * given #EmpathyIrcNetwork.
305 empathy_irc_network_remove_server (EmpathyIrcNetwork *self,
306 EmpathyIrcServer *server)
308 EmpathyIrcNetworkPriv *priv;
311 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
312 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
314 priv = GET_PRIV (self);
316 l = g_slist_find (priv->servers, server);
320 g_object_unref (l->data);
321 priv->servers = g_slist_delete_link (priv->servers, l);
322 g_signal_handlers_disconnect_by_func (server, G_CALLBACK (server_modified_cb),
325 g_signal_emit (self, signals[MODIFIED], 0);
329 * empathy_irc_network_set_server_position:
330 * @network: an #EmpathyIrcNetwork
331 * @server: the #EmpathyIrcServer to move
332 * @pos: the position to move the server. If this is negative, or is larger than
333 * the number of servers in the list, the server is moved to the end of the
336 * Move an #EmpathyIrcServer in the servers list of the given
337 * #EmpathyIrcNetwork.
341 empathy_irc_network_set_server_position (EmpathyIrcNetwork *self,
342 EmpathyIrcServer *server,
345 EmpathyIrcNetworkPriv *priv;
348 g_return_if_fail (EMPATHY_IS_IRC_NETWORK (self));
349 g_return_if_fail (server != NULL && EMPATHY_IS_IRC_SERVER (server));
351 priv = GET_PRIV (self);
353 l = g_slist_find (priv->servers, server);
357 priv->servers = g_slist_delete_link (priv->servers, l);
358 priv->servers = g_slist_insert (priv->servers, server, pos);
360 g_signal_emit (self, signals[MODIFIED], 0);