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