]> git.0d.be Git - empathy.git/blob - libempathy/empathy-irc-server.c
include telepathy-glib.h
[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-lib.h>
26
27 #include <telepathy-glib/telepathy-glib.h>
28
29 #include "empathy-irc-server.h"
30 #include "empathy-utils.h"
31
32 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIrcServer)
33 typedef struct
34 {
35   gchar *address;
36   guint port;
37   gboolean ssl;
38 } EmpathyIrcServerPriv;
39
40 /* properties */
41 enum
42 {
43   PROP_ADDRESS = 1,
44   PROP_PORT,
45   PROP_SSL,
46   LAST_PROPERTY
47 };
48
49 /* signals */
50 enum
51 {
52   MODIFIED,
53   LAST_SIGNAL
54 };
55
56 static guint signals[LAST_SIGNAL] = {0};
57
58 G_DEFINE_TYPE (EmpathyIrcServer, empathy_irc_server, G_TYPE_OBJECT);
59
60 static void
61 empathy_irc_server_get_property (GObject *object,
62                                  guint property_id,
63                                  GValue *value,
64                                  GParamSpec *pspec)
65 {
66   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
67   EmpathyIrcServerPriv *priv = GET_PRIV (self);
68
69   switch (property_id)
70     {
71       case PROP_ADDRESS:
72         g_value_set_string (value, priv->address);
73         break;
74       case PROP_PORT:
75         g_value_set_uint (value, priv->port);
76         break;
77       case PROP_SSL:
78         g_value_set_boolean (value, priv->ssl);
79         break;
80       default:
81         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
82         break;
83     }
84 }
85
86 static void
87 empathy_irc_server_set_property (GObject *object,
88                                  guint property_id,
89                                  const GValue *value,
90                                  GParamSpec *pspec)
91 {
92   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
93   EmpathyIrcServerPriv *priv = GET_PRIV (self);
94
95   switch (property_id)
96     {
97       case PROP_ADDRESS:
98         if (tp_strdiff (priv->address, g_value_get_string (value)))
99           {
100             g_free (priv->address);
101             priv->address = g_value_dup_string (value);
102             g_signal_emit (object, signals[MODIFIED], 0);
103           }
104         break;
105       case PROP_PORT:
106         if (priv->port != g_value_get_uint (value))
107           {
108             priv->port = g_value_get_uint (value);
109             g_signal_emit (object, signals[MODIFIED], 0);
110           }
111         break;
112       case PROP_SSL:
113         if (priv->ssl != g_value_get_boolean (value))
114           {
115             priv->ssl = g_value_get_boolean (value);
116             g_signal_emit (object, signals[MODIFIED], 0);
117           }
118         break;
119       default:
120         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
121         break;
122     }
123 }
124
125 static void
126 empathy_irc_server_finalize (GObject *object)
127 {
128   EmpathyIrcServer *self = EMPATHY_IRC_SERVER (object);
129   EmpathyIrcServerPriv *priv = GET_PRIV (self);
130
131   g_free (priv->address);
132
133   G_OBJECT_CLASS (empathy_irc_server_parent_class)->finalize (object);
134 }
135
136 static void
137 empathy_irc_server_init (EmpathyIrcServer *self)
138 {
139   EmpathyIrcServerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
140       EMPATHY_TYPE_IRC_SERVER, EmpathyIrcServerPriv);
141
142   self->priv = priv;
143 }
144
145 static void
146 empathy_irc_server_class_init (EmpathyIrcServerClass *klass)
147 {
148   GObjectClass *object_class = G_OBJECT_CLASS (klass);
149   GParamSpec *param_spec;
150
151   object_class->get_property = empathy_irc_server_get_property;
152   object_class->set_property = empathy_irc_server_set_property;
153
154   g_type_class_add_private (object_class, sizeof (EmpathyIrcServerPriv));
155
156   object_class->finalize = empathy_irc_server_finalize;
157
158   param_spec = g_param_spec_string (
159       "address",
160       "Server address",
161       "The address of this server",
162       NULL,
163       G_PARAM_READWRITE |
164       G_PARAM_STATIC_NAME |
165       G_PARAM_STATIC_NICK |
166       G_PARAM_STATIC_BLURB);
167   g_object_class_install_property (object_class, PROP_ADDRESS, param_spec);
168
169   param_spec = g_param_spec_uint (
170       "port",
171       "Server port",
172       "The port to use to connect on this server",
173       1, G_MAXUINT16, 6667,
174       G_PARAM_READWRITE |
175       G_PARAM_STATIC_NAME |
176       G_PARAM_STATIC_NICK |
177       G_PARAM_STATIC_BLURB);
178   g_object_class_install_property (object_class, PROP_PORT, param_spec);
179
180   param_spec = g_param_spec_boolean (
181       "ssl",
182       "SSL",
183       "If this server needs SSL connection",
184       FALSE,
185       G_PARAM_READWRITE |
186       G_PARAM_STATIC_NAME |
187       G_PARAM_STATIC_NICK |
188       G_PARAM_STATIC_BLURB);
189   g_object_class_install_property (object_class, PROP_SSL, param_spec);
190
191   /**
192    * EmpathyIrcServer::modified:
193    * @server: the object that received the signal
194    *
195    * Emitted when a property of the server is modified.
196    *
197    */
198   signals[MODIFIED] = g_signal_new (
199       "modified",
200       G_OBJECT_CLASS_TYPE (object_class),
201       G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
202       0,
203       NULL, NULL,
204       g_cclosure_marshal_generic,
205       G_TYPE_NONE, 0);
206 }
207
208 /**
209  * empathy_irc_server_new:
210  * @address: the address
211  * @port: the port
212  * @ssl: %TRUE if the server needs a SSL connection
213  *
214  * Creates a new #EmpathyIrcServer
215  *
216  * Returns: a new #EmpathyIrcServer
217  */
218 EmpathyIrcServer *
219 empathy_irc_server_new (const gchar *address,
220                         guint port,
221                         gboolean ssl)
222 {
223   return g_object_new (EMPATHY_TYPE_IRC_SERVER,
224       "address", address,
225       "port", port,
226       "ssl", ssl,
227       NULL);
228 }