summaryrefslogtreecommitdiff
path: root/salut/caps-manager.c
blob: 7a77e4c4ce28e3c0bf55c54111cd66fc37db4e69 (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
/*
 * caps-manager.c - Source for YstCapsManager
 *
 * Copyright (C) 2011 Intel Corp.
 *
 * 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 "caps-manager.h"

#include <string.h>

#include <telepathy-glib/channel-manager.h>

#include <wocky/wocky-data-form.h>
#include <wocky/wocky-namespaces.h>

#include <salut/caps-channel-manager.h>

#include "utils.h"

#define DEBUG(msg, ...) \
  g_debug ("%s: " msg, G_STRFUNC, ##__VA_ARGS__)

#define CHANNEL_PREFIX "com.meego.xpmn.ytstenut.Channel/"
#define UID CHANNEL_PREFIX "uid/"
#define TYPE CHANNEL_PREFIX "type/"
#define NAME CHANNEL_PREFIX "name/"
#define CAPS CHANNEL_PREFIX "caps/"
#define INTERESTED CHANNEL_PREFIX "interested/"

static void channel_manager_iface_init (gpointer g_iface, gpointer data);
static void caps_channel_manager_iface_init (gpointer g_iface, gpointer data);

G_DEFINE_TYPE_WITH_CODE (YtstCapsManager, ytst_caps_manager, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_CHANNEL_MANAGER, channel_manager_iface_init);
    G_IMPLEMENT_INTERFACE (GABBLE_TYPE_CAPS_CHANNEL_MANAGER, caps_channel_manager_iface_init);
    )

static void
ytst_caps_manager_init (YtstCapsManager *object)
{
}

static void
ytst_caps_manager_class_init (YtstCapsManagerClass *klass)
{
}

static void
add_value_to_field (gpointer data,
    gpointer user_data)
{
  WockyNode *field = user_data;
  WockyNode *tmp;
  const gchar *content = data;

  tmp = wocky_node_add_child (field, "value");
  wocky_node_set_content (tmp, content);
}

static WockyDataForm *
make_new_data_form (const gchar *uid,
    const gchar *type,
    GPtrArray *names,
    GPtrArray *caps)
{
  WockyNode *node, *tmp;
  WockyDataForm *out;
  /* the default */
  const gchar *yts_type = type != NULL ? type : "application";
  gchar *form_type = g_strdup_printf ("%s%s", SERVICE_PREFIX, uid);

  node = wocky_node_new ("x", WOCKY_XMPP_NS_DATA);
  wocky_node_add_build (node,
      '@', "type", "result",

      '(', "field",
        '@', "var", "FORM_TYPE",
        '@', "type", "hidden",
        '(', "value",
          '$', form_type,
        ')',
      ')',

      '(', "field",
        '@', "var", "type",
        '(', "value",
          '$', yts_type,
        ')',
      ')',

      NULL);

  g_free (form_type);

  /* do the next two by hand as we'll need to add the values
   * manually */
  tmp = wocky_node_add_child (node, "field");
  wocky_node_set_attribute (tmp, "var", "name");
  g_ptr_array_foreach (names, add_value_to_field, tmp);

  tmp = wocky_node_add_child (node, "field");
  wocky_node_set_attribute (tmp, "var", "capabilities");
  g_ptr_array_foreach (caps, add_value_to_field, tmp);

  out = wocky_data_form_new_from_node (node, NULL);

  wocky_node_free (node);

  return out;
}

static void
ytst_caps_manager_represent_client (GabbleCapsChannelManager *manager,
    const gchar *client_name,
    const GPtrArray *filters,
    const gchar * const *cap_tokens,
    GabbleCapabilitySet *cap_set,
    GPtrArray *data_forms)
{
  const gchar * const *t;

  WockyDataForm *form;
  const gchar *uid = NULL;
  const gchar *yts_type = NULL;
  GPtrArray *names = g_ptr_array_new ();
  GPtrArray *caps = g_ptr_array_new ();

  for (t = cap_tokens; t != NULL && *t != NULL; t++)
    {
      const gchar *cap = *t;
      gchar *feature;

      if (g_str_has_prefix (*t, INTERESTED))
        {
          cap = *t + strlen (INTERESTED);
          feature = g_strdup_printf ("%s%s+notify", CAPS_FEATURE_PREFIX, cap);
          gabble_capability_set_add (cap_set, feature);
          g_free (feature);
        }
      else if (g_str_has_prefix (*t, UID))
        {
          uid = *t + strlen (UID);
        }
      else if (g_str_has_prefix (*t, TYPE))
        {
          yts_type = *t + strlen (TYPE);
        }
      else if (g_str_has_prefix (*t, NAME))
        {
          g_ptr_array_add (names, (gpointer) (*t + strlen (NAME)));
        }
      else if (g_str_has_prefix (*t, CAPS))
        {
          g_ptr_array_add (caps, (gpointer) (*t + strlen (CAPS)));
        }
    }

  if (uid != NULL)
    {
      form = make_new_data_form (uid, yts_type, names, caps);
      g_ptr_array_add (data_forms, form);
    }

  g_ptr_array_unref (names);
  g_ptr_array_unref (caps);
}

static void
caps_channel_manager_iface_init (
    gpointer g_iface,
    gpointer data G_GNUC_UNUSED)
{
  GabbleCapsChannelManagerIface *iface = g_iface;

  iface->represent_client = ytst_caps_manager_represent_client;
}

static void
channel_manager_iface_init (
    gpointer g_iface,
    gpointer data G_GNUC_UNUSED)
{
}