keduvockvtmlreader.cpp
keduvockvtmlwriter.cpp
keduvocmultiplechoice.cpp
- keduvowqlreader.cpp
- keduvowqlwriter.cpp
+ keduvocwqlreader.cpp
+ keduvocwqlwriter.cpp
leitnerbox.cpp
leitnersystem.cpp
leitnersystemview.cpp
keduvockvtmlreader.h
keduvockvtmlwriter.h
keduvocmultiplechoice.h
- keduvowqlreader.h
- keduvowqlwriter.h
+ keduvocwqlreader.h
+ keduvocwqlwriter.h
leitnerbox.h
leitnersystem.h
leitnersystemview.h
}
+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();
// 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("<qt>Cannot open file<br><b>%1</b></qt>", 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("<qt>Cannot open file<br><b>%1</b></qt>", url.path()));
+ KMessageBox::error(0, errorMessage);
return false;
}
bool read = false;
while (!read) {
-
- QApplication::setOverrideCursor( Qt::WaitCursor );
+ QApplication::setOverrideCursor(Qt::WaitCursor);
switch (ft) {
case kvtml:
{
{
KEduVocWqlReader wqlReader(&f);
read = wqlReader.readDoc(this);
+ if (!read)
+ errorMessage = wqlReader.errorMessage();
}
break;
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;
}
}
-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:
void errorCsv(int line, const QString &text );
- FileType detectFileType(const QString &filename);
+ FileType detectFileType(const QString &fileName);
signals:
void progressChanged (KEduVocDocument *, int curr_percent);
/***************************************************************************
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 <peter.hedlund@kdemail.net>
+ (C) 2005 Eric Pignet
+
***************************************************************************/
/***************************************************************************
* *
***************************************************************************/
-#include <QDom>
#include <QTextStream>
-#include <kapplication.h>
-#include <kdebug.h>
#include <klocale.h>
-#include <kmessagebox.h>
#include "keduvocwqlreader.h"
#include "keduvocdocument.h"
{
// 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();
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")
}
s = inputStream.readLine();
- p = s.find("=", 0);
+ p = s.indexOf("=", 0);
s = s.right(s.length() - (p + 1));
bool it = (s == "1");
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());
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();
/***************************************************************************
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 <peter.hedlund@kdemail.net>
+ : (c) 2005 Eric Pignet
+
***************************************************************************/
/***************************************************************************
#include <QFile>
-#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
* *
***************************************************************************/
-#include <QDom>
+#include <QtXml>
#include <QTextStream>
#include <QFont>
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;
+++ /dev/null
-/***************************************************************************
- 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 <QtXml>
-#include <QTextStream>
-
-#include <kapplication.h>
-#include <kdebug.h>
-#include <klocale.h>
-#include <kmessagebox.h>
-
-#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;
-}
+++ /dev/null
-/***************************************************************************
- 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 <kdeedu_core_export.h>
-
-#include <QFile>
-
-#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
+++ /dev/null
-/***************************************************************************
- 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 <QtXml>
-#include <QTextStream>
-#include <QFont>
-
-#include <kdebug.h>
-
-#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") <<winendl;
- m_outputStream << QString("FontItalic1=%1").arg(font.italic() ? "1" : "0") <<winendl;
- m_outputStream << "FontColor1=0" << winendl;
- m_outputStream << "CharSet1=0" << winendl;
- m_outputStream << "Layout1=0" << winendl;
-
- m_outputStream << "FontName2=\"" << font.family() << "\"" << winendl;
- m_outputStream << "FontSize2=" << QString::number(font.pointSize()) << winendl;
- m_outputStream << QString("FontBold2=%1").arg(font.bold() ? "1" : "0") <<winendl;
- m_outputStream << QString("FontItalic2=%1").arg(font.italic() ? "1" : "0") <<winendl;
- m_outputStream << "FontColor2=0" << winendl;
- m_outputStream << "CharSet2=0" << winendl;
- m_outputStream << "Layout2=0" << winendl << winendl;
-}
-
-void KEduVocWqlWriter::writeCharacters( const QString & s )
-{
- m_outputStream << "[Character Info]" << winendl;
- m_outputStream << "Characters1=" << s << winendl;
- m_outputStream << "Characters2=" << s << winendl << winendl;
-}
-
-void KEduVocWqlWriter::writeGridInfo( int col0, int col1, int col2, int numRows )
-{
- m_outputStream << "[Grid Info]" << winendl;
- m_outputStream << "ColWidth0=" << QString::number(col0) << winendl;
- m_outputStream << "ColWidth1=" << QString::number(col1) << winendl;
- m_outputStream << "ColWidth2=" << QString::number(col2) << winendl;
- m_outputStream << "RowCount=" << QString::number(numRows + 1) << winendl; //Add one for the header
-}
-
-void KEduVocWqlWriter::writeSelection( int lc, int tr, int rc, int br ) //part of [Grid Info]
-{
- m_outputStream << "SelLeft=" << QString::number(lc + 1) << winendl;
- m_outputStream << "SelTop=" << QString::number(tr + 1) << winendl;
- m_outputStream << "SelRight=" << QString::number(rc + 1) << winendl;
- m_outputStream << "SelBottom=" << QString::number(br + 1) << winendl << winendl;
-}
-
-void KEduVocWqlWriter::writeFirstItem( const QString & ll, const QString & rl )
-{
- m_outputStream << "[Vocabulary]" << winendl;
- m_outputStream << ll << " [0000000300]" << winendl;
- m_outputStream << rl << winendl;
-}
-
-void KEduVocWqlWriter::writeItem( const QString & left, const QString & right, int rh )
-{
- m_outputStream << left << QString( " [%1]").arg(rh * 15, 10, 10 ) << winendl ;
- m_outputStream << right << winendl;
-}
+++ /dev/null
-/***************************************************************************
- 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. *
- * *
- ***************************************************************************/
-
-#ifndef KEDUVOCWQLWRITER_H
-#define KEDUVOCWQLWRITER_H
-
-#include <kdeedu_core_export.h>
-
-#include <QFile>
-#include <QFont>
-#include <QTextStream>
-
-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