Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourceBundleManifest Class Reference

#include <KoResourceBundleManifest.h>

Classes

struct  ResourceReference
 

Public Member Functions

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< ResourceReferencefiles (const QString &type=QString()) const
 
 KoResourceBundleManifest ()
 ResourceBundleManifest : Ctor.
 
bool load (QIODevice *device)
 load the ResourceBundleManifest from the given device
 
void removeFile (QString fileName)
 removeFile : remove a file from the manifest
 
void removeResource (ResourceReference &resource)
 
bool save (QIODevice *device)
 save the ResourceBundleManifest to the given device
 
QStringList tags () const
 
QStringList types () const
 
virtual ~KoResourceBundleManifest ()
 ~ResourceBundleManifest : Dtor
 

Private Member Functions

bool parseFileEntry (const QDomElement &e)
 

Private Attributes

QMap< QString, QMap< QString, ResourceReference > > m_resources
 

Detailed Description

Definition at line 19 of file KoResourceBundleManifest.h.

Constructor & Destructor Documentation

◆ KoResourceBundleManifest()

KoResourceBundleManifest::KoResourceBundleManifest ( )

ResourceBundleManifest : Ctor.

Parameters
xmlNamethe name of the XML file to be created

Definition at line 45 of file KoResourceBundleManifest.cpp.

46{
47}

◆ ~KoResourceBundleManifest()

KoResourceBundleManifest::~KoResourceBundleManifest ( )
virtual

~ResourceBundleManifest : Dtor

Definition at line 49 of file KoResourceBundleManifest.cpp.

50{
51}

Member Function Documentation

◆ addResource()

void KoResourceBundleManifest::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.

Parameters
fileTypethe type of the file to be added
fileNamethe name of the file to be added
emptyFiletrue if the file is empty
Returns
the element corresponding to the created tag.

Definition at line 171 of file KoResourceBundleManifest.cpp.

172{
173 ResourceReference ref(fileName, fileTagList, fileTypeName, md5, resourceId, filenameInBundle);
174 if (!m_resources.contains(fileTypeName)) {
175 m_resources[fileTypeName] = QMap<QString, ResourceReference>();
176 }
177 m_resources[fileTypeName].insert(fileName, ref);
178}
QMap< QString, QMap< QString, ResourceReference > > m_resources

References m_resources.

◆ files()

QList< KoResourceBundleManifest::ResourceReference > KoResourceBundleManifest::files ( const QString & type = QString()) const

Definition at line 205 of file KoResourceBundleManifest.cpp.

206{
207 QList<ResourceReference> resources;
208
209 if (type.isEmpty()) {
210 // If no type is specified we return all the resources.
211 Q_FOREACH (const QString &type, m_resources.keys()) {
212 resources += m_resources[type].values();
213 }
214 } else if (m_resources.contains(type)) {
215 resources = m_resources[type].values();
216 }
217
218 return resources;
219}

References m_resources.

◆ load()

bool KoResourceBundleManifest::load ( QIODevice * device)

load the ResourceBundleManifest from the given device

Definition at line 53 of file KoResourceBundleManifest.cpp.

