File indexing completed on 2024-05-12 15:59:16

0001 /*
0002  *  SPDX-FileCopyrightText: 2007, 2009 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "kis_meta_data_schema_registry.h"
0008 
0009 #include <QGlobalStatic>
0010 #include <QString>
0011 
0012 #include <KoResourcePaths.h>
0013 
0014 #include "kis_debug.h"
0015 #include "kis_meta_data_schema_p.h"
0016 
0017 using namespace KisMetaData;
0018 
0019 // ---- Schema Registry ---- //
0020 
0021 struct Q_DECL_HIDDEN SchemaRegistry::Private {
0022     QHash<QString, Schema*> uri2Schema;
0023     QHash<QString, Schema*> prefix2Schema;
0024 };
0025 
0026 Q_GLOBAL_STATIC(SchemaRegistry, s_instance)
0027 
0028 SchemaRegistry* SchemaRegistry::instance()
0029 {
0030     return s_instance;
0031 }
0032 
0033 SchemaRegistry::SchemaRegistry()
0034     : d(new Private)
0035 {
0036     KoResourcePaths::addAssetType("metadata_schema", "data", "/metadata/schemas/");
0037 
0038     QStringList schemasFilenames = KoResourcePaths::findAllAssets("metadata_schema", "*.schema");
0039 
0040     Q_FOREACH (const QString& fileName, schemasFilenames) {
0041         Schema* schema = new Schema();
0042         schema->d->load(fileName);
0043         if (schemaFromUri(schema->uri())) {
0044             errMetaData << "Schema already exist uri: " << schema->uri();
0045             delete schema;
0046         } else if (schemaFromPrefix(schema->prefix())) {
0047             errMetaData << "Schema already exist prefix: " << schema->prefix();
0048             delete schema;
0049         } else {
0050             d->uri2Schema[schema->uri()] = schema;
0051             d->prefix2Schema[schema->prefix()] = schema;
0052         }
0053     }
0054 
0055     // DEPRECATED WRITE A SCHEMA FOR EACH OF THEM
0056     create(Schema::MakerNoteSchemaUri, "mkn");
0057     create(Schema::IPTCSchemaUri, "Iptc4xmpCore");
0058     create(Schema::PhotoshopSchemaUri, "photoshop");
0059 }
0060 
0061 SchemaRegistry::~SchemaRegistry()
0062 {
0063     delete d;
0064 }
0065 
0066 
0067 const Schema* SchemaRegistry::schemaFromUri(const QString & uri) const
0068 {
0069     return d->uri2Schema[uri];
0070 }
0071 
0072 const Schema* SchemaRegistry::schemaFromPrefix(const QString & prefix) const
0073 {
0074     return d->prefix2Schema[prefix];
0075 }
0076 
0077 const Schema* SchemaRegistry::create(const QString & uri, const QString & prefix)
0078 {
0079     // First search for the schema
0080     const Schema* schema = schemaFromUri(uri);
0081     if (schema) {
0082         return schema;
0083     }
0084     // Second search for the prefix
0085     schema = schemaFromPrefix(prefix);
0086     if (schema) {
0087         return 0; // A schema with the same prefix already exist
0088     }
0089     // The schema doesn't exist yet, create it
0090     Schema* nschema = new Schema(uri, prefix);
0091     d->uri2Schema[uri] = nschema;
0092     d->prefix2Schema[prefix] = nschema;
0093     return nschema;
0094 }
0095