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