sp-metadata.cpp revision b2891d33fd63168ae5b124fc6ac3e781e9dd9283
1628N/A#define __SP_METADATA_C__
1628N/A
1628N/A/*
1628N/A * SVG <metadata> implementation
1628N/A *
1628N/A * Authors:
1628N/A * Kees Cook <kees@outflux.net>
1628N/A *
1628N/A * Copyright (C) 2004 Kees Cook
1628N/A *
1628N/A * Released under GNU GPL, read the file 'COPYING' for more information
1628N/A */
1628N/A
1628N/A#ifdef HAVE_CONFIG_H
1628N/A# include "config.h"
1628N/A#endif
1628N/A
1628N/A#include "sp-metadata.h"
1628N/A#include "xml/node-iterators.h"
1628N/A#include "document.h"
1628N/A
1628N/A#include "sp-item-group.h"
1628N/A
1628N/A#define noDEBUG_METADATA
4802N/A#ifdef DEBUG_METADATA
1628N/A# define debug(f, a...) { g_print("%s(%d) %s:", \
1628N/A __FILE__,__LINE__,__FUNCTION__); \
1628N/A g_print(f, ## a); \
3988N/A g_print("\n"); \
1628N/A }
1628N/A#else
1628N/A# define debug(f, a...) /**/
1628N/A#endif
1628N/A
1628N/A/* Metadata base class */
1628N/A
2624N/Astatic void sp_metadata_class_init (SPMetadataClass *klass);
1628N/Astatic void sp_metadata_init (SPMetadata *metadata);
1628N/A
1628N/Astatic void sp_metadata_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
1628N/Astatic void sp_metadata_release (SPObject *object);
1628N/Astatic void sp_metadata_set (SPObject *object, unsigned int key, const gchar *value);
1628N/Astatic void sp_metadata_update(SPObject *object, SPCtx *ctx, guint flags);
1628N/Astatic Inkscape::XML::Node *sp_metadata_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
1628N/A
3853N/Astatic SPObjectClass *metadata_parent_class;
1628N/A
1628N/AGType
1628N/Asp_metadata_get_type (void)
1628N/A{
1628N/A static GType metadata_type = 0;
1628N/A
1628N/A if (!metadata_type) {
1628N/A GTypeInfo metadata_info = {
1628N/A sizeof (SPMetadataClass),
1628N/A NULL, NULL,
1628N/A (GClassInitFunc) sp_metadata_class_init,
1628N/A NULL, NULL,
1628N/A sizeof (SPMetadata),
1628N/A 16,
1628N/A (GInstanceInitFunc) sp_metadata_init,
1628N/A NULL, /* value_table */
3988N/A };
3853N/A metadata_type = g_type_register_static (SP_TYPE_OBJECT, "SPMetadata", &metadata_info, (GTypeFlags)0);
1628N/A }
4802N/A return metadata_type;
1628N/A}
1628N/A
1628N/Astatic void
3853N/Asp_metadata_class_init (SPMetadataClass *klass)
1628N/A{
1628N/A //GObjectClass *gobject_class = (GObjectClass *)klass;
1628N/A SPObjectClass *sp_object_class = (SPObjectClass *)klass;
1628N/A
1628N/A metadata_parent_class = (SPObjectClass*)g_type_class_peek_parent (klass);
1628N/A
1628N/A sp_object_class->build = sp_metadata_build;
1628N/A sp_object_class->release = sp_metadata_release;
1628N/A sp_object_class->write = sp_metadata_write;
1628N/A sp_object_class->set = sp_metadata_set;
1628N/A sp_object_class->update = sp_metadata_update;
1628N/A}
1628N/A
1628N/Astatic void
1628N/Asp_metadata_init (SPMetadata *metadata)
3853N/A{
1628N/A (void)metadata;
1628N/A debug("0x%08x",(unsigned int)metadata);
1628N/A}
1628N/A
1628N/Anamespace {
1628N/A
1628N/Avoid strip_ids_recursively(Inkscape::XML::Node *node) {
1628N/A using Inkscape::XML::NodeSiblingIterator;
1628N/A if ( node->type() == Inkscape::XML::ELEMENT_NODE ) {
1628N/A node->setAttribute("id", NULL);
1628N/A }
1628N/A for ( NodeSiblingIterator iter=node->firstChild() ; iter ; ++iter ) {
1628N/A strip_ids_recursively(iter);
1628N/A }
1628N/A}
1628N/A
3853N/A}
3988N/A
2350N/A/*
1628N/A * \brief Reads the Inkscape::XML::Node, and initializes SPMetadata variables.
1628N/A * For this to get called, our name must be associated with
1628N/A * a repr via "sp_object_type_register". Best done through
1628N/A * sp-object-repr.cpp's repr_name_entries array.
1628N/A */
1628N/Astatic void
1628N/Asp_metadata_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
2606N/A{
2606N/A using Inkscape::XML::NodeSiblingIterator;
1628N/A
2606N/A debug("0x%08x",(unsigned int)object);
1628N/A
1628N/A /* clean up our mess from earlier versions; elements under rdf:RDF should not
1628N/A * have id= attributes... */
static GQuark const rdf_root_name=g_quark_from_static_string("rdf:RDF");
for ( NodeSiblingIterator iter=repr->firstChild() ; iter ; ++iter ) {
if ( (GQuark)iter->code() == rdf_root_name ) {
strip_ids_recursively(iter);
}
}
if (((SPObjectClass *) metadata_parent_class)->build)
((SPObjectClass *) metadata_parent_class)->build (object, document, repr);
}
/*
* \brief Drops any allocated memory
*/
static void
sp_metadata_release (SPObject *object)
{
debug("0x%08x",(unsigned int)object);
/* handle ourself */
if (((SPObjectClass *) metadata_parent_class)->release)
((SPObjectClass *) metadata_parent_class)->release (object);
}
/*
* \brief Sets a specific value in the SPMetadata
*/
static void
sp_metadata_set (SPObject *object, unsigned int key, const gchar *value)
{
debug("0x%08x %s(%u): '%s'",(unsigned int)object,
sp_attribute_name(key),key,value);
SPMetadata * metadata;
metadata = SP_METADATA (object);
/* see if any parents need this value */
if (((SPObjectClass *) metadata_parent_class)->set)
((SPObjectClass *) metadata_parent_class)->set (object, key, value);
}
/*
* \brief Receives update notifications
*/
static void
sp_metadata_update(SPObject *object, SPCtx *ctx, guint flags)
{
debug("0x%08x",(unsigned int)object);
//SPMetadata *metadata = SP_METADATA(object);
if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG |
SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
/* do something? */
}
if (((SPObjectClass *) metadata_parent_class)->update)
((SPObjectClass *) metadata_parent_class)->update(object, ctx, flags);
}
/*
* \brief Writes it's settings to an incoming repr object, if any
*/
static Inkscape::XML::Node *
sp_metadata_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
{
debug("0x%08x",(unsigned int)object);
//SPMetadata *metadata = SP_METADATA(object);
if ( repr != SP_OBJECT_REPR(object) ) {
if (repr) {
repr->mergeFrom(SP_OBJECT_REPR (object), "id");
} else {
repr = SP_OBJECT_REPR (object)->duplicate(doc);
}
}
if (((SPObjectClass *) metadata_parent_class)->write) {
((SPObjectClass *) metadata_parent_class)->write(object, doc, repr, flags);
}
return repr;
}
/*
* \brief Retrieves the metadata object associated with a document
*/
SPMetadata *
sp_document_metadata (SPDocument *document)
{
SPObject *nv;
g_return_val_if_fail (document != NULL, NULL);
nv = sp_item_group_get_child_by_name ((SPGroup *) document->root, NULL,
"metadata");
g_assert (nv != NULL);
return (SPMetadata *)nv;
}
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :