summaryrefslogtreecommitdiff
path: root/telepathy-glib/automatic-proxy-factory.c
blob: 3e526baa78e38783cc1d6d6758ad5350e4563656 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Factory creating higher level proxy objects
 *
 * Copyright © 2010 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
 */

/**
 * SECTION:automatic-proxy-factory
 * @title: TpAutomaticProxyFactory
 * @short_description: factory creating higher level proxy objects
 * @see_also: #TpBasicProxyFactory
 *
 * This factory implements the #TpClientChannelFactoryInterface interface to
 * create specialized #TpChannel subclasses.
 *
 * #TpAutomaticProxyFactory will currently create #TpChannel objects
 * as follows:
 *
 * <itemizedlist>
 *   <listitem>
 *     <para>a #TpStreamTubeChannel, if the channel is of type
 *     %TP_IFACE_CHANNEL_TYPE_STREAM_TUBE;</para>
 *   </listitem>
 *   <listitem>
 *     <para>a #TpTextChannel, if the channel is of type
 *     %TP_IFACE_CHANNEL_TYPE_TEXT and implements
 *     %TP_IFACE_CHANNEL_INTERFACE_MESSAGES;</para>
 *   </listitem>
 *   <listitem>
 *     <para>a #TpFileTransferChannel, if the channel is of type
 *     %TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER;</para>
 *   </listitem>
 *   <listitem>
 *     <para>a plain #TpChannel, otherwise</para>
 *   </listitem>
 * </itemizedlist>
 *
 * It is guaranteed that the objects returned by future versions
 * will be either the class that is currently used, or a more specific
 * subclass of that class.
 *
 * This factory asks to prepare the following properties:
 *
 * <itemizedlist>
 *   <listitem>
 *     <para>%TP_CHANNEL_FEATURE_CORE, %TP_CHANNEL_FEATURE_GROUP
 *     and %TP_CHANNEL_FEATURE_PASSWORD for all
 *     type of channels.</para>
 *   </listitem>
 *   <listitem>
 *     <para>%TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES and
 *     TP_TEXT_CHANNEL_FEATURE_SMS for #TpTextChannel</para>
 *   </listitem>
 *   <listitem>
 *     <para>%TP_FILE_TRANSFER_CHANNEL_FEATURE_CORE
 *     for #TpFileTransferChannel</para>
 *   </listitem>
 * </itemizedlist>
 *
 * TpProxy subclasses other than TpChannel are not currently supported.
 *
 * Since: 0.13.2
 */

/**
 * TpAutomaticProxyFactory:
 *
 * Data structure representing a #TpAutomaticProxyFactory
 *
 * Since: 0.13.2
 */

/**
 * TpAutomaticProxyFactoryClass:
 * @parent_class: the parent class
 *
 * The class of a #TpAutomaticProxyFactory.
 *
 * Since: 0.13.2
 */

#include "config.h"

#include "telepathy-glib/automatic-proxy-factory.h"

#include <telepathy-glib/client-channel-factory.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/stream-tube-channel.h>
#include <telepathy-glib/text-channel.h>
#include <telepathy-glib/file-transfer-channel.h>
#include <telepathy-glib/util.h>

#define DEBUG_FLAG TP_DEBUG_CLIENT
#include "telepathy-glib/debug-internal.h"

static void client_proxy_factory_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE(TpAutomaticProxyFactory,
    tp_automatic_proxy_factory, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_CLIENT_CHANNEL_FACTORY,
      client_proxy_factory_iface_init))

/* Deprecated module can use deprecated APIs */
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

static void
tp_automatic_proxy_factory_init (TpAutomaticProxyFactory *self)
{
}

static void
tp_automatic_proxy_factory_class_init (TpAutomaticProxyFactoryClass *cls)
{
}

static TpChannel *
tp_automatic_proxy_factory_create_channel_impl (
    TpConnection *conn,
    const gchar *path,
    GHashTable *properties,
    GError **error)
{
  const gchar *chan_type;

  chan_type = tp_asv_get_string (properties, TP_PROP_CHANNEL_CHANNEL_TYPE);

  if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_STREAM_TUBE))
    {
      return TP_CHANNEL (tp_stream_tube_channel_new (conn, path, properties,
            error));
    }
  else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_TEXT))
    {
      /* Create a TpTextChannel only if the channel supports Messages */
      const gchar * const * interfaces;

      interfaces = tp_asv_get_strv (properties, TP_PROP_CHANNEL_INTERFACES);

      if (tp_strv_contains (interfaces, TP_IFACE_CHANNEL_INTERFACE_MESSAGES))
        return TP_CHANNEL (tp_text_channel_new (conn, path, properties,
              error));

      DEBUG ("channel %s doesn't implement Messages so we can't create "
          "a TpTextChannel", path);
    }
  else if (!tp_strdiff (chan_type, TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER))
    {
      return TP_CHANNEL (tp_file_transfer_channel_new (conn, path, properties,
            error));
    }

  return tp_channel_new_from_properties (conn, path, properties, error);
}

