summaryrefslogtreecommitdiff
path: root/src/study/StudyFileWriter.cpp
blob: ab68d6f088c2f116c4abdbd6cc7cb1eb3cf672a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 />
    }