2 * @file telepathy-connection.c
6 * Copyright (C) 2012 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <glib-object.h>
31 #include <glib/gstdio.h>
32 #include <telepathy-glib/base-connection.h>
33 #include <telepathy-glib/base-protocol.h>
34 #include <telepathy-glib/contacts-mixin.h>
35 #include <telepathy-glib/handle-repo-dynamic.h>
36 #include <telepathy-glib/presence-mixin.h>
37 #include <telepathy-glib/simple-password-manager.h>
38 #include <telepathy-glib/telepathy-glib.h>
40 #include "sipe-backend.h"
41 #include "sipe-common.h"
42 #include "sipe-core.h"
44 #include "telepathy-private.h"
48 * Connection class - data structures
50 typedef struct _SipeConnectionClass
{
51 TpBaseConnectionClass parent_class
;
52 TpDBusPropertiesMixinClass properties_mixin
;
53 TpContactsMixinClass contacts_mixin
;
54 TpPresenceMixinClass presence_mixin
;
55 } SipeConnectionClass
;
57 typedef struct _SipeConnection
{
58 TpBaseConnection parent
;
59 TpContactsMixinClass contacts_mixin
;
60 TpPresenceMixin presence_mixin
;
62 /* channel managers */
63 TpSimplePasswordManager
*password_manager
;
64 struct _SipeContactList
*contact_list
;
66 struct sipe_backend_private
private;
73 guint authentication_type
;
75 gchar
*authentication
;
76 gboolean is_disconnecting
;
78 GPtrArray
*contact_info_fields
;
81 #define SIPE_PUBLIC_TO_CONNECTION sipe_public->backend_private->connection
84 * Connection class - type macros
86 static GType
sipe_connection_get_type(void) G_GNUC_CONST
;
87 #define SIPE_TYPE_CONNECTION \
88 (sipe_connection_get_type())
89 #define SIPE_CONNECTION(obj) \
90 (G_TYPE_CHECK_INSTANCE_CAST((obj), SIPE_TYPE_CONNECTION, \
95 * Connection class - type definition
97 static void init_aliasing (gpointer
, gpointer
);
98 G_DEFINE_TYPE_WITH_CODE(SipeConnection
,
100 TP_TYPE_BASE_CONNECTION
,
101 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_ALIASING
,
103 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_AVATARS
,
104 sipe_telepathy_avatars_iface_init
);
105 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACTS
,
106 tp_contacts_mixin_iface_init
);
107 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACT_GROUPS
,
108 tp_base_contact_list_mixin_groups_iface_init
);
109 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACT_INFO
,
110 sipe_telepathy_contact_info_iface_init
);
111 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_CONTACT_LIST
,
112 tp_base_contact_list_mixin_list_iface_init
);
113 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_PRESENCE
,
114 tp_presence_mixin_iface_init
);
115 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_CONNECTION_INTERFACE_SIMPLE_PRESENCE
,
116 tp_presence_mixin_simple_presence_iface_init
);
117 G_IMPLEMENT_INTERFACE(TP_TYPE_SVC_DBUS_PROPERTIES
,
118 tp_dbus_properties_mixin_iface_init
);
123 * Connection class - instance methods
125 static gchar
*normalize_contact(SIPE_UNUSED_PARAMETER TpHandleRepoIface
*repo
,
127 SIPE_UNUSED_PARAMETER gpointer context
,
130 return(sipe_telepathy_protocol_normalize_contact(NULL
, id
, error
));
133 static void create_handle_repos(SIPE_UNUSED_PARAMETER TpBaseConnection
*conn
,
134 TpHandleRepoIface
*repos
[NUM_TP_HANDLE_TYPES
])
136 repos
[TP_HANDLE_TYPE_CONTACT
] = tp_dynamic_handle_repo_new(TP_HANDLE_TYPE_CONTACT
,
141 static gboolean
connect_to_core(SipeConnection
*self
,
144 gchar
*login_domain
= NULL
;
145 gchar
*login_account
= NULL
;
146 struct sipe_core_public
*sipe_public
;
149 /* login name specified? */
150 if (self
->login
&& strlen(self
->login
)) {
151 /* Allowed domain-account separators are / or \ */
152 gchar
**domain_user
= g_strsplit_set(self
->login
, "/\\", 2);
153 gboolean has_domain
= domain_user
[1] != NULL
;
154 SIPE_DEBUG_INFO("connect_to_core: login '%s'", self
->login
);
155 login_domain
= has_domain
? g_strdup(domain_user
[0]) : NULL
;
156 login_account
= g_strdup(domain_user
[has_domain
? 1 : 0]);
157 SIPE_DEBUG_INFO("connect_to_core: auth domain '%s' user '%s'",
158 login_domain
? login_domain
: "",
160 g_strfreev(domain_user
);
163 sipe_public
= sipe_core_allocate(self
->account
,
164 login_domain
, login_account
,
166 NULL
, /* @TODO: email */
167 NULL
, /* @TODO: email_url */
169 g_free(login_domain
);
170 g_free(login_account
);
172 SIPE_DEBUG_INFO("connect_to_core: created %p", sipe_public
);
175 struct sipe_backend_private
*telepathy_private
= &self
->private;
177 /* initialize backend private data */
178 sipe_public
->backend_private
= telepathy_private
;
179 telepathy_private
->public = sipe_public
;
180 telepathy_private
->contact_list
= self
->contact_list
;
181 telepathy_private
->connection
= self
;
182 telepathy_private
->activity
= SIPE_ACTIVITY_UNSET
;
183 telepathy_private
->cache_dir
= g_build_path(G_DIR_SEPARATOR_S
,
184 g_get_user_cache_dir(),
189 telepathy_private
->message
= NULL
;
190 telepathy_private
->transport
= NULL
;
191 telepathy_private
->ipaddress
= NULL
;
193 /* make sure cache directory exists */
194 if (!g_file_test(telepathy_private
->cache_dir
,
195 G_FILE_TEST_IS_DIR
) &&
196 (g_mkdir_with_parents(telepathy_private
->cache_dir
,
197 S_IRWXU
| S_IRGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
)
199 SIPE_DEBUG_INFO("connect_to_core: created cache directory %s",
200 telepathy_private
->cache_dir
);
202 /* @TODO: add parameter for SSO */
203 SIPE_CORE_FLAG_UNSET(SSO
);
205 sipe_core_transport_sip_connect(sipe_public
,
207 self
->authentication_type
,
213 g_set_error_literal(error
, TP_ERROR
, TP_ERROR_INVALID_ARGUMENT
,
219 static void password_manager_cb(GObject
*source
,
220 GAsyncResult
*result
,
223 SipeConnection
*self
= data
;
224 TpBaseConnection
*base
= TP_BASE_CONNECTION(self
);
225 GError
*error
= NULL
;
226 const GString
*password
= tp_simple_password_manager_prompt_finish(
227 TP_SIMPLE_PASSWORD_MANAGER(source
),
231 if (password
== NULL
) {
232 SIPE_DEBUG_ERROR("password_manager_cb: failed: %s",
233 error
? error
->message
: "UNKNOWN");
235 if (base
->status
!= TP_CONNECTION_STATUS_DISCONNECTED
) {
236 tp_base_connection_disconnect_with_dbus_error(base
,
237 error
? tp_error_get_dbus_name(error
->code
) : "",
239 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
);
244 g_free(self
->password
);
245 self
->password
= g_strdup(password
->str
);
247 if (!connect_to_core(self
, &error
)) {
248 if (base
->status
!= TP_CONNECTION_STATUS_DISCONNECTED
) {
249 tp_base_connection_disconnect_with_dbus_error(base
,
250 tp_error_get_dbus_name(error
->code
),
252 TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
);
259 static gboolean
start_connecting(TpBaseConnection
*base
,
262 SipeConnection
*self
= SIPE_CONNECTION(base
);
264 gchar
*uri
= sipe_telepathy_protocol_normalize_contact(NULL
,
268 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting");
270 /* set up mandatory self-handle */
272 base
->self_handle
= tp_handle_ensure(tp_base_connection_get_handles(base
,
273 TP_HANDLE_TYPE_CONTACT
),
278 if (!base
->self_handle
) {
279 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: self handle creation failed: %s",
284 SIPE_DEBUG_ERROR("SipeConnection::start_connecting: %s",
289 tp_base_connection_change_status(base
, TP_CONNECTION_STATUS_CONNECTING
,
290 TP_CONNECTION_STATUS_REASON_REQUESTED
);
292 /* map option list to flags - default is NTLM */
293 self
->authentication_type
= SIPE_AUTHENTICATION_TYPE_NTLM
;
295 if (sipe_strequal(self
->authentication
, "krb5")) {
296 SIPE_DEBUG_INFO_NOFORMAT("start_connecting: KRB5 selected");
297 self
->authentication_type
= SIPE_AUTHENTICATION_TYPE_KERBEROS
;
300 if (sipe_strequal(self
->authentication
, "tls-dsk")) {
301 SIPE_DEBUG_INFO_NOFORMAT("start_connecting: TLS-DSK selected");
302 self
->authentication_type
= SIPE_AUTHENTICATION_TYPE_TLS_DSK
;
305 /* Only ask for a password when required */
306 /* @TODO: add parameter for SSO */
307 if (!sipe_core_transport_sip_requires_password(self
->authentication_type
,
309 (self
->password
&& strlen(self
->password
)))
310 rc
= connect_to_core(self
, error
);
312 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting: requesting password from user");
313 tp_simple_password_manager_prompt_async(self
->password_manager
,
321 static gboolean
disconnect_from_core(gpointer data
)
323 TpBaseConnection
*base
= data
;
324 SipeConnection
*self
= SIPE_CONNECTION(base
);
325 struct sipe_backend_private
*telepathy_private
= &self
->private;
326 struct sipe_core_public
*sipe_public
= telepathy_private
->public;
328 SIPE_DEBUG_INFO("disconnect_from_core: %p", sipe_public
);
331 sipe_core_deallocate(sipe_public
);
332 telepathy_private
->public = NULL
;
333 telepathy_private
->transport
= NULL
;
335 g_free(telepathy_private
->ipaddress
);
336 telepathy_private
->ipaddress
= NULL
;
338 g_free(telepathy_private
->message
);
339 telepathy_private
->message
= NULL
;
341 g_free(telepathy_private
->cache_dir
);
342 telepathy_private
->cache_dir
= NULL
;
344 SIPE_DEBUG_INFO_NOFORMAT("disconnect_from_core: core deallocated");
346 /* now it is OK to destroy the connection object */
347 tp_base_connection_finish_shutdown(base
);
352 static void shut_down(TpBaseConnection
*base
)
354 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::shut_down");
356 /* this can be called synchronously, defer destruction */
357 g_idle_add(disconnect_from_core
, base
);
360 static GPtrArray
*create_channel_managers(TpBaseConnection
*base
)
362 SipeConnection
*self
= SIPE_CONNECTION(base
);
363 GPtrArray
*channel_managers
= g_ptr_array_new();
365 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::create_channel_managers");
367 self
->contact_list
= sipe_telepathy_contact_list_new(base
);
368 g_ptr_array_add(channel_managers
, self
->contact_list
);
370 self
->password_manager
= tp_simple_password_manager_new(base
);
371 g_ptr_array_add(channel_managers
, self
->password_manager
);
373 g_ptr_array_add(channel_managers
, sipe_telepathy_search_new(base
));
375 return(channel_managers
);
378 static void aliasing_fill_contact_attributes(GObject
*object
,
379 const GArray
*contacts
,
380 GHashTable
*attributes
)
382 SipeConnection
*self
= SIPE_CONNECTION(object
);
385 for (i
= 0; i
< contacts
->len
; i
++) {
386 TpHandle contact
= g_array_index(contacts
, guint
, i
);
388 tp_contacts_mixin_set_contact_attribute(attributes
,
390 TP_TOKEN_CONNECTION_INTERFACE_ALIASING_ALIAS
,
391 tp_g_value_slice_new_string(
392 sipe_telepathy_buddy_get_alias(self
->contact_list
,
397 static void avatars_fill_contact_attributes(GObject
*object
,
398 const GArray
*contacts
,
399 GHashTable
*attributes
)
401 SipeConnection
*self
= SIPE_CONNECTION(object
);
404 for (i
= 0; i
< contacts
->len
; i
++) {
405 TpHandle contact
= g_array_index(contacts
, guint
, i
);
406 const gchar
*hash
= sipe_telepathy_buddy_get_hash(self
->contact_list
,
409 if (!hash
) hash
= "";
410 tp_contacts_mixin_set_contact_attribute(attributes
,
412 TP_IFACE_CONNECTION_INTERFACE_AVATARS
"/token",
413 tp_g_value_slice_new_string(hash
));
417 static void contact_info_properties_getter(GObject
*object
,
418 SIPE_UNUSED_PARAMETER GQuark interface
,
421 gpointer getter_data
)
423 GQuark fields
= g_quark_from_static_string("SupportedFields");
426 g_value_set_boxed(value
,
427 SIPE_CONNECTION(object
)->contact_info_fields
);
429 g_value_set_uint(value
,
430 GPOINTER_TO_UINT(getter_data
));
433 static void sipe_connection_constructed(GObject
*object
)
435 SipeConnection
*self
= SIPE_CONNECTION(object
);
436 TpBaseConnection
*base
= TP_BASE_CONNECTION(object
);
437 void (*chain_up
)(GObject
*) = G_OBJECT_CLASS(sipe_connection_parent_class
)->constructed
;
442 tp_contacts_mixin_init(object
,
443 G_STRUCT_OFFSET(SipeConnection
, contacts_mixin
));
444 tp_base_connection_register_with_contacts_mixin(base
);
446 tp_base_contact_list_mixin_register_with_contacts_mixin(base
);
448 tp_contacts_mixin_add_contact_attributes_iface(object
,
449 TP_IFACE_CONNECTION_INTERFACE_ALIASING
,
450 aliasing_fill_contact_attributes
);
451 tp_contacts_mixin_add_contact_attributes_iface(object
,
452 TP_IFACE_CONNECTION_INTERFACE_AVATARS
,
453 avatars_fill_contact_attributes
);
455 tp_presence_mixin_init(object
,
456 G_STRUCT_OFFSET(SipeConnection
,
458 tp_presence_mixin_simple_presence_register_with_contacts_mixin(object
);
460 self
->contact_info_fields
= sipe_telepathy_contact_info_fields();
463 static void sipe_connection_finalize(GObject
*object
)
465 SipeConnection
*self
= SIPE_CONNECTION(object
);
467 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::finalize");
469 tp_contacts_mixin_finalize(object
);
470 tp_presence_mixin_finalize(object
);
471 g_boxed_free(TP_ARRAY_TYPE_FIELD_SPECS
, self
->contact_info_fields
);
473 g_free(self
->authentication
);
474 g_free(self
->user_agent
);
476 g_free(self
->server
);
477 g_free(self
->password
);
479 g_free(self
->account
);
481 G_OBJECT_CLASS(sipe_connection_parent_class
)->finalize(object
);
485 * Connection class - type implementation
487 static const gchar
*interfaces_always_present
[] = {
489 TP_IFACE_CONNECTION_INTERFACE_ALIASING
,
490 TP_IFACE_CONNECTION_INTERFACE_AVATARS
,
491 TP_IFACE_CONNECTION_INTERFACE_CONTACT_GROUPS
,
492 TP_IFACE_CONNECTION_INTERFACE_CONTACT_INFO
,
493 TP_IFACE_CONNECTION_INTERFACE_CONTACT_LIST
,
494 TP_IFACE_CONNECTION_INTERFACE_CONTACTS
,
495 TP_IFACE_CONNECTION_INTERFACE_PRESENCE
,
496 TP_IFACE_CONNECTION_INTERFACE_REQUESTS
,
497 TP_IFACE_CONNECTION_INTERFACE_SIMPLE_PRESENCE
,
501 static void sipe_connection_class_init(SipeConnectionClass
*klass
)
503 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
504 TpBaseConnectionClass
*base_class
= TP_BASE_CONNECTION_CLASS(klass
);
505 static TpDBusPropertiesMixinIfaceImpl prop_interfaces
[] = {
508 .name
= TP_IFACE_CONNECTION_INTERFACE_CONTACT_INFO
,
509 .getter
= contact_info_properties_getter
,
518 /* initalize non-constant fields */
519 prop_interfaces
[0].props
= sipe_telepathy_contact_info_props();
521 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::class_init");
523 object_class
->constructed
= sipe_connection_constructed
;
524 object_class
->finalize
= sipe_connection_finalize
;
526 base_class
->create_handle_repos
= create_handle_repos
;
527 base_class
->start_connecting
= start_connecting
;
528 base_class
->shut_down
= shut_down
;
529 base_class
->create_channel_managers
= create_channel_managers
;
531 base_class
->interfaces_always_present
= interfaces_always_present
;
533 klass
->properties_mixin
.interfaces
= prop_interfaces
;
534 tp_dbus_properties_mixin_class_init(object_class
,
535 G_STRUCT_OFFSET(SipeConnectionClass
,
537 tp_contacts_mixin_class_init(object_class
,
538 G_STRUCT_OFFSET(SipeConnectionClass
,
540 sipe_telepathy_status_init(object_class
,
541 G_STRUCT_OFFSET(SipeConnectionClass
,
543 tp_presence_mixin_simple_presence_init_dbus_properties(object_class
);
544 tp_base_contact_list_mixin_class_init(base_class
);
547 static void sipe_connection_init(SIPE_UNUSED_PARAMETER SipeConnection
*self
)
549 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::init");
553 * Connection class - interface implementation
557 static void get_alias_flags(TpSvcConnectionInterfaceAliasing
*aliasing
,
558 DBusGMethodInvocation
*context
)
560 TpBaseConnection
*base
= TP_BASE_CONNECTION(aliasing
);
562 TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED(base
, context
);
563 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::get_alias_flags called");
565 tp_svc_connection_interface_aliasing_return_from_get_alias_flags(context
,
566 TP_CONNECTION_ALIAS_FLAG_USER_SET
);
569 static void get_aliases(TpSvcConnectionInterfaceAliasing
*aliasing
,
570 const GArray
*contacts
,
571 DBusGMethodInvocation
*context
)
573 SipeConnection
*self
= SIPE_CONNECTION(aliasing
);
574 TpBaseConnection
*base
= TP_BASE_CONNECTION(aliasing
);
575 TpHandleRepoIface
*contact_repo
= tp_base_connection_get_handles(base
,
576 TP_HANDLE_TYPE_CONTACT
);
577 GError
*error
= NULL
;
581 TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED(base
, context
);
582 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::get_aliases called");
584 if (!tp_handles_are_valid(contact_repo
, contacts
, FALSE
, &error
)) {
585 dbus_g_method_return_error(context
, error
);
590 result
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
592 for (i
= 0; i
< contacts
->len
; i
++) {
593 TpHandle contact
= g_array_index(contacts
, TpHandle
, i
);
594 const gchar
*alias
= sipe_telepathy_buddy_get_alias(self
->contact_list
,
596 g_hash_table_insert(result
,
597 GUINT_TO_POINTER(contact
),
601 tp_svc_connection_interface_aliasing_return_from_get_aliases(context
,
603 g_hash_table_unref(result
);
606 static void request_aliases(TpSvcConnectionInterfaceAliasing
*aliasing
,
607 const GArray
*contacts
,
608 DBusGMethodInvocation
*context
)
610 SipeConnection
*self
= SIPE_CONNECTION(aliasing
);
611 TpBaseConnection
*base
= TP_BASE_CONNECTION(aliasing
);
612 TpHandleRepoIface
*contact_repo
= tp_base_connection_get_handles(base
,
613 TP_HANDLE_TYPE_CONTACT
);
614 GError
*error
= NULL
;
619 TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED(base
, context
);
620 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::request_aliases called");
622 if (!tp_handles_are_valid(contact_repo
, contacts
, FALSE
, &error
)) {
623 dbus_g_method_return_error(context
, error
);
628 result
= g_ptr_array_sized_new(contacts
->len
+ 1);
630 for (i
= 0; i
< contacts
->len
; i
++) {
631 TpHandle contact
= g_array_index(contacts
, TpHandle
, i
);
632 const gchar
*alias
= sipe_telepathy_buddy_get_alias(self
->contact_list
,
634 g_ptr_array_add(result
, (gchar
*) alias
);
637 g_ptr_array_add(result
, NULL
);
638 strings
= (gchar
**) g_ptr_array_free(result
, FALSE
);
640 tp_svc_connection_interface_aliasing_return_from_request_aliases(context
,
641 (const gchar
**) strings
);
645 static void set_aliases(TpSvcConnectionInterfaceAliasing
*aliasing
,
647 DBusGMethodInvocation
*context
)
649 SipeConnection
*self
= SIPE_CONNECTION(aliasing
);
650 TpBaseConnection
*base
= TP_BASE_CONNECTION(aliasing
);
651 TpHandleRepoIface
*contact_repo
= tp_base_connection_get_handles(base
,
652 TP_HANDLE_TYPE_CONTACT
);
656 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::set_aliases called");
658 g_hash_table_iter_init(&iter
, aliases
);
660 while (g_hash_table_iter_next(&iter
, &key
, NULL
)) {
661 GError
*error
= NULL
;
663 if (!tp_handle_is_valid(contact_repo
,
664 GPOINTER_TO_UINT(key
),
666 dbus_g_method_return_error(context
, error
);
672 g_hash_table_iter_init(&iter
, aliases
);
674 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
675 sipe_telepathy_buddy_set_alias(self
->contact_list
,
676 GPOINTER_TO_UINT(key
),
680 tp_svc_connection_interface_aliasing_return_from_set_aliases(context
);
683 static void init_aliasing(gpointer iface
,
684 SIPE_UNUSED_PARAMETER gpointer iface_data
)
686 TpSvcConnectionInterfaceAliasingClass
*klass
= iface
;
688 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::init_aliasing called");
690 tp_svc_connection_interface_aliasing_implement_get_alias_flags(klass
, get_alias_flags
);
691 tp_svc_connection_interface_aliasing_implement_request_aliases(klass
, request_aliases
);
692 tp_svc_connection_interface_aliasing_implement_get_aliases(klass
, get_aliases
);
693 tp_svc_connection_interface_aliasing_implement_set_aliases(klass
, set_aliases
);
696 /* create new connection object */
697 TpBaseConnection
*sipe_telepathy_connection_new(TpBaseProtocol
*protocol
,
699 SIPE_UNUSED_PARAMETER GError
**error
)
701 SipeConnection
*conn
= g_object_new(SIPE_TYPE_CONNECTION
,
702 "protocol", tp_base_protocol_get_name(protocol
),
708 SIPE_DEBUG_INFO_NOFORMAT("sipe_telepathy_connection_new");
710 /* initialize private fields */
711 conn
->is_disconnecting
= FALSE
;
713 /* account & login are required fields */
714 conn
->account
= g_strdup(tp_asv_get_string(params
, "account"));
715 conn
->login
= g_strdup(tp_asv_get_string(params
, "login"));
718 value
= tp_asv_get_string(params
, "password");
719 if (value
&& strlen(value
))
720 conn
->password
= g_strdup(value
);
722 conn
->password
= NULL
;
725 value
= tp_asv_get_string(params
, "server");
726 if (value
&& strlen(value
))
727 conn
->server
= g_strdup(value
);
731 /* server port: core expects a string */
732 port
= tp_asv_get_uint32(params
, "port", &valid
);
734 conn
->port
= g_strdup_printf("%d", port
);
739 value
= tp_asv_get_string(params
, "transport");
740 if (sipe_strequal(value
, "auto")) {
741 conn
->transport
= conn
->server
?
742 SIPE_TRANSPORT_TLS
: SIPE_TRANSPORT_AUTO
;
743 } else if (sipe_strequal(value
, "tls")) {
744 conn
->transport
= SIPE_TRANSPORT_TLS
;
746 conn
->transport
= SIPE_TRANSPORT_TCP
;
749 /* User-Agent: override */
750 value
= tp_asv_get_string(params
, "useragent");
751 if (value
&& strlen(value
))
752 conn
->user_agent
= g_strdup(value
);
754 conn
->user_agent
= NULL
;
756 /* authentication type */
757 value
= tp_asv_get_string(params
, "authentication");
758 if (value
&& strlen(value
) && strcmp(value
, "ntlm"))
759 conn
->authentication
= g_strdup(value
);
761 conn
->authentication
= NULL
; /* NTLM is default */
763 return(TP_BASE_CONNECTION(conn
));
766 void sipe_telepathy_connection_alias_updated(TpBaseConnection
*connection
,
770 GPtrArray
*aliases
= g_ptr_array_sized_new(1);
771 GValueArray
*pair
= g_value_array_new(2);
773 g_value_array_append(pair
, NULL
);
774 g_value_array_append(pair
, NULL
);
775 g_value_init(pair
->values
+ 0, G_TYPE_UINT
);
776 g_value_init(pair
->values
+ 1, G_TYPE_STRING
);
777 g_value_set_uint(pair
->values
+ 0, contact
);
778 g_value_set_string(pair
->values
+ 1, alias
);
779 g_ptr_array_add(aliases
, pair
);
781 tp_svc_connection_interface_aliasing_emit_aliases_changed(SIPE_CONNECTION(connection
),
784 g_ptr_array_unref(aliases
);
785 g_value_array_free(pair
);
788 struct sipe_backend_private
*sipe_telepathy_connection_private(GObject
*object
)
790 SipeConnection
*self
= SIPE_CONNECTION(object
);
791 /* connected to core already? */
792 if (self
->private.public)
793 return(&self
->private);
799 * Backend adaptor functions
801 void sipe_backend_connection_completed(struct sipe_core_public
*sipe_public
)
803 SipeConnection
*self
= SIPE_PUBLIC_TO_CONNECTION
;
804 TpBaseConnection
*base
= TP_BASE_CONNECTION(self
);
806 /* we are only allowed to do this once */
807 if (base
->status
!= TP_CONNECTION_STATUS_CONNECTED
)
808 tp_base_connection_change_status(base
,
809 TP_CONNECTION_STATUS_CONNECTED
,
810 TP_CONNECTION_STATUS_REASON_REQUESTED
);
813 void sipe_backend_connection_error(struct sipe_core_public
*sipe_public
,
814 sipe_connection_error error
,
817 SipeConnection
*self
= SIPE_PUBLIC_TO_CONNECTION
;
818 TpBaseConnection
*base
= TP_BASE_CONNECTION(self
);
819 GHashTable
*details
= tp_asv_new("server-message", G_TYPE_STRING
, msg
,
821 TpConnectionStatusReason reason
;
824 self
->is_disconnecting
= TRUE
;
827 case SIPE_CONNECTION_ERROR_NETWORK
:
828 reason
= TP_CONNECTION_STATUS_REASON_NETWORK_ERROR
;
829 if (base
->status
== TP_CONNECTION_STATUS_CONNECTING
)
830 name
= TP_ERROR_STR_CONNECTION_FAILED
;
832 name
= TP_ERROR_STR_CONNECTION_LOST
;
835 case SIPE_CONNECTION_ERROR_INVALID_USERNAME
:
836 case SIPE_CONNECTION_ERROR_INVALID_SETTINGS
:
837 case SIPE_CONNECTION_ERROR_AUTHENTICATION_FAILED
:
838 case SIPE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE
:
839 /* copied from haze code. I agree there should be better ones */
840 reason
= TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED
;
841 name
= TP_ERROR_STR_AUTHENTICATION_FAILED
;
845 reason
= TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED
;
846 name
= TP_ERROR_STR_DISCONNECTED
;
850 SIPE_DEBUG_ERROR("sipe_backend_connection_error: %s (%s)", name
, msg
);
851 tp_base_connection_disconnect_with_dbus_error(base
,
855 g_hash_table_unref(details
);
858 gboolean
sipe_backend_connection_is_disconnecting(struct sipe_core_public
*sipe_public
)
860 SipeConnection
*self
= SIPE_PUBLIC_TO_CONNECTION
;
862 /* disconnect was requested or transport was already disconnected */
863 return(self
->is_disconnecting
||
864 self
->private.transport
== NULL
);
867 gboolean
sipe_backend_connection_is_valid(struct sipe_core_public
*sipe_public
)
869 return(!sipe_backend_connection_is_disconnecting(sipe_public
));
872 const gchar
*sipe_backend_setting(struct sipe_core_public
*sipe_public
,
875 SipeConnection
*self
= SIPE_PUBLIC_TO_CONNECTION
;
879 case SIPE_SETTING_USER_AGENT
:
880 value
= self
->user_agent
;
883 /* @TODO: update when settings are implemented */