1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-2007 Imendio AB
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Mikael Hallendal <micke@imendio.com>
25 #include "gossip-message.h"
27 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOSSIP_TYPE_MESSAGE, GossipMessagePriv))
29 typedef struct _GossipMessagePriv GossipMessagePriv;
31 struct _GossipMessagePriv {
32 GossipMessageType type;
33 GossipContact *sender;
39 static void gossip_message_class_init (GossipMessageClass *class);
40 static void gossip_message_init (GossipMessage *message);
41 static void gossip_message_finalize (GObject *object);
42 static void message_get_property (GObject *object,
46 static void message_set_property (GObject *object,
59 static gpointer parent_class = NULL;
62 gossip_message_get_gtype (void)
64 static GType type = 0;
67 static const GTypeInfo info = {
68 sizeof (GossipMessageClass),
70 NULL, /* base_finalize */
71 (GClassInitFunc) gossip_message_class_init,
72 NULL, /* class_finalize */
73 NULL, /* class_data */
74 sizeof (GossipMessage),
76 (GInstanceInitFunc) gossip_message_init
79 type = g_type_register_static (G_TYPE_OBJECT,
88 gossip_message_class_init (GossipMessageClass *class)
90 GObjectClass *object_class;
92 object_class = G_OBJECT_CLASS (class);
93 parent_class = g_type_class_peek_parent (class);
95 object_class->finalize = gossip_message_finalize;
96 object_class->get_property = message_get_property;
97 object_class->set_property = message_set_property;
99 g_object_class_install_property (object_class,
101 g_param_spec_int ("type",
103 "The type of message",
104 GOSSIP_MESSAGE_TYPE_NORMAL,
105 GOSSIP_MESSAGE_TYPE_LAST,
106 GOSSIP_MESSAGE_TYPE_NORMAL,
108 g_object_class_install_property (object_class,
110 g_param_spec_object ("sender",
112 "The sender of the message",
116 g_object_class_install_property (object_class,
118 g_param_spec_string ("body",
120 "The content of the message",
123 g_object_class_install_property (object_class,
125 g_param_spec_long ("timestamp",
134 g_type_class_add_private (object_class, sizeof (GossipMessagePriv));
139 gossip_message_init (GossipMessage *message)
141 GossipMessagePriv *priv;
143 priv = GET_PRIV (message);
145 priv->type = GOSSIP_MESSAGE_TYPE_NORMAL;
148 priv->timestamp = gossip_time_get_current ();
152 gossip_message_finalize (GObject *object)
154 GossipMessagePriv *priv;
156 priv = GET_PRIV (object);
159 g_object_unref (priv->sender);
164 (G_OBJECT_CLASS (parent_class)->finalize) (object);
168 message_get_property (GObject *object,
173 GossipMessagePriv *priv;
175 priv = GET_PRIV (object);
179 g_value_set_int (value, priv->type);
182 g_value_set_object (value, priv->sender);
185 g_value_set_string (value, priv->body);
188 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
194 message_set_property (GObject *object,
199 GossipMessagePriv *priv;
201 priv = GET_PRIV (object);
205 gossip_message_set_type (GOSSIP_MESSAGE (object),
206 g_value_get_int (value));
209 gossip_message_set_sender (GOSSIP_MESSAGE (object),
210 GOSSIP_CONTACT (g_value_get_object (value)));
213 gossip_message_set_body (GOSSIP_MESSAGE (object),
214 g_value_get_string (value));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
223 gossip_message_new (const gchar *body)
225 return g_object_new (GOSSIP_TYPE_MESSAGE,
231 gossip_message_get_type (GossipMessage *message)
233 GossipMessagePriv *priv;
235 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message),
236 GOSSIP_MESSAGE_TYPE_NORMAL);
238 priv = GET_PRIV (message);
244 gossip_message_set_type (GossipMessage *message,
245 GossipMessageType type)
247 GossipMessagePriv *priv;
249 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
251 priv = GET_PRIV (message);
255 g_object_notify (G_OBJECT (message), "type");
259 gossip_message_get_sender (GossipMessage *message)
261 GossipMessagePriv *priv;
263 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
265 priv = GET_PRIV (message);
271 gossip_message_set_sender (GossipMessage *message, GossipContact *contact)
273 GossipMessagePriv *priv;
274 GossipContact *old_sender;
276 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
277 g_return_if_fail (GOSSIP_IS_CONTACT (contact));
279 priv = GET_PRIV (message);
281 old_sender = priv->sender;
282 priv->sender = g_object_ref (contact);
285 g_object_unref (old_sender);
288 g_object_notify (G_OBJECT (message), "sender");
292 gossip_message_get_body (GossipMessage *message)
294 GossipMessagePriv *priv;
296 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), NULL);
298 priv = GET_PRIV (message);
304 gossip_message_set_body (GossipMessage *message,
307 GossipMessagePriv *priv;
308 GossipMessageType type;
310 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
312 priv = GET_PRIV (message);
317 type = GOSSIP_MESSAGE_TYPE_NORMAL;
318 if (g_str_has_prefix (body, "/me")) {
319 type = GOSSIP_MESSAGE_TYPE_ACTION;
324 priv->body = g_strdup (body);
327 if (type != priv->type) {
328 gossip_message_set_type (message, type);
331 g_object_notify (G_OBJECT (message), "body");
335 gossip_message_get_timestamp (GossipMessage *message)
337 GossipMessagePriv *priv;
339 g_return_val_if_fail (GOSSIP_IS_MESSAGE (message), -1);
341 priv = GET_PRIV (message);
343 return priv->timestamp;
347 gossip_message_set_timestamp (GossipMessage *message,
348 GossipTime timestamp)
350 GossipMessagePriv *priv;
352 g_return_if_fail (GOSSIP_IS_MESSAGE (message));
353 g_return_if_fail (timestamp >= -1);
355 priv = GET_PRIV (message);
357 if (timestamp <= 0) {
358 priv->timestamp = gossip_time_get_current ();
360 priv->timestamp = timestamp;
363 g_object_notify (G_OBJECT (message), "timestamp");