summaryrefslogtreecommitdiff
path: root/src/kludge-transport.c
blob: bdd80b8125935662e109239b51d6ab53d24065df (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * kludge-transport.c - the shortest path to NM integration
 * Copyright ©2011 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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 "kludge-transport.h"

#include <telepathy-glib/util.h>

#include "mcd-debug.h"

#include "connectivity-monitor.h"

struct _McdKludgeTransportPrivate {
    /* Rawr! I'm a mythical creature. */
    McdConnectivityMonitor *minotaur;

    /* Pointers representing network connections, exposed to the application as
     * opaque McdTransport pointers.
     *
     * In this generate example of an McdTransportPlugin, we only have one
     * transport, representing "the internet". So in fact this list always
     * contains a single pointer, namely the McdKludgeTransport instance
     * itself.
     */
    GList *transports;

    /* Hold a set of McdAccounts which would like to go online. */
    GHashTable *pending_accounts;

    /* Application settings we steal from under Empathy's nose. */
    GSettings *settings;
};

static void transport_iface_init (
    gpointer g_iface,
    gpointer iface_data);
static void monitor_state_changed_cb (
    McdConnectivityMonitor *monitor,
    gboolean connected,
    gpointer user_data);

G_DEFINE_TYPE_WITH_CODE (McdKludgeTransport, mcd_kludge_transport,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCD_TYPE_TRANSPORT_PLUGIN, transport_iface_init);
    )

static void
mcd_kludge_transport_init (McdKludgeTransport *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      MCD_TYPE_KLUDGE_TRANSPORT, McdKludgeTransportPrivate);
}

static void
mcd_kludge_transport_constructed (GObject *object)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (object);
  McdKludgeTransportPrivate *priv = self->priv;
  GObjectClass *parent_class = mcd_kludge_transport_parent_class;

  if (parent_class->constructed != NULL)
    parent_class->constructed (object);

  priv->minotaur = mcd_connectivity_monitor_new ();
  tp_g_signal_connect_object (priv->minotaur, "state-change",
      (GCallback) monitor_state_changed_cb, self, 0);

  /* We just use ourself as the McdTransport pointer... */
  priv->transports = g_list_prepend (NULL, self);

  priv->pending_accounts = g_hash_table_new_full (NULL, NULL,
      g_object_unref, NULL);

  priv->settings = g_settings_new ("im.telepathy.MissionControl.FromEmpathy");
  g_settings_bind (priv->settings, "use-conn", priv->minotaur, "use-conn",
      G_SETTINGS_BIND_GET);
}

static void
mcd_kludge_transport_dispose (GObject *object)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (object);
  McdKludgeTransportPrivate *priv = self->priv;
  GObjectClass *parent_class = mcd_kludge_transport_parent_class;

  tp_clear_object (&priv->minotaur);
  tp_clear_object (&priv->settings);
  g_list_free (priv->transports);
  priv->transports = NULL;

  g_hash_table_unref (priv->pending_accounts);

  if (parent_class->dispose != NULL)
    parent_class->dispose (object);
}

static void
mcd_kludge_transport_class_init (McdKludgeTransportClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = mcd_kludge_transport_constructed;
  object_class->dispose = mcd_kludge_transport_dispose;

  g_type_class_add_private (klass, sizeof (McdKludgeTransportPrivate));
}

static const gchar *
mcd_kludge_transport_get_name (
    McdTransportPlugin *plugin)
{
  g_return_val_if_fail (MCD_IS_KLUDGE_TRANSPORT (plugin), NULL);

  return "McdKludgeTransport";
}

static const GList *
mcd_kludge_transport_get_transports (
    McdTransportPlugin *plugin)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (plugin);

  g_return_val_if_fail (MCD_IS_KLUDGE_TRANSPORT (plugin), NULL);

  return self->priv->transports;
}

static const gchar *
mcd_kludge_transport_get_transport_name (
    McdTransportPlugin *plugin,
    McdTransport *transport)
{
  g_return_val_if_fail (MCD_IS_KLUDGE_TRANSPORT (plugin), NULL);
  g_return_val_if_fail (plugin == (McdTransportPlugin *) transport, NULL);

  return "i love the internet";
}

