]> git.0d.be Git - empathy.git/blob - libempathy/empathy-irc-server.c
Import tools from telepathy-glib 0.7.3 and build a static libemp-extensions.la.
[empathy.git] / libempathy / empathy-irc-server.c
1 /*
2  * Copyright (C) 2007-2008 Guillaume Desmottes
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Guillaume Desmottes <gdesmott@gnome.org>
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include <telepathy-glib/util.h>
28
29 #include "empathy-marshal.h"
30 #include "empathy-irc-server.h"
31
32 G_DEFINE_TYPE (EmpathyIrcServer, empathy_irc_server, G_TYPE_OBJECT);
33
34 /* properties */
35 enum
36 {
37   PROP_ADDRESS = 1,
38   PROP_PORT,
39   PROP_SSL,
40   LAST_PROPERTY
41 };
42
43 /* signals */
44 enum
45 {
46   MODIFIED,
47   LAST_SIGNAL
48 };
49
50 static guint signals[LAST_SIGNAL] = {0};
51
52 typedef struct _EmpathyIrcServerPrivate EmpathyIrcServerPrivate;
53
54 struct _EmpathyIrcServerPrivate
55 {
56   gchar *address;
57   gint port;
58   gboolean ssl;
59 };
60
61 #define EMPATHY_IRC_SERVER_GET_PRIVATE(obj)\
62     ((EmpathyIrcServerPrivate *) obj->priv)
63
64 static void
65 empathy_irc_server_get_property (GObject *object,
66                                  guint property_id,
67                                  GValue *value,
68                                  GParamSpec *pspec)
69 {
70   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
71   EmpathyIrcServerPrivate *priv = EMPATHY_IRC_SERVER_GET_PRIVATE (self);
72
73   switch (property_id)
74     {
75       case PROP_ADDRESS:
76         g_value_set_string (value, priv->address);
77         break;
78       case PROP_PORT:
79         g_value_set_uint (value, priv->port);
80         break;
81       case PROP_SSL:
82         g_value_set_boolean (value, priv->ssl);
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_server_set_property (GObject *object,
92                                  guint property_id,
93                                  const GValue *value,
94                                  GParamSpec *pspec)
95 {
96   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
97   EmpathyIrcServerPrivate *priv = EMPATHY_IRC_SERVER_GET_PRIVATE (self);
98
99   switch (property_id)
100     {
101       case PROP_ADDRESS:
102         if (tp_strdiff (priv->address, g_value_get_string (value)))
103           {
104             g_free (priv->address);
105             priv->address = g_value_dup_string (value);
106             g_signal_emit (object, signals[MODIFIED], 0);
107           }
108         break;
109       case PROP_PORT:
110         if (priv->port != g_value_get_uint (value))
111           {
112             priv->port = g_value_get_uint (value);
113             g_signal_emit (object, signals[MODIFIED], 0);
114           }
115         break;
116       case PROP_SSL:
117         if (priv->ssl != g_value_get_boolean (value))
118           {
119             priv->ssl = g_value_get_boolean (value);
120             g_signal_emit (object, signals[MODIFIED], 0);
121           }
122         break;
123       default:
124         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
125         break;
126     }
127 }
128
129 static void
130 empathy_irc_server_finalize (GObject *object)
131 {
132   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
133   EmpathyIrcServerPrivate *priv = EMPATHY_IRC_SERVER_GET_PRIVATE (self);
134
135   g_free (priv->address);
136
137   G_OBJECT_CLASS (empathy_irc_server_parent_class)->finalize (object);
138 }
139
140 static void
141 empathy_irc_server_init (EmpathyIrcServer *self)
142 {
143   EmpathyIrcServerPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
144       EMPATHY_TYPE_IRC_SERVER, EmpathyIrcServerPrivate);
145
146   self->priv = priv;
147 }
148
149 static void
150 empathy_irc_server_class_init (EmpathyIrcServerClass *klass)
151 {
152   GObjectClass *object_class = G_OBJECT_CLASS (klass);
153   GParamSpec *param_spec;
154
155   object_class->get_property = empathy_irc_server_get_property;
156   object_class->set_property = empathy_irc_server_set_property;
157
158   g_type_class_add_private (object_class,
159           sizeof (EmpathyIrcServerPrivate));
160
161   object_class->finalize = empathy_irc_server_finalize;
162
163   param_spec = g_param_spec_string (
164       "address",
165       "Server address",
166       "The address of this server",
167       NULL,
168       G_PARAM_READWRITE |
169       G_PARAM_STATIC_NAME |
170       G_PARAM_STATIC_NICK |
171       G_PARAM_STATIC_BLURB);
172   g_object_class_install_property (object_class, PROP_ADDRESS, param_spec);
173
174   param_spec = g_param_spec_uint (
175       "port",
176       "Server port",
177       "The port to use to connect on this server",
178       1, G_MAXUINT16, 6667,
179       G_PARAM_READWRITE |
180       G_PARAM_STATIC_NAME |
181       G_PARAM_STATIC_NICK |
182       G_PARAM_STATIC_BLURB);
183   g_object_class_install_property (object_class, PROP_PORT, param_spec);
184
185   param_spec = g_param_spec_boolean (
186       "ssl",
187       "SSL",
188       "If this server needs SSL connection",
189       FALSE,
190       G_PARAM_READWRITE |
191       G_PARAM_STATIC_NAME |
192       G_PARAM_STATIC_NICK |
193       G_PARAM_STATIC_BLURB);
194   g_object_class_install_property (object_class, PROP_SSL, param_spec);
195
196   /**
197    * EmpathyIrcServer::modified:
198    * @server: the object that received the signal
199    *
200    * Emitted when a property of the server is modified.
201    *
202    */
203   signals[MODIFIED] = g_signal_new (
204       "modified",
205       G_OBJECT_CLASS_TYPE (object_class),
206       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
207       0,
208       NULL, NULL,
209       g_cclosure_marshal_VOID__VOID,
210       G_TYPE_NONE, 0);
211 }
212
213 /**
214  * empathy_irc_server_new:
215  * @address: the address
216  * @port: the port
217  * @ssl: %TRUE if the server needs a SSL connection
218  *
219  * Creates a new #EmpathyIrcServer
220  *
221  * Returns: a new #EmpathyIrcServer
222  */
223 EmpathyIrcServer *
224 empathy_irc_server_new (const gchar *address,
225                         guint port,
226                         gboolean ssl)
227 {
228   return g_object_new (EMPATHY_TYPE_IRC_SERVER,
229       "address", address,
230       "port", port,
231       "ssl", ssl,
232       NULL);
233 }