File indexing completed on 2024-05-19 04:27:40
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 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) 0041 QStringList parts = url.split('/', Qt::SkipEmptyParts); 0042 #else 0043 QStringList parts = url.split('/', QString::SkipEmptyParts); 0044 #endif 0045 0046 if (parts.isEmpty()) return nullptr; 0047 0048 const QString resourceType = parts[0]; 0049 parts.removeFirst(); 0050 const QString resourceFilename = parts.join('/'); 0051 0052 const QString mime = KisMimeDatabase::mimeTypeForSuffix(resourceFilename); 0053 0054 KisResourceLoaderBase *loader = KisResourceLoaderRegistry::instance()->loader(resourceType, mime); 0055 if (!loader) { 0056 qWarning() << "Could not create loader for" << resourceType << resourceFilename << mime; 0057 return nullptr; 0058 } 0059 0060 KoResourceSP resource = loader->create(resourceFilename); 0061 return loadVersionedResource(resource) ? resource : nullptr; 0062 } 0063 0064 QString KisStoragePlugin::resourceMd5(const QString &url) 0065 { 0066 // a fallback implementation for the storages with 0067 // ephemeral resources 0068 KoResourceSP res = resource(url); 0069 if (res) { 0070 return res->md5Sum(); 0071 } else { 0072 return QString(); 0073 } 0074 } 0075 0076 QString KisStoragePlugin::resourceFilePath(const QString &url) 0077 { 0078 Q_UNUSED(url); 0079 return QString(); 0080 } 0081 0082 bool KisStoragePlugin::supportsVersioning() const 0083 { 0084 return true; 0085 } 0086 0087 QDateTime KisStoragePlugin::timestamp() 0088 { 0089 if (d->timestamp.isNull()) { 0090 return QFileInfo(d->location).lastModified(); 0091 } 0092 return d->timestamp; 0093 } 0094 0095 QString KisStoragePlugin::location() const 0096 { 0097 return d->location; 0098 } 0099 0100 void KisStoragePlugin::sanitizeResourceFileNameCase(KoResourceSP resource, const QDir &parentDir) 0101 { 0102 const QStringList result = parentDir.entryList({resource->filename()}); 0103 0104 if (result.size() == 1) { 0105 const QString realName = result.first(); 0106 if (realName != resource->filename()) { 0107 resource->setFilename(result.first()); 0108 } 0109 } 0110 } 0111 0112 bool KisStoragePlugin::isValid() const 0113 { 0114 qWarning() << "Storage plugins should implement their own checks!"; 0115 return true; 0116 }