54{
55 m_resources.clear();
56 if (!device->isOpen()) {
57 if (!device->open(QIODevice::ReadOnly)) {
58 return false;
59 }
60 }
61
62 QDomDocument manifestDocument;
63#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
64 QString errorMessage;
65 int errorLine;
66 int errorColumn;
67 if (!manifestDocument.setContent(device, true, &errorMessage, &errorLine, &errorColumn)) {
68 warnKrita << "Error parsing manifest" << errorMessage
69 << "line" << errorLine
70 << "column" << errorColumn;
71#else
72 QDomDocument::ParseResult result = manifestDocument.setContent(device, QDomDocument::ParseOption::UseNamespaceProcessing);
73 if (!result) {
74 warnKrita << "Error parsing manifest" << result.errorMessage
75 << "line" << result.errorLine
76 << "column" << result.errorColumn;
77#endif
78 return false;
79 }
80
81 QDomElement root = manifestDocument.documentElement();
82 if (root.localName() != "manifest" || root.namespaceURI() != KoXmlNS::manifest) {
83 return false;
84 }
85
86 QDomElement e = root.firstChildElement("file-entry");
87 for (; !e.isNull(); e = e.nextSiblingElement("file-entry")) {
88 if (!parseFileEntry(e)) {
89 warnKrita << "Skipping invalid manifest entry"
90 << "line" << e.lineNumber();
91 }
92 }
93
94 return true;
95}
bool parseFileEntry(const QDomElement &e)
static const QString manifest
Definition KoXmlNS.h:35
#define warnKrita
Definition kis_debug.h:87

References m_resources, KoXmlNS::manifest, parseFileEntry(), and warnKrita.

◆ parseFileEntry()

bool KoResourceBundleManifest::parseFileEntry ( const QDomElement & e)
private

Definition at line 97 of file KoResourceBundleManifest.cpp.

98{
99 if (e.localName() != "file-entry" || e.namespaceURI() != KoXmlNS::manifest) {
100 return false;
101 }
102
103 QString fullPath = e.attributeNS(KoXmlNS::manifest, "full-path");
104 QString mediaType = e.attributeNS(KoXmlNS::manifest, "media-type");
105 QString md5sum = e.attributeNS(KoXmlNS::manifest, "md5sum");
106 QString version = e.attributeNS(KoXmlNS::manifest, "version");
107
108 if (fullPath == "/" && mediaType == "application/x-krita-resourcebundle") {
109 // The manifest always contains an entry for the bundle root.
110 // This is not a resource, so skip it without indicating failure.
111 return true;
112 } else if (fullPath.isNull() || mediaType.isNull() || md5sum.isNull()) {
113 return false;
114 }
115
116 QStringList tagList;
117 QDomElement t = e.firstChildElement("tags")
118 .firstChildElement("tag");
119 for (; !t.isNull(); t = t.nextSiblingElement("tag")) {
120 QString tag = t.text();
121 if (!tag.isNull()) {
122 tagList.append(tag);
123 }
124 }
125
126 addResource(mediaType, fullPath, tagList, QByteArray::fromHex(md5sum.toLatin1()), -1);
127 return true;
128}
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.

References addResource(), and KoXmlNS::manifest.

◆ removeFile()

void KoResourceBundleManifest::removeFile ( QString fileName)

removeFile : remove a file from the manifest

Parameters
fileName: the name of the file to be removed
Returns
the list of resource tags to be removed from meta file.

Definition at line 221 of file KoResourceBundleManifest.cpp.

222{
224 Q_FOREACH (const QString &type, m_resources.keys()) {
225 if (m_resources[type].contains(fileName)) {
226 m_resources[type].remove(fileName);
227 }
228 }
229}

References m_resources, and tags().

◆ removeResource()

◆ save()

bool KoResourceBundleManifest::save ( QIODevice * device)

save the ResourceBundleManifest to the given device

Definition at line 130 of file KoResourceBundleManifest.cpp.

131{
132 if (!device->isOpen()) {
133 if (!device->open(QIODevice::WriteOnly)) {
134 return false;
135 }
136 }
137 KoXmlWriter manifestWriter(device);
138 manifestWriter.startDocument("manifest:manifest");
139 manifestWriter.startElement("manifest:manifest");
140 manifestWriter.addAttribute("xmlns:manifest", KoXmlNS::manifest);
141 manifestWriter.addAttribute("manifest:version", "1.2");
142 manifestWriter.addManifestEntry("/", "application/x-krita-resourcebundle");
143
144 Q_FOREACH (QString resourceType, m_resources.keys()) {
145 Q_FOREACH (const ResourceReference &resource, m_resources[resourceType].values()) {
146 manifestWriter.startElement("manifest:file-entry");
147 manifestWriter.addAttribute("manifest:media-type", resourceTypeToManifestType(resourceType));
148 // we cannot just use QFileInfo(resource.resourcePath).fileName() because it would cut off the subfolder
149 // but the resourcePath is already correct, so let's just add the resourceType
150 manifestWriter.addAttribute("manifest:full-path", resourceTypeToManifestType(resourceType) + "/" + resource.filenameInBundle);
151 manifestWriter.addAttribute("manifest:md5sum", resource.md5sum);
152 if (!resource.tagList.isEmpty()) {
153 manifestWriter.startElement("manifest:tags");
154 Q_FOREACH (const QString tag, resource.tagList) {
155 manifestWriter.startElement("manifest:tag");
156 manifestWriter.addTextNode(tag);
157 manifestWriter.endElement();
158 }
159 manifestWriter.endElement();
160 }
161 manifestWriter.endElement();
162 }
163 }
164
165 manifestWriter.endElement();
166 manifestWriter.endDocument();
167
168 return true;
169}
QString resourceTypeToManifestType(const QString &type)

References KoXmlWriter::addAttribute(), KoXmlWriter::addManifestEntry(), KoXmlWriter::addTextNode(), KoXmlWriter::endDocument(), KoXmlWriter::endElement(), KoResourceBundleManifest::ResourceReference::filenameInBundle, m_resources, KoXmlNS::manifest, KoResourceBundleManifest::ResourceReference::md5sum, resourceTypeToManifestType(), KoXmlWriter::startDocument(), KoXmlWriter::startElement(), and KoResourceBundleManifest::ResourceReference::tagList.

◆ tags()

QStringList KoResourceBundleManifest::tags ( ) const

Definition at line 194 of file KoResourceBundleManifest.cpp.

195{
196 QSet<QString> tags;
197 Q_FOREACH (const QString &type, m_resources.keys()) {
198 Q_FOREACH (const ResourceReference &ref, m_resources[type].values()) {
199 tags += QSet<QString>(ref.tagList.begin(), ref.tagList.end());
200 }
201 }
202 return QStringList(tags.begin(), tags.end());
203 }
QList< QString > QStringList

References m_resources, KoResourceBundleManifest::ResourceReference::tagList, and tags().

◆ types()

QStringList KoResourceBundleManifest::types ( ) const

Definition at line 189 of file KoResourceBundleManifest.cpp.

190{
191 return m_resources.keys();
192}

References m_resources.

Member Data Documentation

◆ m_resources

QMap<QString, QMap<QString, ResourceReference> > KoResourceBundleManifest::m_resources
private

Definition at line 93 of file KoResourceBundleManifest.h.


The documentation for this class was generated from the following files: