1 /*
2  *  Copyright (c) 2007,2009 Cyrille Berger <cberger@cberger.net>
3  *
4  *  This library is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published by
6  *  the Free Software Foundation; either version 2.1 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_meta_data_schema_registry.h"
20 
21 #include <QString>
22 
23 
24 #include <KoResourcePaths.h>
25 
26 #include "kis_debug.h"
27 #include "kis_meta_data_schema_p.h"
28 
29 using namespace KisMetaData;
30 
31 // ---- Schema Registry ---- //
32 
33 struct Q_DECL_HIDDEN SchemaRegistry::Private {
34     static SchemaRegistry *singleton;
35     QHash<QString, Schema*> uri2Schema;
36     QHash<QString, Schema*> prefix2Schema;
37 };
38 
39 SchemaRegistry *SchemaRegistry::Private::singleton = 0;
40 
instance()41 SchemaRegistry* SchemaRegistry::instance()
42 {
43     if (SchemaRegistry::Private::singleton == 0) {
44         SchemaRegistry::Private::singleton = new SchemaRegistry();
45     }
46     return SchemaRegistry::Private::singleton;
47 }
48 
SchemaRegistry()49 SchemaRegistry::SchemaRegistry()
50     : d(new Private)
51 {
52     KoResourcePaths::addResourceType("metadata_schema", "data", "/metadata/schemas/");
53 
54     QStringList schemasFilenames = KoResourcePaths::findAllResources("metadata_schema", "*.schema");
55 
56     Q_FOREACH (const QString& fileName, schemasFilenames) {
57         Schema* schema = new Schema();
58         schema->d->load(fileName);
59         if (schemaFromUri(schema->uri())) {
60             errMetaData << "Schema already exist uri: " << schema->uri();
61             delete schema;
62         } else if (schemaFromPrefix(schema->prefix())) {
63             errMetaData << "Schema already exist prefix: " << schema->prefix();
64             delete schema;
65         } else {
66             d->uri2Schema[schema->uri()] = schema;
67             d->prefix2Schema[schema->prefix()] = schema;
68         }
69     }
70 
71     // DEPRECATED WRITE A SCHEMA FOR EACH OF THEM
72     create(Schema::MakerNoteSchemaUri, "mkn");
73     create(Schema::IPTCSchemaUri, "Iptc4xmpCore");
74     create(Schema::PhotoshopSchemaUri, "photoshop");
75 }
76 
~SchemaRegistry()77 SchemaRegistry::~SchemaRegistry()
78 {
79     delete d;
80 }
81 
82 
schemaFromUri(const QString & uri) const83 const Schema* SchemaRegistry::schemaFromUri(const QString & uri) const
84 {
85     return d->uri2Schema[uri];
86 }
87 
schemaFromPrefix(const QString & prefix) const88 const Schema* SchemaRegistry::schemaFromPrefix(const QString & prefix) const
89 {
90     return d->prefix2Schema[prefix];
91 }
92 
create(const QString & uri,const QString & prefix)93 const Schema* SchemaRegistry::create(const QString & uri, const QString & prefix)
94 {
95     // First search for the schema
96     const Schema* schema = schemaFromUri(uri);
97     if (schema) {
98         return schema;
99     }
100     // Second search for the prefix
101     schema = schemaFromPrefix(prefix);
102     if (schema) {
103         return 0; // A schema with the same prefix already exist
104     }
105     // The schema doesn't exist yet, create it
106     Schema* nschema = new Schema(uri, prefix);
107     d->uri2Schema[uri] = nschema;
108     d->prefix2Schema[prefix] = nschema;
109     return nschema;
110 }
111 
112