From 47b36ab401b2454f9b2326b508e824a3e690b210 Mon Sep 17 00:00:00 2001 From: Peter Hedlund Date: Tue, 20 Feb 2007 02:04:37 +0000 Subject: [PATCH] Improvements to the WordQuiz import filter. Now activated also for KVocTrain. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=635451 --- kdeeducore/CMakeLists.txt | 8 +- kdeeducore/keduvocdocument.cpp | 113 +++++++++++----------- kdeeducore/keduvocdocument.h | 2 +- kdeeducore/keduvocwqlreader.cpp | 64 +++++++------ kdeeducore/keduvocwqlreader.h | 15 ++- kdeeducore/keduvocwqlwriter.cpp | 4 +- kdeeducore/keduvowqlreader.cpp | 162 -------------------------------- kdeeducore/keduvowqlreader.h | 42 --------- kdeeducore/keduvowqlwriter.cpp | 130 ------------------------- kdeeducore/keduvowqlwriter.h | 50 ---------- 10 files changed, 103 insertions(+), 487 deletions(-) delete mode 100644 kdeeducore/keduvowqlreader.cpp delete mode 100644 kdeeducore/keduvowqlreader.h delete mode 100644 kdeeducore/keduvowqlwriter.cpp delete mode 100644 kdeeducore/keduvowqlwriter.h diff --git a/kdeeducore/CMakeLists.txt b/kdeeducore/CMakeLists.txt index 99467dd..55cc383 100644 --- a/kdeeducore/CMakeLists.txt +++ b/kdeeducore/CMakeLists.txt @@ -8,8 +8,8 @@ set(kdeeducore_LIB_SRCS keduvockvtmlreader.cpp keduvockvtmlwriter.cpp keduvocmultiplechoice.cpp - keduvowqlreader.cpp - keduvowqlwriter.cpp + keduvocwqlreader.cpp + keduvocwqlwriter.cpp leitnerbox.cpp leitnersystem.cpp leitnersystemview.cpp @@ -39,8 +39,8 @@ install(FILES keduvockvtmlreader.h keduvockvtmlwriter.h keduvocmultiplechoice.h - keduvowqlreader.h - keduvowqlwriter.h + keduvocwqlreader.h + keduvocwqlwriter.h leitnerbox.h leitnersystem.h leitnersystemview.h diff --git a/kdeeducore/keduvocdocument.cpp b/kdeeducore/keduvocdocument.cpp index 7720964..eaf3273 100644 --- a/kdeeducore/keduvocdocument.cpp +++ b/kdeeducore/keduvocdocument.cpp @@ -119,6 +119,50 @@ void KEduVocDocument::Init () } +KEduVocDocument::FileType KEduVocDocument::detectFileType(const QString &fileName) +{ + QFile f(fileName); + if (!f.open(QIODevice::ReadOnly)) + return csv; + + QDataStream is(&f); + + qint8 c1, c2, c3, c4, c5; + is >> c1 + >> c2 + >> c3 + >> c4 + >> c5; // guess filetype by first x bytes + + QTextStream ts(&f); + QString line; + line = ts.readLine(); + line.prepend(c5); + line.prepend(c4); + line.prepend(c3); + line.prepend(c2); + line.prepend(c1); + + if (!is.device()->isOpen()) + return kvd_none; + + f.close(); + if (c1 == '<' && c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l') + return kvtml; + + if (line == WQL_IDENT) + return wql; + + if (line.indexOf(VCB_SEPARATOR) >= 0) + return vt_vcb; + + if (line == LEX_IDENT_50) + return vt_lex; + + return csv; +} + + bool KEduVocDocument::open(const KUrl& url, bool /*append*/) { Init(); @@ -127,13 +171,14 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/) // TODO EPT connect( this, SIGNAL(progressChanged(KEduVocDocument*,int)), parent, SLOT(slotProgress(KEduVocDocument*,int)) ); - QString tmpfile; - if (KIO::NetAccess::download( url, tmpfile, 0 )) + QString errorMessage = i18n("Cannot open file
%1
", url.path()); + QString temporaryFile; + if (KIO::NetAccess::download(url, temporaryFile, 0)) { - QFile f(tmpfile); + QFile f(temporaryFile); if (!f.open(QIODevice::ReadOnly)) { - KMessageBox::error(0, i18n("Cannot open file
%1
", url.path())); + KMessageBox::error(0, errorMessage); return false; } @@ -141,8 +186,7 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/) bool read = false; while (!read) { - - QApplication::setOverrideCursor( Qt::WaitCursor ); + QApplication::setOverrideCursor(Qt::WaitCursor); switch (ft) { case kvtml: { @@ -155,6 +199,8 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/) { KEduVocWqlReader wqlReader(&f); read = wqlReader.readDoc(this); + if (!read) + errorMessage = wqlReader.errorMessage(); } break; @@ -193,20 +239,16 @@ bool KEduVocDocument::open(const KUrl& url, bool /*append*/) Init(); return false; } - // TODO new readers provide an explicite error message - // the two messages should be merged - QString msg = i18n("Could not load \"%1\"\nDo you want to try again?", url.path()); - int result = KMessageBox::warningContinueCancel(0, msg, - i18n("I/O Failure"), - KGuiItem(i18n("&Retry"))); - if ( result == KMessageBox::Cancel ) { + QString msg = i18n("Could not open \"%1\"\nDo you want to try again?\n(Error reported: %2)", url.path(), errorMessage); + int result = KMessageBox::warningContinueCancel(0, msg, i18n("Error Opening File"), KGuiItem(i18n("&Retry"))); + if (result == KMessageBox::Cancel) { Init(); return false; } } } f.close(); - KIO::NetAccess::removeTempFile( tmpfile ); + KIO::NetAccess::removeTempFile(temporaryFile); } return true; } @@ -795,49 +837,6 @@ int KEduVocDocument::search(const QString &substr, int id, int first, int last, } -KEduVocDocument::FileType KEduVocDocument::detectFileType(const QString &filename) -{ - QFile f( filename ); - if (!f.open( QIODevice::ReadOnly )) - return csv; - - QDataStream is( &f ); - - qint8 c1, c2, c3, c4, c5; - is >> c1 - >> c2 - >> c3 - >> c4 - >> c5; // guess filetype by first x bytes - - QTextStream ts (&f); - QString line; - line = ts.readLine(); - line.insert (0, c5); - line.insert (0, c4); - line.insert (0, c3); - line.insert (0, c2); - line.insert (0, c1); - f.close(); - - if (!is.device()->isOpen()) - return kvd_none; - if (c1 == '<' && c2 == '?' && c3 == 'x' && c4 == 'm' && c5 == 'l') - return kvtml; - - if (line == WQL_IDENT) - return wql; - - if (line.indexOf(VCB_SEPARATOR) >= 0) - return vt_vcb; - - if (line == LEX_IDENT_50) - return vt_lex; - - return csv; -} - - class ExpRef { public: diff --git a/kdeeducore/keduvocdocument.h b/kdeeducore/keduvocdocument.h index cffbb10..15c3240 100644 --- a/kdeeducore/keduvocdocument.h +++ b/kdeeducore/keduvocdocument.h @@ -734,7 +734,7 @@ class KDEEDUCORE_EXPORT KEduVocDocument : public QObject void errorCsv(int line, const QString &text ); - FileType detectFileType(const QString &filename); + FileType detectFileType(const QString &fileName); signals: void progressChanged (KEduVocDocument *, int curr_percent); diff --git a/kdeeducore/keduvocwqlreader.cpp b/kdeeducore/keduvocwqlreader.cpp index 57be9ec..4f7d971 100644 --- a/kdeeducore/keduvocwqlreader.cpp +++ b/kdeeducore/keduvocwqlreader.cpp @@ -1,9 +1,9 @@ /*************************************************************************** read a KEduVocDocument from a WQL file ----------------------------------------------------------------------- - copyright : (C) 2004 Peter Hedlund - (C) 2005 Eric Pignet - email : peter.hedlund@kdemail.net + copyright : (C) 2004, 2007 Peter Hedlund + (C) 2005 Eric Pignet + ***************************************************************************/ /*************************************************************************** @@ -15,13 +15,9 @@ * * ***************************************************************************/ -#include #include -#include -#include #include -#include #include "keduvocwqlreader.h" #include "keduvocdocument.h" @@ -30,24 +26,24 @@ KEduVocWqlReader::KEduVocWqlReader(QFile *file) { // the file must be already open m_inputFile = file; + m_errorMessage = ""; } -KEduVocWqlReader::~KEduVocWqlReader() -{ -} bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) { m_doc = doc; QTextStream inputStream(m_inputFile); - inputStream.setEncoding(QTextStream::Latin1); + inputStream.setCodec("ISO-8851-1"); + inputStream.setAutoDetectUnicode(false); + inputStream.seek(0); QString s = ""; s=inputStream.readLine(); if (s != "WordQuiz") { - KMessageBox::error(0, i18n("This does not appear to be a (K)WordQuiz file") + s); + m_errorMessage = i18n("This does not appear to be a (K)WordQuiz file"); return false; } s = inputStream.readLine(); @@ -55,24 +51,27 @@ bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) int iFV = s.toInt(0); if (iFV != 5) { - KMessageBox::error(0, i18n("KWordQuiz can only open files created by WordQuiz 5.x")); + m_errorMessage = i18n("Only files created by WordQuiz 5.x or later can be opened"); return false; } - // TODO these loops cause crashes if the input file is invalid ! - while (inputStream.readLine() != "[Font Info]"); + + m_errorMessage = i18n("Error while reading file"); + + while (!inputStream.atEnd() && inputStream.readLine() != "[Font Info]"); + if (inputStream.atEnd()) + return false; s = inputStream.readLine(); - int p = s.find("=", 0); + int p = s.indexOf("=", 0); QString fam = s.right(s.length() - (p + 1)); fam = fam.mid(1, fam.length() - 2); - //g->font().setFamily(s); s = inputStream.readLine(); - p = s.find("=", 0); + p = s.indexOf("=", 0); s = s.right(s.length() - (p + 1)); int ps = s.toInt(0); s = inputStream.readLine(); - p = s.find("=", 0); + p = s.indexOf("=", 0); s = s.right(s.length() - (p + 1)); int b = 0; if (s == "1") @@ -81,7 +80,7 @@ bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) } s = inputStream.readLine(); - p = s.find("=", 0); + p = s.indexOf("=", 0); s = s.right(s.length() - (p + 1)); bool it = (s == "1"); @@ -93,16 +92,18 @@ bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) p = s.find("=", 0); m_specialCharacters = s.right(s.length() - (p + 1)); */ - while (inputStream.readLine() != "[Grid Info]"); + while (!inputStream.atEnd() && inputStream.readLine() != "[Grid Info]"); + if (inputStream.atEnd()) + return false; inputStream.readLine(); //skip value for width of row headers s = inputStream.readLine(); - p = s.find("=", 0); + p = s.indexOf("=", 0); s = s.right(s.length() - (p + 1)); m_doc->setSizeHint(0, s.toInt()); s = inputStream.readLine(); - p = s.find("=", 0); + p = s.indexOf("=", 0); s = s.right(s.length() - (p + 1)); m_doc->setSizeHint(1, s.toInt()); @@ -132,23 +133,24 @@ bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) s = s.right(s.length() - (p + 1)); m_bottomRight =s.toInt(0, 10) - 1 ; */ - while ((inputStream.readLine() != "[Vocabulary]")); - + while (!inputStream.atEnd() && inputStream.readLine() != "[Vocabulary]"); + if (inputStream.atEnd()) + return false; s = inputStream.readLine(); - p = s.find(" [", 0); + p = s.indexOf(" [", 0); s = s.left(p); - s = s.trimmed(); - m_doc->m_identifiers.push_back(s); - m_doc->m_identifiers.push_back(inputStream.readLine()); + s = s.simplified(); + m_doc->m_identifiers.append(s); + m_doc->m_identifiers.append(inputStream.readLine()); while (!s.isNull()) { s = inputStream.readLine(); - p = s.find("[", 0); + p = s.indexOf("[", 0); QString r = s.mid(p + 1, 10); int h = r.toInt(); s = s.left(p); - s = s.trimmed(); + s = s.simplified(); QString b; b = inputStream.readLine(); diff --git a/kdeeducore/keduvocwqlreader.h b/kdeeducore/keduvocwqlreader.h index e1592aa..a7a47cd 100644 --- a/kdeeducore/keduvocwqlreader.h +++ b/kdeeducore/keduvocwqlreader.h @@ -1,9 +1,9 @@ /*************************************************************************** read a KEduVocDocument from a WQL file ----------------------------------------------------------------------- - copyright : (C) 2004 Peter Hedlund - : (c) 2005 Eric Pignet - email : peter.hedlund@kdemail.net + copyright : (C) 2004, 2007 Peter Hedlund + : (c) 2005 Eric Pignet + ***************************************************************************/ /*************************************************************************** @@ -22,21 +22,20 @@ #include -#include "keduvocdocument.h" - class KEduVocDocument; class KDEEDUCORE_EXPORT KEduVocWqlReader : public QObject { public: - KEduVocWqlReader(QFile *file); - ~KEduVocWqlReader(); + KEduVocWqlReader(QFile *file); - bool readDoc(KEduVocDocument *doc); + bool readDoc(KEduVocDocument *doc); + QString errorMessage() const {return m_errorMessage;}; private: QFile *m_inputFile; KEduVocDocument *m_doc; + QString m_errorMessage; }; #endif diff --git a/kdeeducore/keduvocwqlwriter.cpp b/kdeeducore/keduvocwqlwriter.cpp index 545bbcd..d7188bb 100644 --- a/kdeeducore/keduvocwqlwriter.cpp +++ b/kdeeducore/keduvocwqlwriter.cpp @@ -15,7 +15,7 @@ * * ***************************************************************************/ -#include +#include #include #include @@ -41,7 +41,7 @@ bool KEduVocWqlWriter::writeDoc(KEduVocDocument *doc) m_doc = doc; m_outputStream.setDevice(m_outputFile); - m_outputStream.setEncoding(QTextStream::Latin1); + m_outputStream.setCodec("ISO-8851-1"); m_outputStream << "WordQuiz" << winendl; m_outputStream << "5.9.0" << winendl << winendl; diff --git a/kdeeducore/keduvowqlreader.cpp b/kdeeducore/keduvowqlreader.cpp deleted file mode 100644 index 51a0bf7..0000000 --- a/kdeeducore/keduvowqlreader.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/*************************************************************************** - read a KEduVocDocument from a WQL file - ----------------------------------------------------------------------- - copyright : (C) 2004 Peter Hedlund - (C) 2005 Eric Pignet - email : peter.hedlund@kdemail.net - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -#include "keduvocwqlreader.h" -#include "keduvocdocument.h" - -KEduVocWqlReader::KEduVocWqlReader(QFile *file) -{ - // the file must be already open - m_inputFile = file; -} - -KEduVocWqlReader::~KEduVocWqlReader() -{ -} - -bool KEduVocWqlReader::readDoc(KEduVocDocument *doc) -{ - m_doc = doc; - - QTextStream inputStream(m_inputFile); - inputStream.setCodec("ISO-8851-1"); - inputStream.setAutoDetectUnicode(false); - - QString s = ""; - s=inputStream.readLine(); - if (s != "WordQuiz") - { - KMessageBox::error(0, i18n("This does not appear to be a (K)WordQuiz file") + s); - return false; - } - s = inputStream.readLine(); - s = s.left(1); - int iFV = s.toInt(0); - if (iFV != 5) - { - KMessageBox::error(0, i18n("KWordQuiz can only open files created by WordQuiz 5.x")); - return false; - } - // TODO these loops cause crashes if the input file is invalid ! - while (inputStream.readLine() != "[Font Info]"); - s = inputStream.readLine(); - int p = s.indexOf("=", 0); - QString fam = s.right(s.length() - (p + 1)); - fam = fam.mid(1, fam.length() - 2); - //g->font().setFamily(s); - - s = inputStream.readLine(); - p = s.indexOf("=", 0); - s = s.right(s.length() - (p + 1)); - int ps = s.toInt(0); - - s = inputStream.readLine(); - p = s.indexOf("=", 0); - s = s.right(s.length() - (p + 1)); - int b = 0; - if (s == "1") - { - b = QFont::Bold; - } - - s = inputStream.readLine(); - p = s.indexOf("=", 0); - s = s.right(s.length() - (p + 1)); - bool it = (s == "1"); - - m_doc->setFont(new QFont(fam, ps, b, it)); - -/* TODO - while (inputStream.readLine() != "[Character Info]"); - s = inputStream.readLine(); - p = s.find("=", 0); - m_specialCharacters = s.right(s.length() - (p + 1)); -*/ - while (inputStream.readLine() != "[Grid Info]"); - inputStream.readLine(); //skip value for width of row headers - - s = inputStream.readLine(); - p = s.indexOf("=", 0); - s = s.right(s.length() - (p + 1)); - m_doc->setSizeHint(0, s.toInt()); - - s = inputStream.readLine(); - p = s.indexOf("=", 0); - s = s.right(s.length() - (p + 1)); - m_doc->setSizeHint(1, s.toInt()); - -/* TODO - s = inputStream.readLine(); - p = s.find("=", 0); - s = s.right(s.length() - (p + 1)); - m_numRows = (s.toInt() - 1); //We need to reduce by one since the header is not included - // Selection - s = inputStream.readLine(); - p = s.find("=", 0); - s = s.right(s.length() - (p + 1)); - m_topLeft =s.toInt(0, 10) - 1; - - s = inputStream.readLine(); - p = s.find("=", 0); - s = s.right(s.length() - (p + 1)); - m_topRight =s.toInt(0, 10) - 1; - - s = inputStream.readLine(); - p = s.find("=", 0); - s = s.right(s.length() - (p + 1)); - m_bottomLeft =s.toInt(0, 10) - 1; - - s = inputStream.readLine(); - p = s.find("=", 0); - s = s.right(s.length() - (p + 1)); - m_bottomRight =s.toInt(0, 10) - 1 ; -*/ - while ((inputStream.readLine() != "[Vocabulary]")); - - s = inputStream.readLine(); - p = s.indexOf(" [", 0); - s = s.left(p); - s = s.simplified(); - m_doc->m_identifiers.push_back(s); - m_doc->m_identifiers.push_back(inputStream.readLine()); - - while (!s.isNull()) - { - s = inputStream.readLine(); - p = s.indexOf("[", 0); - QString r = s.mid(p + 1, 10); - int h = r.toInt(); - s = s.left(p); - s = s.simplified(); - - QString b; - b = inputStream.readLine(); - - KEduVocExpression expr = KEduVocExpression(s); - expr.setTranslation(1, b); - m_doc->appendEntry(&expr); - } - return true; -} diff --git a/kdeeducore/keduvowqlreader.h b/kdeeducore/keduvowqlreader.h deleted file mode 100644 index f7ad04b..0000000 --- a/kdeeducore/keduvowqlreader.h +++ /dev/null @@ -1,42 +0,0 @@ -/*************************************************************************** - read a KEduVocDocument from a WQL file - ----------------------------------------------------------------------- - copyright : (C) 2004 Peter Hedlund - : (c) 2005 Eric Pignet - email : peter.hedlund@kdemail.net - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#ifndef KEDUVOCWQLREADER_H -#define KEDUVOCWQLREADER_H - -#include - -#include - -#include "keduvocdocument.h" - -class KEduVocDocument; - -class KDEEDUCORE_EXPORT KEduVocWqlReader : public QObject -{ -public: - KEduVocWqlReader(QFile *file); - ~KEduVocWqlReader(); - - bool readDoc(KEduVocDocument *doc); - -private: - QFile *m_inputFile; - KEduVocDocument *m_doc; -}; - -#endif diff --git a/kdeeducore/keduvowqlwriter.cpp b/kdeeducore/keduvowqlwriter.cpp deleted file mode 100644 index 842ae77..0000000 --- a/kdeeducore/keduvowqlwriter.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/*************************************************************************** - export a KEduVocDocument to a WQL file - ----------------------------------------------------------------------- - copyright : (C) 2004 Peter Hedlund - (C) 2005 Eric Pignet - email : peter.hedlund@kdemail.net - ***************************************************************************/ - -/*************************************************************************** - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - ***************************************************************************/ - -#include -#include -#include - -#include - -#include "keduvocwqlwriter.h" -#include "keduvocdocument.h" - -#define winendl "\r\n" - -KEduVocWqlWriter::KEduVocWqlWriter(QFile *file) -{ - // the file must be already open - m_outputFile = file; -} - -KEduVocWqlWriter::~KEduVocWqlWriter() -{ -} - -bool KEduVocWqlWriter::writeDoc(KEduVocDocument *doc) -{ - m_doc = doc; - - m_outputStream.setDevice(m_outputFile); - m_outputStream.setCodec("ISO-8851-1"); - m_outputStream.setAutoDetectUnicode(false); - - m_outputStream << "WordQuiz" << winendl; - m_outputStream << "5.9.0" << winendl << winendl; - - if (doc->font() != NULL) - writeFont(*(doc->font())); - // TODO writeCharacters(Prefs::specialCharacters()); - // TODO Find the vertical header width, handle the case where there are not exactly two languages - writeGridInfo(10, m_doc->sizeHint(0), m_doc->sizeHint(1), m_doc->numEntries()); - /* TODO Find the selected cells - if (g->numSelections() > 0) - { - QTableSelection qts = g->selection(0); - writeSelection(qts.leftCol(), qts.topRow(), qts.rightCol(), qts.bottomRow()); - } - else - { - writeSelection(g->currentColumn(), g->currentRow(), g->currentColumn(), g->currentRow()); - }*/ - writeFirstItem(m_doc->originalIdentifier(), m_doc->identifier(1)); - int r = m_doc->numEntries(); - for (int w=0; w < r; w++) - { - // TODO Find the row height (g->rowHeight(w)) - writeItem(m_doc->entry(w)->original(), m_doc->entry(w)->translation(1), 30); - } - return true; -} - -void KEduVocWqlWriter::writeFont( const QFont & font ) -{ - m_outputStream << "[Font Info]" << winendl; - m_outputStream << "FontName1=\"" << font.family() << "\"" << winendl; - m_outputStream << "FontSize1=" << QString::number(font.pointSize()) << winendl; - m_outputStream << QString("FontBold1=%1").arg(font.bold() ? "1" : "0") < - -#include -#include -#include - -class KEduVocDocument; - -class KDEEDUCORE_EXPORT KEduVocWqlWriter -{ -public: - KEduVocWqlWriter(QFile *file); - ~KEduVocWqlWriter(); - - bool writeDoc(KEduVocDocument *doc); - - void writeFont(const QFont & font); - void writeCharacters(const QString & s); - void writeGridInfo(int col0, int col1, int col2, int numRows); - void writeSelection(int lc, int tr, int rc, int br); - void writeFirstItem(const QString &ll, const QString &rl); - void writeItem(const QString &left, const QString &right, int rh); - -private: - QFile *m_outputFile; - QTextStream m_outputStream; - KEduVocDocument *m_doc; -}; - -#endif -- 2.47.3