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-irc-network.h"
31 #include "empathy-utils.h"
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcNetwork)
39 } EmpathyIrcNetworkPriv
;
56 static guint signals
[LAST_SIGNAL
] = {0};
58 G_DEFINE_TYPE (EmpathyIrcNetwork
, empathy_irc_network
, G_TYPE_OBJECT
);
61 server_modified_cb (EmpathyIrcServer
*server
,
62 EmpathyIrcNetwork
*self
)
64 g_signal_emit (self
, signals
[MODIFIED
], 0);
68 empathy_irc_network_get_property (GObject
*object
,
73 EmpathyIrcNetwork
*self
= EMPATHY_IRC_NETWORK (object
);
74 EmpathyIrcNetworkPriv
*priv
= GET_PRIV (self
);
79 g_value_set_string (value
, priv
->name
);
82 g_value_set_string (value
, priv
->charset
);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
91 empathy_irc_network_set_property (GObject
*object
,
96 EmpathyIrcNetwork
*self
= EMPATHY_IRC_NETWORK (object
);
97 EmpathyIrcNetworkPriv
*priv
= GET_PRIV (self
);
102 if (tp_strdiff (priv
->name
, g_value_get_string (value
)))
105 priv
->name
= g_value_dup_string (value
);
106 g_signal_emit (object
, signals
[MODIFIED
], 0);
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);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
124 empathy_irc_network_dispose (GObject
*object
)
126 EmpathyIrcNetwork
*self
= EMPATHY_IRC_NETWORK (object
);
127 EmpathyIrcNetworkPriv
*priv
= GET_PRIV (self
);
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
);
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
);
148 g_free (priv
->charset
);
150 G_OBJECT_CLASS (empathy_irc_network_parent_class
)->finalize (object
);
154 empathy_irc_network_init (EmpathyIrcNetwork
*self
)
156 EmpathyIrcNetworkPriv
*priv
= G_TYPE_INSTANCE_GET_PRIVATE (self
,
157 EMPATHY_TYPE_IRC_NETWORK
, EmpathyIrcNetworkPriv
);
161 priv
->servers
= NULL
;
163 self
->user_defined
= TRUE
;
164 self
->dropped
= FALSE
;
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 (
184 "The displayed name of this network",
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 (
195 "The charset to use on this network",
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 (
214 G_OBJECT_CLASS_TYPE (object_class
),
215 G_SIGNAL_RUN_LAST
| G_SIGNAL_DETAILED
,
218 g_cclosure_marshal_generic
,
223 * empathy_irc_network_activate:
224 * @self: the name of the network
226 * Activates a #EmpathyIrcNetwork.
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
249 empathy_irc_network_new (const gchar
*name
)
251 return g_object_new (EMPATHY_TYPE_IRC_NETWORK
,
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
265 * Returns: a new #GSList of refed #EmpathyIrcServer.
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.
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.
323 empathy_irc_network_remove_server (EmpathyIrcNetwork
*self
,
324 EmpathyIrcServer
*server
)
326 EmpathyIrcNetworkPriv
*priv
;
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
);
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
),
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
354 * Move an #EmpathyIrcServer in the servers list of the given
355 * #EmpathyIrcNetwork.
359 empathy_irc_network_set_server_position (EmpathyIrcNetwork
*self
,
360 EmpathyIrcServer
*server
,
363 EmpathyIrcNetworkPriv
*priv
;
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
);
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);
382 empathy_irc_network_get_name (EmpathyIrcNetwork
*self
)
384 EmpathyIrcNetworkPriv
*priv
= GET_PRIV (self
);
390 empathy_irc_network_get_charset (EmpathyIrcNetwork
*self
)
392 EmpathyIrcNetworkPriv
*priv
= GET_PRIV (self
);
394 return priv
->charset
;