]> git.0d.be Git - empathy.git/blob - libempathy/empathy-client-factory.c
License to publish the author's pastebin API developer key in open
[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 #include <telepathy-yell/telepathy-yell.h>
30
31 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
32     TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
33
34 #define chainup ((TpSimpleClientFactoryClass *) \
35     empathy_client_factory_parent_class)
36
37 /* FIXME: move to yell */
38 static TpyCallChannel *
39 call_channel_new_with_factory (TpSimpleClientFactory *factory,
40     TpConnection *conn,
41     const gchar *object_path,
42     const GHashTable *immutable_properties,
43     GError **error)
44 {
45   TpProxy *conn_proxy = (TpProxy *) conn;
46
47   g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
48   g_return_val_if_fail (object_path != NULL, NULL);
49   g_return_val_if_fail (immutable_properties != NULL, NULL);
50
51   if (!tp_dbus_check_valid_object_path (object_path, error))
52     return NULL;
53
54   return g_object_new (TPY_TYPE_CALL_CHANNEL,
55       "factory", factory,
56       "connection", conn,
57       "dbus-daemon", conn_proxy->dbus_daemon,
58       "bus-name", conn_proxy->bus_name,
59       "object-path", object_path,
60       "handle-type", (guint) TP_UNKNOWN_HANDLE_TYPE,
61       "channel-properties", immutable_properties,
62       NULL);
63 }
64
65 static TpChannel *
66 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
67     TpConnection *conn,
68     const gchar *path,
69     const GHashTable *properties,
70     GError **error)
71 {
72   const gchar *chan_type;
73
74   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
75
76   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
77     {
78       TpAccount *account;
79
80       account = tp_connection_get_account (conn);
81
82       return TP_CHANNEL (empathy_tp_chat_new (
83             TP_SIMPLE_CLIENT_FACTORY (factory), account, conn, path,
84             properties));
85     }
86   else if (!tp_strdiff (chan_type, TPY_IFACE_CHANNEL_TYPE_CALL))
87     {
88       return TP_CHANNEL (call_channel_new_with_factory (
89             TP_SIMPLE_CLIENT_FACTORY (factory), conn, path, properties, error));
90     }
91
92   return chainup->create_channel (factory, conn, path, properties, error);
93 }
94
95 static GArray *
96 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
97     TpChannel *channel)
98 {
99   GArray *features;
100   GQuark feature;
101
102   features = chainup->dup_channel_features (factory, channel);
103
104   if (EMPATHY_IS_TP_CHAT (channel))
105     {
106       feature = TP_CHANNEL_FEATURE_CHAT_STATES;
107       g_array_append_val (features, feature);
108
109       feature = EMPATHY_TP_CHAT_FEATURE_READY;
110       g_array_append_val (features, feature);
111     }
112
113   return features;
114 }
115
116 static GArray *
117 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
118     TpAccount *account)
119 {
120   GArray *features;
121   GQuark feature;
122
123   features = chainup->dup_account_features (factory, account);
124
125   feature = TP_ACCOUNT_FEATURE_CONNECTION;
126   g_array_append_val (features, feature);
127
128   feature = TP_ACCOUNT_FEATURE_ADDRESSING;
129   g_array_append_val (features, feature);
130
131   return features;
132 }
133
134 static GArray *
135 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
136     TpConnection *connection)
137 {
138   GArray *features;
139   GQuark feature;
140
141   features = chainup->dup_connection_features (factory, connection);
142
143   feature = TP_CONNECTION_FEATURE_CAPABILITIES;
144   g_array_append_val (features, feature);
145
146   feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
147   g_array_append_val (features, feature);
148
149   feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
150   g_array_append_val (features, feature);
151
152   feature = TP_CONNECTION_FEATURE_BALANCE;
153   g_array_append_val (features, feature);
154
155   feature = TP_CONNECTION_FEATURE_CONTACT_BLOCKING;
156   g_array_append_val (features, feature);
157
158   /* Most empathy-* may allow user to add a contact to his contact list. We
159    * need this property to check if the connection allows it. It's cheap to
160    * prepare anyway as it will just call GetAll() on the ContactList iface. */
161   feature = TP_CONNECTION_FEATURE_CONTACT_LIST_PROPERTIES;
162   g_array_append_val (features, feature);
163
164   return features;
165 }
166
167 static GArray *
168 empathy_client_factory_dup_contact_features (TpSimpleClientFactory *factory,
169         TpConnection *connection)
170 {
171   GArray *features;
172   TpContactFeature extra_features[] = {
173       TP_CONTACT_FEATURE_ALIAS,
174       TP_CONTACT_FEATURE_PRESENCE,
175       TP_CONTACT_FEATURE_AVATAR_TOKEN,
176       TP_CONTACT_FEATURE_AVATAR_DATA,
177       TP_CONTACT_FEATURE_CAPABILITIES,
178       /* Needed by empathy_individual_add_menu_item_new to check if a contact
179        * is already in the contact list. This feature is pretty cheap to
180        * prepare as it doesn't prepare the full roster. */
181       TP_CONTACT_FEATURE_SUBSCRIPTION_STATES,
182   };
183
184   features = chainup->dup_contact_features (factory, connection);
185
186   g_array_append_vals (features, extra_features, G_N_ELEMENTS (extra_features));
187
188   return features;
189 }
190
191 static void
192 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
193 {
194   TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
195
196   simple_class->create_channel = empathy_client_factory_create_channel;
197   simple_class->dup_channel_features =
198     empathy_client_factory_dup_channel_features;
199
200   simple_class->dup_account_features =
201     empathy_client_factory_dup_account_features;
202
203   simple_class->dup_connection_features =
204     empathy_client_factory_dup_connection_features;
205
206   simple_class->dup_contact_features =
207     empathy_client_factory_dup_contact_features;
208 }
209
210 static void
211 empathy_client_factory_init (EmpathyClientFactory *self)
212 {
213 }
214
215 static EmpathyClientFactory *
216 empathy_client_factory_new (TpDBusDaemon *dbus)
217 {
218     return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
219         "dbus-daemon", dbus,
220         NULL);
221 }
222
223 EmpathyClientFactory *
224 empathy_client_factory_dup (void)
225 {
226   static EmpathyClientFactory *singleton = NULL;
227   TpDBusDaemon *dbus;
228   GError *error = NULL;
229
230   if (singleton != NULL)
231     return g_object_ref (singleton);
232
233   dbus = tp_dbus_daemon_dup (&error);
234   if (dbus == NULL)
235     {
236       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
237       g_error_free (error);
238       return NULL;
239     }
240
241   singleton = empathy_client_factory_new (dbus);
242   g_object_unref (dbus);
243
244   g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
245
246   return singleton;
247 }