11#include <QCryptographicHash>
16#include <QProcessEnvironment>
17#include <QScopedPointer>
20#include <klocalizedstring.h>
41 : m_filename(fileName),
53 return QString(
".bundle");
61 if (!resourceStore || resourceStore->bad()) {
62 qWarning() <<
"Could not open store on bundle" <<
m_filename;
68 if (resourceStore->open(
"META-INF/manifest.xml")) {
70 qWarning() <<
"Could not open manifest for bundle" <<
m_filename;
73 resourceStore->close();
83 qWarning() <<
"Could not load META-INF/manifest.xml";
87 bool versionFound =
false;
89 qWarning() <<
"Could not load meta.xml";
93 if (resourceStore->open(
"preview.png")) {
96 QByteArray data = resourceStore->device()->readAll();
97 QBuffer buffer(&data);
99 resourceStore->close();
101 qWarning() <<
"Could not open preview.png";
123 qWarning() <<
"No Resource";
127 if (!resource->valid()) {
128 qWarning() <<
"Resource is not valid";
131 if (!store || store->
bad()) {
132 qWarning() <<
"No Store or Store is Bad";
137 buf.open(QFile::WriteOnly);
141 qWarning() <<
"Cannot save to device";
145 if (!store->
open(resType +
"/" + filename)) {
146 qWarning() <<
"Could not open file in store for resource";
150 qint64 size = store->
write(buf.data());
153 if (size != buf.size()) {
154 qWarning() <<
"Cannot save resource to the store" << size << buf.size();
158 if (!resource->thumbnailPath().isEmpty()) {
160 const QImage thumbnail = resource->thumbnail();
164 clonedResource->setFilename(filename);
166 if (!store->
open(resType +
"/" + clonedResource->thumbnailPath())) {
167 qWarning() <<
"Could not open file in store for resource thumbnail";
171 buf.open(QFile::ReadWrite);
172 thumbnail.save(&buf,
"PNG");
174 int size2 = store->
write(buf.data());
175 if (size2 != buf.size()) {
176 qWarning() <<
"Cannot save thumbnail to the store" << size << buf.size();
183 return size == buf.size();
200 if (!store || store->bad())
return false;
216 qWarning() <<
"Could not save resource" << resType << res->name();
222 QByteArray byteArray;
223 QBuffer buffer(&byteArray);
225 if (!store->open(
"preview.png")) qWarning() <<
"Could not open preview.png";
226 if (store->write(byteArray) != buffer.size()) qWarning() <<
"Could not write preview.png";
262 Q_FOREACH(
KisTagSP tag, fileTagList) {
280 if (!
image.isNull()) {
285 m_thumbnail = QImage(256, 256, QImage::Format_ARGB32);
287 gc.fillRect(0, 0, 256, 256, Qt::red);
295 QByteArray mt = metaTag.toUtf8();
315 if (!resourceStore->
open(
"meta.xml")) {
316 qWarning() <<
"Could not open meta.xml for" <<
m_filename;
321 if (!doc.setContent(resourceStore->
device())) {
322 qWarning() <<
"Could not parse meta.xml for" <<
m_filename;
326 const QDomElement root = doc.documentElement();
327 if (root.tagName() !=
"meta:meta") {
328 qWarning() <<
"Expected meta:meta element root, but found"
334 for (e = root.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
335 QString name = e.tagName();
336 QString
value = e.text();
337 if (name ==
"meta:meta-userdefined") {
338 name = e.attribute(
"meta:name");
339 value = e.attribute(
"meta:value");
346 if (name !=
"email" &&
349 qWarning() <<
"Unrecognized metadata: "
356 name =
"meta:" + name;
357 }
else if (name ==
"cd:creator") {
367 resourceStore->
close();
375 store->open(
"meta.xml");
376 buf.open(QBuffer::WriteOnly);
423 store->write(buf.data());
429 store->open(
"META-INF/manifest.xml");
431 buf.open(QBuffer::WriteOnly);
434 store->write(buf.data());
453 qWarning() <<
"Could not create loader for" << resourceType << filepath << mime;
457 QStringList parts = filepath.split(
'/', Qt::SkipEmptyParts);
459 Q_ASSERT(parts.size() == 2);
471 if (!resourceStore || resourceStore->bad()) {
472 qWarning() <<
"Could not open store on bundle" <<
m_filename;
475 const QString filePath = QString(
"%1/%2").arg(resourceType).arg(fileName);
477 if (!resourceStore->open(filePath)) {
478 qWarning() <<
"Could not open file in bundle" << filePath;
482 device->write(resourceStore->device()->readAll());
491 const QString resourceType =
resource->resourceType().first;
495 if (!resourceStore || resourceStore->bad()) {
496 qWarning() <<
"Could not open store on bundle" <<
m_filename;
499 const QString fileName = QString(
"%1/%2").arg(resourceType).arg(
resource->filename());
501 if (!resourceStore->open(fileName)) {
502 qWarning() <<
"Could not open file in bundle" << fileName;
506 if (!
resource->loadFromDevice(resourceStore->device(),
508 qWarning() <<
"Could not load the resource from the bundle" << resourceType << fileName <<
m_filename;
512 resourceStore->close();
516 if (!resourceStore->open(resourceType +
'/' +
resource->thumbnailPath())) {
517 qWarning() <<
"Could not open thumbnail in bundle" <<
resource->thumbnailPath();
522 img.load(resourceStore->device(), QFileInfo(
resource->thumbnailPath()).completeSuffix().toLatin1());
526 resourceStore->close();
540 if (!resourceStore || resourceStore->bad()) {
541 qWarning() <<
"Could not open store on bundle" <<
m_filename;
544 if (!resourceStore->open(url)) {
545 qWarning() <<
"Could not open file in bundle" << url;
550 resourceStore->close();
float value(const T *src, size_t ch)
bool saveResourceToStore(const QString &filename, KoResourceSP resource, KoStore *store, const QString &resType, KisResourceModel &model)
static KisResourcesInterfaceSP instance()
static QString mimeTypeForSuffix(const QString &suffix)
Find the mimetype for a given extension. The extension may have the form "*.xxx" or "xxx".
The KisResourceLoader class is an abstract interface class that must be implemented by actual resourc...
virtual KoResourceSP create(const QString &name)=0
static KisResourceLoaderRegistry * instance()
KisResourceLoaderBase * loader(const QString &resourceType, const QString &mimetype) const
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
KoResourceSP resourceForId(int id) const
bool exportResource(KoResourceSP resource, QIODevice *device) override
exportResource exports a resource into a QIODevice
QVector< KoResourceSP > resourcesForMD5(const QString md5sum) const
QVector< KoResourceSP > resourcesForFilename(QString fileName) const
void setResourceFilter(ResourceFilter filter) override
static const QString s_meta_author
static const QString s_meta_title
static const QString s_meta_user_defined
static const QString s_meta_creation_date
static const QString s_meta_value
static const QString s_meta_email
static const QString s_meta_license
static const QString s_xmlns_meta
static const QString s_meta_description
static const QString s_meta_dc_date
static const QString s_meta_website
static const QString s_meta_generator
static const QString s_meta_version
static const QString s_meta_name
static const QString s_xmlns_dc
static const QString s_meta_initial_creator
static const QString s_meta_creator
static QString generateHash(const QString &filename)
generateHash reads the given file and generates a hex-encoded md5sum for the file.
QStringList types() const
bool save(QIODevice *device)
save the ResourceBundleManifest to the given device
bool load(QIODevice *device)
load the ResourceBundleManifest from the given device
void removeResource(ResourceReference &resource)
void addResource(const QString &fileType, const QString &fileName, const QStringList &tagFileList, const QString &md5, const int resourceId=-1, const QString filenameInBundle="")
addTag : Add a file tag as a child of the fileType tag.
QList< ResourceReference > files(const QString &type=QString()) const
KoResourceBundle(QString const &fileName)
ResourceBundle : Ctor *.
void saveMetadata(QScopedPointer< KoStore > &store)
saveMetadata: saves bundle metadata
void saveManifest(QScopedPointer< KoStore > &store)
saveManifest: saves bundle manifest
KoResourceSP resource(const QString &resourceType, const QString &filepath)
QMap< QString, QString > m_metadata
bool save()
save : Save this resource.
bool loadFromDevice(QIODevice *dev)
QStringList resourceTypes() const
void addResource(QString fileType, QString filePath, QVector< KisTagSP > fileTagList, const QString md5sum, const int resourceId=-1, const QString filenameInBundle="")
addFile : Add a file to the bundle
QSet< QString > m_bundletags
void writeMeta(const QString &metaTag, KoXmlWriter *writer)
bool load()
load : Load this resource.
bool readMetaData(KoStore *resourceStore)
QList< QString > getTagsList()
bool saveToDevice(QIODevice *dev) const
KoResourceBundleManifest & manifest()
void setMetaData(const QString &key, const QString &value)
addMeta : Add a Metadata to the resource
int resourceCount() const
bool loadResource(KoResourceSP resource)
QString defaultFileExtension() const
defaultFileExtension
bool exportResource(const QString &resourceType, const QString &fileName, QIODevice *device)
virtual ~KoResourceBundle()
~ResourceBundle : Dtor
void writeUserDefinedMeta(const QString &metaTag, KoXmlWriter *writer)
const QString metaData(const QString &key, const QString &defaultValue=QString()) const
void setThumbnail(QImage)
QString resourceMd5(const QString &url)
KoResourceBundleManifest m_manifest
static QString saveLocation(const QString &type, const QString &suffix=QString(), bool create=true)
qint64 write(const QByteArray &data)
QIODevice * device() const
static KoStore * createStore(const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
bool open(const QString &name)
void startElement(const char *tagName, bool indentInside=true)
void endDocument()
Call this to terminate an XML document.
void startDocument(const char *rootElemName, const char *publicId=0, const char *systemId=0)
void addTextNode(const QString &str)
void addAttribute(const char *attrName, const QString &value)
KRITAVERSION_EXPORT QString versionString(bool checkGit=false)