summaryrefslogtreecommitdiff
path: root/src/mcd-account-connection.c
blob: 09b3a219269d7e979933f9a734b014669c815d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* vi: set et sw=4 ts=8 cino=t0,(0: */
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 8 -*- */
/*
 * This file is part of mission-control
 *
 * Copyright (C) 2008-2009 Nokia Corporation.
 * Copyright (C) 2009 Collabora Ltd.
 *
 * Contact: Alberto Mardegan  <alberto.mardegan@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <glib/gstdio.h>

#include "mcd-master.h"
#include "mcd-master-priv.h"
#include "mcd-account.h"
#include "mcd-account-priv.h"
#include "mcd-account-manager.h"
#include "mcd-connection-priv.h"

struct _McdAccountConnectionContext {
    GHashTable *params;
    gboolean user_initiated;
};

void
_mcd_account_connection_context_free (McdAccountConnectionContext *c)
{
    g_hash_table_unref (c->params);
    g_free (c);
}

void
_mcd_account_connection_begin (McdAccount *account,
                               gboolean user_initiated)
{
    McdAccountConnectionContext *ctx;

    /* check whether a connection process is already ongoing */
    if (_mcd_account_get_connection_context (account) != NULL)
    {
        DEBUG ("already trying to connect");
        return;
    }

    /* get account params */
    /* create dynamic params HT */
    /* run the handlers */
    ctx = g_malloc (sizeof (McdAccountConnectionContext));
    ctx->user_initiated = user_initiated;

    /* If we get this far, the account should be valid, so getting the
     * parameters should succeed.
     */
    ctx->params = _mcd_account_dup_parameters (account);
    g_assert (ctx->params != NULL);

    _mcd_account_set_connection_status (account,
                                        TP_CONNECTION_STATUS_CONNECTING,
                                        TP_CONNECTION_STATUS_REASON_REQUESTED,
                                        NULL, NULL, NULL);
    _mcd_account_set_connection_context (account, ctx);
    mcd_account_connection_proceed (account, TRUE);
}

void
mcd_account_connection_proceed_with_reason (McdAccount *account,
                                            gboolean success,
                                            TpConnectionStatusReason reason)
{
    McdAccountConnectionContext *ctx;
    gboolean delayed;

    /* call next handler, or terminate the chain (emitting proper signal).
     * if everything is fine, call mcd_manager_create_connection() and
     * _mcd_connection_connect () with the dynamic parameters. Remove that call
     * from mcd_manager_create_connection() */
    ctx = _mcd_account_get_connection_context (account);
    g_return_if_fail (ctx != NULL);
    g_return_if_fail (ctx->params != NULL);

    if (success)
    {
        if (mcd_connectivity_monitor_is_online (
              mcd_account_get_connectivity_monitor (account)))
        {
            DEBUG ("%s wants to connect and we're online - go for it",
                mcd_account_get_unique_name (account));
            delayed = FALSE;
        }
        else if (!mcd_account_get_waiting_for_connectivity (account))
        {
            DEBUG ("%s wants to connect, but we're offline; queuing it up",
                mcd_account_get_unique_name (account));
            delayed = TRUE;
            mcd_account_set_waiting_for_connectivity (account, TRUE);
        }
        else
        {
            DEBUG ("%s wants to connect, but is already waiting for "
                "connectivity?", mcd_account_get_unique_name (account));
            delayed = TRUE;
        }
    }
    else
    {
        DEBUG ("%s failed to connect: reason code %d",
            mcd_account_get_unique_name (account), reason);
        delayed = FALSE;
    }

    if (!delayed)
    {
	/* end of the chain */
	if (success)
	{
	    _mcd_account_connect (account, ctx->params);
	}
        else
        {
            _mcd_account_set_connection_status
                (account, TP_CONNECTION_STATUS_DISCONNECTED, reason, NULL,
                 TP_ERROR_STR_DISCONNECTED, NULL);
        }
        _mcd_account_set_connection_context (account, NULL);
    }
}

void
mcd_account_connection_proceed (McdAccount *account, gboolean success)
{
    mcd_account_connection_proceed_with_reason
        (account, success, TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED);
}