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 | qDebug()<< "Getting the reference"; | ||||
66 | setId(xmlReader->attributes().value(QStringLiteral("id")).toString()); | ||||
67 | setLanguage(xmlReader->attributes().value(QStringLiteral("lang")).toString()); | ||||
68 | while(xmlReader->readNextStartElement()) | ||||
69 | { | ||||
70 | if(xmlReader->name() == QStringLiteral("p")) | ||||
71 | { | ||||
72 | qDebug()<<"Getting the paragraphs"; | ||||
73 | d->paragraphs.append(xmlReader->readElementText(QXmlStreamReader::IncludeChildElements)); | ||||
74 | } | ||||
75 | else | ||||
76 | { | ||||
77 | qWarning() << Q_FUNC_INFO << "currently unsupported subsection in text-area:" << xmlReader->name(); | ||||
78 | xmlReader->skipCurrentElement(); | ||||
79 | } | ||||
80 | } | ||||
81 | if (xmlReader->hasError()) { | ||||
82 | qWarning() << Q_FUNC_INFO << "Failed to read ACBF XML document at token" << xmlReader->name() << "(" << xmlReader->lineNumber() << ":" << xmlReader->columnNumber() << ") The reported error was:" << xmlReader->errorString(); | ||||
83 | } | ||||
84 | qDebug() << Q_FUNC_INFO << "Created a reference of with id" << id(); | ||||
85 | return !xmlReader->hasError(); | ||||
86 | } | ||||
87 | | ||||
88 | QString Reference::id() const | ||||
89 | { | ||||
90 | return d->id; | ||||
91 | } | ||||
92 | | ||||
93 | void Reference::setId(const QString& newId) | ||||
94 | { | ||||
95 | d->id = newId; | ||||
96 | } | ||||
97 | | ||||
98 | QString Reference::language() const | ||||
99 | { | ||||
100 | return d->language; | ||||
101 | } | ||||
102 | | ||||
103 | void Reference::setLanguage(const QString& language) | ||||
104 | { | ||||
105 | d->language = language; | ||||
106 | } | ||||
107 | | ||||
108 | QStringList Reference::paragraphs() const | ||||
109 | { | ||||
110 | return d->paragraphs; | ||||
111 | } | ||||
112 | | ||||
113 | void Reference::setParagraphs(const QStringList& paragraphs) | ||||
114 | { | ||||
115 | d->paragraphs = paragraphs; | ||||
116 | } |