1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
4 * Copyright (C) 2007 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Mikael Hallendal <micke@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
27 #include "gossip-message.h"
29 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_MESSAGE, GossipMessagePriv))
31 typedef struct _GossipMessagePriv GossipMessagePriv;
33 struct _GossipMessagePriv {
34 GossipMessageType type;
35 GossipContact *sender;
41 static void gossip_message_class_init (GossipMessageClass *class);
42 static void gossip_message_init (GossipMessage *message);
43 static void gossip_message_finalize (GObject *object);
44 static void message_get_property (GObject *object,
48 static void message_set_property (GObject *object,
61 static gpointer parent_class = NULL;
64 gossip_message_get_gtype (void)
66 static GType type = 0;
69 static const GTypeInfo info = {
70 sizeof (GossipMessageClass),
72 NULL, /* base_finalize */
73 (GClassInitFunc) gossip_message_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
76 sizeof (GossipMessage),
78 (GInstanceInitFunc) gossip_message_init
81 type = g_type_register_static (G_TYPE_OBJECT,
90 gossip_message_class_init (GossipMessageClass *class)
92 GObjectClass *object_class;
94 object_class = G_OBJECT_CLASS (class);
95 parent_class = g_type_class_peek_parent (class);
97 object_class->finalize = gossip_message_finalize;
98 object_class->get_property = message_get_property;
99 object_class->set_property = message_set_property;
101 g_object_class_install_property (object_class,
103 g_param_spec_int ("type",
105 "The type of message",
106 GOSSIP_MESSAGE_TYPE_NORMAL,
107 GOSSIP_MESSAGE_TYPE_LAST,
108 GOSSIP_MESSAGE_TYPE_NORMAL,
110 g_object_class_install_property (object_class,
112 g_param_spec_object ("sender",
114 "The sender of the message",
118 g_object_class_install_property (object_class,
120 g_param_spec_string ("body",
122 "The content of the message",
125 g_object_class_install_property (object_class,
127 g_param_spec_long ("timestamp",
136 g_type_class_add_private (object_class, sizeof (GossipMessagePriv));
141 gossip_message_init (GossipMessage *message)
143 GossipMessagePriv *priv;
145 priv = GET_PRIV (message);
147 priv->type = GOSSIP_MESSAGE_TYPE_NORMAL;
150 priv->timestamp = gossip_time_get_current ();
154 gossip_message_finalize (GObject *object)
156 GossipMessagePriv *priv;
158 priv = GET_PRIV (object);
161 g_object_unref (priv->sender);
166 (G_OBJECT_CLASS (parent_class)->finalize) (object);
170 message_get_property (GObject *object,
175 GossipMessagePriv *priv;
177 priv = GET_PRIV (object);
181 g_value_set_int (value, priv->type);
184 g_value_set_object (value, priv->sender);
187 g_value_set_string (value, priv->body);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
196 message_set_property (GObject *object,
201 GossipMessagePriv *priv;
203 priv = GET_PRIV (object);
207 gossip_message_set_type (GOSSIP_MESSAGE (object),
208 g_value_get_int (value));
211 gossip_message_set_sender (GOSSIP_MESSAGE (object),
212 GOSSIP_CONTACT (g_value_get_object (value)));
215 gossip_message_set_body (GOSSIP_MESSAGE (object),
216 g_value_get_string (value));
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
225 gossip_message_new (const gchar *body)
227 return g_object_new (GOSSIP_TYPE_MESSAGE,
233 gossip_message_get_type (GossipMessage *message)
235 GossipMessagePriv *priv;
237 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message),
238 GOSSIP_MESSAGE_TYPE_NORMAL);
240 priv = GET_PRIV (message);
246 gossip_message_set_type (GossipMessage *message,
247 GossipMessageType type)
249 GossipMessagePriv *priv;
251 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
253 priv = GET_PRIV (message);
257 g_object_notify (G_OBJECT (message), "type");
261 gossip_message_get_sender (GossipMessage *message)
263 GossipMessagePriv *priv;
265 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
267 priv = GET_PRIV (message);
273 gossip_message_set_sender (GossipMessage *message, GossipContact *contact)
275 GossipMessagePriv *priv;
276 GossipContact *old_sender;
278 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
279 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
281 priv = GET_PRIV (message);
283 old_sender = priv->sender;
284 priv->sender = g_object_ref (contact);
287 g_object_unref (old_sender);
290 g_object_notify (G_OBJECT (message), "sender");
294 gossip_message_get_body (GossipMessage *message)
296 GossipMessagePriv *priv;
298 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
300 priv = GET_PRIV (message);
306 gossip_message_set_body (GossipMessage *message,
309 GossipMessagePriv *priv;
310 GossipMessageType type;
312 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
314 priv = GET_PRIV (message);
319 type = GOSSIP_MESSAGE_TYPE_NORMAL;
320 if (g_str_has_prefix (body, "/me")) {
321 type = GOSSIP_MESSAGE_TYPE_ACTION;
326 priv->body = g_strdup (body);
329 if (type != priv->type) {
330 gossip_message_set_type (message, type);
333 g_object_notify (G_OBJECT (message), "body");
337 gossip_message_get_timestamp (GossipMessage *message)
339 GossipMessagePriv *priv;
341 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), -1);
343 priv = GET_PRIV (message);
345 return priv->timestamp;
349 gossip_message_set_timestamp (GossipMessage *message,
350 GossipTime timestamp)
352 GossipMessagePriv *priv;
354 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
355 g_return_if_fail (timestamp >= -1);
357 priv = GET_PRIV (message);
359 if (timestamp <= 0) {
360 priv->timestamp = gossip_time_get_current ();
362 priv->timestamp = timestamp;
365 g_object_notify (G_OBJECT (message), "timestamp");