]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contact-factory.c
Import tools from telepathy-glib 0.7.3 and build a static libemp-extensions.la.
[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 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-tp-contact-factory.h"
26 #include "empathy-utils.h"
27
28 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
29                        EMPATHY_TYPE_CONTACT_FACTORY, EmpathyContactFactoryPriv))
30
31 struct _EmpathyContactFactoryPriv {
32         GHashTable *accounts;
33 };
34
35 static void empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass);
36 static void empathy_contact_factory_init       (EmpathyContactFactory      *factory);
37
38 G_DEFINE_TYPE (EmpathyContactFactory, empathy_contact_factory, G_TYPE_OBJECT);
39
40 static EmpathyTpContactFactory *
41 contact_factory_get_tp_factory (EmpathyContactFactory *factory,
42                                 McAccount             *account)
43 {
44         EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
45         EmpathyTpContactFactory   *tp_factory;
46
47         tp_factory = g_hash_table_lookup (priv->accounts, account);
48         if (!tp_factory) {
49                 tp_factory = empathy_tp_contact_factory_new (account);
50                 g_hash_table_insert (priv->accounts,
51                                      g_object_ref (account),
52                                      tp_factory);
53         }
54
55         return tp_factory;
56 }
57
58 EmpathyContact *
59 empathy_contact_factory_get_user (EmpathyContactFactory *factory,
60                                   McAccount             *account)
61 {
62         EmpathyTpContactFactory *tp_factory;
63
64         tp_factory = contact_factory_get_tp_factory (factory, account);
65
66         return empathy_tp_contact_factory_get_user (tp_factory);
67 }
68
69 EmpathyContact *
70 empathy_contact_factory_get_from_id (EmpathyContactFactory *factory,
71                                      McAccount             *account,
72                                      const gchar           *id)
73 {
74         EmpathyTpContactFactory *tp_factory;
75
76         tp_factory = contact_factory_get_tp_factory (factory, account);
77
78         return empathy_tp_contact_factory_get_from_id (tp_factory, id);
79 }
80
81 EmpathyContact *
82 empathy_contact_factory_get_from_handle (EmpathyContactFactory *factory,
83                                          McAccount             *account,
84                                          guint                  handle)
85 {
86         EmpathyTpContactFactory *tp_factory;
87
88         tp_factory = contact_factory_get_tp_factory (factory, account);
89
90         return empathy_tp_contact_factory_get_from_handle (tp_factory, handle);
91 }
92
93 GList *
94 empathy_contact_factory_get_from_handles (EmpathyContactFactory *factory,
95                                           McAccount             *account,
96                                           GArray                *handles)
97 {
98         EmpathyTpContactFactory *tp_factory;
99
100         tp_factory = contact_factory_get_tp_factory (factory, account);
101
102         return empathy_tp_contact_factory_get_from_handles (tp_factory, handles);
103 }
104
105 void
106 empathy_contact_factory_set_alias (EmpathyContactFactory *factory,
107                                    EmpathyContact        *contact,
108                                    const gchar           *alias)
109 {
110         EmpathyTpContactFactory *tp_factory;
111         McAccount               *account;
112
113         account = empathy_contact_get_account (contact);
114         tp_factory = contact_factory_get_tp_factory (factory, account);
115
116         return empathy_tp_contact_factory_set_alias (tp_factory, contact, alias);
117 }
118
119 void
120 empathy_contact_factory_set_avatar (EmpathyContactFactory *factory,
121                                     McAccount             *account,
122                                     const gchar           *data,
123                                     gsize                  size,
124                                     const gchar           *mime_type)
125 {
126         EmpathyTpContactFactory *tp_factory;
127
128         tp_factory = contact_factory_get_tp_factory (factory, account);
129
130         return empathy_tp_contact_factory_set_avatar (tp_factory,
131                                                       data, size, mime_type);
132 }
133
134 static void
135 contact_factory_finalize (GObject *object)
136 {
137         EmpathyContactFactoryPriv *priv = GET_PRIV (object);
138
139         g_hash_table_destroy (priv->accounts);
140
141         G_OBJECT_CLASS (empathy_contact_factory_parent_class)->finalize (object);
142 }
143
144 static void
145 empathy_contact_factory_class_init (EmpathyContactFactoryClass *klass)
146 {
147         GObjectClass *object_class = G_OBJECT_CLASS (klass);
148
149         object_class->finalize = contact_factory_finalize;
150
151         g_type_class_add_private (object_class, sizeof (EmpathyContactFactoryPriv));
152 }
153
154 static void
155 empathy_contact_factory_init (EmpathyContactFactory *factory)
156 {
157         EmpathyContactFactoryPriv *priv = GET_PRIV (factory);
158
159         priv->accounts = g_hash_table_new_full (empathy_account_hash,
160                                                 empathy_account_equal,
161                                                 g_object_unref,
162                                                 g_object_unref);
163 }
164
165 EmpathyContactFactory *
166 empathy_contact_factory_new (void)
167 {
168         static EmpathyContactFactory *factory = NULL;
169
170         if (!factory) {
171                 factory = g_object_new (EMPATHY_TYPE_CONTACT_FACTORY, NULL);
172                 g_object_add_weak_pointer (G_OBJECT (factory), (gpointer) &factory);
173         } else {
174                 g_object_ref (factory);
175         }
176
177         return factory;
178 }
179