]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact.c
Removed unused PROP_GROUPS
[empathy.git] / libempathy / empathy-contact.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2004 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  *          Martyn Russell <martyn@imendio.com>
23  *          Xavier Claessens <xclaesse@gmail.com>
24  */
25
26 #include "config.h"
27
28 #include <string.h>
29
30 #include <glib/gi18n.h>
31
32 #include <telepathy-glib/util.h>
33
34 #include "empathy-contact.h"
35 #include "empathy-utils.h"
36 #include "empathy-debug.h"
37 #include "empathy-enum-types.h"
38
39 #define DEBUG_DOMAIN "Contact"
40
41 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_CONTACT, EmpathyContactPriv))
42
43 typedef struct _EmpathyContactPriv EmpathyContactPriv;
44
45 struct _EmpathyContactPriv {
46         gchar              *id;
47         gchar              *name;
48         EmpathyAvatar      *avatar;
49         McAccount          *account;
50         McPresence          presence;
51         gchar              *presence_message;
52         guint               handle;
53         EmpathyCapabilities capabilities;
54         gboolean            is_user;
55 };
56
57 static void empathy_contact_class_init (EmpathyContactClass *class);
58 static void empathy_contact_init       (EmpathyContact      *contact);
59 static void contact_finalize           (GObject             *object);
60 static void contact_get_property       (GObject             *object,
61                                         guint                param_id,
62                                         GValue              *value,
63                                         GParamSpec          *pspec);
64 static void contact_set_property       (GObject             *object,
65                                         guint                param_id,
66                                         const GValue        *value,
67                                         GParamSpec          *pspec);
68
69 G_DEFINE_TYPE (EmpathyContact, empathy_contact, G_TYPE_OBJECT);
70
71 enum {
72         PROP_0,
73         PROP_ID,
74         PROP_NAME,
75         PROP_AVATAR,
76         PROP_ACCOUNT,
77         PROP_PRESENCE,
78         PROP_PRESENCE_MESSAGE,
79         PROP_SUBSCRIPTION,
80         PROP_HANDLE,
81         PROP_CAPABILITIES,
82         PROP_IS_USER
83 };
84
85 static void
86 empathy_contact_class_init (EmpathyContactClass *class)
87 {
88         GObjectClass *object_class;
89
90         object_class = G_OBJECT_CLASS (class);
91
92         object_class->finalize     = contact_finalize;
93         object_class->get_property = contact_get_property;
94         object_class->set_property = contact_set_property;
95
96         g_object_class_install_property (object_class,
97                                          PROP_ID,
98                                          g_param_spec_string ("id",
99                                                               "Contact id",
100                                                               "String identifying contact",
101                                                               NULL,
102                                                               G_PARAM_READWRITE));
103
104         g_object_class_install_property (object_class,
105                                          PROP_NAME,
106                                          g_param_spec_string ("name",
107                                                               "Contact Name",
108                                                               "The name of the contact",
109                                                               NULL,
110                                                               G_PARAM_READWRITE));
111
112         g_object_class_install_property (object_class,
113                                          PROP_AVATAR,
114                                          g_param_spec_boxed ("avatar",
115                                                              "Avatar image",
116                                                              "The avatar image",
117                                                              EMPATHY_TYPE_AVATAR,
118                                                              G_PARAM_READWRITE));
119
120         g_object_class_install_property (object_class,
121                                          PROP_ACCOUNT,
122                                          g_param_spec_object ("account",
123                                                               "Contact Account",
124                                                               "The account associated with the contact",
125                                                               MC_TYPE_ACCOUNT,
126                                                               G_PARAM_READWRITE));
127
128         g_object_class_install_property (object_class,
129                                          PROP_PRESENCE,
130                                          g_param_spec_uint ("presence",
131                                                             "Contact presence",
132                                                             "Presence of contact",
133                                                             MC_PRESENCE_UNSET,
134                                                             LAST_MC_PRESENCE,
135                                                             MC_PRESENCE_UNSET,
136                                                             G_PARAM_READWRITE));
137
138         g_object_class_install_property (object_class,
139                                          PROP_PRESENCE_MESSAGE,
140                                          g_param_spec_string ("presence-message",
141                                                               "Contact presence message",
142                                                               "Presence message of contact",
143                                                               NULL,
144                                                               G_PARAM_READWRITE));
145         g_object_class_install_property (object_class,
146                                          PROP_HANDLE,
147                                          g_param_spec_uint ("handle",
148                                                             "Contact Handle",
149                                                             "The handle of the contact",
150                                                             0,
151                                                             G_MAXUINT,
152                                                             0,
153                                                             G_PARAM_READWRITE));
154
155         g_object_class_install_property (object_class,
156                                          PROP_CAPABILITIES,
157                                          g_param_spec_flags ("capabilities",
158                                                              "Contact Capabilities",
159                                                              "Capabilities of the contact",
160                                                              EMPATHY_TYPE_CAPABILITIES,
161                                                              EMPATHY_CAPABILITIES_UNKNOWN,
162                                                              G_PARAM_READWRITE));
163
164         g_object_class_install_property (object_class,
165                                          PROP_IS_USER,
166                                          g_param_spec_boolean ("is-user",
167                                                                "Contact is-user",
168                                                                "Is contact the user",
169                                                                FALSE,
170                                                                G_PARAM_READWRITE));
171
172         g_type_class_add_private (object_class, sizeof (EmpathyContactPriv));
173 }
174
175 static void
176 empathy_contact_init (EmpathyContact *contact)
177 {
178 }
179
180 static void
181 contact_finalize (GObject *object)
182 {
183         EmpathyContactPriv *priv;
184
185         priv = GET_PRIV (object);
186
187         empathy_debug (DEBUG_DOMAIN, "finalize: %p", object);
188
189         g_free (priv->name);
190         g_free (priv->id);
191         g_free (priv->presence_message);
192
193         if (priv->avatar) {
194                 empathy_avatar_unref (priv->avatar);
195         }
196
197         if (priv->account) {
198                 g_object_unref (priv->account);
199         }
200
201         G_OBJECT_CLASS (empathy_contact_parent_class)->finalize (object);
202 }
203
204 static void
205 contact_get_property (GObject    *object,
206                       guint       param_id,
207                       GValue     *value,
208                       GParamSpec *pspec)
209 {
210         EmpathyContactPriv *priv;
211
212         priv = GET_PRIV (object);
213
214         switch (param_id) {
215         case PROP_ID:
216                 g_value_set_string (value,
217                                     empathy_contact_get_id (EMPATHY_CONTACT (object)));
218                 break;
219         case PROP_NAME:
220                 g_value_set_string (value,
221                                     empathy_contact_get_name (EMPATHY_CONTACT (object)));
222                 break;
223         case PROP_AVATAR:
224                 g_value_set_boxed (value, priv->avatar);
225                 break;
226         case PROP_ACCOUNT:
227                 g_value_set_object (value, priv->account);
228                 break;
229         case PROP_PRESENCE:
230                 g_value_set_uint (value, priv->presence);
231                 break;
232         case PROP_PRESENCE_MESSAGE:
233                 g_value_set_string (value, priv->presence_message);
234                 break;
235         case PROP_HANDLE:
236                 g_value_set_uint (value, priv->handle);
237                 break;
238         case PROP_CAPABILITIES:
239                 g_value_set_flags (value, priv->capabilities);
240                 break;
241         case PROP_IS_USER:
242                 g_value_set_boolean (value, priv->is_user);
243                 break;
244         default:
245                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
246                 break;
247         };
248 }
249
250 static void
251 contact_set_property (GObject      *object,
252                       guint         param_id,
253                       const GValue *value,
254                       GParamSpec   *pspec)
255 {
256         EmpathyContactPriv *priv;
257
258         priv = GET_PRIV (object);
259
260         switch (param_id) {
261         case PROP_ID:
262                 empathy_contact_set_id (EMPATHY_CONTACT (object),
263                                        g_value_get_string (value));
264                 break;
265         case PROP_NAME:
266                 empathy_contact_set_name (EMPATHY_CONTACT (object),
267                                          g_value_get_string (value));
268                 break;
269         case PROP_AVATAR:
270                 empathy_contact_set_avatar (EMPATHY_CONTACT (object),
271                                            g_value_get_boxed (value));
272                 break;
273         case PROP_ACCOUNT:
274                 empathy_contact_set_account (EMPATHY_CONTACT (object),
275                                             MC_ACCOUNT (g_value_get_object (value)));
276                 break;
277         case PROP_PRESENCE:
278                 empathy_contact_set_presence (EMPATHY_CONTACT (object),
279                                               g_value_get_uint (value));
280                 break;
281         case PROP_PRESENCE_MESSAGE:
282                 empathy_contact_set_presence_message (EMPATHY_CONTACT (object),
283                                                       g_value_get_string (value));
284                 break;
285         case PROP_HANDLE:
286                 empathy_contact_set_handle (EMPATHY_CONTACT (object),
287                                            g_value_get_uint (value));
288                 break;
289         case PROP_CAPABILITIES:
290                 empathy_contact_set_capabilities (EMPATHY_CONTACT (object),
291                                                   g_value_get_flags (value));
292                 break;
293         case PROP_IS_USER:
294                 empathy_contact_set_is_user (EMPATHY_CONTACT (object),
295                                             g_value_get_boolean (value));
296                 break;
297         default:
298                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
299                 break;
300         };
301 }
302
303 EmpathyContact *
304 empathy_contact_new (McAccount *account)
305 {
306         return g_object_new (EMPATHY_TYPE_CONTACT,
307                              "account", account,
308                              NULL);
309 }
310
311 EmpathyContact *
312 empathy_contact_new_full (McAccount   *account,
313                           const gchar *id,
314                           const gchar *name)
315 {
316         return g_object_new (EMPATHY_TYPE_CONTACT,
317                              "account", account,
318                              "name", name,
319                              "id", id,
320                              NULL);
321 }
322
323 const gchar *
324 empathy_contact_get_id (EmpathyContact *contact)
325 {
326         EmpathyContactPriv *priv;
327
328         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
329
330         priv = GET_PRIV (contact);
331
332         if (priv->id) {
333                 return priv->id;
334         }
335
336         return "";
337 }
338
339 void
340 empathy_contact_set_id (EmpathyContact *contact,
341                        const gchar   *id)
342 {
343         EmpathyContactPriv *priv;
344
345         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
346         g_return_if_fail (id != NULL);
347
348         priv = GET_PRIV (contact);
349
350         if (priv->id && strcmp (id, priv->id) == 0) {
351                 return;
352         }
353
354         g_free (priv->id);
355         priv->id = g_strdup (id);
356
357         g_object_notify (G_OBJECT (contact), "id");
358 }
359
360 const gchar *
361 empathy_contact_get_name (EmpathyContact *contact)
362 {
363         EmpathyContactPriv *priv;
364
365         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
366
367         priv = GET_PRIV (contact);
368
369         if (G_STR_EMPTY (priv->name)) {
370                 return empathy_contact_get_id (contact);
371         }
372
373         return priv->name;
374 }
375
376 void
377 empathy_contact_set_name (EmpathyContact *contact,
378                          const gchar   *name)
379 {
380         EmpathyContactPriv *priv;
381
382         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
383         g_return_if_fail (name != NULL);
384
385         priv = GET_PRIV (contact);
386
387         if (priv->name && strcmp (name, priv->name) == 0) {
388                 return;
389         }
390
391         g_free (priv->name);
392         priv->name = g_strdup (name);
393
394         g_object_notify (G_OBJECT (contact), "name");
395 }
396
397 EmpathyAvatar *
398 empathy_contact_get_avatar (EmpathyContact *contact)
399 {
400         EmpathyContactPriv *priv;
401
402         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
403
404         priv = GET_PRIV (contact);
405
406         return priv->avatar;
407 }
408
409 void
410 empathy_contact_set_avatar (EmpathyContact *contact,
411                            EmpathyAvatar  *avatar)
412 {
413         EmpathyContactPriv *priv;
414
415         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
416
417         priv = GET_PRIV (contact);
418
419         if (priv->avatar == avatar) {
420                 return;
421         }
422
423         if (priv->avatar) {
424                 empathy_avatar_unref (priv->avatar);
425                 priv->avatar = NULL;
426         }
427
428         if (avatar) {
429                 priv->avatar = empathy_avatar_ref (avatar);
430         }
431
432         g_object_notify (G_OBJECT (contact), "avatar");
433 }
434
435 McAccount *
436 empathy_contact_get_account (EmpathyContact *contact)
437 {
438         EmpathyContactPriv *priv;
439
440         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
441
442         priv = GET_PRIV (contact);
443
444         return priv->account;
445 }
446
447 void
448 empathy_contact_set_account (EmpathyContact *contact,
449                              McAccount      *account)
450 {
451         EmpathyContactPriv *priv;
452
453         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
454         g_return_if_fail (MC_IS_ACCOUNT (account));
455
456         priv = GET_PRIV (contact);
457
458         if (account == priv->account) {
459                 return;
460         }
461
462         if (priv->account) {
463                 g_object_unref (priv->account);
464         }
465         priv->account = g_object_ref (account);
466
467         g_object_notify (G_OBJECT (contact), "account");
468 }
469
470 McPresence
471 empathy_contact_get_presence (EmpathyContact *contact)
472 {
473         EmpathyContactPriv *priv;
474
475         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), MC_PRESENCE_UNSET);
476
477         priv = GET_PRIV (contact);
478
479         return priv->presence;
480 }
481
482 void
483 empathy_contact_set_presence (EmpathyContact *contact,
484                               McPresence      presence)
485 {
486         EmpathyContactPriv *priv;
487
488         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
489
490         priv = GET_PRIV (contact);
491
492         if (presence == priv->presence) {
493                 return;
494         }
495
496         priv->presence = presence;
497
498         g_object_notify (G_OBJECT (contact), "presence");
499 }
500
501 const gchar *
502 empathy_contact_get_presence_message (EmpathyContact *contact)
503 {
504         EmpathyContactPriv *priv;
505
506         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);
507
508         priv = GET_PRIV (contact);
509
510         return priv->presence_message;
511 }
512
513 void
514 empathy_contact_set_presence_message (EmpathyContact *contact,
515                                       const gchar    *message)
516 {
517         EmpathyContactPriv *priv = GET_PRIV (contact);
518
519         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
520
521         if (!tp_strdiff (message, priv->presence_message)) {
522                 return;
523         }
524
525         g_free (priv->presence_message);
526         priv->presence_message = g_strdup (message);
527
528         g_object_notify (G_OBJECT (contact), "presence-message");
529 }
530
531 guint
532 empathy_contact_get_handle (EmpathyContact *contact)
533 {
534         EmpathyContactPriv *priv;
535
536         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
537
538         priv = GET_PRIV (contact);
539
540         return priv->handle;
541 }
542
543 void
544 empathy_contact_set_handle (EmpathyContact *contact,
545                            guint          handle)
546 {
547         EmpathyContactPriv *priv;
548
549         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
550
551         priv = GET_PRIV (contact);
552
553         if (priv->handle == handle) {
554                 return;
555         }
556
557         priv->handle = handle;
558
559         g_object_notify (G_OBJECT (contact), "handle");
560 }
561
562 EmpathyCapabilities
563 empathy_contact_get_capabilities (EmpathyContact *contact)
564 {
565         EmpathyContactPriv *priv;
566
567         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), 0);
568
569         priv = GET_PRIV (contact);
570
571         return priv->capabilities;
572 }
573
574 void
575 empathy_contact_set_capabilities (EmpathyContact      *contact,
576                                   EmpathyCapabilities  capabilities)
577 {
578         EmpathyContactPriv *priv;
579
580         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
581
582         priv = GET_PRIV (contact);
583
584         if (priv->capabilities == capabilities) {
585                 return;
586         }
587
588         priv->capabilities = capabilities;
589
590         g_object_notify (G_OBJECT (contact), "capabilities");
591 }
592
593 gboolean
594 empathy_contact_is_user (EmpathyContact *contact)
595 {
596         EmpathyContactPriv *priv;
597
598         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
599
600         priv = GET_PRIV (contact);
601
602         return priv->is_user;
603 }
604
605 void
606 empathy_contact_set_is_user (EmpathyContact *contact,
607                             gboolean       is_user)
608 {
609         EmpathyContactPriv *priv;
610
611         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
612
613         priv = GET_PRIV (contact);
614
615         if (priv->is_user == is_user) {
616                 return;
617         }
618
619         priv->is_user = is_user;
620
621         g_object_notify (G_OBJECT (contact), "is-user");
622 }
623
624 gboolean
625 empathy_contact_is_online (EmpathyContact *contact)
626 {
627         EmpathyContactPriv *priv;
628
629         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
630
631         priv = GET_PRIV (contact);
632
633         return (priv->presence > MC_PRESENCE_OFFLINE);
634 }
635
636 const gchar *
637 empathy_contact_get_status (EmpathyContact *contact)
638 {
639         EmpathyContactPriv *priv;
640
641         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), "");
642
643         priv = GET_PRIV (contact);
644
645         if (priv->presence_message) {
646                 return priv->presence_message;
647         }
648
649         return empathy_presence_get_default_message (priv->presence);
650 }
651
652 gboolean
653 empathy_contact_can_voip (EmpathyContact *contact)
654 {
655         EmpathyContactPriv *priv;
656
657         g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), FALSE);
658
659         priv = GET_PRIV (contact);
660
661         return priv->capabilities & (EMPATHY_CAPABILITIES_AUDIO |
662                                      EMPATHY_CAPABILITIES_VIDEO);
663 }
664
665 gboolean
666 empathy_contact_equal (gconstpointer v1,
667                       gconstpointer v2)
668 {
669         McAccount   *account_a;
670         McAccount   *account_b;
671         const gchar *id_a;
672         const gchar *id_b;
673
674         g_return_val_if_fail (EMPATHY_IS_CONTACT (v1), FALSE);
675         g_return_val_if_fail (EMPATHY_IS_CONTACT (v2), FALSE);
676
677         account_a = empathy_contact_get_account (EMPATHY_CONTACT (v1));
678         account_b = empathy_contact_get_account (EMPATHY_CONTACT (v2));
679
680         id_a = empathy_contact_get_id (EMPATHY_CONTACT (v1));
681         id_b = empathy_contact_get_id (EMPATHY_CONTACT (v2));
682
683         return empathy_account_equal (account_a, account_b) && g_str_equal (id_a, id_b);
684 }
685
686 guint
687 empathy_contact_hash (gconstpointer key)
688 {
689         EmpathyContactPriv *priv;
690         guint              hash;
691
692         g_return_val_if_fail (EMPATHY_IS_CONTACT (key), +1);
693
694         priv = GET_PRIV (EMPATHY_CONTACT (key));
695
696         hash = empathy_account_hash (empathy_contact_get_account (EMPATHY_CONTACT (key)));
697         hash += g_str_hash (empathy_contact_get_id (EMPATHY_CONTACT (key)));
698
699         return hash;
700 }
701