Changeset View
Changeset View
Standalone View
Standalone View
src/acbf/AcbfReference.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 "AcbfReference.h" | ||||
23 | #include "AcbfReferences.h" | ||||
24 | #include <QString> | ||||
25 | #include <QDebug> | ||||
26 | #include <QXmlStreamWriter> | ||||
27 | #include <QXmlStreamReader> | ||||
28 | | ||||
29 | using namespace AdvancedComicBookFormat; | ||||
30 | | ||||
31 | class Reference::Private { | ||||
32 | public: | ||||
33 | Private() {} | ||||
34 | QString id; | ||||
35 | QString language; | ||||
36 | QStringList paragraphs; | ||||
37 | }; | ||||
38 | | ||||
39 | Reference::Reference(References* parent) | ||||
40 | : QObject(parent) | ||||
41 | , d(new Private) | ||||
42 | { | ||||
43 | qRegisterMetaType<Reference*>("Reference*"); | ||||
44 | } | ||||
45 | | ||||
46 | Reference::~Reference() = default; | ||||
47 | | ||||
48 | void Reference::toXml(QXmlStreamWriter* writer) | ||||
49 | { | ||||
50 | writer->writeStartElement(QStringLiteral("reference")); | ||||
51 | writer->writeAttribute(QStringLiteral("id"), id()); | ||||
52 | /* ACBF 1.2 | ||||
53 | writer->writeAttribute(QStringLiteral("lang"), language()); | ||||
54 | */ | ||||
55 | Q_FOREACH(const QString& paragraph, d->paragraphs) { | ||||
56 | writer->writeStartElement(QStringLiteral("p")); | ||||
57 | writer->writeCharacters(paragraph); | ||||
58 | writer->writeEndElement(); | ||||
59 | } | ||||
60 | writer->writeEndElement(); | ||||
61 | } | ||||
62 | | ||||
63 | bool Reference::fromXml(QXmlStreamReader *xmlReader) | ||||
64 | { | ||||
65 | setId(xmlReader->attributes().value(QStringLiteral("id")).toString()); | ||||
66 | setLanguage(xmlReader->attributes().value(QStringLiteral("lang")).toString()); | ||||
67 | while(xmlReader->readNextStartElement()) | ||||
68 | { | ||||
69 | if(xmlReader->name() == QStringLiteral("p")) | ||||
70 | { | ||||
71 | d->paragraphs.append(xmlReader->readElementText(QXmlStreamReader::IncludeChildElements)); | ||||
72 | } | ||||
73 | else | ||||
74 | { | ||||
75 | qWarning() << Q_FUNC_INFO << "currently unsupported subsection in text-area:" << xmlReader->name(); | ||||
76 | xmlReader->skipCurrentElement(); | ||||
77 | } | ||||
78 | } | ||||
79 | if (xmlReader->hasError()) { | ||||
80 | qWarning() << Q_FUNC_INFO << "Failed to read ACBF XML document at token" << xmlReader->name() << "(" << xmlReader->lineNumber() << ":" << xmlReader->columnNumber() << ") The reported error was:" << xmlReader->errorString(); | ||||
81 | } | ||||
82 | qDebug() << Q_FUNC_INFO << "Created a reference of with id" << id(); | ||||
83 | return !xmlReader->hasError(); | ||||
84 | } | ||||
85 | | ||||
86 | QString Reference::id() const | ||||
87 | { | ||||
88 | return d->id; | ||||
89 | } | ||||
90 | | ||||
91 | void Reference::setId(const QString& newId) | ||||
92 | { | ||||
93 | d->id = newId; | ||||
94 | } | ||||
95 | | ||||
96 | QString Reference::language() const | ||||
97 | { | ||||
98 | return d->language; | ||||
99 | } | ||||
100 | | ||||
101 | void Reference::setLanguage(const QString& language) | ||||
102 | { | ||||
103 | d->language = language; | ||||
104 | } | ||||
105 | | ||||
106 | QStringList Reference::paragraphs() const | ||||
107 | { | ||||
108 | return d->paragraphs; | ||||
109 | } | ||||
110 | | ||||
111 | void Reference::setParagraphs(const QStringList& paragraphs) | ||||
112 | { | ||||
113 | d->paragraphs = paragraphs; | ||||
114 | } |