1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
3 * Copyright (C) 2007-2013 Collabora Ltd.
4 * Copyright (C) 2013 Intel Corporation
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.
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.
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
20 * Authors: Xavier Claessens <xclaesse@gmail.com>
21 * Jonny Lamb <jonny.lamb@collabora.co.uk>
22 * Marco Barisione <marco.barisione@collabora.co.uk>
26 #include "tpaw-protocol.h"
28 #include <glib/gi18n-lib.h>
29 #include <tp-account-widgets/tpaw-connection-managers.h>
30 #include <tp-account-widgets/tpaw-utils.h>
32 struct _TpawProtocolPriv
34 TpConnectionManager *cm;
50 G_DEFINE_TYPE (TpawProtocol, tpaw_protocol, G_TYPE_OBJECT);
53 tpaw_protocol_create_account_settings (TpawProtocol *self)
55 TpawAccountSettings *settings = NULL;
59 /* To translator: %s is the name of the protocol, such as "Google Talk" or
62 str = g_strdup_printf (_("New %s account"), self->priv->display_name);
64 settings = tpaw_account_settings_new (tpaw_protocol_get_cm_name (self),
65 self->priv->protocol_name,
66 self->priv->service_name,
71 if (!tp_strdiff (self->priv->service_name, "google-talk"))
73 const gchar *fallback_servers[] = {
75 "talkx.l.google.com:443,oldssl",
76 "talkx.l.google.com:80",
79 const gchar *extra_certificate_identities[] = {
83 tpaw_account_settings_set_icon_name_async (settings, "im-google-talk",
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));
92 if (tpaw_account_settings_have_tp_param (settings,
93 "extra-certificate-identities"))
95 tpaw_account_settings_set (settings,
96 "extra-certificate-identities",
97 g_variant_new_strv (extra_certificate_identities, -1));
100 else if (!tp_strdiff (self->priv->service_name, "facebook"))
102 const gchar *fallback_servers[] = {
103 "chat.facebook.com:443",
106 tpaw_account_settings_set_icon_name_async (settings, "im-facebook",
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));
119 TpConnectionManager *
120 tpaw_protocol_get_cm (TpawProtocol *self)
122 return self->priv->cm;
126 tpaw_protocol_get_cm_name (TpawProtocol *self)
128 return tp_connection_manager_get_name (self->priv->cm);
132 tpaw_protocol_get_protocol_name (TpawProtocol *self)
134 return self->priv->protocol_name;
138 tpaw_protocol_get_service_name (TpawProtocol *self)
140 return self->priv->service_name;
144 tpaw_protocol_get_display_name (TpawProtocol *self)
146 return self->priv->display_name;
150 tpaw_protocol_get_icon_name (TpawProtocol *self)
152 return self->priv->icon_name;
156 tpaw_protocol_get_property (GObject *object,
161 TpawProtocol *self = TPAW_PROTOCOL (object);
166 g_value_set_object (value, self->priv->cm);
169 g_value_set_string (value,
170 tp_connection_manager_get_name (self->priv->cm));
172 case PROP_PROTOCOL_NAME:
173 g_value_set_string (value, self->priv->protocol_name);
175 case PROP_SERVICE_NAME:
176 g_value_set_string (value, self->priv->service_name);
178 case PROP_DISPLAY_NAME:
179 g_value_set_string (value, self->priv->display_name);
182 g_value_set_string (value, self->priv->icon_name);
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 tpaw_protocol_set_property (GObject *object,
195 TpawProtocol *self = TPAW_PROTOCOL (object);
200 self->priv->cm = g_value_dup_object (value);
202 case PROP_PROTOCOL_NAME:
203 self->priv->protocol_name = g_value_dup_string (value);
205 case PROP_SERVICE_NAME:
206 self->priv->service_name = g_value_dup_string (value);
208 case PROP_DISPLAY_NAME:
209 self->priv->display_name = g_value_dup_string (value);
212 self->priv->icon_name = g_value_dup_string (value);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 tpaw_protocol_constructed (GObject *object)
222 TpawProtocol *self = TPAW_PROTOCOL (object);
224 if (G_OBJECT_CLASS (tpaw_protocol_parent_class)->constructed != NULL)
225 G_OBJECT_CLASS (tpaw_protocol_parent_class)->constructed (object);
227 if (g_strcmp0 (self->priv->protocol_name, self->priv->service_name) == 0)
229 /* We want the service name only if it's different from the
231 g_clear_pointer (&self->priv->service_name, g_free);
236 tpaw_protocol_init (TpawProtocol *self)
238 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TPAW_TYPE_PROTOCOL,
243 tpaw_protocol_finalize (GObject *object)
245 TpawProtocol *self = TPAW_PROTOCOL (object);
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);
252 (G_OBJECT_CLASS (tpaw_protocol_parent_class)->finalize) (object);
256 tpaw_protocol_class_init (TpawProtocolClass *klass)
258 GObjectClass *oclass = G_OBJECT_CLASS (klass);
259 GParamSpec *param_spec;
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;
266 g_type_class_add_private (oclass, sizeof (TpawProtocolPriv));
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);
274 param_spec = g_param_spec_string ("cm-name",
275 "CM name", "The connection manager name",
277 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
278 g_object_class_install_property (oclass, PROP_CM_NAME, param_spec);
280 param_spec = g_param_spec_string ("protocol-name",
281 "Protocol name", "The name of the protocol",
283 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
284 g_object_class_install_property (oclass, PROP_PROTOCOL_NAME, param_spec);
286 param_spec = g_param_spec_string ("service-name",
287 "Service name", "The name of the service",
289 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
290 g_object_class_install_property (oclass, PROP_SERVICE_NAME, param_spec);
292 param_spec = g_param_spec_string ("display-name",
293 "Display name", "The human-readable name of the protocol",
295 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
296 g_object_class_install_property (oclass, PROP_DISPLAY_NAME, param_spec);
298 param_spec = g_param_spec_string ("icon-name",
299 "Icon name", "The name of the icon for the protocol",
301 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
302 g_object_class_install_property (oclass, PROP_ICON_NAME, param_spec);
307 GSimpleAsyncResult *result;
308 GList *protocols; /* List of (owned) TpawProtocol* */
309 GHashTable *seen_protocols; /* Table of (owned) protocol names -> (owned) cm names */
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)
320 TpawProtocol *protocol;
322 protocol = g_object_new (TPAW_TYPE_PROTOCOL,
324 "protocol-name", protocol_name,
325 "service-name", service_name,
326 "display-name", display_name,
327 "icon-name", icon_name,
329 data->protocols = g_list_prepend (data->protocols, protocol);
333 compare_protocol_to_name (TpawProtocol *protocol,
334 const gchar *proto_name)
336 return g_strcmp0 (tpaw_protocol_get_protocol_name (protocol), proto_name);
340 add_cm (GetProtocolsData *data,
341 TpConnectionManager *cm)
343 GList *protocols, *l;
344 const gchar *cm_name;
346 cm_name = tp_connection_manager_get_name (cm);
347 protocols = tp_connection_manager_dup_protocols (cm);
349 for (l = protocols; l != NULL; l = l->next)
351 TpProtocol *tp_protocol = l->data;
353 const gchar *display_name;
354 const gchar *proto_name;
355 const gchar *saved_cm_name;
357 proto_name = tp_protocol_get_name (tp_protocol);
358 saved_cm_name = g_hash_table_lookup (data->seen_protocols, proto_name);
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
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 */
372 if (!tp_strdiff (cm_name, "haze") &&
373 !tp_strdiff (proto_name, "sip"))
374 /* Haze's SIP implementation is pretty useless (bgo #629736) */
377 if (!tp_strdiff (cm_name, "butterfly"))
378 /* Butterfly isn't supported any more */
381 if (tp_strdiff (cm_name, "haze") && !tp_strdiff (saved_cm_name, "haze"))
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);
387 g_object_unref (existing->data);
388 data->protocols = g_list_delete_link (data->protocols, existing);
391 g_hash_table_replace (data->seen_protocols,
392 g_strdup (proto_name), g_strdup (cm_name));
394 display_name = tpaw_protocol_name_to_display_name (proto_name);
395 icon_name = tpaw_protocol_icon_name (proto_name);
397 add_protocol (data, cm, proto_name, proto_name, display_name,
400 if (!tp_strdiff (proto_name, "jabber") &&
401 !tp_strdiff (cm_name, "gabble"))
403 add_protocol (data, cm, proto_name, "google-talk",
404 tpaw_service_name_to_display_name ("google-talk"),
407 add_protocol (data, cm, proto_name, "facebook",
408 tpaw_service_name_to_display_name ("facebook"),
415 g_list_free_full (protocols, g_object_unref);
419 sort_protocol_value (const gchar *protocol_name)
422 const gchar *names[] = {
429 for (i = 0 ; names[i]; i++)
431 if (g_strcmp0 (protocol_name, names[i]) == 0)
439 protocol_sort_func (TpawProtocol *proto_a,
440 TpawProtocol *proto_b)
442 const gchar *name_a = tpaw_protocol_get_protocol_name (proto_a);
443 const gchar *name_b = tpaw_protocol_get_protocol_name (proto_b);
446 cmp = sort_protocol_value (name_a);
447 cmp -= sort_protocol_value (name_b);
450 cmp = g_strcmp0 (name_a, name_b);
451 /* only happens for jabber where there is one entry for gtalk and one for
455 const gchar *service = tpaw_protocol_get_service_name (proto_a);
468 cms_prepare_cb (GObject *source,
469 GAsyncResult *result,
472 TpawConnectionManagers *cms = TPAW_CONNECTION_MANAGERS (source);
473 GetProtocolsData *data = user_data;
475 GError *error = NULL;
477 if (!tpaw_connection_managers_prepare_finish (cms, result, &error))
479 g_simple_async_result_take_error (data->result, error);
480 g_simple_async_result_complete_in_idle (data->result);
484 for (l = tpaw_connection_managers_get_cms (cms); l != NULL; l = l->next)
485 add_cm (data, l->data);
487 data->protocols = g_list_sort (data->protocols,
488 (GCompareFunc) protocol_sort_func);
490 g_simple_async_result_complete_in_idle (data->result);
494 destroy_get_protocols_data (GetProtocolsData *data)
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);
503 tpaw_protocol_get_all_async (GAsyncReadyCallback callback,
506 GetProtocolsData *data;
507 TpawConnectionManagers *cms;
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,
517 cms = tpaw_connection_managers_dup_singleton ();
518 tpaw_connection_managers_prepare_async (cms,
519 cms_prepare_cb, data);
520 g_object_unref (cms);
524 tpaw_protocol_get_all_finish (GList **out_protocols,
525 GAsyncResult *result,
528 GSimpleAsyncResult *simple = (GSimpleAsyncResult *) result;
529 GetProtocolsData *data;
531 g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
532 tpaw_protocol_get_all_async), FALSE);
534 if (g_simple_async_result_propagate_error (simple, error))
537 if (out_protocols != NULL)
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);