summaryrefslogtreecommitdiff
path: root/lib/loudmouth/lm-message-node.c
blob: 8f5417cf9c161601e6679073ee8dadcec585d525 (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
/*
 * lm-message-node.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-message-node.h"

#include <wocky/wocky-utils.h>

static gboolean
copy_attribute (const gchar *key,
    const gchar *value,
    const gchar *prefix,
    const gchar *ns,
    gpointer user_data)
{
  WockyNode *copy = (WockyNode *) user_data;

  wocky_node_set_attribute_ns (copy, key, value, ns);
  return TRUE;
}

static WockyNode *
copy_node (WockyNode *node)
{
  WockyNode *copy;
  GSList *l;

  copy = wocky_node_new (node->name, wocky_node_get_ns (node));
  wocky_node_set_content (copy, node->content);
  wocky_node_set_language (copy, wocky_node_get_language (node));

  wocky_node_each_attribute (node, copy_attribute, copy);

  for (l = node->children; l != NULL; l = g_slist_next (l))
    {
      WockyNode *child = l->data;

      copy->children = g_slist_prepend (copy->children, copy_node (child));
    }
  copy->children = g_slist_reverse (copy->children);

  return copy;
}

LmMessageNode *
lm_message_node_ref (LmMessageNode *node)
{
  /* WockyNode is not ref counted. Return a copy of the node */
  return copy_node (node);
}

void
lm_message_node_unref (LmMessageNode *node)
{
  wocky_node_free (node);
}

void
lm_message_node_set_attribute (LmMessageNode *node,
    const gchar *name,
    const gchar *value)
{
  if (!wocky_strdiff (name, "xmlns"))
    node->ns = g_quark_from_string (value);
  else if (!wocky_strdiff (name, "xml:lang"))
    wocky_node_set_language (node, value);
  else
    wocky_node_set_attribute (node, name, value);
}

const gchar *
lm_message_node_get_attribute (LmMessageNode *node,
    const gchar *name)
{
  if (!wocky_strdiff (name, "xmlns"))
    return wocky_node_get_ns (node);
  else if (!wocky_strdiff (name, "xml:lang"))
    return wocky_node_get_language (node);

  return wocky_node_get_attribute (node, name);
}

void
lm_message_node_set_attributes (LmMessageNode *node,
    const gchar *name,
    ...)
{
  va_list args;

  g_return_if_fail (node != NULL);

  va_start (args, name);
  while (name != NULL)
    {
      const gchar *value;
      value = (const gchar *) va_arg (args, gchar *);

      lm_message_node_set_attribute (node, name, value);

      name = (const gchar *) va_arg (args, gchar *);
    }
  va_end (args);
}

LmMessageNode *
lm_message_node_add_child (LmMessageNode *node,
    const gchar *name,
    const gchar *value)
{
  return wocky_node_add_child_with_content (node, name, value);
}

LmMessageNode *
lm_message_node_get_child (LmMessageNode *node,
    const gchar *child_name)
{
  return wocky_node_get_child (node, child_name);
}

LmMessageNode *
lm_message_node_find_child (LmMessageNode *node,
    const gchar *child_name)
{
  WockyNode *found;
  GSList *l;

  found = wocky_node_get_child (node, child_name);
  if (found != NULL)
    return found;

  for (l = node->children; l != NULL; l = g_slist_next (l))
    {
      WockyNode *child = l->data;

      found = lm_message_node_find_child (child, child_name);
      if (found != NULL)
        return found;
    }

  return NULL;
}

const gchar *
lm_message_node_get_value (LmMessageNode *node)
{
  return node->content;
}

void
lm_message_node_set_value (LmMessageNode *node,
    const gchar *value)
{
  wocky_node_set_content (node, value);
}

gchar *
lm_message_node_to_string (LmMessageNode *node)
{
  return wocky_node_to_string (node);
}