]> git.0d.be Git - empathy.git/blob - libempathy/empathy-client-factory.c
Merge remote-tracking branch 'glassrose/debug-window-send-to-pastebin-button-658724'
[empathy.git] / libempathy / empathy-client-factory.c
1 /*
2  * Copyright (C) 2010 Collabora Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA  02110-1301  USA
18  *
19  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
20  */
21
22 #include <config.h>
23
24 #include "empathy-client-factory.h"
25
26 #include "empathy-tp-chat.h"
27 #include "empathy-utils.h"
28
29 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
30     TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
31
32 #define chainup ((TpSimpleClientFactoryClass *) \
33     empathy_client_factory_parent_class)
34
35 static TpChannel *
36 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
37     TpConnection *conn,
38     const gchar *path,
39     const GHashTable *properties,
40     GError **error)
41 {
42   const gchar *chan_type;
43
44   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
45
46   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
47     {
48       TpAccount *account;
49
50       account = tp_connection_get_account (conn);
51
52       return TP_CHANNEL (empathy_tp_chat_new (
53             TP_SIMPLE_CLIENT_FACTORY (factory), account, conn, path,
54             properties));
55     }
56
57   return chainup->create_channel (factory, conn, path, properties, error);
58 }
59
60 static GArray *
61 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
62     TpChannel *channel)
63 {
64   GArray *features;
65   GQuark feature;
66
67   features = chainup->dup_channel_features (factory, channel);
68
69   if (EMPATHY_IS_TP_CHAT (channel))
70     {
71       feature = TP_CHANNEL_FEATURE_CHAT_STATES;
72       g_array_append_val (features, feature);
73
74       feature = EMPATHY_TP_CHAT_FEATURE_READY;
75       g_array_append_val (features, feature);
76     }
77
78   return features;
79 }
80
81 static GArray *
82 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
83     TpAccount *account)
84 {
85   GArray *features;
86   GQuark feature;
87
88   features = chainup->dup_account_features (factory, account);
89
90   feature = TP_ACCOUNT_FEATURE_CONNECTION;
91   g_array_append_val (features, feature);
92
93   feature = TP_ACCOUNT_FEATURE_ADDRESSING;
94   g_array_append_val (features, feature);
95
96   return features;
97 }
98
99 static GArray *
100 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
101     TpConnection *connection)
102 {
103   GArray *features;
104   GQuark feature;
105
106   features = chainup->dup_connection_features (factory, connection);
107
108   feature = TP_CONNECTION_FEATURE_CAPABILITIES;
109   g_array_append_val (features, feature);
110
111   feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
112   g_array_append_val (features, feature);
113
114   feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
115   g_array_append_val (features, feature);
116
117   feature = TP_CONNECTION_FEATURE_BALANCE;
118   g_array_append_val (features, feature);
119
120   feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
121   g_array_append_val (features, feature);
122
123   /* Most empathy-* may allow user to add a contact to his contact list. We
124    * need this property to check if the connection allows it. It's cheap to
125    * prepare anyway as it will just call GetAll() on the ContactList iface. */
126   feature = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
127   g_array_append_val (features, feature);
128
129   return features;
130 }
131
132 static GArray *
133 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
134         TpConnection *connection)
135 {
136   GArray *features;
137   TpContactFeature extra_features[] = {
138       TP_CONTACT_FEATURE_ALIAS,
139       TP_CONTACT_FEATURE_PRESENCE,
140       TP_CONTACT_FEATURE_AVATAR_TOKEN,
141       TP_CONTACT_FEATURE_AVATAR_DATA,
142       TP_CONTACT_FEATURE_CAPABILITIES,
143       /* Needed by empathy_individual_add_menu_item_new to check if a contact
144        * is already in the contact list. This feature is pretty cheap to
145        * prepare as it doesn't prepare the full roster. */
146       TP_CONTACT_FEATURE_SUBSCRIPTION_STATES,
147   };
148
149   features = chainup->dup_contact_features (factory, connection);
150
151   g_array_append_vals (features, extra_features, G_N_ELEMENTS (extra_features));
152
153   return features;
154 }
155
156 static void
157 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
158 {
159   TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
160
161   simple_class->create_channel = empathy_client_factory_create_channel;
162   simple_class->dup_channel_features =
163     empathy_client_factory_dup_channel_features;
164
165   simple_class->dup_account_features =
166     empathy_client_factory_dup_account_features;
167
168   simple_class->dup_connection_features =
169     empathy_client_factory_dup_connection_features;
170
171   simple_class->dup_contact_features =
172     empathy_client_factory_dup_contact_features;
173 }
174
175 static void
176 empathy_client_factory_init (EmpathyClientFactory *self)
177 {
178 }
179
180 static EmpathyClientFactory *
181 empathy_client_factory_new (TpDBusDaemon *dbus)
182 {
183     return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
184         "dbus-daemon", dbus,
185         NULL);
186 }
187
188 EmpathyClientFactory *
189 empathy_client_factory_dup (void)
190 {
191   static EmpathyClientFactory *singleton = NULL;
192   TpDBusDaemon *dbus;
193   GError *error = NULL;
194
195   if (singleton != NULL)
196     return g_object_ref (singleton);
197
198   dbus = tp_dbus_daemon_dup (&error);
199   if (dbus == NULL)
200     {
201       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
202       g_error_free (error);
203       return NULL;
204     }
205
206   singleton = empathy_client_factory_new (dbus);
207   g_object_unref (dbus);
208
209   g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
210
211   return singleton;
212 }