LibKEduVocDocument

keduvocvokabelnreader.cpp
1/*
2 * create a KEduVocDocument from a Vokabeln file
3 *
4 * SPDX-FileCopyrightText: 2005, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
5 * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "keduvocvokabelnreader.h"
10
11#include <KLocalizedString>
12#include <QIODevice>
13
14#include <QDebug>
15
16#include "keduvocexpression.h"
17
19 : m_inputFile(&file)
20{
21 m_errorMessage = QLatin1String("");
22}
23
25{
26 QTextStream ts(m_inputFile);
27 QString line1(ts.readLine());
28 QString line2(ts.readLine());
29 /*
30 * Vokabeln.de files:
31 The header seems to be something like this:
32
33 "Name
34 Lang1 - Lang2",123,234,456
35 0
36
37 or something longer:
38
39 "Name
40 Lang1 - Lang2
41 [..]
42 Blah, blah, blah...",123,234,456
43 0
44 */
45
46 QString tmp;
47 bool isgood = false;
48
49 if (line1.startsWith(QChar::fromLatin1('"'))) {
50 ts.seek(0);
51 tmp = ts.readLine();
52 // There shouldn't be headers longer than 10 lines.
53 for (int x = 0; x < 10; x++) {
54 if (tmp.contains(QLatin1String("\","))) {
55 tmp = ts.readLine();
56 if (tmp.endsWith('0')) {
57 isgood = true;
58 break;
59 }
60 }
61 tmp = ts.readLine();
62 }
63 }
64
65 m_inputFile->seek(0);
66 return isgood;
67}
68
73
75{
76 qDebug() << "Reading vokabeln.de document...";
77 m_doc = &doc;
78
79 m_doc->setAuthor(QStringLiteral("http://www.vokabeln.de"));
80
81 QTextStream inputStream(m_inputFile);
82 qWarning() << " Need to fix inputStream.setCodec( ISO-8859-1 );";
83 inputStream.setAutoDetectUnicode(false);
84 inputStream.seek(0);
85
86 QString title;
87 QString lang1;
88 QString expression;
89 QString original;
90 QString translation;
91 QString temp;
92 QString comment;
93
94 int i;
95 int wordCount;
96 int lessonNumber;
97 int maxLessonNumber = -1;
98
99 int lines = 10000;
100
101 QStringList titles;
102 QStringList languages;
103 QStringList words;
104
105 temp = inputStream.readLine();
106
107 if (temp.startsWith(QChar::fromLatin1('"'))) {
108 while (temp != QChar::fromLatin1('0')) {
109 if (title.isEmpty()) {
110 title = temp;
111 } else {
112 comment.append(temp.append('\n'));
113 }
114
115 if (inputStream.atEnd()) {
116 m_errorMessage = i18n("Error while reading file: Truncated header");
118 }
119
120 temp = inputStream.readLine();
121 }
122 } else {
123 m_errorMessage = i18n("Error while reading file: No title");
125 }
126
127 // 1 line header
128 if (comment.isEmpty()) {
129 titles = title.split(QStringLiteral("\","));
130 m_doc->setTitle(titles[0].mid(1));
131 qDebug() << "TitleA " << m_doc->title();
132 }
133 // longer header
134 else {
135 titles = comment.split(QStringLiteral("\","));
136 m_doc->setDocumentComment(titles[0]);
137 m_doc->setTitle(title.mid(1));
138 qDebug() << "TitleB " << m_doc->title();
139 }
140
141 wordCount = titles[1].section(',', 0, 0).toInt();
142 qDebug() << "WordCount " << wordCount;
143
144 inputStream.readLine();
145
146 lang1 = inputStream.readLine();
147 languages = lang1.split(QStringLiteral("\","));
148
149 if (languages.size() < 2) {
150 m_errorMessage = i18n("Error while reading file: Didn't find two languages in %1", lang1);
152 }
153
154 m_doc->appendIdentifier();
155 QString language = languages[0].mid(1);
156 m_doc->identifier(0).setLocale(language);
157 m_doc->identifier(0).setName(language);
158 qDebug() << "First language: " << language;
159
160 m_doc->appendIdentifier();
161 language = languages[1].mid(1);
162 m_doc->identifier(1).setLocale(language);
163 m_doc->identifier(1).setName(language);
164 qDebug() << "Second language: " << language;
165
166 QString section8Header(QStringLiteral("8. Lernhilfe")); // DO NOT translate
167 while (!temp.contains(section8Header)) {
168 if (inputStream.atEnd()) {
169 m_errorMessage = i18n("Error while reading file: Missing \"%1\"", section8Header);
171 }
172 temp = inputStream.readLine();
173 }
174
175 for (i = 0; i <= 14; ++i) {
176 inputStream.readLine();
177 }
178
179 for (i = 0; i < wordCount - 1; ++i) {
180 int c = 0;
181 expression.clear();
182
183 while (c < 2) {
184 if (inputStream.atEnd()) {
185 m_errorMessage = i18n("Error while reading file: Expecting something like \"dog\",\"Hund\",1");
187 }
188 temp = inputStream.readLine();
189 c += temp.count(QStringLiteral("\","), Qt::CaseSensitive);
190 expression.append(temp);
191 if (c < 2) {
192 expression.append(' ');
193 }
194 }
195
196 words = expression.split(QStringLiteral("\","));
197 original = words[0].mid(1);
198 translation = words[1].mid(1);
199 lessonNumber = words[2].toInt() - 1;
200 maxLessonNumber = qMax(lessonNumber, maxLessonNumber);
201
202 qDebug() << "Reading entry: " << original << " - " << translation << " Lesson: " << lessonNumber;
203
204 // fallback if it's not read correctly
205 if (lessonNumber < 0) {
206 qDebug() << "Warning, invalid lesson found!";
207 lessonNumber = 0;
208 }
209
210 while (m_doc->lesson()->childContainerCount() <= lessonNumber) {
211 KEduVocLesson *lesson = new KEduVocLesson(i18n("Lesson %1", lessonNumber), m_doc->lesson());
212 m_doc->lesson()->appendChildContainer(lesson);
213 qDebug() << "Created lesson " << lessonNumber;
214 }
215
217 kve->setTranslation(0, original);
218 kve->setTranslation(1, translation);
219
220 static_cast<KEduVocLesson *>(m_doc->lesson()->childContainer(lessonNumber))->appendEntry(kve);
221
222 inputStream.readLine();
223 inputStream.readLine();
224 }
225
226 inputStream.readLine();
227 inputStream.readLine();
228 inputStream.readLine();
229
230 int ii = 0;
231 while (!inputStream.atEnd() && ii < lines && ii <= maxLessonNumber) {
232 QString lessonDescr = inputStream.readLine();
233 qDebug() << "Found lesson description " << lessonDescr;
234 lessonDescr = lessonDescr.mid(1, lessonDescr.length() - 2);
235 m_doc->lesson()->childContainer(ii)->setName(lessonDescr);
236 if (lessonDescr.isEmpty()) {
237 break;
238 }
239 inputStream.readLine();
240 ++ii;
241 }
242
244}
void setName(const QString &name)
set the container name
int childContainerCount() const
Find a child container.
The primary entry point to the hierarchy of objects describing vocabularies.
int appendIdentifier(const KEduVocIdentifier &identifier=KEduVocIdentifier())
Appends a new identifier (usually a language)
void setTitle(const QString &title)
Set the title of the file.
void setDocumentComment(const QString &comment)
Set the comment of the file.
void setAuthor(const QString &author)
Set the author of the file.
KEduVocLesson * lesson()
Get the lesson root object.
ErrorCode
the return code when opening/saving
@ FileReaderFailed
file reader failed
QString title() const
KEduVocIdentifier & identifier(int index)
Returns the identifier of translation index.
FileType
known vocabulary file types
@ Vokabeln
Vokabeln format.
This class contains one vocabulary expression as an original with one or more translations.
void setTranslation(int index, const QString &expression)
Add a translation to this expression.
void setLocale(const QString &name)
Set the locale.
void setName(const QString &name)
Set the name.
class to store information about a lesson
void appendEntry(KEduVocExpression *entry)
append an entry to the lesson
KEduVocDocument::ErrorCode read(KEduVocDocument &doc) override
Parse file and write into doc.
KEduVocDocument::FileType fileTypeHandled() override
returns the KEduVocDocument::FileType that this reader handles
KEduVocVokabelnReader(QIODevice &file)
constructor
bool isParsable() override
Can this reader parse this file.
QString i18n(const char *text, const TYPE &arg...)
QChar fromLatin1(char c)
virtual bool seek(qint64 pos)
QList< T > mid(qsizetype pos, qsizetype length) const const
qsizetype size() const const
qsizetype count() const const
QString & append(QChar ch)
void clear()
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
CaseSensitive
bool atEnd() const const
QString readLine(qint64 maxlen)
bool seek(qint64 pos)
void setAutoDetectUnicode(bool enabled)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.