telepathy: add mandatory create_channel_managers to connection
[siplcs.git] / src / telepathy / telepathy-connection.c
blob129f3a8e43f08bb5ecb0e4b2de97aaaf21a19e55
1 /**
2 * @file telepathy-connection.c
4 * pidgin-sipe
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
23 #include <string.h>
25 #include <glib-object.h>
26 #include <telepathy-glib/base-connection.h>
27 #include <telepathy-glib/base-protocol.h>
28 #include <telepathy-glib/handle-repo-dynamic.h>
29 #include <telepathy-glib/telepathy-glib.h>
31 #include "sipe-backend.h"
32 #include "sipe-common.h"
33 #include "sipe-core.h"
34 #include "telepathy-private.h"
36 G_BEGIN_DECLS
38 * Connection class - data structures
40 typedef struct _SipeConnectionClass {
41 TpBaseConnectionClass parent_class;
42 } SipeConnectionClass;
44 typedef struct _SipeConnection {
45 TpBaseConnection parent;
46 struct sipe_core_public *public;
47 gchar *server;
48 gchar *port;
49 guint transport;
50 } SipeConnection;
53 * Connection class - type macros
55 static GType sipe_connection_get_type(void) G_GNUC_CONST;
56 #define SIPE_TYPE_CONNECTION \
57 (sipe_connection_get_type())
58 #define SIPE_CONNECTION(obj) \
59 (G_TYPE_CHECK_INSTANCE_CAST((obj), SIPE_TYPE_CONNECTION, \
60 SipeConnection))
61 #define SIPE_CONNECTION_CLASS(klass) \
62 (G_TYPE_CHECK_CLASS_CAST((klass), SIPE_TYPE_CONNECTION, \
63 SipeConnectionClass))
64 #define SIPE_IS_CONNECTION(obj) \
65 (G_TYPE_CHECK_INSTANCE_TYPE((obj), SIPE_TYPE_CONNECTION))
66 #define SIPE_IS_CONNECTION_CLASS(klass) \
67 (G_TYPE_CHECK_CLASS_TYPE((klass), SIPE_TYPE_CONNECTION))
68 #define SIPE_CONNECTION_GET_CLASS(obj) \
69 (G_TYPE_INSTANCE_GET_CLASS((obj), SIPE_TYPE_CONNECTION, \
70 SipeConnectionClass))
71 G_END_DECLS
74 * Connection class - type definition
76 G_DEFINE_TYPE(SipeConnection,
77 sipe_connection,
78 TP_TYPE_BASE_CONNECTION)
81 * Connection class - instance methods
83 static gchar *normalize_contact(SIPE_UNUSED_PARAMETER TpHandleRepoIface *repo,
84 const gchar *id,
85 SIPE_UNUSED_PARAMETER gpointer context,
86 GError **error)
88 return(sipe_telepathy_protocol_normalize_contact(NULL, id, error));
91 static void create_handle_repos(SIPE_UNUSED_PARAMETER TpBaseConnection *conn,
92 TpHandleRepoIface *repos[NUM_TP_HANDLE_TYPES])
94 repos[TP_HANDLE_TYPE_CONTACT] = tp_dynamic_handle_repo_new(TP_HANDLE_TYPE_CONTACT,
95 normalize_contact,
96 NULL);
99 static gboolean start_connecting(TpBaseConnection *base,
100 SIPE_UNUSED_PARAMETER GError **error)
102 SipeConnection *self = SIPE_CONNECTION(base);
104 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::start_connecting");
106 g_return_val_if_fail(self->public, FALSE);
108 tp_base_connection_change_status(base, TP_CONNECTION_STATUS_CONNECTING,
109 TP_CONNECTION_STATUS_REASON_REQUESTED);
111 sipe_core_transport_sip_connect(self->public,
112 self->transport,
113 self->server,
114 self->port);
115 return(TRUE);
118 static void shut_down(TpBaseConnection *base)
120 SipeConnection *self = SIPE_CONNECTION(base);
121 struct sipe_core_public *sipe_public = self->public;
123 SIPE_DEBUG_INFO("SipeConnection::shut_down: closing %p", sipe_public);
125 if (sipe_public)
126 sipe_core_deallocate(sipe_public);
128 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::shut_down: core deallocated");
131 static GPtrArray *create_channel_managers(SIPE_UNUSED_PARAMETER TpBaseConnection *base)
133 /* @TODO */
134 return(g_ptr_array_sized_new(0));
137 static void sipe_connection_finalize(GObject *object)
139 SipeConnection *self = SIPE_CONNECTION(object);
141 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::finalize");
143 g_free(self->port);
144 g_free(self->server);
146 G_OBJECT_CLASS(sipe_connection_parent_class)->finalize(object);
150 * Connection class - type implementation
152 static void sipe_connection_class_init(SipeConnectionClass *klass)
154 GObjectClass *object_class = G_OBJECT_CLASS(klass);
155 TpBaseConnectionClass *base_class = TP_BASE_CONNECTION_CLASS(klass);
157 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::class_init");
159 object_class->finalize = sipe_connection_finalize;
161 base_class->create_handle_repos = create_handle_repos;
162 base_class->start_connecting = start_connecting;
163 base_class->shut_down = shut_down;
164 base_class->create_channel_managers = create_channel_managers;
167 static void sipe_connection_init(SIPE_UNUSED_PARAMETER SipeConnection *self)
169 SIPE_DEBUG_INFO_NOFORMAT("SipeConnection::init");
172 /* create new connection object and attach it to SIPE core */
173 TpBaseConnection *sipe_telepathy_connection_new(TpBaseProtocol *protocol,
174 GHashTable *params,
175 GError **error)
177 const gchar *password = tp_asv_get_string(params, "password");
178 const gchar *login = tp_asv_get_string(params, "login");
179 gchar *login_domain = NULL;
180 gchar *login_account = NULL;
181 TpBaseConnection *base = NULL;
182 struct sipe_core_public *sipe_public;
183 const gchar *errmsg;
185 SIPE_DEBUG_INFO_NOFORMAT("sipe_telepathy_connection_new");
187 /* login name specified? */
188 if (login && strlen(login)) {
189 /* Allowed domain-account separators are / or \ */
190 gchar **domain_user = g_strsplit_set(login, "/\\", 2);
191 gboolean has_domain = domain_user[1] != NULL;
192 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: login '%s'", login);
193 login_domain = has_domain ? g_strdup(domain_user[0]) : NULL;
194 login_account = g_strdup(domain_user[has_domain ? 1 : 0]);
195 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: auth domain '%s' user '%s'",
196 login_domain ? login_domain : "",
197 login_account);
198 g_strfreev(domain_user);
201 sipe_public = sipe_core_allocate(tp_asv_get_string(params, "account"),
202 login_domain, login_account,
203 password,
204 NULL, /* @TODO: email */
205 NULL, /* @TODO: email_url */
206 &errmsg);
207 g_free(login_domain);
208 g_free(login_account);
210 SIPE_DEBUG_INFO("sipe_telepathy_connection_new: created %p", sipe_public);
212 if (sipe_public) {
213 const gchar *server = tp_asv_get_string(params, "server");
214 const gchar *transport = tp_asv_get_string(params, "transport");
215 SipeConnection *conn = g_object_new(SIPE_TYPE_CONNECTION,
216 "protocol", tp_base_protocol_get_name(protocol),
217 NULL);
218 guint port;
219 gboolean valid;
221 /* initialize backend private data */
222 sipe_public->backend_private = (struct sipe_backend_private *) conn;
223 conn->public = sipe_public;
225 /* map option list to flags - default is NTLM */
226 SIPE_CORE_FLAG_UNSET(KRB5);
227 SIPE_CORE_FLAG_UNSET(TLS_DSK);
228 SIPE_CORE_FLAG_UNSET(SSO);
229 /* @TODO: add parameters for these */
231 /* server name */
232 if (server && strlen(server))
233 conn->server = g_strdup(server);
234 else
235 conn->server = NULL;
237 /* server port: core expects a string */
238 port = tp_asv_get_uint32(params, "port", &valid);
239 if (valid)
240 conn->port = g_strdup_printf("%d", port);
241 else
242 conn->port = NULL;
244 /* transport type */
245 if (sipe_strequal(transport, "auto")) {
246 conn->transport = conn->server ?
247 SIPE_TRANSPORT_TLS : SIPE_TRANSPORT_AUTO;
248 } else if (sipe_strequal(transport, "tls")) {
249 conn->transport = SIPE_TRANSPORT_TLS;
250 } else {
251 conn->transport = SIPE_TRANSPORT_TCP;
254 base = TP_BASE_CONNECTION(conn);
256 } else
257 g_set_error_literal(error, TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
258 errmsg);
260 return(base);
265 Local Variables:
266 mode: c
267 c-file-style: "bsd"
268 indent-tabs-mode: t
269 tab-width: 8
270 End: