summaryrefslogtreecommitdiff
path: root/lib/loudmouth/lm-connection.c
blob: 6fbffdf7b72d7651ec62978a08bf385d76e0dee9 (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
/*
 * lm-connection.c - Loudmouth-Wocky compatibility layer
 * Copyright (C) 2009 Collabora Ltd.
 * @author Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
 *
 * 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 "lm-connection.h"
#include "lm-message-handler.h"

static gboolean
stanza_cb (WockyPorter *self,
    WockyStanza *stanza,
    gpointer user_data)
{
  LmMessageHandler *handler = (LmMessageHandler *) user_data;
  LmHandlerResult result;

  result = handler->function (handler, handler->connection, stanza,
      handler->user_data);

  if (result == LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS)
    return FALSE;
  else
    return TRUE;
}

typedef struct
{
  LmMessageHandler *handler;
  WockyStanzaType type;
  LmHandlerPriority priority;
} delayed_handler;

void
lm_connection_register_message_handler (LmConnection *connection,
    LmMessageHandler *handler,
    WockyStanzaType type,
    LmHandlerPriority priority)
{
  if (connection->porter == NULL)
    {
      /* Loudmouth lets you register handlers before the connection is
       * connected. We can't do currently do that with Wocky so we store the
       * handler and will register it once lm_connection_set_porter is called.*/
      delayed_handler *delayed;

      delayed = g_slice_new (delayed_handler);
      delayed->handler = handler;
      delayed->type = type;
      delayed->priority = priority;

      connection->delayed_handlers = g_slist_prepend (
          connection->delayed_handlers, delayed);
      return;
    }

  /* Genuine Loudmouth lets you register the same handler once per message
   * type, but this compatibility shim only lets you register each
   * LmMessageHandler once. */
  g_assert (handler->handler_id == 0);
  g_assert (handler->connection == NULL);

  handler->connection = connection;

  handler->handler_id = wocky_porter_register_handler_from_anyone (
      connection->porter, type, WOCKY_STANZA_SUB_TYPE_NONE, priority, stanza_cb,
      handler, NULL);
}

void
lm_connection_unregister_message_handler (LmConnection *connection,
    LmMessageHandler *handler,
    WockyStanzaType type)
{
  if (handler->handler_id == 0)
    return;

  g_assert (handler->connection != NULL);

  wocky_porter_unregister_handler (handler->connection->porter,
      handler->handler_id);

  handler->handler_id = 0;
  handler->connection = NULL;
}

void
lm_connection_unref (LmConnection *connection)
{
  GSList *l;

  for (l = connection->delayed_handlers; l != NULL; l = g_slist_next (l))
    {
      delayed_handler *delayed = l->data;

      g_slice_free (delayed_handler, delayed);
    }

  g_slist_free (connection->delayed_handlers);
  connection->delayed_handlers = NULL;

  g_cancellable_cancel (connection->iq_reply_cancellable);
  g_object_unref (connection->iq_reply_cancellable);
  connection->iq_reply_cancellable = NULL;

  if (connection->porter != NULL)
    {
      g_object_unref (connection->porter);
      connection->porter = NULL;
    }

  g_free (connection);
}

LmConnection *
lm_connection_new (void)
{
  LmConnection *connection;

  connection = g_malloc (sizeof (LmConnection));
  connection->porter = NULL;
  connection->delayed_handlers = NULL;
  connection->iq_reply_cancellable = g_cancellable_new ();

  return connection;
}

void
lm_connection_set_porter (LmConnection *connection,
    WockyPorter *porter)
{
  GSList *l;

  g_assert (connection != NULL);
  g_assert (connection->porter == NULL);
  connection->porter = g_object_ref (porter);

  /* Now that we have a porter we can register the delayed handlers */
  for (l = connection->delayed_handlers; l != NULL; l = g_slist_next (l))
    {
      delayed_handler *delayed = l->data;

      lm_connection_register_message_handler (connection, delayed->handler,
          delayed->type, delayed->priority);

      g_slice_free (delayed_handler, delayed);
    }

  g_slist_free (connection->delayed_handlers);
  connection->delayed_handlers = NULL;
}