File: xmp-encode.c

package info (click to toggle)
gimp 2.6.10-1%2Bsqueeze4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 128,948 kB
  • ctags: 55,327
  • sloc: ansic: 635,142; lisp: 10,678; sh: 10,420; makefile: 9,729; python: 3,366; perl: 2,713; xml: 1,152; yacc: 554; lex: 339
file content (246 lines) | stat: -rw-r--r-- 8,661 bytes parent folder | download | duplicates (2)
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
/* xmp-encode.c - generate XMP metadata from the tree model
 *
 * Copyright (C) 2005, Raphaƫl Quinet <raphael@gimp.org>
 *
 * 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 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>

#include <gtk/gtk.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"

#include "xmp-encode.h"
#include "xmp-schemas.h"

static void
gen_schema_start (GString         *buffer,
                  const XMPSchema *schema)
{
  g_string_append_printf (buffer, " <rdf:Description xmlns:%s='%s'>\n",
                          schema->prefix, schema->uri);
}

static void
gen_schema_end (GString *buffer)
{
  g_string_append (buffer, " </rdf:Description>\n\n");
}

static void
gen_property (GString            *buffer,
              const XMPSchema    *schema,
              const XMPProperty  *property,
              const gchar       **value_array)
{
  gint         i;
  const gchar *ns_prefix;
  gchar       *escaped_value;

  switch (property->type)
    {
    case XMP_TYPE_BOOLEAN:
    case XMP_TYPE_DATE:
    case XMP_TYPE_INTEGER:
    case XMP_TYPE_REAL:
    case XMP_TYPE_MIME_TYPE:
    case XMP_TYPE_TEXT:
    case XMP_TYPE_RATIONAL:
      escaped_value = g_markup_escape_text (value_array[0], -1);
      g_string_append_printf (buffer, "  <%s:%s>%s</%s:%s>\n",
                              schema->prefix, property->name,
                              escaped_value,
                              schema->prefix, property->name);
      g_free (escaped_value);
      break;

    case XMP_TYPE_LOCALE_BAG:
    case XMP_TYPE_TEXT_BAG:
    case XMP_TYPE_XPATH_BAG:
    case XMP_TYPE_JOB_BAG:
      g_string_append_printf (buffer, "  <%s:%s>\n   <rdf:Bag>\n",
                              schema->prefix, property->name);
      for (i = 0; value_array[i] != NULL; i++)
        {
          escaped_value = g_markup_escape_text (value_array[i], -1);
          g_string_append_printf (buffer, "    <rdf:li>%s</rdf:li>\n",
                                  escaped_value);
          g_free (escaped_value);
        }
      g_string_append_printf (buffer, "   </rdf:Bag>\n  </%s:%s>\n",
                              schema->prefix, property->name);
      break;

    case XMP_TYPE_INTEGER_SEQ:
    case XMP_TYPE_TEXT_SEQ:
    case XMP_TYPE_RESOURCE_EVENT_SEQ:
    case XMP_TYPE_RATIONAL_SEQ:
      g_string_append_printf (buffer, "  <%s:%s>\n   <rdf:Seq>\n",
                              schema->prefix, property->name);
      for (i = 0; value_array[i] != NULL; i++)
        {
          escaped_value = g_markup_escape_text (value_array[i], -1);
          g_string_append_printf (buffer, "    <rdf:li>%s</rdf:li>\n",
                                  escaped_value);
          g_free (escaped_value);
        }
      g_string_append_printf (buffer, "   </rdf:Seq>\n  </%s:%s>\n",
                              schema->prefix, property->name);
      break;

    case XMP_TYPE_LANG_ALT:
      g_string_append_printf (buffer, "  <%s:%s>\n   <rdf:Alt>\n",
                              schema->prefix, property->name);
      for (i = 0; value_array[i] != NULL; i += 2)
        {
          escaped_value = g_markup_escape_text (value_array[i + 1], -1);
          g_string_append_printf (buffer,
                                  "    <rdf:li xml:lang='%s'>%s</rdf:li>\n",
                                  value_array[i], escaped_value);
          g_free (escaped_value);
        }
      g_string_append_printf (buffer, "   </rdf:Alt>\n  </%s:%s>\n",
                              schema->prefix, property->name);
      break;

    case XMP_TYPE_URI:
      g_string_append_printf (buffer, "  <%s:%s rdf:resource='%s' />\n",
                              schema->prefix, property->name, value_array[0]);
      break;

    case XMP_TYPE_RESOURCE_REF:
    case XMP_TYPE_DIMENSIONS:
    case XMP_TYPE_GPS_COORDINATE:
    case XMP_TYPE_FLASH:
    case XMP_TYPE_OECF_SFR:
    case XMP_TYPE_CFA_PATTERN:
    case XMP_TYPE_DEVICE_SETTINGS:
    case XMP_TYPE_CONTACT_INFO:
    case XMP_TYPE_GENERIC_STRUCTURE:
      if (value_array[0] && value_array[1]
          && !! strcmp (value_array[1], schema->uri))
        {
          g_string_append_printf (buffer,
                                  "  <%s:%s rdf:parseType='Resource'\n"
                                  "   xmlns:%s='%s'>\n",
                                  schema->prefix, property->name,
                                  value_array[0], value_array[1]);
          ns_prefix = value_array[0];
        }
      else
        {
          g_string_append_printf (buffer,
                                  "  <%s:%s rdf:parseType='Resource'>\n",
                                  schema->prefix, property->name);
          ns_prefix = schema->prefix;
        }
      if (value_array[0] && value_array[1])
        for (i = 2; value_array[i] != NULL; i += 2)
          {
            escaped_value = g_markup_escape_text (value_array[i + 1], -1);
            g_string_append_printf (buffer, "   <%s:%s>%s</%s:%s>\n",
                                    ns_prefix, value_array[i],
                                    escaped_value,
                                    ns_prefix, value_array[i]);
            g_free (escaped_value);
          }
      g_string_append_printf (buffer, "  </%s:%s>\n",
                              schema->prefix, property->name);
      break;

    case XMP_TYPE_THUMBNAIL_ALT:
      g_printerr ("FIXME: output not implemented yet (%s:%s)",
                  schema->prefix, property->name);
      break;

    case XMP_TYPE_UNKNOWN:
      g_printerr ("Unknown property type for %s", property->name);
      break;
    }
}

/**
 * xmp_generate_block:
 * @xmp_model: An #XMPModel
 * @buffer: A #GString in which the generated XMP packet will be stored.
 *
 * Generate XMP packet from xmp_model and store it in the supplied buffer.
 *
 */
