]> git.0d.be Git - empathy.git/blob - libempathy/empathy-message.c
Rename all filenames starting with "gossip" by "empathy", change namespace
[empathy.git] / libempathy / empathy-message.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004-2007 Imendio AB
4  * Copyright (C) 2007 Collabora Ltd.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Mikael Hallendal <micke@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include "config.h"
26
27 #include "empathy-message.h"
28
29 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_MESSAGE, EmpathyMessagePriv))
30
31 typedef struct _EmpathyMessagePriv EmpathyMessagePriv;
32
33 struct _EmpathyMessagePriv {
34         EmpathyMessageType     type;
35         EmpathyContact        *sender;
36         EmpathyContact        *receiver;
37         gchar                *body;
38         EmpathyTime            timestamp;
39
40 };
41
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,
46                                        guint               param_id,
47                                        GValue             *value,
48                                        GParamSpec         *pspec);
49 static void message_set_property      (GObject            *object,
50                                        guint               param_id,
51                                        const GValue       *value,
52                                        GParamSpec         *pspec);
53
54 enum {
55         PROP_0,
56         PROP_TYPE,
57         PROP_SENDER,
58         PROP_RECEIVER,
59         PROP_BODY,
60         PROP_TIMESTAMP,
61 };
62
63 static gpointer parent_class = NULL;
64
65 GType
66 empathy_message_get_gtype (void)
67 {
68         static GType type = 0;
69
70         if (!type) {
71                 static const GTypeInfo info = {
72                         sizeof (EmpathyMessageClass),
73                         NULL, /* base_init */
74                         NULL, /* base_finalize */
75                         (GClassInitFunc) empathy_message_class_init,
76                         NULL, /* class_finalize */
77                         NULL, /* class_data */
78                         sizeof (EmpathyMessage),
79                         0,    /* n_preallocs */
80                         (GInstanceInitFunc) empathy_message_init
81                 };
82
83                 type = g_type_register_static (G_TYPE_OBJECT,
84                                                "EmpathyMessage",
85                                                &info, 0);
86         }
87
88         return type;
89 }
90
91 static void
92 empathy_message_class_init (EmpathyMessageClass *class)
93 {
94         GObjectClass *object_class;
95
96         object_class = G_OBJECT_CLASS (class);
97         parent_class = g_type_class_peek_parent (class);
98
99         object_class->finalize     = empathy_message_finalize;
100         object_class->get_property = message_get_property;
101         object_class->set_property = message_set_property;
102
103         g_object_class_install_property (object_class,
104                                          PROP_TYPE,
105                                          g_param_spec_int ("type",
106                                                            "Message Type",
107                                                            "The type of message",
108                                                            EMPATHY_MESSAGE_TYPE_NORMAL,
109                                                            EMPATHY_MESSAGE_TYPE_LAST,
110                                                            EMPATHY_MESSAGE_TYPE_NORMAL,
111                                                            G_PARAM_READWRITE));
112         g_object_class_install_property (object_class,
113                                          PROP_SENDER,
114                                          g_param_spec_object ("sender",
115                                                               "Message Sender",
116                                                               "The sender of the message",
117                                                               EMPATHY_TYPE_CONTACT,
118                                                               G_PARAM_READWRITE));
119         g_object_class_install_property (object_class,
120                                          PROP_RECEIVER,
121                                          g_param_spec_object ("receiver",
122                                                               "Message Receiver",
123                                                               "The receiver of the message",
124                                                               EMPATHY_TYPE_CONTACT,
125                                                               G_PARAM_READWRITE));
126         g_object_class_install_property (object_class,
127                                          PROP_BODY,
128                                          g_param_spec_string ("body",
129                                                               "Message Body",
130                                                               "The content of the message",
131                                                               NULL,
132                                                               G_PARAM_READWRITE));
133         g_object_class_install_property (object_class,
134                                          PROP_TIMESTAMP,
135                                          g_param_spec_long ("timestamp",
136                                                             "timestamp",
137                                                             "timestamp",
138                                                             -1,
139                                                             G_MAXLONG,
140                                                             -1,
141                                                             G_PARAM_READWRITE));
142
143
144         g_type_class_add_private (object_class, sizeof (EmpathyMessagePriv));
145
146 }
147
148 static void
149 empathy_message_init (EmpathyMessage *message)
150 {
151         EmpathyMessagePriv *priv;
152
153         priv = GET_PRIV (message);
154
155         priv->timestamp = empathy_time_get_current ();
156 }
157
158 static void
159 empathy_message_finalize (GObject *object)
160 {
161         EmpathyMessagePriv *priv;
162
163         priv = GET_PRIV (object);
164
165         if (priv->sender) {
166                 g_object_unref (priv->sender);
167         }
168         if (priv->receiver) {
169                 g_object_unref (priv->receiver);
170         }
171
172         g_free (priv->body);
173
174         (G_OBJECT_CLASS (parent_class)->finalize) (object);
175 }
176
177 static void
178 message_get_property (GObject    *object,
179                       guint       param_id,
180                       GValue     *value,
181                       GParamSpec *pspec)
182 {
183         EmpathyMessagePriv *priv;
184
185         priv = GET_PRIV (object);
186
187         switch (param_id) {
188         case PROP_TYPE:
189                 g_value_set_int (value, priv->type);
190                 break;
191         case PROP_SENDER:
192                 g_value_set_object (value, priv->sender);
193                 break;
194         case PROP_RECEIVER:
195                 g_value_set_object (value, priv->receiver);
196                 break;
197         case PROP_BODY:
198                 g_value_set_string (value, priv->body);
199                 break;
200         default:
201                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
202                 break;
203         };
204 }
205
206 static void
207 message_set_property (GObject      *object,
208                       guint         param_id,
209                       const GValue *value,
210                       GParamSpec   *pspec)
211 {
212         EmpathyMessagePriv *priv;
213
214         priv = GET_PRIV (object);
215
216         switch (param_id) {
217         case PROP_TYPE:
218                 empathy_message_set_type (EMPATHY_MESSAGE (object),
219                                          g_value_get_int (value));
220                 break;
221         case PROP_SENDER:
222                 empathy_message_set_sender (EMPATHY_MESSAGE (object),
223                                            EMPATHY_CONTACT (g_value_get_object (value)));
224                 break;
225         case PROP_RECEIVER:
226                 empathy_message_set_receiver (EMPATHY_MESSAGE (object),
227                                              EMPATHY_CONTACT (g_value_get_object (value)));
228                 break;
229         case PROP_BODY:
230                 empathy_message_set_body (EMPATHY_MESSAGE (object),
231                                          g_value_get_string (value));
232                 break;
233         default:
234                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
235                 break;
236         };
237 }
238
239 EmpathyMessage *
240 empathy_message_new (const gchar *body)
241 {
242         return g_object_new (EMPATHY_TYPE_MESSAGE,
243                              "body", body,
244                              NULL);
245 }
246
247 EmpathyMessageType
248 empathy_message_get_type (EmpathyMessage *message)
249 {
250         EmpathyMessagePriv *priv;
251
252         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message),
253                               EMPATHY_MESSAGE_TYPE_NORMAL);
254
255         priv = GET_PRIV (message);
256
257         return priv->type;
258 }
259
260 void
261 empathy_message_set_type (EmpathyMessage     *message,
262                          EmpathyMessageType  type)
263 {
264         EmpathyMessagePriv *priv;
265
266         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
267
268         priv = GET_PRIV (message);
269
270         priv->type = type;
271
272         g_object_notify (G_OBJECT (message), "type");
273 }
274
275 EmpathyContact *
276 empathy_message_get_sender (EmpathyMessage *message)
277 {
278         EmpathyMessagePriv *priv;
279
280         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
281
282         priv = GET_PRIV (message);
283
284         return priv->sender;
285 }
286
287 void
288 empathy_message_set_sender (EmpathyMessage *message, EmpathyContact *contact)
289 {
290         EmpathyMessagePriv *priv;
291         EmpathyContact     *old_sender;
292
293         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
294         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
295
296         priv = GET_PRIV (message);
297
298         old_sender = priv->sender;
299         priv->sender = g_object_ref (contact);
300
301         if (old_sender) {
302                 g_object_unref (old_sender);
303         }
304
305         g_object_notify (G_OBJECT (message), "sender");
306 }
307
308 EmpathyContact *
309 empathy_message_get_receiver (EmpathyMessage *message)
310 {
311         EmpathyMessagePriv *priv;
312
313         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
314
315         priv = GET_PRIV (message);
316
317         return priv->receiver;
318 }
319
320 void
321 empathy_message_set_receiver (EmpathyMessage *message, EmpathyContact *contact)
322 {
323         EmpathyMessagePriv *priv;
324         EmpathyContact     *old_receiver;
325
326         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
327         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
328
329         priv = GET_PRIV (message);
330
331         old_receiver = priv->receiver;
332         priv->receiver = g_object_ref (contact);
333
334         if (old_receiver) {
335                 g_object_unref (old_receiver);
336         }
337
338         g_object_notify (G_OBJECT (message), "receiver");
339 }
340
341 const gchar *
342 empathy_message_get_body (EmpathyMessage *message)
343 {
344         EmpathyMessagePriv *priv;
345
346         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), NULL);
347
348         priv = GET_PRIV (message);
349
350         return priv->body;
351 }
352
353 void
354 empathy_message_set_body (EmpathyMessage *message,
355                          const gchar   *body)
356 {
357         EmpathyMessagePriv *priv;
358         EmpathyMessageType  type;
359
360         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
361
362         priv = GET_PRIV (message);
363
364         g_free (priv->body);
365         priv->body = NULL;
366
367         type = EMPATHY_MESSAGE_TYPE_NORMAL;
368         if (g_str_has_prefix (body, "/me")) {
369                 type = EMPATHY_MESSAGE_TYPE_ACTION;
370                 body += 4;
371         }
372         else if (g_str_has_prefix (body, "/say")) {
373                 body += 5;
374         }
375
376         if (body) {
377                 priv->body = g_strdup (body);
378         }
379
380         if (type != priv->type) {
381                 empathy_message_set_type (message, type);
382         }
383
384         g_object_notify (G_OBJECT (message), "body");
385 }
386
387 EmpathyTime
388 empathy_message_get_timestamp (EmpathyMessage *message)
389 {
390         EmpathyMessagePriv *priv;
391
392         g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), -1);
393
394         priv = GET_PRIV (message);
395
396         return priv->timestamp;
397 }
398
399 void
400 empathy_message_set_timestamp (EmpathyMessage *message,
401                               EmpathyTime     timestamp)
402 {
403         EmpathyMessagePriv *priv;
404
405         g_return_if_fail (EMPATHY_IS_MESSAGE (message));
406         g_return_if_fail (timestamp >= -1);
407
408         priv = GET_PRIV (message);
409
410         if (timestamp <= 0) {
411                 priv->timestamp = empathy_time_get_current ();
412         } else {
413                 priv->timestamp = timestamp;
414         }
415
416         g_object_notify (G_OBJECT (message), "timestamp");
417 }
418