]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact-factory.c
e9469bac9ca2dc49771502f6259346d2d8bc9a66
[empathy.git] / libempathy / empathy-contact-factory.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  */
21
22 #include <config.h>
23
24 #include "empathy-contact-factory.h"
25 #include "empathy-utils.h"
26
27 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
28                        EMPATHY_TYPE_CONTACT_FACTORY, EmpathyContactFactoryPriv))
29
30 struct _EmpathyContactFactoryPriv {
31         GHashTable *accounts;
32 };
33
34 static void empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass);
35 static void empathy_contact_factory_init       (EmpathyContactFactory      *factory);
36
37 G_DEFINE_TYPE (EmpathyContactFactory, empathy_contact_factory, G_TYPE_OBJECT);
38
39 EmpathyTpContactFactory *
40 empathy_contact_factory_get_tp_factory (EmpathyContactFactory *factory,
41                                         McAccount             *account)
42 {
43         EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
44         EmpathyTpContactFactory   *tp_factory;
45
46         tp_factory = g_hash_table_lookup (priv->accounts, account);
47         if (!tp_factory) {
48                 tp_factory = empathy_tp_contact_factory_new (account);
49                 g_hash_table_insert (priv->accounts,
50                                      g_object_ref (account),
51                                      tp_factory);
52         }
53
54         return tp_factory;
55 }
56
57 EmpathyContact *
58 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
59                                   McAccount             *account)
60 {
61         EmpathyTpContactFactory *tp_factory;
62
63         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
64
65         return empathy_tp_contact_factory_get_user (tp_factory);
66 }
67
68 EmpathyContact *
69 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
70                                      McAccount             *account,
71                                      const gchar           *id)
72 {
73         EmpathyTpContactFactory *tp_factory;
74
75         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
76
77         return empathy_tp_contact_factory_get_from_id (tp_factory, id);
78 }
79
80 EmpathyContact *
81 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
82                                          McAccount             *account,
83                                          guint                  handle)
84 {
85         EmpathyTpContactFactory *tp_factory;
86
87         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
88
89         return empathy_tp_contact_factory_get_from_handle (tp_factory, handle);
90 }
91
92 GList *
93 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
94                                           McAccount             *account,
95                                           const GArray          *handles)
96 {
97         EmpathyTpContactFactory *tp_factory;
98
99         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
100
101         return empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
102 }
103
104 void
105 empathy_contact_factory_set_alias (EmpathyContactFactory *factory,
106                                    EmpathyContact        *contact,
107                                    const gchar           *alias)
108 {
109         EmpathyTpContactFactory *tp_factory;
110         McAccount               *account;
111
112         account = empathy_contact_get_account (contact);
113         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
114
115         return empathy_tp_contact_factory_set_alias (tp_factory, contact, alias);
116 }
117
118 void
119 empathy_contact_factory_set_avatar (EmpathyContactFactory *factory,
120                                     McAccount             *account,
121                                     const gchar           *data,
122                                     gsize                  size,
123                                     const gchar           *mime_type)
124 {
125         EmpathyTpContactFactory *tp_factory;
126
127         tp_factory = empathy_contact_factory_get_tp_factory (factory, account);
128
129         return empathy_tp_contact_factory_set_avatar (tp_factory,
130                                                       data, size, mime_type);
131 }
132
133 static void
134 contact_factory_finalize (GObject *object)
135 {
136         EmpathyContactFactoryPriv *priv = GET_PRIV (object);
137
138         g_hash_table_destroy (priv->accounts);
139
140         G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
141 }
142
143 static void
144 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
145 {
146         GObjectClass *object_class = G_OBJECT_CLASS (klass);
147
148         object_class->finalize = contact_factory_finalize;
149
150         g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
151 }
152
153 static void
154 empathy_contact_factory_init (EmpathyContactFactory *factory)
155 {
156         EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
157
158         priv->accounts = g_hash_table_new_full (empathy_account_hash,
159                                                 empathy_account_equal,
160                                                 g_object_unref,
161                                                 g_object_unref);
162 }
163
164 EmpathyContactFactory *
165 empathy_contact_factory_new (void)
166 {
167         static EmpathyContactFactory *factory = NULL;
168
169         if (!factory) {
170                 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
171                 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
172         } else {
173                 g_object_ref (factory);
174         }
175
176         return factory;
177 }
178