]> git.0d.be Git - empathy.git/blob - tp-account-widgets/tpaw-protocol.c
Use the telepathy-account-widgets submodule instead of the internal copy
[empathy.git] / tp-account-widgets / tpaw-protocol.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 /*
3  * Copyright (C) 2007-2013 Collabora Ltd.
4  * Copyright (C) 2013 Intel Corporation
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
22  *          Marco Barisione <marco.barisione@collabora.co.uk>
23  */
24
25 #include "config.h"
26 #include "tpaw-protocol.h"
27
28 #include <glib/gi18n-lib.h>
29 #include <tp-account-widgets/tpaw-connection-managers.h>
30 #include <tp-account-widgets/tpaw-utils.h>
31
32 struct _TpawProtocolPriv
33 {
34   TpConnectionManager *cm;
35   gchar *protocol_name;
36   gchar *service_name;
37   gchar *display_name;
38   gchar *icon_name;
39 };
40
41 enum {
42   PROP_CM = 1,
43   PROP_CM_NAME,
44   PROP_PROTOCOL_NAME,
45   PROP_SERVICE_NAME,
46   PROP_DISPLAY_NAME,
47   PROP_ICON_NAME,
48 };
49
50 G_DEFINE_TYPE (TpawProtocol, tpaw_protocol, G_TYPE_OBJECT);
51
52 TpawAccountSettings *
53 tpaw_protocol_create_account_settings (TpawProtocol *self)
54 {
55   TpawAccountSettings *settings = NULL;
56   gchar *str;
57
58   /* Create account */
59   /* To translator: %s is the name of the protocol, such as "Google Talk" or
60    * "Yahoo!"
61    */
62   str = g_strdup_printf (_("New %s account"), self->priv->display_name);
63
64   settings = tpaw_account_settings_new (tpaw_protocol_get_cm_name (self),
65       self->priv->protocol_name,
66       self->priv->service_name,
67       str);
68
69   g_free (str);
70
71   if (!tp_strdiff (self->priv->service_name, "google-talk"))
72     {
73       const gchar *fallback_servers[] = {
74           "talkx.l.google.com",
75           "talkx.l.google.com:443,oldssl",
76           "talkx.l.google.com:80",
77           NULL};
78
79       const gchar *extra_certificate_identities[] = {
80           "talk.google.com",
81           NULL};
82
83       tpaw_account_settings_set_icon_name_async (settings, "im-google-talk",
84           NULL, NULL);
85       tpaw_account_settings_set (settings, "server",
86           g_variant_new_string (extra_certificate_identities[0]));
87       tpaw_account_settings_set (settings, "require-encryption",
88           g_variant_new_boolean (TRUE));
89       tpaw_account_settings_set (settings, "fallback-servers",
90           g_variant_new_strv (fallback_servers, -1));
91
92       if (tpaw_account_settings_have_tp_param (settings,
93               "extra-certificate-identities"))
94         {
95           tpaw_account_settings_set (settings,
96               "extra-certificate-identities",
97               g_variant_new_strv (extra_certificate_identities, -1));
98         }
99     }
100   else if (!tp_strdiff (self->priv->service_name, "facebook"))
101     {
102       const gchar *fallback_servers[] = {
103           "chat.facebook.com:443",
104           NULL };
105
106       tpaw_account_settings_set_icon_name_async (settings, "im-facebook",
107           NULL, NULL);
108       tpaw_account_settings_set (settings, "require-encryption",
109           g_variant_new_boolean (TRUE));
110       tpaw_account_settings_set (settings, "server",
111           g_variant_new_string ("chat.facebook.com"));
112       tpaw_account_settings_set (settings, "fallback-servers",
113           g_variant_new_strv (fallback_servers, -1));
114     }
115
116   return settings;
117 }
118
119 TpConnectionManager *
120 tpaw_protocol_get_cm (TpawProtocol *self)
121 {
122   return self->priv->cm;
123 }
124
125 const gchar *
126 tpaw_protocol_get_cm_name (TpawProtocol *self)
127 {
128   return tp_connection_manager_get_name (self->priv->cm);
129 }
130
131 const gchar *
132 tpaw_protocol_get_protocol_name (TpawProtocol *self)
133 {
134   return self->priv->protocol_name;
135 }
136
137 const gchar *
138 tpaw_protocol_get_service_name (TpawProtocol *self)
139 {
140   return self->priv->service_name;
141 }
142
143 const gchar *
144 tpaw_protocol_get_display_name (TpawProtocol *self)
145 {
146   return self->priv->display_name;
147 }
148
149 const gchar *
150 tpaw_protocol_get_icon_name (TpawProtocol *self)
151 {
152   return self->priv->icon_name;
153 }
154
155 static void
156 tpaw_protocol_get_property (GObject *object,
157     guint prop_id,
158     GValue *value,
159     GParamSpec *pspec)
160 {
161   TpawProtocol *self = TPAW_PROTOCOL (object);
162
163   switch (prop_id)
164     {
165     case PROP_CM:
166       g_value_set_object (value, self->priv->cm);
167       break;
168     case PROP_CM_NAME:
169       g_value_set_string (value,
170           tp_connection_manager_get_name (self->priv->cm));
171       break;
172     case PROP_PROTOCOL_NAME:
173       g_value_set_string (value, self->priv->protocol_name);
174       break;
175     case PROP_SERVICE_NAME:
176       g_value_set_string (value, self->priv->service_name);
177       break;
178     case PROP_DISPLAY_NAME:
179       g_value_set_string (value, self->priv->display_name);
180       break;
181     case PROP_ICON_NAME:
182       g_value_set_string (value, self->priv->icon_name);
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186     }
187 }
188
189 static void
190 tpaw_protocol_set_property (GObject *object,
191     guint prop_id,
192     const GValue *value,
193     GParamSpec *pspec)
194 {
195   TpawProtocol *self = TPAW_PROTOCOL (object);
196
197   switch (prop_id)
198     {
199     case PROP_CM:
200       self->priv->cm = g_value_dup_object (value);
201       break;
202     case PROP_PROTOCOL_NAME:
203       self->priv->protocol_name = g_value_dup_string (value);
204       break;
205     case PROP_SERVICE_NAME:
206       self->priv->service_name = g_value_dup_string (value);
207       break;
208     case PROP_DISPLAY_NAME:
209       self->priv->display_name = g_value_dup_string (value);
210       break;
211     case PROP_ICON_NAME:
212       self->priv->icon_name = g_value_dup_string (value);
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216     }
217 }
218
219 static void
220 tpaw_protocol_constructed (GObject *object)
221 {
222   TpawProtocol *self = TPAW_PROTOCOL (object);
223
224   if (G_OBJECT_CLASS (tpaw_protocol_parent_class)->constructed != NULL)
225     G_OBJECT_CLASS (tpaw_protocol_parent_class)->constructed (object);
226
227   if (g_strcmp0 (self->priv->protocol_name, self->priv->service_name) == 0)
228     {
229       /* We want the service name only if it's different from the
230        * protocol name */
231       g_clear_pointer (&self->priv->service_name, g_free);
232     }
233 }
234
235 static void
236 tpaw_protocol_init (TpawProtocol *self)
237 {
238   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TPAW_TYPE_PROTOCOL,
239       TpawProtocolPriv);
240 }
241
242 static void
243 tpaw_protocol_finalize (GObject *object)
244 {
245   TpawProtocol *self = TPAW_PROTOCOL (object);
246
247   g_clear_object (&self->priv->cm);
248   g_free (self->priv->protocol_name);
249   g_free (self->priv->display_name);
250   g_free (self->priv->icon_name);
251
252   (G_OBJECT_CLASS (tpaw_protocol_parent_class)->finalize) (object);
253 }
254
255 static void
256 tpaw_protocol_class_init (TpawProtocolClass *klass)
257 {
258   GObjectClass *oclass = G_OBJECT_CLASS (klass);
259   GParamSpec *param_spec;
260
261   oclass->finalize = tpaw_protocol_finalize;
262   oclass->constructed = tpaw_protocol_constructed;
263   oclass->get_property = tpaw_protocol_get_property;
264   oclass->set_property = tpaw_protocol_set_property;
265
266   g_type_class_add_private (oclass, sizeof (TpawProtocolPriv));
267
268   param_spec = g_param_spec_object ("cm",
269       "CM", "The connection manager",
270       TP_TYPE_CONNECTION_MANAGER,
271       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
272   g_object_class_install_property (oclass, PROP_CM, param_spec);
273
274   param_spec = g_param_spec_string ("cm-name",
275       "CM name", "The connection manager name",
276       NULL,
277       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
278   g_object_class_install_property (oclass, PROP_CM_NAME, param_spec);
279
280   param_spec = g_param_spec_string ("protocol-name",
281       "Protocol name", "The name of the protocol",
282       NULL,
283       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
284   g_object_class_install_property (oclass, PROP_PROTOCOL_NAME, param_spec);
285
286   param_spec = g_param_spec_string ("service-name",
287       "Service name", "The name of the service",
288       NULL,
289       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
290   g_object_class_install_property (oclass, PROP_SERVICE_NAME, param_spec);
291
292   param_spec = g_param_spec_string ("display-name",
293       "Display name", "The human-readable name of the protocol",
294       NULL,
295       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
296   g_object_class_install_property (oclass, PROP_DISPLAY_NAME, param_spec);
297
298   param_spec = g_param_spec_string ("icon-name",
299       "Icon name", "The name of the icon for the protocol",
300       NULL,
301       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
302   g_object_class_install_property (oclass, PROP_ICON_NAME, param_spec);
303 }
304
305 typedef struct
306 {
307   GSimpleAsyncResult *result;
308   GList *protocols; /* List of (owned) TpawProtocol* */
309   GHashTable *seen_protocols; /* Table of (owned) protocol names -> (owned) cm names */
310 } GetProtocolsData;
311
312 static void
313 add_protocol (GetProtocolsData *data,
314     TpConnectionManager *cm,
315     const gchar *protocol_name,
316     const gchar *service_name,
317     const gchar *display_name,
318     const gchar *icon_name)
319 {
320   TpawProtocol *protocol;
321
322   protocol = g_object_new (TPAW_TYPE_PROTOCOL,
323       "cm", cm,
324       "protocol-name", protocol_name,
325       "service-name", service_name,
326       "display-name", display_name,
327       "icon-name", icon_name,
328       NULL);
329   data->protocols = g_list_prepend (data->protocols, protocol);
330 }
331
332 static gint
333 compare_protocol_to_name (TpawProtocol *protocol,
334     const gchar *proto_name)
335 {
336   return g_strcmp0 (tpaw_protocol_get_protocol_name (protocol), proto_name);
337 }
338
339 static void
340 add_cm (GetProtocolsData *data,
341     TpConnectionManager *cm)
342 {
343   GList *protocols, *l;
344   const gchar *cm_name;
345
346   cm_name = tp_connection_manager_get_name (cm);
347   protocols = tp_connection_manager_dup_protocols (cm);
348
349   for (l = protocols; l != NULL; l = l->next)
350     {
351       TpProtocol *tp_protocol = l->data;
352       gchar *icon_name;
353       const gchar *display_name;
354       const gchar *proto_name;
355       const gchar *saved_cm_name;
356
357       proto_name = tp_protocol_get_name (tp_protocol);
358       saved_cm_name = g_hash_table_lookup (data->seen_protocols, proto_name);
359
360       if (!tp_strdiff (cm_name, "haze") && saved_cm_name != NULL &&
361           tp_strdiff (saved_cm_name, "haze"))
362         /* the CM we're adding is a haze implementation of something we already
363          * have; drop it. */
364         continue;
365
366       if (!tp_strdiff (cm_name, "haze") &&
367           !tp_strdiff (proto_name, "facebook"))
368         /* Facebook now supports XMPP so drop the purple facebook plugin; user
369          * should use Gabble */
370         continue;
371
372       if (!tp_strdiff (cm_name, "haze") &&
373           !tp_strdiff (proto_name, "sip"))
374         /* Haze's SIP implementation is pretty useless (bgo #629736) */
375         continue;
376
377       if (!tp_strdiff (cm_name, "butterfly"))
378         /* Butterfly isn't supported any more */
379         continue;
380
381       if (tp_strdiff (cm_name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
382         {
383           /* Let this CM replace the haze implementation */
384           GList *existing = g_list_find_custom (data->protocols, proto_name,
385               (GCompareFunc) compare_protocol_to_name);
386           g_assert (existing);
387           g_object_unref (existing->data);
388           data->protocols = g_list_delete_link (data->protocols, existing);
389         }
390
391       g_hash_table_replace (data->seen_protocols,
392           g_strdup (proto_name), g_strdup (cm_name));
393
394       display_name = tpaw_protocol_name_to_display_name (proto_name);
395       icon_name = tpaw_protocol_icon_name (proto_name);
396
397       add_protocol (data, cm, proto_name, proto_name, display_name,
398           icon_name);
399
400       if (!tp_strdiff (proto_name, "jabber") &&
401           !tp_strdiff (cm_name, "gabble"))
402         {
403           add_protocol (data, cm, proto_name, "google-talk",
404               tpaw_service_name_to_display_name ("google-talk"),
405               "im-google-talk");
406
407           add_protocol (data, cm, proto_name, "facebook",
408               tpaw_service_name_to_display_name ("facebook"),
409               "im-facebook");
410         }
411
412       g_free (icon_name);
413     }
414
415   g_list_free_full (protocols, g_object_unref);
416 }
417
418 static gint
419 sort_protocol_value (const gchar *protocol_name)
420 {
421   guint i;
422   const gchar *names[] = {
423     "jabber",
424     "local-xmpp",
425     "gtalk",
426     NULL
427   };
428
429   for (i = 0 ; names[i]; i++)
430     {
431       if (g_strcmp0 (protocol_name, names[i]) == 0)
432         return i;
433     }
434
435   return i;
436 }
437
438 static gint
439 protocol_sort_func (TpawProtocol *proto_a,
440     TpawProtocol *proto_b)
441 {
442   const gchar *name_a = tpaw_protocol_get_protocol_name (proto_a);
443   const gchar *name_b = tpaw_protocol_get_protocol_name (proto_b);
444   gint cmp = 0;
445
446   cmp = sort_protocol_value (name_a);
447   cmp -= sort_protocol_value (name_b);
448   if (cmp == 0)
449     {
450       cmp = g_strcmp0 (name_a, name_b);
451       /* only happens for jabber where there is one entry for gtalk and one for
452        * non-gtalk */
453       if (cmp == 0)
454         {
455           const gchar *service = tpaw_protocol_get_service_name (proto_a);
456
457           if (service != NULL)
458             cmp = 1;
459           else
460             cmp = -1;
461         }
462     }
463
464   return cmp;
465 }
466
467 static void
468 cms_prepare_cb (GObject *source,
469     GAsyncResult *result,
470     gpointer user_data)
471 {
472   TpawConnectionManagers *cms = TPAW_CONNECTION_MANAGERS (source);
473   GetProtocolsData *data = user_data;
474   GList *l = NULL;
475   GError *error = NULL;
476
477   if (!tpaw_connection_managers_prepare_finish (cms, result, &error))
478     {
479       g_simple_async_result_take_error (data->result, error);
480       g_simple_async_result_complete_in_idle (data->result);
481       return;
482     }
483
484   for (l = tpaw_connection_managers_get_cms (cms); l != NULL; l = l->next)
485     add_cm (data, l->data);
486
487   data->protocols = g_list_sort (data->protocols,
488       (GCompareFunc) protocol_sort_func);
489
490   g_simple_async_result_complete_in_idle (data->result);
491 }
492
493 static void
494 destroy_get_protocols_data (GetProtocolsData *data)
495 {
496   g_object_unref (data->result);
497   g_hash_table_unref (data->seen_protocols);
498   g_list_free_full (data->protocols, g_object_unref);
499   g_slice_free (GetProtocolsData, data);
500 }
501
502 void
503 tpaw_protocol_get_all_async (GAsyncReadyCallback callback,
504     gpointer user_data)
505 {
506   GetProtocolsData *data;
507   TpawConnectionManagers *cms;
508
509   data = g_slice_new0 (GetProtocolsData);
510   data->result = g_simple_async_result_new (NULL, callback, user_data,
511       tpaw_protocol_get_all_async);
512   g_simple_async_result_set_op_res_gpointer (data->result, data,
513       (GDestroyNotify) destroy_get_protocols_data);
514   data->seen_protocols = g_hash_table_new_full (g_str_hash, g_str_equal,
515       g_free, g_free);
516
517   cms = tpaw_connection_managers_dup_singleton ();
518   tpaw_connection_managers_prepare_async (cms,
519       cms_prepare_cb, data);
520   g_object_unref (cms);
521 }
522
523 gboolean
524 tpaw_protocol_get_all_finish (GList **out_protocols,
525     GAsyncResult *result,
526     GError **error)
527 {
528   GSimpleAsyncResult *simple = (GSimpleAsyncResult *) result;
529   GetProtocolsData *data;
530
531   g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
532         tpaw_protocol_get_all_async), FALSE);
533
534   if (g_simple_async_result_propagate_error (simple, error))
535     return FALSE;
536
537   if (out_protocols != NULL)
538     {
539       data = g_simple_async_result_get_op_res_gpointer (simple);
540       *out_protocols = g_list_copy_deep (data->protocols, (GCopyFunc) g_object_ref, NULL);
541     }
542
543   return TRUE;
544 }