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
25 #include <glib-object.h>
26 #include <telepathy-glib/base-connection.h>
27 #include <telepathy-glib/base-protocol.h>
28 #include <telepathy-glib/telepathy-glib.h>
30 #include "sipe-backend.h"
31 #include "sipe-common.h"
32 #include "sipe-core.h"
33 #include "telepathy-private.h"
37 * Connection class - data structures
39 typedef struct _SipeConnectionClass
{
40 TpBaseConnectionClass parent_class
;
41 } SipeConnectionClass
;
43 typedef struct _SipeConnection
{
44 TpBaseConnection parent
;
45 struct sipe_core_public
*public;
52 * Connection class - type macros
54 static GType
sipe_connection_get_type(void) G_GNUC_CONST
;
55 #define SIPE_TYPE_CONNECTION \
56 (sipe_connection_get_type())
57 #define SIPE_CONNECTION(obj) \
58 (G_TYPE_CHECK_INSTANCE_CAST((obj), SIPE_TYPE_CONNECTION, \
60 #define SIPE_CONNECTION_CLASS(klass) \
61 (G_TYPE_CHECK_CLASS_CAST((klass), SIPE_TYPE_CONNECTION, \
63 #define SIPE_IS_CONNECTION(obj) \
64 (G_TYPE_CHECK_INSTANCE_TYPE((obj), SIPE_TYPE_CONNECTION))
65 #define SIPE_IS_CONNECTION_CLASS(klass) \
66 (G_TYPE_CHECK_CLASS_TYPE((klass), SIPE_TYPE_CONNECTION))
67 #define SIPE_CONNECTION_GET_CLASS(obj) \
68 (G_TYPE_INSTANCE_GET_CLASS((obj), SIPE_TYPE_CONNECTION, \
73 * Connection class - type definition
75 G_DEFINE_TYPE(SipeConnection
,
77 TP_TYPE_BASE_CONNECTION
)
80 * Connection class - instance methods
82 static gboolean
start_connecting(TpBaseConnection
*base
,
83 SIPE_UNUSED_PARAMETER GError
**error
)
85 SipeConnection
*self
= SIPE_CONNECTION(base
);
87 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting");
89 g_return_val_if_fail(self
->public, FALSE
);
91 tp_base_connection_change_status(base
, TP_CONNECTION_STATUS_CONNECTING
,
92 TP_CONNECTION_STATUS_REASON_REQUESTED
);
94 sipe_core_transport_sip_connect(self
->public,
101 static void shut_down(TpBaseConnection
*base
)
103 SipeConnection
*self
= SIPE_CONNECTION(base
);
104 struct sipe_core_public
*sipe_public
= self
->public;
106 SIPE_DEBUG_INFO("SipeConnection::shut_down: closing %p", sipe_public
);
109 sipe_core_deallocate(sipe_public
);
112 static void sipe_connection_finalize(GObject
*object
)
114 SipeConnection
*self
= SIPE_CONNECTION(object
);
116 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::finalize");
119 g_free(self
->server
);
121 G_OBJECT_CLASS(sipe_connection_parent_class
)->finalize(object
);
125 * Connection class - type implementation
127 static void sipe_connection_class_init(SipeConnectionClass
*klass
)
129 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
130 TpBaseConnectionClass
*base_class
= TP_BASE_CONNECTION_CLASS(klass
);
132 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::class_init");
134 object_class
->finalize
= sipe_connection_finalize
;
136 base_class
->start_connecting
= start_connecting
;
137 base_class
->shut_down
= shut_down
;
140 static void sipe_connection_init(SIPE_UNUSED_PARAMETER SipeConnection
*self
)
142 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::init");
145 /* create new connection object and attach it to SIPE core */
146 TpBaseConnection
*sipe_telepathy_connection_new(TpBaseProtocol
*protocol
,
150 const gchar
*password
= tp_asv_get_string(params
, "password");
151 const gchar
*login
= tp_asv_get_string(params
, "login");
152 gchar
*login_domain
= NULL
;
153 gchar
*login_account
= NULL
;
154 TpBaseConnection
*base
= NULL
;
155 struct sipe_core_public
*sipe_public
;
158 SIPE_DEBUG_INFO_NOFORMAT("sipe_telepathy_connection_new");
160 /* login name specified? */
161 if (login
&& strlen(login
)) {
162 /* Allowed domain-account separators are / or \ */
163 gchar
**domain_user
= g_strsplit_set(login
, "/\\", 2);
164 gboolean has_domain
= domain_user
[1] != NULL
;
165 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: login '%s'", login
);
166 login_domain
= has_domain
? g_strdup(domain_user
[0]) : NULL
;
167 login_account
= g_strdup(domain_user
[has_domain
? 1 : 0]);
168 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: auth domain '%s' user '%s'",
169 login_domain
? login_domain
: "",
171 g_strfreev(domain_user
);
174 sipe_public
= sipe_core_allocate(tp_asv_get_string(params
, "account"),
175 login_domain
, login_account
,
177 NULL
, /* @TODO: email */
178 NULL
, /* @TODO: email_url */
180 g_free(login_domain
);
181 g_free(login_account
);
183 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: created %p", sipe_public
);
186 const gchar
*server
= tp_asv_get_string(params
, "server");
187 const gchar
*transport
= tp_asv_get_string(params
, "transport");
188 SipeConnection
*conn
= g_object_new(SIPE_TYPE_CONNECTION
,
189 "protocol", tp_base_protocol_get_name(protocol
),
194 /* initialize backend private data */
195 sipe_public
->backend_private
= (struct sipe_backend_private
*) conn
;
196 conn
->public = sipe_public
;
198 /* map option list to flags - default is NTLM */
199 SIPE_CORE_FLAG_UNSET(KRB5
);
200 SIPE_CORE_FLAG_UNSET(TLS_DSK
);
201 SIPE_CORE_FLAG_UNSET(SSO
);
202 /* @TODO: add parameters for these */
205 if (server
&& strlen(server
))
206 conn
->server
= g_strdup(server
);
210 /* server port: core expects a string */
211 port
= tp_asv_get_uint32(params
, "port", &valid
);
213 conn
->port
= g_strdup_printf("%d", port
);
218 if (sipe_strequal(transport
, "auto")) {
219 conn
->transport
= conn
->server
?
220 SIPE_TRANSPORT_TLS
: SIPE_TRANSPORT_AUTO
;
221 } else if (sipe_strequal(transport
, "tls")) {
222 conn
->transport
= SIPE_TRANSPORT_TLS
;
224 conn
->transport
= SIPE_TRANSPORT_TCP
;
227 base
= TP_BASE_CONNECTION(conn
);
230 g_set_error_literal(error
, TP_ERROR
, TP_ERROR_INVALID_ARGUMENT
,