1 #define __SP_METADATA_C__
3 /*
4 * SVG <metadata> implementation
5 *
6 * Authors:
7 * Kees Cook <kees@outflux.net>
8 *
9 * Copyright (C) 2004 Kees Cook
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include "sp-metadata.h"
19 #include "xml/node-iterators.h"
20 #include "document.h"
22 #include "sp-item-group.h"
24 #define noDEBUG_METADATA
25 #ifdef DEBUG_METADATA
26 # define debug(f, a...) { g_print("%s(%d) %s:", \
27 __FILE__,__LINE__,__FUNCTION__); \
28 g_print(f, ## a); \
29 g_print("\n"); \
30 }
31 #else
32 # define debug(f, a...) /**/
33 #endif
35 /* Metadata base class */
37 static void sp_metadata_class_init (SPMetadataClass *klass);
38 static void sp_metadata_init (SPMetadata *metadata);
40 static void sp_metadata_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
41 static void sp_metadata_release (SPObject *object);
42 static void sp_metadata_set (SPObject *object, unsigned int key, const gchar *value);
43 static void sp_metadata_update(SPObject *object, SPCtx *ctx, guint flags);
44 static Inkscape::XML::Node *sp_metadata_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
46 static SPObjectClass *metadata_parent_class;
48 GType
49 sp_metadata_get_type (void)
50 {
51 static GType metadata_type = 0;
53 if (!metadata_type) {
54 GTypeInfo metadata_info = {
55 sizeof (SPMetadataClass),
56 NULL, NULL,
57 (GClassInitFunc) sp_metadata_class_init,
58 NULL, NULL,
59 sizeof (SPMetadata),
60 16,
61 (GInstanceInitFunc) sp_metadata_init,
62 NULL, /* value_table */
63 };
64 metadata_type = g_type_register_static (SP_TYPE_OBJECT, "SPMetadata", &metadata_info, (GTypeFlags)0);
65 }
66 return metadata_type;
67 }
69 static void
70 sp_metadata_class_init (SPMetadataClass *klass)
71 {
72 //GObjectClass *gobject_class = (GObjectClass *)klass;
73 SPObjectClass *sp_object_class = (SPObjectClass *)klass;
75 metadata_parent_class = (SPObjectClass*)g_type_class_peek_parent (klass);
77 sp_object_class->build = sp_metadata_build;
78 sp_object_class->release = sp_metadata_release;
79 sp_object_class->write = sp_metadata_write;
80 sp_object_class->set = sp_metadata_set;
81 sp_object_class->update = sp_metadata_update;
82 }
84 static void
85 sp_metadata_init (SPMetadata *metadata)
86 {
87 debug("0x%08x",(unsigned int)metadata);
88 }
90 namespace {
92 void strip_ids_recursively(Inkscape::XML::Node *node) {
93 using Inkscape::XML::NodeSiblingIterator;
94 if ( node->type() == Inkscape::XML::ELEMENT_NODE ) {
95 node->setAttribute("id", NULL);
96 }
97 for ( NodeSiblingIterator iter=node->firstChild() ; iter ; ++iter ) {
98 strip_ids_recursively(iter);
99 }
100 }
102 }
104 /*
105 * \brief Reads the Inkscape::XML::Node, and initializes SPMetadata variables.
106 * For this to get called, our name must be associated with
107 * a repr via "sp_object_type_register". Best done through
108 * sp-object-repr.cpp's repr_name_entries array.
109 */
110 static void
111 sp_metadata_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
112 {
113 using Inkscape::XML::NodeSiblingIterator;
115 debug("0x%08x",(unsigned int)object);
117 /* clean up our mess from earlier versions; elements under rdf:RDF should not
118 * have id= attributes... */
119 static GQuark const rdf_root_name=g_quark_from_static_string("rdf:RDF");
120 for ( NodeSiblingIterator iter=repr->firstChild() ; iter ; ++iter ) {
121 if ( (GQuark)iter->code() == rdf_root_name ) {
122 strip_ids_recursively(iter);
123 }
124 }
126 if (((SPObjectClass *) metadata_parent_class)->build)
127 ((SPObjectClass *) metadata_parent_class)->build (object, document, repr);
128 }
130 /*
131 * \brief Drops any allocated memory
132 */
133 static void
134 sp_metadata_release (SPObject *object)
135 {
136 debug("0x%08x",(unsigned int)object);
138 /* handle ourself */
140 if (((SPObjectClass *) metadata_parent_class)->release)
141 ((SPObjectClass *) metadata_parent_class)->release (object);
142 }
144 /*
145 * \brief Sets a specific value in the SPMetadata
146 */
147 static void
148 sp_metadata_set (SPObject *object, unsigned int key, const gchar *value)
149 {
150 debug("0x%08x %s(%u): '%s'",(unsigned int)object,
151 sp_attribute_name(key),key,value);
152 SPMetadata * metadata;
154 metadata = SP_METADATA (object);
156 /* see if any parents need this value */
157 if (((SPObjectClass *) metadata_parent_class)->set)
158 ((SPObjectClass *) metadata_parent_class)->set (object, key, value);
159 }
161 /*
162 * \brief Receives update notifications
163 */
164 static void
165 sp_metadata_update(SPObject *object, SPCtx *ctx, guint flags)
166 {
167 debug("0x%08x",(unsigned int)object);
168 //SPMetadata *metadata = SP_METADATA(object);
170 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
171 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
173 /* do something? */
175 }
177 if (((SPObjectClass *) metadata_parent_class)->update)
178 ((SPObjectClass *) metadata_parent_class)->update(object, ctx, flags);
179 }
181 /*
182 * \brief Writes it's settings to an incoming repr object, if any
183 */
184 static Inkscape::XML::Node *
185 sp_metadata_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
186 {
187 debug("0x%08x",(unsigned int)object);
188 //SPMetadata *metadata = SP_METADATA(object);
190 // only create a repr when we're writing out an Inkscape SVG
191 if ( flags & SP_OBJECT_WRITE_EXT && repr != SP_OBJECT_REPR(object) ) {
192 if (repr) {
193 repr->mergeFrom(SP_OBJECT_REPR (object), "id");
194 } else {
195 repr = SP_OBJECT_REPR (object)->duplicate();
196 }
197 }
199 if (((SPObjectClass *) metadata_parent_class)->write)
200 ((SPObjectClass *) metadata_parent_class)->write(object, repr, flags);
202 return repr;
203 }
205 /*
206 * \brief Retrieves the metadata object associated with a document
207 */
208 SPMetadata *
209 sp_document_metadata (SPDocument *document)
210 {
211 SPObject *nv;
213 g_return_val_if_fail (document != NULL, NULL);
215 nv = sp_item_group_get_child_by_name ((SPGroup *) document->root, NULL,
216 "metadata");
217 g_assert (nv != NULL);
219 return (SPMetadata *)nv;
220 }
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :