1 /* vi: set et sw=4 ts=8 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
3 /*
4 * This file is part of mission-control
5 *
6 * Copyright (C) 2008-2009 Nokia Corporation.
7 * Copyright (C) 2009 Collabora Ltd.
8 *
9 * Contact: Alberto Mardegan <alberto.mardegan@nokia.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * version 2.1 as published by the Free Software Foundation.
14 *
15 * This library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
24 *
25 */
26
27 #include "config.h"
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib/gstdio.h>
32
33 #include "mcd-master.h"
34 #include "mcd-master-priv.h"
35 #include "mcd-account.h"
36 #include "mcd-account-priv.h"
37 #include "mcd-account-manager.h"
38 #include "mcd-connection-priv.h"
39
40 struct _McdAccountConnectionContext {
41 GHashTable *params;
42 gboolean user_initiated;
43 };
44
45 void
_mcd_account_connection_context_free(McdAccountConnectionContext * c)46 _mcd_account_connection_context_free (McdAccountConnectionContext *c)
47 {
48 g_hash_table_unref (c->params);
49 g_free (c);
50 }
51
52 void
_mcd_account_connection_begin(McdAccount * account,gboolean user_initiated)53 _mcd_account_connection_begin (McdAccount *account,
54 gboolean user_initiated)
55 {
56 McdAccountConnectionContext *ctx;
57
58 /* check whether a connection process is already ongoing */
59 if (_mcd_account_get_connection_context (account) != NULL)
60 {
61 DEBUG ("already trying to connect");
62 return;
63 }
64
65 /* get account params */
66 /* create dynamic params HT */
67 /* run the handlers */
68 ctx = g_malloc (sizeof (McdAccountConnectionContext));
69 ctx->user_initiated = user_initiated;
70
71 /* If we get this far, the account should be valid, so getting the
72 * parameters should succeed.
73 */
74 ctx->params = _mcd_account_dup_parameters (account);
75 g_assert (ctx->params != NULL);
76
77 _mcd_account_set_connection_status (account,
78 TP_CONNECTION_STATUS_CONNECTING,
79 TP_CONNECTION_STATUS_REASON_REQUESTED,
80 NULL, NULL, NULL);
81 _mcd_account_set_connection_context (account, ctx);
82 mcd_account_connection_proceed (account, TRUE);
83 }
84
85 void
mcd_account_connection_proceed_with_reason(McdAccount * account,gboolean success,TpConnectionStatusReason reason)86 mcd_account_connection_proceed_with_reason (McdAccount *account,
87 gboolean success,
88 TpConnectionStatusReason reason)
89 {
90 McdAccountConnectionContext *ctx;
91 gboolean delayed;
92
93 /* call next handler, or terminate the chain (emitting proper signal).
94 * if everything is fine, call mcd_manager_create_connection() and
95 * _mcd_connection_connect () with the dynamic parameters. Remove that call
96 * from mcd_manager_create_connection() */
97 ctx = _mcd_account_get_connection_context (account);
98 g_return_if_fail (ctx != NULL);
99 g_return_if_fail (ctx->params != NULL);
100
101 if (success)
102 {
103 if (mcd_connectivity_monitor_is_online (
104 mcd_account_get_connectivity_monitor (account)) || _mcd_account_needs_dispatch (account))
105 {
106 DEBUG ("%s wants to connect and we're online - go for it",
107 mcd_account_get_unique_name (account));
108 delayed = FALSE;
109 }
110 else if (!mcd_account_get_waiting_for_connectivity (account))
111 {
112 DEBUG ("%s wants to connect, but we're offline; queuing it up",
113 mcd_account_get_unique_name (account));
114 delayed = TRUE;
115 mcd_account_set_waiting_for_connectivity (account, TRUE);
116 }
117 else
118 {
119 DEBUG ("%s wants to connect, but is already waiting for "
120 "connectivity?", mcd_account_get_unique_name (account));
121 delayed = TRUE;
122 }
123 }
124 else
125 {
126 DEBUG ("%s failed to connect: reason code %d",
127 mcd_account_get_unique_name (account), reason);
128 delayed = FALSE;
129 }
130
131 if (!delayed)
132 {
133 /* end of the chain */
134 if (success)
135 {
136 _mcd_account_connect (account, ctx->params);
137 }
138 else
139 {
140 _mcd_account_set_connection_status
141 (account, TP_CONNECTION_STATUS_DISCONNECTED, reason, NULL,
142 TP_ERROR_STR_DISCONNECTED, NULL);
143 }
144 _mcd_account_set_connection_context (account, NULL);
145 }
146 }
147
148 void
mcd_account_connection_proceed(McdAccount * account,gboolean success)149 mcd_account_connection_proceed (McdAccount *account, gboolean success)
150 {
151 mcd_account_connection_proceed_with_reason
152 (account, success, TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED);
153 }
154