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;
36 GossipContact *receiver;
42 static void gossip_message_class_init (GossipMessageClass *class);
43 static void gossip_message_init (GossipMessage *message);
44 static void gossip_message_finalize (GObject *object);
45 static void message_get_property (GObject *object,
49 static void message_set_property (GObject *object,
63 static gpointer parent_class = NULL;
66 gossip_message_get_gtype (void)
68 static GType type = 0;
71 static const GTypeInfo info = {
72 sizeof (GossipMessageClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) gossip_message_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof (GossipMessage),
80 (GInstanceInitFunc) gossip_message_init
83 type = g_type_register_static (G_TYPE_OBJECT,
92 gossip_message_class_init (GossipMessageClass *class)
94 GObjectClass *object_class;
96 object_class = G_OBJECT_CLASS (class);
97 parent_class = g_type_class_peek_parent (class);
99 object_class->finalize = gossip_message_finalize;
100 object_class->get_property = message_get_property;
101 object_class->set_property = message_set_property;
103 g_object_class_install_property (object_class,
105 g_param_spec_int ("type",
107 "The type of message",
108 GOSSIP_MESSAGE_TYPE_NORMAL,
109 GOSSIP_MESSAGE_TYPE_LAST,
110 GOSSIP_MESSAGE_TYPE_NORMAL,
112 g_object_class_install_property (object_class,
114 g_param_spec_object ("sender",
116 "The sender of the message",
119 g_object_class_install_property (object_class,
121 g_param_spec_object ("receiver",
123 "The receiver of the message",
126 g_object_class_install_property (object_class,
128 g_param_spec_string ("body",
130 "The content of the message",
133 g_object_class_install_property (object_class,
135 g_param_spec_long ("timestamp",
144 g_type_class_add_private (object_class, sizeof (GossipMessagePriv));
149 gossip_message_init (GossipMessage *message)
151 GossipMessagePriv *priv;
153 priv = GET_PRIV (message);
155 priv->timestamp = gossip_time_get_current ();
159 gossip_message_finalize (GObject *object)
161 GossipMessagePriv *priv;
163 priv = GET_PRIV (object);
166 g_object_unref (priv->sender);
168 if (priv->receiver) {
169 g_object_unref (priv->receiver);
174 (G_OBJECT_CLASS (parent_class)->finalize) (object);
178 message_get_property (GObject *object,
183 GossipMessagePriv *priv;
185 priv = GET_PRIV (object);
189 g_value_set_int (value, priv->type);
192 g_value_set_object (value, priv->sender);
195 g_value_set_object (value, priv->receiver);
198 g_value_set_string (value, priv->body);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
207 message_set_property (GObject *object,
212 GossipMessagePriv *priv;
214 priv = GET_PRIV (object);
218 gossip_message_set_type (GOSSIP_MESSAGE (object),
219 g_value_get_int (value));
222 gossip_message_set_sender (GOSSIP_MESSAGE (object),
223 GOSSIP_CONTACT (g_value_get_object (value)));
226 gossip_message_set_receiver (GOSSIP_MESSAGE (object),
227 GOSSIP_CONTACT (g_value_get_object (value)));
230 gossip_message_set_body (GOSSIP_MESSAGE (object),
231 g_value_get_string (value));
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
240 gossip_message_new (const gchar *body)
242 return g_object_new (GOSSIP_TYPE_MESSAGE,
248 gossip_message_get_type (GossipMessage *message)
250 GossipMessagePriv *priv;
252 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message),
253 GOSSIP_MESSAGE_TYPE_NORMAL);
255 priv = GET_PRIV (message);
261 gossip_message_set_type (GossipMessage *message,
262 GossipMessageType type)
264 GossipMessagePriv *priv;
266 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
268 priv = GET_PRIV (message);
272 g_object_notify (G_OBJECT (message), "type");
276 gossip_message_get_sender (GossipMessage *message)
278 GossipMessagePriv *priv;
280 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
282 priv = GET_PRIV (message);
288 gossip_message_set_sender (GossipMessage *message, GossipContact *contact)
290 GossipMessagePriv *priv;
291 GossipContact *old_sender;
293 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
294 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
296 priv = GET_PRIV (message);
298 old_sender = priv->sender;
299 priv->sender = g_object_ref (contact);
302 g_object_unref (old_sender);
305 g_object_notify (G_OBJECT (message), "sender");
309 gossip_message_get_receiver (GossipMessage *message)
311 GossipMessagePriv *priv;
313 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
315 priv = GET_PRIV (message);
317 return priv->receiver;
321 gossip_message_set_receiver (GossipMessage *message, GossipContact *contact)
323 GossipMessagePriv *priv;
324 GossipContact *old_receiver;
326 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
327 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
329 priv = GET_PRIV (message);
331 old_receiver = priv->receiver;
332 priv->receiver = g_object_ref (contact);
335 g_object_unref (old_receiver);
338 g_object_notify (G_OBJECT (message), "receiver");
342 gossip_message_get_body (GossipMessage *message)
344 GossipMessagePriv *priv;
346 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
348 priv = GET_PRIV (message);
354 gossip_message_set_body (GossipMessage *message,
357 GossipMessagePriv *priv;
358 GossipMessageType type;
360 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
362 priv = GET_PRIV (message);
367 type = GOSSIP_MESSAGE_TYPE_NORMAL;
368 if (g_str_has_prefix (body, "/me")) {
369 type = GOSSIP_MESSAGE_TYPE_ACTION;
372 else if (g_str_has_prefix (body, "/say")) {
377 priv->body = g_strdup (body);
380 if (type != priv->type) {
381 gossip_message_set_type (message, type);
384 g_object_notify (G_OBJECT (message), "body");
388 gossip_message_get_timestamp (GossipMessage *message)
390 GossipMessagePriv *priv;
392 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), -1);
394 priv = GET_PRIV (message);
396 return priv->timestamp;
400 gossip_message_set_timestamp (GossipMessage *message,
401 GossipTime timestamp)
403 GossipMessagePriv *priv;
405 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
406 g_return_if_fail (timestamp >= -1);
408 priv = GET_PRIV (message);
410 if (timestamp <= 0) {
411 priv->timestamp = gossip_time_get_current ();
413 priv->timestamp = timestamp;
416 g_object_notify (G_OBJECT (message), "timestamp");