File indexing completed on 2024-05-05 07:47:55
0001 /* 0002 * read a KEduVocDocument from a WQL file 0003 * SPDX-FileCopyrightText: 2004, 2007, 2008 Peter Hedlund <peter.hedlund@kdemail.net> 0004 * SPDX-FileCopyrightText: 2005 Eric Pignet 0005 * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net> 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "keduvocwqlreader.h" 0010 0011 #include <KLocalizedString> 0012 #include <QIODevice> 0013 #include <QTextStream> 0014 0015 #include "keduvocexpression.h" 0016 0017 KEduVocWqlReader::KEduVocWqlReader(QIODevice &file) 0018 : m_inputFile(&file) 0019 { 0020 m_errorMessage = QLatin1String(""); 0021 } 0022 0023 bool KEduVocWqlReader::isParsable() 0024 { 0025 QTextStream ts(m_inputFile); 0026 QString line1(ts.readLine()); 0027 0028 m_inputFile->seek(0); 0029 return (line1 == QLatin1String("WordQuiz")); 0030 } 0031 0032 KEduVocDocument::FileType KEduVocWqlReader::fileTypeHandled() 0033 { 0034 return KEduVocDocument::Wql; 0035 } 0036 0037 KEduVocDocument::ErrorCode KEduVocWqlReader::read(KEduVocDocument &doc) 0038 { 0039 m_doc = &doc; 0040 0041 QTextStream inputStream(m_inputFile); 0042 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // TODO fix in qt6! 0043 inputStream.setCodec("Windows-1252"); //("ISO-8851-1"); 0044 #else 0045 qWarning() << " Need to fix inputStream.setCodec( Windows-1252 );"; 0046 #endif 0047 inputStream.setAutoDetectUnicode(false); 0048 inputStream.seek(0); 0049 0050 QString s = QLatin1String(""); 0051 s = inputStream.readLine(); 0052 if (s != QLatin1String("WordQuiz")) { 0053 m_errorMessage = i18n("This does not appear to be a (K)WordQuiz file: Missing First line \"WordQuiz\""); 0054 return KEduVocDocument::FileTypeUnknown; 0055 } 0056 s = inputStream.readLine(); 0057 0058 if (!s.isEmpty()) { 0059 s = s.at(0); 0060 } 0061 int iFV = s.toInt(nullptr); 0062 if (iFV != 5) { 0063 m_errorMessage = i18n("Only files created by WordQuiz 5.x or later can be opened: Missing Second Line \"5\""); 0064 return KEduVocDocument::FileTypeUnknown; 0065 } 0066 0067 m_errorMessage = i18n("Error while reading file"); 0068 0069 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Font Info]")) 0070 ; 0071 if (inputStream.atEnd()) { 0072 m_errorMessage = i18n("Error while reading file: Missing [Font Info]"); 0073 return KEduVocDocument::FileReaderFailed; 0074 } 0075 s = inputStream.readLine(); 0076 int p = s.indexOf(QLatin1Char('='), 0); 0077 QString fam = s.right(s.length() - (p + 1)); 0078 fam = fam.mid(1, fam.length() - 2); 0079 0080 s = inputStream.readLine(); 0081 p = s.indexOf(QLatin1Char('='), 0); 0082 s = s.right(s.length() - (p + 1)); 0083 // int ps = s.toInt(0); 0084 0085 s = inputStream.readLine(); 0086 p = s.indexOf(QLatin1Char('='), 0); 0087 s = s.right(s.length() - (p + 1)); 0088 // int b = 0; 0089 // if ( s == "1" ) { 0090 // b = QFont::Bold; 0091 // } 0092 0093 s = inputStream.readLine(); 0094 p = s.indexOf(QLatin1Char('='), 0); 0095 s = s.right(s.length() - (p + 1)); 0096 // bool it = (s == "1"); 0097 0098 // m_doc->setFont(new QFont(fam, ps, b, it)); 0099 0100 /* TODO 0101 while (inputStream.readLine() != "[Character Info]"); 0102 s = inputStream.readLine(); 0103 p = s.find("=", 0); 0104 m_specialCharacters = s.right(s.length() - (p + 1)); 0105 */ 0106 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Grid Info]")) 0107 ; 0108 if (inputStream.atEnd()) { 0109 m_errorMessage = i18n("Error while reading file: Missing [Grid Info]"); 0110 return KEduVocDocument::FileReaderFailed; 0111 } 0112 inputStream.readLine(); // skip value for width of row headers 0113 0114 s = inputStream.readLine(); 0115 p = s.indexOf(QLatin1Char('='), 0); 0116 s = s.right(s.length() - (p + 1)); 0117 // m_doc->setSizeHint( 0, s.toInt() ); 0118 0119 s = inputStream.readLine(); 0120 p = s.indexOf(QLatin1Char('='), 0); 0121 s = s.right(s.length() - (p + 1)); 0122 // m_doc->setSizeHint( 1, s.toInt() ); 0123 0124 /* TODO 0125 s = inputStream.readLine(); 0126 p = s.find("=", 0); 0127 s = s.right(s.length() - (p + 1)); 0128 m_numRows = (s.toInt() - 1); //We need to reduce by one since the header is not included 0129 // Selection 0130 s = inputStream.readLine(); 0131 p = s.find("=", 0); 0132 s = s.right(s.length() - (p + 1)); 0133 m_topLeft =s.toInt(0, 10) - 1; 0134 0135 s = inputStream.readLine(); 0136 p = s.find("=", 0); 0137 s = s.right(s.length() - (p + 1)); 0138 m_topRight =s.toInt(0, 10) - 1; 0139 0140 s = inputStream.readLine(); 0141 p = s.find("=", 0); 0142 s = s.right(s.length() - (p + 1)); 0143 m_bottomLeft =s.toInt(0, 10) - 1; 0144 0145 s = inputStream.readLine(); 0146 p = s.find("=", 0); 0147 s = s.right(s.length() - (p + 1)); 0148 m_bottomRight =s.toInt(0, 10) - 1 ; 0149 */ 0150 while (!inputStream.atEnd() && inputStream.readLine() != QLatin1String("[Vocabulary]")) 0151 ; 0152 if (inputStream.atEnd()) { 0153 m_errorMessage = i18n("Error while reading file: Missing [Vocabulary]"); 0154 return KEduVocDocument::FileReaderFailed; 0155 } 0156 0157 KEduVocLesson *lesson = new KEduVocLesson(i18n("Vocabulary"), m_doc->lesson()); 0158 m_doc->lesson()->appendChildContainer(lesson); 0159 0160 s = inputStream.readLine(); 0161 p = s.indexOf(QLatin1String(" ["), 0); 0162 s = s.left(p); 0163 s = s.simplified(); 0164 m_doc->appendIdentifier(); 0165 m_doc->identifier(0).setLocale(s); 0166 m_doc->identifier(0).setName(s); 0167 s = inputStream.readLine(); 0168 m_doc->appendIdentifier(); 0169 m_doc->identifier(1).setLocale(s); 0170 m_doc->identifier(1).setName(s); 0171 0172 while (!s.isNull()) { 0173 s = inputStream.readLine(); 0174 p = s.indexOf(QLatin1String("["), 0); 0175 // QString r = s.mid(p + 1, 10); 0176 // int h = r.toInt(); 0177 s = s.left(p); 0178 s = s.simplified(); 0179 QString b; 0180 b = inputStream.readLine(); 0181 0182 KEduVocExpression *expr = new KEduVocExpression(s); 0183 expr->setTranslation(1, b); 0184 lesson->appendEntry(expr); 0185 } 0186 return KEduVocDocument::NoError; 0187 }