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 163 of file KoResourceBundleManifest.cpp.

164{
165 ResourceReference ref(fileName, fileTagList, fileTypeName, md5, resourceId, filenameInBundle);
166 if (!m_resources.contains(fileTypeName)) {
167 m_resources[fileTypeName] = QMap<QString, ResourceReference>();
168 }
169 m_resources[fileTypeName].insert(fileName, ref);
170}
QMap< QString, QMap< QString, ResourceReference > > m_resources

References m_resources.

◆ files()

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

Definition at line 197 of file KoResourceBundleManifest.cpp.

198{
199 QList<ResourceReference> resources;
200
201 if (type.isEmpty()) {
202 // If no type is specified we return all the resources.
203 Q_FOREACH (const QString &type, m_resources.keys()) {
204 resources += m_resources[type].values();
205 }
206 } else if (m_resources.contains(type)) {
207 resources = m_resources[type].values();
208 }
209
210 return resources;
211}

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 QString errorMessage;
64 int errorLine;
65 int errorColumn;
66 if (!manifestDocument.setContent(device, true, &errorMessage, &errorLine, &errorColumn)) {
67 warnKrita << "Error parsing manifest" << errorMessage
68 << "line" << errorLine
69 << "column" << errorColumn;
70 return false;
71 }
72
73 QDomElement root = manifestDocument.documentElement();
74 if (root.localName() != "manifest" || root.namespaceURI() != KoXmlNS::manifest) {
75 return false;
76 }
77
78 QDomElement e = root.firstChildElement("file-entry");
79 for (; !e.isNull(); e = e.nextSiblingElement("file-entry")) {
80 if (!parseFileEntry(e)) {
81 warnKrita << "Skipping invalid manifest entry"
82 << "line" << e.lineNumber();
83 }
84 }
85
86 return true;
87}
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 89 of file KoResourceBundleManifest.cpp.

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

214{
216 Q_FOREACH (const QString &type, m_resources.keys()) {
217 if (m_resources[type].contains(fileName)) {
218 m_resources[type].remove(fileName);
219 }
220 }
221}

References m_resources, and tags().

◆ removeResource()

◆ save()

bool KoResourceBundleManifest::save ( QIODevice * device)

save the ResourceBundleManifest to the given device

Definition at line 122 of file KoResourceBundleManifest.cpp.

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

187{
188 QSet<QString> tags;
189 Q_FOREACH (const QString &type, m_resources.keys()) {
190 Q_FOREACH (const ResourceReference &ref, m_resources[type].values()) {
191 tags += QSet<QString>(ref.tagList.begin(), ref.tagList.end());
192 }
193 }
194 return QStringList(tags.begin(), tags.end());
195 }
QList< QString > QStringList

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

◆ types()

QStringList KoResourceBundleManifest::types ( ) const

Definition at line 181 of file KoResourceBundleManifest.cpp.

182{
183 return m_resources.keys();
184}

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: