File indexing completed on 2024-04-28 07:36:28

0001 /*
0002  * export a KEduVocDocument to a delimited text file
0003  * SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
0004  * SPDX-FileCopyrightText: 2007 Peter Hedlund <peter.hedlund@kdemail.net>
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "keduvoccsvwriter.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QFile>
0012 #include <QTextStream>
0013 
0014 #include "keduvocdocument.h"
0015 #include "keduvocexpression.h"
0016 #include "keduvoclesson.h"
0017 
0018 KEduVocCsvWriter::KEduVocCsvWriter(QFile *file)
0019     : m_outputFile(file) // the file must be already open
0020     , m_doc(nullptr)
0021 {
0022 }
0023 
0024 bool KEduVocCsvWriter::writeDoc(KEduVocDocument *doc, const QString &generator)
0025 {
0026     Q_UNUSED(generator);
0027 
0028     m_doc = doc;
0029 
0030     QString separator = m_doc->csvDelimiter();
0031 
0032     QTextStream outputStream;
0033     outputStream.setDevice(m_outputFile);
0034 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0035     outputStream.setCodec("UTF-8");
0036 #endif
0037 
0038     outputStream << i18nc("@item:intable the title of the document will be written here", "Title:") << separator << m_doc->title() << "\n";
0039     outputStream << i18nc("@item:intable the author will be written here", "Author:") << separator << m_doc->author() << "\n";
0040 
0041     KEduVocExpression *expression;
0042     int idCount = m_doc->identifierCount();
0043     QString currentRow;
0044 
0045     for (int e = 0; e < m_doc->lesson()->entryCount(KEduVocLesson::Recursive); e++) {
0046         expression = m_doc->lesson()->entries(KEduVocLesson::Recursive).value(e);
0047         currentRow = QLatin1String("");
0048         bool sep = false;
0049 
0050         for (int i = 0; i < idCount; i++) {
0051             if (!sep)
0052                 sep = true;
0053             else
0054                 currentRow += separator;
0055 
0056             currentRow += expression->translation(i)->text();
0057         }
0058 
0059         if (!currentRow.isEmpty())
0060             outputStream << currentRow << "\n";
0061     }
0062 
0063     return true;
0064 }