summaryrefslogtreecommitdiff
path: root/lib/maps-osm-object.c
blob: 165477f6267f7617349d8d246d1bdcc46d8a431a (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (c) 2015 Marcus Lundblad
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps 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 General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>
 *
 * Author: Marcus Lundblad <ml@update.uu.se>
 */

#include "maps-osm-object.h"

struct _MapsOSMObjectPrivate
{
  guint64 id;
  guint version;
  guint64 changeset;

  GHashTable *tags;
};

enum {
  PROP_0,

  PROP_ID,
  PROP_VERSION,
  PROP_CHANGESET
};

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (MapsOSMObject, maps_osm_object,
                                     G_TYPE_OBJECT)

static void
maps_osm_object_set_property (GObject      *object,
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  MapsOSMObject *osm_object = MAPS_OSMOBJECT (object);
  MapsOSMObjectPrivate *priv =
    maps_osm_object_get_instance_private (osm_object);
  
  switch (property_id)
    {
    case PROP_ID:
      priv->id = g_value_get_uint64 (value);
      break;

    case PROP_VERSION:
      priv->version = g_value_get_uint (value);
      break;

    case PROP_CHANGESET:
      priv->changeset = g_value_get_uint64 (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
maps_osm_object_get_property (GObject    *object,
                              guint       property_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  MapsOSMObject *osm_object = MAPS_OSMOBJECT (object);
  MapsOSMObjectPrivate *priv =
    maps_osm_object_get_instance_private (osm_object);

  switch (property_id)
    {
    case PROP_ID:
      g_value_set_uint64 (value, priv->id);
      break;

    case PROP_VERSION:
      g_value_set_uint (value, priv->version);
      break;

    case PROP_CHANGESET:
      g_value_set_uint64 (value, priv->changeset);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
maps_osm_object_dispose (GObject *object)
{
  MapsOSMObject *osm_object = MAPS_OSMOBJECT (object);
  MapsOSMObjectPrivate *priv =
    maps_osm_object_get_instance_private (osm_object);
  
  g_hash_table_destroy (priv->tags);
  priv->tags = NULL;

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

/* base implementation returning no object-specific XML attributes */
static GHashTable *
maps_osm_object_get_xml_attributes (const MapsOSMObject *object)
{
  return NULL;
}

/* base implementation return no object-specific child XML nodes */
static xmlNodePtr
maps_osm_object_get_xml_child_nodes (const MapsOSMObject *object)
{
  return NULL;
}

static void
maps_osm_object_class_init (MapsOSMObjectClass *klass)
{
  GObjectClass *maps_class = G_OBJECT_CLASS (klass);
  MapsOSMObjectClass *object_class = MAPS_OSMOBJECT_CLASS (klass);
  GParamSpec *pspec;

  maps_class->dispose = maps_osm_object_dispose;
  maps_class->get_property = maps_osm_object_get_property;
  maps_class->set_property = maps_osm_object_set_property;
  object_class->get_xml_attributes = maps_osm_object_get_xml_attributes;
  object_class->get_xml_child_nodes = maps_osm_object_get_xml_child_nodes;
  
  /**
   * MapsOSMObject:id:
   *
   * The OSM id of the object.
   */
  pspec = g_param_spec_uint64 ("id",
                               "ID",
                               "ID",
                               0,
                               G_MAXUINT64,
                               0,
                               G_PARAM_READWRITE);
  g_object_class_install_property (maps_class, PROP_ID, pspec);

  /**
   * MapsOSMObject:version:
   *
   * The latest OSM version of the object.
   */
  pspec = g_param_spec_uint("version",
                            "Version",
                            "Version",
                            0,
                            G_MAXUINT,
                            0,
                            G_PARAM_READWRITE);
  g_object_class_install_property (maps_class, PROP_VERSION, pspec);

  /**
   * MapsOSMObject:changeset:
   *
   * The OSM changeset for the current upload of the object.
   */
  pspec = g_param_spec_uint64 ("changeset",
                               "Changeset",
                               "Changeset",
                               0,
                               G_MAXUINT64,
                               0,
                               G_PARAM_READWRITE);
  g_object_class_install_property (maps_class, PROP_CHANGESET, pspec);
}
  
static void
maps_osm_object_init (MapsOSMObject *object)
{
  MapsOSMObjectPrivate *priv = maps_osm_object_get_instance_private (object);

  priv->tags = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
}

const char *
maps_osm_object_get_tag (const MapsOSMObject *object, const char *key)
{
  MapsOSMObjectPrivate *priv = maps_osm_object_get_instance_private ((MapsOSMObject *) object);

  g_return_val_if_fail (key != NULL, NULL);

  return g_hash_table_lookup (priv->tags, key);
}

void
maps_osm_object_set_tag (MapsOSMObject *object, const char *key,
                         const char *value)
{
  MapsOSMObjectPrivate *priv = maps_osm_object_get_instance_private (object);

  g_return_if_fail (key != NULL);

  g_hash_table_insert (priv->tags, g_strdup (key), g_strdup (value));
}

void
maps_osm_object_delete_tag (MapsOSMObject *object, const char *key)
{
  MapsOSMObjectPrivate *priv = maps_osm_object_get_instance_private (object);

  g_return_if_fail (key != NULL);

  g_hash_table_remove (priv->tags, key);
}

static void
maps_osm_object_foreach_tag (gpointer key, gpointer value, gpointer user_data)
{
  const xmlChar *name = (const xmlChar *) key;
  const xmlChar *val = (const xmlChar *) value;
  xmlNodePtr object_node = (xmlNodePtr) user_data;

  /* skip tag if it has an empty placeholder value */
  if (val && *val)
    {
      xmlNodePtr tag_node;

      tag_node = xmlNewNode (NULL, (xmlChar *) "tag");
      xmlNewProp (tag_node, (xmlChar *) "k", name);
      xmlNewProp (tag_node, (xmlChar *) "v", val);
      xmlAddChild (object_node, tag_node);
    }
}

static void
maps_osm_object_foreach_type_attr (gpointer key, gpointer value,
                                   gpointer user_data)
{
  const xmlChar *name = (const xmlChar *) key;
  const xmlChar *val = (const xmlChar *) value;
  xmlNodePtr object_node = (xmlNodePtr) user_data;

  xmlNewProp (object_node, name, val);
}

xmlDocPtr
maps_osm_object_to_xml (const MapsOSMObject *object)
{
  MapsOSMObjectPrivate *priv;
  xmlDocPtr doc;
  xmlNodePtr osm_node;
  xmlNodePtr object_node;
  const char *type;
  GHashTable *type_attrs;
  xmlNodePtr type_sub_nodes;

  doc = xmlNewDoc ((xmlChar *) "1.0");
  osm_node = xmlNewNode (NULL, (xmlChar *) "osm");
  priv = (MapsOSMObjectPrivate *) maps_osm_object_get_instance_private ((MapsOSMObject *) object);
  type = MAPS_OSMOBJECT_GET_CLASS ((MapsOSMObject *) object)->get_xml_tag_name ();
  object_node = xmlNewNode (NULL, (const xmlChar *) type);

  /* add common OSM attributes */
  if (priv->id != 0)
    {
      char buf[32];
      g_snprintf (buf, 32, "%" G_GUINT64_FORMAT, priv->id);
      xmlNewProp (object_node, (xmlChar *) "id", (xmlChar *) buf);
    }
    
  if (priv->version != 0)
    {
      char buf[16];
      g_snprintf (buf, 16, "%d", priv->version);
      xmlNewProp (object_node, (xmlChar *) "version", (xmlChar *) buf);
    }
    
  if (priv->changeset != 0)
    {
      char buf[32];
      g_snprintf (buf, 32, "%" G_GUINT64_FORMAT, priv->changeset);
      xmlNewProp (object_node, (xmlChar *) "changeset", (xmlChar *) buf);
    }

  /* add OSM tags */
  g_hash_table_foreach (priv->tags, maps_osm_object_foreach_tag, object_node);
  
  /* add type-specific attributes */
  type_attrs = MAPS_OSMOBJECT_GET_CLASS ((MapsOSMObject *) object)->get_xml_attributes (object);
  if (type_attrs)
    {
      g_hash_table_foreach (type_attrs, maps_osm_object_foreach_type_attr,
                            object_node);
      g_hash_table_destroy (type_attrs);
    }
 
  /* add type-specific sub-nodes */
  type_sub_nodes =
    MAPS_OSMOBJECT_GET_CLASS ((MapsOSMObject *) object)->get_xml_child_nodes (object);
  if (type_sub_nodes)
    xmlAddChildList (object_node, type_sub_nodes);

  /* add type node to top node */
  xmlAddChild (osm_node, object_node);
  xmlDocSetRootElement (doc, osm_node);
  
  return doc;
}


char *
maps_osm_object_serialize (const MapsOSMObject *object)
{
  xmlDocPtr doc;
  xmlChar *result;
  int size;
  
  doc = maps_osm_object_to_xml (object);
  xmlDocDumpMemory (doc, &result, &size);
  xmlFreeDoc (doc);
  
  return (char *) result;
}

/**
 * maps_osm_object_get_tags:
 *
 * Returns: (element-type utf8 utf8): a hash table with key/values
 */
const GHashTable *
maps_osm_object_get_tags (const MapsOSMObject *object)
{
  const MapsOSMObjectPrivate *priv =
    maps_osm_object_get_instance_private ((MapsOSMObject *) object);

  return priv->tags;
}