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 "empathy-message.h"
29 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv))
31 typedef struct _EmpathyMessagePriv EmpathyMessagePriv;
33 struct _EmpathyMessagePriv {
34 EmpathyMessageType type;
35 EmpathyContact *sender;
36 EmpathyContact *receiver;
38 EmpathyTime timestamp;
42 static void empathy_message_class_init (EmpathyMessageClass *class);
43 static void empathy_message_init (EmpathyMessage *message);
44 static void empathy_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 empathy_message_get_gtype (void)
68 static GType type = 0;
71 static const GTypeInfo info = {
72 sizeof (EmpathyMessageClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) empathy_message_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof (EmpathyMessage),
80 (GInstanceInitFunc) empathy_message_init
83 type = g_type_register_static (G_TYPE_OBJECT,
92 empathy_message_class_init (EmpathyMessageClass *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 = empathy_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 EMPATHY_MESSAGE_TYPE_NORMAL,
109 EMPATHY_MESSAGE_TYPE_LAST,
110 EMPATHY_MESSAGE_TYPE_NORMAL,
112 g_object_class_install_property (object_class,
114 g_param_spec_object ("sender",
116 "The sender of the message",
117 EMPATHY_TYPE_CONTACT,
119 g_object_class_install_property (object_class,
121 g_param_spec_object ("receiver",
123 "The receiver of the message",
124 EMPATHY_TYPE_CONTACT,
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 (EmpathyMessagePriv));
149 empathy_message_init (EmpathyMessage *message)
151 EmpathyMessagePriv *priv;
153 priv = GET_PRIV (message);
155 priv->timestamp = empathy_time_get_current ();
159 empathy_message_finalize (GObject *object)
161 EmpathyMessagePriv *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 EmpathyMessagePriv *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 EmpathyMessagePriv *priv;
214 priv = GET_PRIV (object);
218 empathy_message_set_type (EMPATHY_MESSAGE (object),
219 g_value_get_int (value));
222 empathy_message_set_sender (EMPATHY_MESSAGE (object),
223 EMPATHY_CONTACT (g_value_get_object (value)));
226 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
227 EMPATHY_CONTACT (g_value_get_object (value)));
230 empathy_message_set_body (EMPATHY_MESSAGE (object),
231 g_value_get_string (value));
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
240 empathy_message_new (const gchar *body)
242 return g_object_new (EMPATHY_TYPE_MESSAGE,
248 empathy_message_get_type (EmpathyMessage *message)
250 EmpathyMessagePriv *priv;
252 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
253 EMPATHY_MESSAGE_TYPE_NORMAL);
255 priv = GET_PRIV (message);
261 empathy_message_set_type (EmpathyMessage *message,
262 EmpathyMessageType type)
264 EmpathyMessagePriv *priv;
266 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
268 priv = GET_PRIV (message);
272 g_object_notify (G_OBJECT (message), "type");
276 empathy_message_get_sender (EmpathyMessage *message)
278 EmpathyMessagePriv *priv;
280 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
282 priv = GET_PRIV (message);
288 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
290 EmpathyMessagePriv *priv;
291 EmpathyContact *old_sender;
293 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
294 g_return_if_fail (EMPATHY_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 empathy_message_get_receiver (EmpathyMessage *message)
311 EmpathyMessagePriv *priv;
313 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
315 priv = GET_PRIV (message);
317 return priv->receiver;
321 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
323 EmpathyMessagePriv *priv;
324 EmpathyContact *old_receiver;
326 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
327 g_return_if_fail (EMPATHY_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 empathy_message_get_body (EmpathyMessage *message)
344 EmpathyMessagePriv *priv;
346 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
348 priv = GET_PRIV (message);
354 empathy_message_set_body (EmpathyMessage *message,
357 EmpathyMessagePriv *priv;
358 EmpathyMessageType type;
360 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
362 priv = GET_PRIV (message);
367 type = EMPATHY_MESSAGE_TYPE_NORMAL;
368 if (g_str_has_prefix (body, "/me")) {
369 type = EMPATHY_MESSAGE_TYPE_ACTION;
372 else if (g_str_has_prefix (body, "/say")) {
377 priv->body = g_strdup (body);
380 if (type != priv->type) {
381 empathy_message_set_type (message, type);
384 g_object_notify (G_OBJECT (message), "body");
388 empathy_message_get_timestamp (EmpathyMessage *message)
390 EmpathyMessagePriv *priv;
392 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
394 priv = GET_PRIV (message);
396 return priv->timestamp;
400 empathy_message_set_timestamp (EmpathyMessage *message,
401 EmpathyTime timestamp)
403 EmpathyMessagePriv *priv;
405 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
406 g_return_if_fail (timestamp >= -1);
408 priv = GET_PRIV (message);
410 if (timestamp <= 0) {
411 priv->timestamp = empathy_time_get_current ();
413 priv->timestamp = timestamp;
416 g_object_notify (G_OBJECT (message), "timestamp");