summaryrefslogtreecommitdiff
path: root/src/study/StudyFileWriter.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/study/StudyFileWriter.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/study/StudyFileWriter.cpp')
-rw-r--r--src/study/StudyFileWriter.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/study/StudyFileWriter.cpp b/src/study/StudyFileWriter.cpp
new file mode 100644
index 0000000..ab68d6f
--- /dev/null
+++ b/src/study/StudyFileWriter.cpp
@@ -0,0 +1,68 @@
+#include "StudyFileWriter.h"
+#include "StudySettings.h"
+#include "../dictionary/Dictionary.h"
+#include "../version.h"
+#include "../dictionary/CardPack.h"
+
+StudyFileWriter::StudyFileWriter( const Dictionary* aDict ):
+ m_dict( aDict )
+{
+ setAutoFormatting( true );
+}
+
+bool StudyFileWriter::write( QIODevice* aDevice )
+{
+ setDevice( aDevice );
+ writeStartDocument();
+ writeDTD( "<!DOCTYPE freshmemory-study>" );
+ writeStartElement("study");
+ writeAttribute( "version", STUDY_VERSION );
+
+ foreach( CardPack* pack, m_dict->cardPacks() )
+ writePack( pack );
+
+ writeEndDocument();
+ return true;
+}
+
+void StudyFileWriter::writePack( const CardPack* aPack )
+ {
+ QStringList cardIds = aPack->getCardQuestions();
+ if( cardIds.isEmpty() )
+ return; // Don't write empty pack
+ writeStartElement( "pack" );
+ writeAttribute( "id", aPack->id() );
+ if( !aPack->curCardName().isEmpty() )
+ {
+ writeEmptyElement( "cur-card" );
+ writeAttribute( "id", aPack->curCardName() );
+ }
+ foreach( QString cardId, cardIds )
+ writeCard( cardId, aPack );
+ writeEndElement();
+ }
+
+void StudyFileWriter::writeCard( const QString& aCardId, const CardPack* aPack )
+ {
+ QList<StudyRecord> studyRecords( aPack->getStudyRecords( aCardId ) );
+ if( studyRecords.isEmpty() ) // Don't write cards without records
+ return;
+ writeStartElement( "c" );
+ writeAttribute( "id", aCardId );
+ // Take study records from the list in reverse order. The first is the most recent.
+ QListIterator<StudyRecord> it( studyRecords );
+ it.toBack();
+ while( it.hasPrevious() )
+ {
+ StudyRecord record = it.previous();
+ writeEmptyElement( "r" );
+ writeAttribute( "d", record.date.toString( Qt::ISODate ) );
+ writeAttribute( "l", QString::number( record.level ) );
+ writeAttribute( "g", QString::number( record.grade ) );
+ writeAttribute( "e", QString::number( record.easiness ) );
+ writeAttribute( "rt", QString::number( record.recallTime, 'g', 4 ) );
+ writeAttribute( "at", QString::number( record.answerTime, 'g', 4 ) );
+ writeAttribute( "i", QString::number( record.interval, 'g', 6 ) );
+ }
+ writeEndElement(); // <c />
+ }