static TpChannel *
tp_automatic_proxy_factory_create_channel (
    TpClientChannelFactoryInterface *iface G_GNUC_UNUSED,
    TpConnection *conn,
    const gchar *path,
    GHashTable *properties,
    GError **error)
{
  return tp_automatic_proxy_factory_create_channel_impl (conn, path,
      properties, error);
}

static TpChannel *
tp_automatic_proxy_factory_obj_create_channel (
    TpClientChannelFactory *self G_GNUC_UNUSED,
    TpConnection *conn,
    const gchar *path,
    GHashTable *properties,
    GError **error)
{
  return tp_automatic_proxy_factory_create_channel_impl (conn, path,
      properties, error);
}

static GArray *
tp_automatic_proxy_factory_dup_channel_features_impl (TpChannel *channel)
{
  GArray *features;
  GQuark feature;

  features = g_array_sized_new (TRUE, FALSE, sizeof (GQuark), 2);

  feature = TP_CHANNEL_FEATURE_CORE;
  g_array_append_val (features, feature);

  feature = TP_CHANNEL_FEATURE_GROUP;
  g_array_append_val (features, feature);

  feature = TP_CHANNEL_FEATURE_PASSWORD;
  g_array_append_val (features, feature);

  if (TP_IS_TEXT_CHANNEL (channel))
    {
      feature = TP_TEXT_CHANNEL_FEATURE_INCOMING_MESSAGES;
      g_array_append_val (features, feature);

      feature = TP_TEXT_CHANNEL_FEATURE_SMS;
      g_array_append_val (features, feature);
    }
  else if (TP_IS_FILE_TRANSFER_CHANNEL (channel))
    {
      feature = TP_FILE_TRANSFER_CHANNEL_FEATURE_CORE;
      g_array_append_val (features, feature);
    }

  return features;
}

static GArray *
tp_automatic_proxy_factory_obj_dup_channel_features (
    TpClientChannelFactory *self G_GNUC_UNUSED,
    TpChannel *channel)
{
  return tp_automatic_proxy_factory_dup_channel_features_impl (channel);
}

static GArray *
tp_automatic_proxy_factory_dup_channel_features (
    TpClientChannelFactoryInterface *iface G_GNUC_UNUSED,
    TpChannel *channel)
{
  return tp_automatic_proxy_factory_dup_channel_features_impl (channel);
}

static void
client_proxy_factory_iface_init (gpointer g_iface,
    gpointer unused G_GNUC_UNUSED)
{
  TpClientChannelFactoryInterface *iface = g_iface;

  iface->create_channel = tp_automatic_proxy_factory_create_channel;
  iface->dup_channel_features = tp_automatic_proxy_factory_dup_channel_features;
  iface->obj_create_channel = tp_automatic_proxy_factory_obj_create_channel;
  iface->obj_dup_channel_features = tp_automatic_proxy_factory_obj_dup_channel_features;
}

/**
 * tp_automatic_proxy_factory_new:
 *
 * Convenient function to create a new #TpAutomaticProxyFactory instance.
 *
 * Returns: a new #TpAutomaticProxyFactory
 *
 * Since: 0.13.2
 * Deprecated: New code should use #TpAutomaticClientFactory instead
 */
static TpAutomaticProxyFactory *
_tp_automatic_proxy_factory_new (void)
{
  return g_object_new (TP_TYPE_AUTOMATIC_PROXY_FACTORY,
      NULL);
}

TpAutomaticProxyFactory *
tp_automatic_proxy_factory_new (void)
{
  return _tp_automatic_proxy_factory_new ();
}

/**
 * tp_automatic_proxy_factory_dup:
 *
 * Returns a cached #TpAutomaticProxyFactory; the same
 * #TpAutomaticProxyFactory object will be returned by this function repeatedly,
 * as long as at least one reference exists.
 *
 * Returns: (transfer full): a #TpAutomaticProxyFactory
 *
 * Since: 0.13.2
 * Deprecated: New code should use #TpAutomaticClientFactory instead
 */
TpAutomaticProxyFactory *
tp_automatic_proxy_factory_dup (void)
{
  static TpAutomaticProxyFactory *singleton = NULL;

  if (singleton != NULL)
    return g_object_ref (singleton);

  singleton = _tp_automatic_proxy_factory_new ();

  g_object_add_weak_pointer (G_OBJECT (singleton), (gpointer) &singleton);

  return singleton;
}

G_GNUC_END_IGNORE_DEPRECATIONS