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