summaryrefslogtreecommitdiff
path: root/plugins/console/channel-manager.c
blob: b2a17902d83a0140d39e235f63413ba4c9e5cba6 (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
/* XML console plugin
 *
 * Copyright © 2011–2013 Collabora Ltd. <http://www.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 "config.h"
#include "console/channel-manager.h"

#include "extensions/extensions.h"
#include "console/sidecar.h"

static void channel_manager_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (GabbleConsoleChannelManager, gabble_console_channel_manager,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_CHANNEL_MANAGER, channel_manager_iface_init)
    G_IMPLEMENT_INTERFACE (GABBLE_TYPE_CAPS_CHANNEL_MANAGER, NULL);
    )

enum {
    PROP_CONNECTION = 1,
};

static void
gabble_console_channel_manager_init (GabbleConsoleChannelManager *self)
{
}

static void
gabble_console_channel_manager_set_property (
    GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  GabbleConsoleChannelManager *self = GABBLE_CONSOLE_CHANNEL_MANAGER (object);

  switch (property_id)
    {
      case PROP_CONNECTION:
        /* Not reffing this: the connection owns all channel managers, so it
         * must outlive us. Taking a reference leads to a cycle.
         */
        self->plugin_connection = g_value_get_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
gabble_console_channel_manager_get_property (
    GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  GabbleConsoleChannelManager *self = GABBLE_CONSOLE_CHANNEL_MANAGER (object);

  switch (property_id)
    {
      case PROP_CONNECTION:
        g_value_set_object (value, self->plugin_connection);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}


static void
gabble_console_channel_manager_dispose (
    GObject *object)
{
  GabbleConsoleChannelManager *self = GABBLE_CONSOLE_CHANNEL_MANAGER (object);
  TpBaseChannel *channel;

  while ((channel = g_queue_peek_head (&self->console_channels)) != NULL)
    {
      tp_base_channel_close (channel);
    }

  G_OBJECT_CLASS (gabble_console_channel_manager_parent_class)->dispose (object);
}


static void
gabble_console_channel_manager_class_init (GabbleConsoleChannelManagerClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  oclass->set_property = gabble_console_channel_manager_set_property;
  oclass->get_property = gabble_console_channel_manager_get_property;
  oclass->dispose = gabble_console_channel_manager_dispose;

  g_object_class_install_property (oclass, PROP_CONNECTION,
      g_param_spec_object ("plugin-connection", "Gabble Plugin Connection",
          "Gabble Plugin Connection",
          GABBLE_TYPE_PLUGIN_CONNECTION,
          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}


static const gchar * const allowed[] = {
    TP_PROP_CHANNEL_CHANNEL_TYPE,
    TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
    NULL
};

static void
gabble_console_channel_manager_type_foreach_channel_class (GType type,
    TpChannelManagerTypeChannelClassFunc func,
    gpointer user_data)
{
  GHashTable *table = tp_asv_new (
      TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, GABBLE_IFACE_GABBLE_PLUGIN_CONSOLE,
      TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_NONE,
      NULL);

  func (type, table, NULL, user_data);

  g_hash_table_unref (table);
}


static void
console_channel_closed_cb (
    GabbleConsoleSidecar *channel,
    gpointer user_data)
{
  GabbleConsoleChannelManager *self = GABBLE_CONSOLE_CHANNEL_MANAGER (user_data);

  tp_channel_manager_emit_channel_closed_for_object (self,
      TP_EXPORTABLE_CHANNEL (channel));

  if (g_queue_remove (&self->console_channels, channel))
    {
      g_object_unref (channel);
    }
}


static gboolean
gabble_console_channel_manager_create_channel (
    TpChannelManager *manager,
    gpointer request_token,
    GHashTable *request_properties)
{
  GabbleConsoleChannelManager *self = GABBLE_CONSOLE_CHANNEL_MANAGER (manager);
  TpBaseChannel *channel = NULL;
  GError *error = NULL;
  GSList *request_tokens;

  if (tp_strdiff (tp_asv_get_string (request_properties,
          TP_IFACE_CHANNEL ".ChannelType"),
        GABBLE_IFACE_GABBLE_PLUGIN_CONSOLE))
    return FALSE;

  if (tp_asv_get_uint32 (request_properties,
       TP_IFACE_CHANNEL ".TargetHandleType", NULL) != 0)
    {
      g_set_error (&error, TP_ERROR, TP_ERROR_NOT_IMPLEMENTED,
          "Console channels can't have a target handle");
      goto error;
    }

  if (tp_channel_manager_asv_has_unknown_properties (request_properties,
          allowed,
          allowed,
          &error))
    goto error;

  channel = g_object_new (GABBLE_TYPE_CONSOLE_SIDECAR,
      "connection", self->plugin_connection,
      NULL);
  tp_base_channel_register (channel);
  g_signal_connect (channel, "closed", (GCallback) console_channel_closed_cb,
      self);
  g_queue_push_tail (&self->console_channels, channel);

  request_tokens = g_slist_prepend (NULL, request_token);
  tp_channel_manager_emit_new_channel (self,
      TP_EXPORTABLE_CHANNEL (channel), request_tokens);
  g_slist_free (request_tokens);

  return TRUE;

error:
  tp_channel_manager_emit_request_failed (self, request_token,
      error->domain, error->code, error->message);
  g_error_free (error);
  return TRUE;
}


static void
channel_manager_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  TpChannelManagerIface *iface = g_iface;

  iface->type_foreach_channel_class = gabble_console_channel_manager_type_foreach_channel_class;
  iface->create_channel = gabble_console_channel_manager_create_channel;
}