]> git.0d.be Git - empathy.git/blob - libempathy/empathy-client-factory.c
Revert "contact-blocking-dialog: use tp-glib high level blocking API"
[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-tp-file.h"
28 #include "empathy-utils.h"
29
30 #include <telepathy-yell/telepathy-yell.h>
31
32 G_DEFINE_TYPE (EmpathyClientFactory, empathy_client_factory,
33     TP_TYPE_AUTOMATIC_CLIENT_FACTORY)
34
35 #define chainup ((TpSimpleClientFactoryClass *) \
36     empathy_client_factory_parent_class)
37
38 /* FIXME: move to yell */
39 static TpyCallChannel *
40 call_channel_new_with_factory (TpSimpleClientFactory *factory,
41     TpConnection *conn,
42     const gchar *object_path,
43     const GHashTable *immutable_properties,
44     GError **error)
45 {
46   TpProxy *conn_proxy = (TpProxy *) conn;
47
48   g_return_val_if_fail (TP_IS_CONNECTION (conn), NULL);
49   g_return_val_if_fail (object_path != NULL, NULL);
50   g_return_val_if_fail (immutable_properties != NULL, NULL);
51
52   if (!tp_dbus_check_valid_object_path (object_path, error))
53     return NULL;
54
55   return g_object_new (TPY_TYPE_CALL_CHANNEL,
56       "factory", factory,
57       "connection", conn,
58       "dbus-daemon", conn_proxy->dbus_daemon,
59       "bus-name", conn_proxy->bus_name,
60       "object-path", object_path,
61       "handle-type", (guint) TP_UNKNOWN_HANDLE_TYPE,
62       "channel-properties", immutable_properties,
63       NULL);
64 }
65
66 static TpChannel *
67 empathy_client_factory_create_channel (TpSimpleClientFactory *factory,
68     TpConnection *conn,
69     const gchar *path,
70     const GHashTable *properties,
71     GError **error)
72 {
73   const gchar *chan_type;
74
75   chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);
76
77   if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
78     {
79       TpAccount *account;
80
81       account = tp_connection_get_account (conn);
82
83       return TP_CHANNEL (empathy_tp_chat_new (
84             TP_SIMPLE_CLIENT_FACTORY (factory), account, conn, path,
85             properties));
86     }
87   else if (!tp_strdiff (chan_type, TPY_IFACE_CHANNEL_TYPE_CALL))
88     {
89       return TP_CHANNEL (call_channel_new_with_factory (
90             TP_SIMPLE_CLIENT_FACTORY (factory), conn, path, properties, error));
91     }
92   else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
93     {
94       return TP_CHANNEL (empathy_tp_file_new (
95             TP_SIMPLE_CLIENT_FACTORY (factory), conn, path, properties, error));
96     }
97
98   return chainup->create_channel (factory, conn, path, properties, error);
99 }
100
101 static GArray *
102 empathy_client_factory_dup_channel_features (TpSimpleClientFactory *factory,
103     TpChannel *channel)
104 {
105   GArray *features;
106   GQuark feature;
107
108   features = chainup->dup_channel_features (factory, channel);
109
110   if (EMPATHY_IS_TP_CHAT (channel))
111     {
112       feature = TP_CHANNEL_FEATURE_CHAT_STATES;
113       g_array_append_val (features, feature);
114
115       feature = EMPATHY_TP_CHAT_FEATURE_READY;
116       g_array_append_val (features, feature);
117     }
118
119   return features;
120 }
121
122 static GArray *
123 empathy_client_factory_dup_account_features (TpSimpleClientFactory *factory,
124     TpAccount *account)
125 {
126   GArray *features;
127   GQuark feature;
128
129   features = chainup->dup_account_features (factory, account);
130
131   feature = TP_ACCOUNT_FEATURE_CONNECTION;
132   g_array_append_val (features, feature);
133
134   feature = TP_ACCOUNT_FEATURE_ADDRESSING;
135   g_array_append_val (features, feature);
136
137   return features;
138 }
139
140 static GArray *
141 empathy_client_factory_dup_connection_features (TpSimpleClientFactory *factory,
142     TpConnection *connection)
143 {
144   GArray *features;
145   GQuark feature;
146
147   features = chainup->dup_connection_features (factory, connection);
148
149   feature = TP_CONNECTION_FEATURE_CAPABILITIES;
150   g_array_append_val (features, feature);
151
152   feature = TP_CONNECTION_FEATURE_AVATAR_REQUIREMENTS;
153   g_array_append_val (features, feature);
154
155   feature = TP_CONNECTION_FEATURE_CONTACT_INFO;
156   g_array_append_val (features, feature);
157
158   feature = TP_CONNECTION_FEATURE_BALANCE;
159   g_array_append_val (features, feature);
160
161   return features;
162 }
163
164 static void
165 empathy_client_factory_class_init (EmpathyClientFactoryClass *cls)
166 {
167   TpSimpleClientFactoryClass *simple_class = (TpSimpleClientFactoryClass *) cls;
168
169   simple_class->create_channel = empathy_client_factory_create_channel;
170   simple_class->dup_channel_features =
171     empathy_client_factory_dup_channel_features;
172
173   simple_class->dup_account_features =
174     empathy_client_factory_dup_account_features;
175
176   simple_class->dup_connection_features =
177     empathy_client_factory_dup_connection_features;
178 }
179
180 static void
181 empathy_client_factory_init (EmpathyClientFactory *self)
182 {
183 }
184
185 static EmpathyClientFactory *
186 empathy_client_factory_new (TpDBusDaemon *dbus)
187 {
188     return g_object_new (EMPATHY_TYPE_CLIENT_FACTORY,
189         "dbus-daemon", dbus,
190         NULL);
191 }
192
193 EmpathyClientFactory *
194 empathy_client_factory_dup (void)
195 {
196   static EmpathyClientFactory *singleton = NULL;
197   TpDBusDaemon *dbus;
198   GError *error = NULL;
199
200   if (singleton != NULL)
201     return g_object_ref (singleton);
202
203   dbus = tp_dbus_daemon_dup (&error);
204   if (dbus == NULL)
205     {
206       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
207       g_error_free (error);
208       return NULL;
209     }
210
211   singleton = empathy_client_factory_new (dbus);
212   g_object_unref (dbus);
213
214   g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);
215
216   return singleton;
217 }