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

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KisStoragePlugin.h"
0008 #include <QFileInfo>
0009 #include <QDir>
0010 
0011 #include <KoResource.h>
0012 #include <KisMimeDatabase.h>
0013 #include "KisResourceLoaderRegistry.h"
0014 
0015 class KisStoragePlugin::Private
0016 {
0017 public:
0018     QString location;
0019     QDateTime timestamp;
0020 };
0021 
0022 KisStoragePlugin::KisStoragePlugin(const QString &location)
0023     : d(new Private())
0024 {
0025     d->location = location;
0026 
0027     if (!QFileInfo(d->location).exists()) {
0028         d->timestamp = QDateTime::currentDateTime();
0029     }
0030 }
0031 
0032 KisStoragePlugin::~KisStoragePlugin()
0033 {
0034 }
0035 
0036 KoResourceSP KisStoragePlugin::resource(const QString &url)
0037 {
0038     if (!url.contains('/')) return nullptr;
0039 
0040     QStringList parts = url.split('/', QString::SkipEmptyParts);
0041     if (parts.isEmpty()) return nullptr;
0042 
0043     const QString resourceType = parts[0];
0044     parts.removeFirst();
0045     const QString resourceFilename = parts.join('/');
0046 
0047     const QString mime = KisMimeDatabase::mimeTypeForSuffix(resourceFilename);
0048 
0049     KisResourceLoaderBase *loader = KisResourceLoaderRegistry::instance()->loader(resourceType, mime);
0050     if (!loader) {
0051         qWarning() << "Could not create loader for" << resourceType << resourceFilename << mime;
0052         return nullptr;
0053     }
0054 
0055     KoResourceSP resource = loader->create(resourceFilename);
0056     return loadVersionedResource(resource) ? resource : nullptr;
0057 }
0058 
0059 QString KisStoragePlugin::resourceMd5(const QString &url)
0060 {
0061     // a fallback implementation for the storages with
0062     // ephemeral resources
0063     KoResourceSP res = resource(url);
0064     if (res) {
0065         return res->md5Sum();
0066     } else {
0067         return QString();
0068     }
0069 }
0070 
0071 QString KisStoragePlugin::resourceFilePath(const QString &url)
0072 {
0073     return QString();
0074 }
0075 
0076 bool KisStoragePlugin::supportsVersioning() const
0077 {
0078     return true;
0079 }
0080 
0081 QDateTime KisStoragePlugin::timestamp()
0082 {
0083     if (d->timestamp.isNull()) {
0084         return QFileInfo(d->location).lastModified();
0085     }
0086     return d->timestamp;
0087 }
0088 
0089 QString KisStoragePlugin::location() const
0090 {
0091     return d->location;
0092 }
0093 
0094 void KisStoragePlugin::sanitizeResourceFileNameCase(KoResourceSP resource, const QDir &parentDir)
0095 {
0096     const QStringList result = parentDir.entryList({resource->filename()});
0097 
0098     if (result.size() == 1) {
0099         const QString realName = result.first();
0100         if (realName != resource->filename()) {
0101             resource->setFilename(result.first());
0102         }
0103     }
0104 }
0105 
0106 bool KisStoragePlugin::isValid() const
0107 {
0108     qWarning() << "Storage plugins should implement their own checks!";
0109     return true;
0110 }