Changeset View
Changeset View
Standalone View
Standalone View
src/acbf/AcbfReferences.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | #include "AcbfReferences.h" | ||||
23 | #include <QDebug> | ||||
24 | #include <QXmlStreamReader> | ||||
25 | | ||||
26 | using namespace AdvancedComicBookFormat; | ||||
27 | | ||||
28 | class References::Private { | ||||
29 | public: | ||||
30 | Private() {} | ||||
31 | QHash<QString, Reference*> references; | ||||
32 | }; | ||||
33 | | ||||
34 | References::References(Document* parent) | ||||
35 | : QObject(parent) | ||||
36 | , d(new Private) | ||||
37 | { | ||||
38 | qRegisterMetaType<References*>("References*"); | ||||
39 | } | ||||
40 | | ||||
41 | References::~References() = default; | ||||
42 | | ||||
43 | void References::toXml(QXmlStreamWriter* writer) { | ||||
44 | writer->writeStartElement(QStringLiteral("references")); | ||||
45 | | ||||
46 | Q_FOREACH(Reference* reference, d->references) { | ||||
47 | reference->toXml(writer); | ||||
48 | } | ||||
49 | writer->writeEndElement(); | ||||
50 | } | ||||
51 | | ||||
52 | bool References::fromXml(QXmlStreamReader *xmlReader) | ||||
53 | { | ||||
54 | | ||||
55 | while(xmlReader->readNextStartElement()) | ||||
56 | { | ||||
57 | if(xmlReader->name() == QStringLiteral("reference")) | ||||
58 | { | ||||
59 | Reference* newReference = new Reference(this); | ||||
60 | if(!newReference->fromXml(xmlReader)) { | ||||
61 | return false; | ||||
62 | } | ||||
63 | d->references.insert(newReference->id(), newReference); | ||||
64 | } | ||||
65 | else | ||||
66 | { | ||||
67 | qWarning() << Q_FUNC_INFO << "currently unsupported subsection:" << xmlReader->name(); | ||||
68 | xmlReader->skipCurrentElement(); | ||||
69 | } | ||||
70 | } | ||||
71 | | ||||
72 | if (xmlReader->hasError()) { | ||||
73 | qWarning() << Q_FUNC_INFO << "Failed to read ACBF XML document at token" << xmlReader->name() << "(" << xmlReader->lineNumber() << ":" << xmlReader->columnNumber() << ") The reported error was:" << xmlReader->errorString(); | ||||
74 | } | ||||
75 | | ||||
76 | qDebug() << Q_FUNC_INFO << "Created reference section with" << d->references.count() << "references"; | ||||
77 | | ||||
78 | return !xmlReader->hasError(); | ||||
79 | } | ||||
80 | | ||||
81 | Reference* References::reference(const QString& id) const | ||||
82 | { | ||||
83 | return d->references.value(id); | ||||
84 | } | ||||
85 | | ||||
86 | void References::setReference(const QString& id, const QStringList& paragraphs, const QString& language) { | ||||
87 | Reference* ref = new Reference(this); | ||||
88 | ref->setId(id); | ||||
89 | ref->setParagraphs(paragraphs); | ||||
90 | ref->setLanguage(language); | ||||
91 | d->references.insert(ref->id(), ref); | ||||
92 | } |