void
xmp_generate_packet (XMPModel *xmp_model,
                     GString  *buffer)
{
  GtkTreeModel    *model;
  GtkTreeIter      iter;
  GtkTreeIter      child;
  const XMPSchema *schema;
  gpointer         saved_ref;

  g_return_if_fail (xmp_model != NULL);
  g_return_if_fail (buffer != NULL);
  model = xmp_model_get_tree_model (xmp_model);
  g_return_if_fail (model != NULL);
  if (! buffer)
    buffer = g_string_new (NULL);
  buffer = g_string_append (buffer,
    "<?xpacket begin='\357\273\277' id='W5M0MpCehiHzreSzNTczkc9d'?>\n"
    "<x:xmpmeta xmlns:x='adobe:ns:meta/'>\n"
    "<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n"
    "\n");
  /* generate the contents of the XMP packet */
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
    {
      do
        {
          gtk_tree_model_get (model, &iter,
                              COL_XMP_TYPE_XREF, &schema,
                              -1);
          gen_schema_start (buffer, schema);
          if (gtk_tree_model_iter_children (model, &child, &iter))
            {
              saved_ref = NULL;
              do
                {
                  const XMPProperty  *property;
                  const gchar       **value_array;

                  gtk_tree_model_get (model, &child,
                                      COL_XMP_TYPE_XREF, &property,
                                      COL_XMP_VALUE_RAW, &value_array,
                                      -1);
                  /* do not process structured types multiple times */
                  if (saved_ref != value_array)
                    {
                      saved_ref = value_array;
                      g_return_if_fail (property->name != NULL);
                      gen_property (buffer, schema, property, value_array);
                    }
                }
              while (gtk_tree_model_iter_next (model, &child));
            }
          gen_schema_end (buffer);
        }
      while (gtk_tree_model_iter_next (model, &iter));
    }
  g_string_append (buffer, "</rdf:RDF>\n</x:xmpmeta>\n<?xpacket end='r'?>\n");
}