static McdTransportStatus
mcd_kludge_transport_get_transport_status (
    McdTransportPlugin *plugin,
    McdTransport *transport)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (plugin);
  gboolean online;

  g_return_val_if_fail (MCD_IS_KLUDGE_TRANSPORT (plugin),
      MCD_TRANSPORT_STATUS_DISCONNECTED);
  g_return_val_if_fail (plugin == (McdTransportPlugin *) transport,
      MCD_TRANSPORT_STATUS_DISCONNECTED);

  online = mcd_connectivity_monitor_is_online (self->priv->minotaur);
  DEBUG ("we are allegedly %s", online ? "online" : "offline");

  if (online)
    return MCD_TRANSPORT_STATUS_CONNECTED;
  else
    return MCD_TRANSPORT_STATUS_DISCONNECTED;
}

static void
transport_iface_init (
    gpointer g_iface,
    gpointer iface_data)
{
  McdTransportPluginIface *klass = g_iface;

  klass->get_name = mcd_kludge_transport_get_name;
  klass->get_transports = mcd_kludge_transport_get_transports;
  klass->get_transport_name = mcd_kludge_transport_get_transport_name;
  klass->get_transport_status = mcd_kludge_transport_get_transport_status;
}

static void
monitor_state_changed_cb (
    McdConnectivityMonitor *monitor,
    gboolean connected,
    gpointer user_data)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (user_data);
  McdTransportStatus new_status =
      connected ? MCD_TRANSPORT_STATUS_CONNECTED
                : MCD_TRANSPORT_STATUS_DISCONNECTED;
  GHashTableIter iter;
  gpointer key;

  g_signal_emit_by_name (self, "status-changed", self, new_status);

  g_hash_table_iter_init (&iter, self->priv->pending_accounts);
  while (g_hash_table_iter_next (&iter, &key, NULL))
    {
      McdAccount *account = MCD_ACCOUNT (key);

      /* If we've gone online, allow the account to actually try to connect;
       * if we've fallen offline, say as much. (I don't actually think this
       * code will be reached if !connected, but.)
       */
      DEBUG ("telling %s to %s", mcd_account_get_unique_name (account),
          connected ? "proceed" : "give up");
      mcd_account_connection_bind_transport (account, (McdTransport *) self);
      mcd_account_connection_proceed_with_reason (account, connected,
          connected ? TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED
                    : TP_CONNECTION_STATUS_REASON_NETWORK_ERROR);
      g_hash_table_iter_remove (&iter);
    }
}

/*
 * mcd_kludge_transport_account_connection_cb:
 * @account: an account which would like to go online
 * @parameters: the connection parameters to be used
 * @user_data: the McdKludgeTransport.
 *
 * Called when an account would like to sign in.
 */
static void
mcd_kludge_transport_account_connection_cb (
    McdAccount *account,
    GHashTable *parameters,
    gpointer user_data)
{
  McdKludgeTransport *self = MCD_KLUDGE_TRANSPORT (user_data);
  McdKludgeTransportPrivate *priv = self->priv;

  if (mcd_connectivity_monitor_is_online (priv->minotaur))
    {
      mcd_account_connection_bind_transport (account, (McdTransport *) self);
      mcd_account_connection_proceed (account, TRUE);
    }
  else if (g_hash_table_lookup (priv->pending_accounts, account) == NULL)
    {
      DEBUG ("%s wants to connect, but we're offline; queuing it up",
          mcd_account_get_unique_name (account));
      g_object_ref (account);
      g_hash_table_insert (priv->pending_accounts, account, account);
    }
  /* ... else we're already waiting, I guess */
}

static McdTransportPlugin *
mcd_kludge_transport_new (void)
{
  return g_object_new (MCD_TYPE_KLUDGE_TRANSPORT, NULL);
}

void
mcd_kludge_transport_install (
    McdPlugin *plugin)
{
  McdTransportPlugin *self = mcd_kludge_transport_new ();

  mcd_plugin_register_transport (plugin, self);
  mcd_plugin_register_account_connection (plugin,
      mcd_kludge_transport_account_connection_cb,
      MCD_ACCOUNT_CONNECTION_PRIORITY_TRANSPORT, self);
}