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;
41 static void empathy_message_class_init (EmpathyMessageClass *class);
42 static void empathy_message_init (EmpathyMessage *message);
43 static void empathy_message_finalize (GObject *object);
44 static void message_get_property (GObject *object,
48 static void message_set_property (GObject *object,
62 static gpointer parent_class = NULL;
65 empathy_message_get_gtype (void)
67 static GType type = 0;
70 static const GTypeInfo info = {
71 sizeof (EmpathyMessageClass),
73 NULL, /* base_finalize */
74 (GClassInitFunc) empathy_message_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
77 sizeof (EmpathyMessage),
79 (GInstanceInitFunc) empathy_message_init
82 type = g_type_register_static (G_TYPE_OBJECT,
91 empathy_message_class_init (EmpathyMessageClass *class)
93 GObjectClass *object_class;
95 object_class = G_OBJECT_CLASS (class);
96 parent_class = g_type_class_peek_parent (class);
98 object_class->finalize = empathy_message_finalize;
99 object_class->get_property = message_get_property;
100 object_class->set_property = message_set_property;
102 g_object_class_install_property (object_class,
104 g_param_spec_int ("type",
106 "The type of message",
107 EMPATHY_MESSAGE_TYPE_NORMAL,
108 EMPATHY_MESSAGE_TYPE_LAST,
109 EMPATHY_MESSAGE_TYPE_NORMAL,
111 g_object_class_install_property (object_class,
113 g_param_spec_object ("sender",
115 "The sender of the message",
116 EMPATHY_TYPE_CONTACT,
118 g_object_class_install_property (object_class,
120 g_param_spec_object ("receiver",
122 "The receiver of the message",
123 EMPATHY_TYPE_CONTACT,
125 g_object_class_install_property (object_class,
127 g_param_spec_string ("body",
129 "The content of the message",
132 g_object_class_install_property (object_class,
134 g_param_spec_long ("timestamp",
143 g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
148 empathy_message_init (EmpathyMessage *message)
150 EmpathyMessagePriv *priv;
152 priv = GET_PRIV (message);
154 priv->timestamp = empathy_time_get_current ();
158 empathy_message_finalize (GObject *object)
160 EmpathyMessagePriv *priv;
162 priv = GET_PRIV (object);
165 g_object_unref (priv->sender);
167 if (priv->receiver) {
168 g_object_unref (priv->receiver);
173 (G_OBJECT_CLASS (parent_class)->finalize) (object);
177 message_get_property (GObject *object,
182 EmpathyMessagePriv *priv;
184 priv = GET_PRIV (object);
188 g_value_set_int (value, priv->type);
191 g_value_set_object (value, priv->sender);
194 g_value_set_object (value, priv->receiver);
197 g_value_set_string (value, priv->body);
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
206 message_set_property (GObject *object,
211 EmpathyMessagePriv *priv;
213 priv = GET_PRIV (object);
217 empathy_message_set_type (EMPATHY_MESSAGE (object),
218 g_value_get_int (value));
221 empathy_message_set_sender (EMPATHY_MESSAGE (object),
222 EMPATHY_CONTACT (g_value_get_object (value)));
225 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
226 EMPATHY_CONTACT (g_value_get_object (value)));
229 empathy_message_set_body (EMPATHY_MESSAGE (object),
230 g_value_get_string (value));
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
239 empathy_message_new (const gchar *body)
241 return g_object_new (EMPATHY_TYPE_MESSAGE,
247 empathy_message_get_type (EmpathyMessage *message)
249 EmpathyMessagePriv *priv;
251 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
252 EMPATHY_MESSAGE_TYPE_NORMAL);
254 priv = GET_PRIV (message);
260 empathy_message_set_type (EmpathyMessage *message,
261 EmpathyMessageType type)
263 EmpathyMessagePriv *priv;
265 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
267 priv = GET_PRIV (message);
271 g_object_notify (G_OBJECT (message), "type");
275 empathy_message_get_sender (EmpathyMessage *message)
277 EmpathyMessagePriv *priv;
279 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
281 priv = GET_PRIV (message);
287 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
289 EmpathyMessagePriv *priv;
290 EmpathyContact *old_sender;
292 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
293 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
295 priv = GET_PRIV (message);
297 old_sender = priv->sender;
298 priv->sender = g_object_ref (contact);
301 g_object_unref (old_sender);
304 g_object_notify (G_OBJECT (message), "sender");
308 empathy_message_get_receiver (EmpathyMessage *message)
310 EmpathyMessagePriv *priv;
312 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
314 priv = GET_PRIV (message);
316 return priv->receiver;
320 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
322 EmpathyMessagePriv *priv;
323 EmpathyContact *old_receiver;
325 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
326 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
328 priv = GET_PRIV (message);
330 old_receiver = priv->receiver;
331 priv->receiver = g_object_ref (contact);
334 g_object_unref (old_receiver);
337 g_object_notify (G_OBJECT (message), "receiver");
341 empathy_message_get_body (EmpathyMessage *message)
343 EmpathyMessagePriv *priv;
345 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
347 priv = GET_PRIV (message);
353 empathy_message_set_body (EmpathyMessage *message,
356 EmpathyMessagePriv *priv;
357 EmpathyMessageType type;
359 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
361 priv = GET_PRIV (message);
366 type = EMPATHY_MESSAGE_TYPE_NORMAL;
367 if (g_str_has_prefix (body, "/me")) {
368 type = EMPATHY_MESSAGE_TYPE_ACTION;
371 else if (g_str_has_prefix (body, "/say")) {
376 priv->body = g_strdup (body);
379 if (type != priv->type) {
380 empathy_message_set_type (message, type);
383 g_object_notify (G_OBJECT (message), "body");
387 empathy_message_get_timestamp (EmpathyMessage *message)
389 EmpathyMessagePriv *priv;
391 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
393 priv = GET_PRIV (message);
395 return priv->timestamp;
399 empathy_message_set_timestamp (EmpathyMessage *message,
400 EmpathyTime timestamp)
402 EmpathyMessagePriv *priv;
404 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
405 g_return_if_fail (timestamp >= -1);
407 priv = GET_PRIV (message);
409 if (timestamp <= 0) {
410 priv->timestamp = empathy_time_get_current ();
412 priv->timestamp = timestamp;
415 g_object_notify (G_OBJECT (message), "timestamp");
419 empathy_message_type_from_str (const gchar *type_str)
421 if (strcmp (type_str, "normal") == 0) {
422 return EMPATHY_MESSAGE_TYPE_NORMAL;
424 if (strcmp (type_str, "action") == 0) {
425 return EMPATHY_MESSAGE_TYPE_ACTION;
427 else if (strcmp (type_str, "notice") == 0) {
428 return EMPATHY_MESSAGE_TYPE_NOTICE;
430 else if (strcmp (type_str, "auto-reply") == 0) {
431 return EMPATHY_MESSAGE_TYPE_AUTO_REPLY;
434 return EMPATHY_MESSAGE_TYPE_NORMAL;
438 empathy_message_type_to_str (EmpathyMessageType type)
441 case EMPATHY_MESSAGE_TYPE_ACTION:
443 case EMPATHY_MESSAGE_TYPE_NOTICE:
445 case EMPATHY_MESSAGE_TYPE_AUTO_REPLY: