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::Document *doc, 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 (void)metadata;
88 debug("0x%08x",(unsigned int)metadata);
89 }
91 namespace {
93 void strip_ids_recursively(Inkscape::XML::Node *node) {
94 using Inkscape::XML::NodeSiblingIterator;
95 if ( node->type() == Inkscape::XML::ELEMENT_NODE ) {
96 node->setAttribute("id", NULL);
97 }
98 for ( NodeSiblingIterator iter=node->firstChild() ; iter ; ++iter ) {
99 strip_ids_recursively(iter);
100 }
101 }
103 }
105 /*
106 * \brief Reads the Inkscape::XML::Node, and initializes SPMetadata variables.
107 * For this to get called, our name must be associated with
108 * a repr via "sp_object_type_register". Best done through
109 * sp-object-repr.cpp's repr_name_entries array.
110 */
111 static void
112 sp_metadata_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
113 {
114 using Inkscape::XML::NodeSiblingIterator;
116 debug("0x%08x",(unsigned int)object);
118 /* clean up our mess from earlier versions; elements under rdf:RDF should not
119 * have id= attributes... */
120 static GQuark const rdf_root_name=g_quark_from_static_string("rdf:RDF");
121 for ( NodeSiblingIterator iter=repr->firstChild() ; iter ; ++iter ) {
122 if ( (GQuark)iter->code() == rdf_root_name ) {
123 strip_ids_recursively(iter);
124 }
125 }
127 if (((SPObjectClass *) metadata_parent_class)->build)
128 ((SPObjectClass *) metadata_parent_class)->build (object, document, repr);
129 }
131 /*
132 * \brief Drops any allocated memory
133 */
134 static void
135 sp_metadata_release (SPObject *object)
136 {
137 debug("0x%08x",(unsigned int)object);
139 /* handle ourself */
141 if (((SPObjectClass *) metadata_parent_class)->release)
142 ((SPObjectClass *) metadata_parent_class)->release (object);
143 }
145 /*
146 * \brief Sets a specific value in the SPMetadata
147 */
148 static void
149 sp_metadata_set (SPObject *object, unsigned int key, const gchar *value)
150 {
151 debug("0x%08x %s(%u): '%s'",(unsigned int)object,
152 sp_attribute_name(key),key,value);
153 SPMetadata * metadata;
155 metadata = SP_METADATA (object);
157 /* see if any parents need this value */
158 if (((SPObjectClass *) metadata_parent_class)->set)
159 ((SPObjectClass *) metadata_parent_class)->set (object, key, value);
160 }
162 /*
163 * \brief Receives update notifications
164 */
165 static void
166 sp_metadata_update(SPObject *object, SPCtx *ctx, guint flags)
167 {
168 debug("0x%08x",(unsigned int)object);
169 //SPMetadata *metadata = SP_METADATA(object);
171 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
172 SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
174 /* do something? */
176 }
178 if (((SPObjectClass *) metadata_parent_class)->update)
179 ((SPObjectClass *) metadata_parent_class)->update(object, ctx, flags);
180 }
182 /*
183 * \brief Writes it's settings to an incoming repr object, if any
184 */
185 static Inkscape::XML::Node *
186 sp_metadata_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
187 {
188 debug("0x%08x",(unsigned int)object);
189 //SPMetadata *metadata = SP_METADATA(object);
191 if ( 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(doc);
196 }
197 }
199 if (((SPObjectClass *) metadata_parent_class)->write) {
200 ((SPObjectClass *) metadata_parent_class)->write(object, doc, repr, flags);
201 }
203 return repr;
204 }
206 /*
207 * \brief Retrieves the metadata object associated with a document
208 */
209 SPMetadata *
210 sp_document_metadata (SPDocument *document)
211 {
212 SPObject *nv;
214 g_return_val_if_fail (document != NULL, NULL);
216 nv = sp_item_group_get_child_by_name ((SPGroup *) document->root, NULL,
217 "metadata");
218 g_assert (nv != NULL);
220 return (SPMetadata *)nv;
221 }
224 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :