summaryrefslogtreecommitdiff
path: root/src/study/StudyFileReader.cpp
blob: ecc4dbfc97b9c78933996bc57dfc52116e06b36e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "StudyFileReader.h"
#include "../dictionary/Dictionary.h"
#include "../version.h"
#include "../dictionary/CardPack.h"
#include "../dictionary/DicRecord.h"

#include <QMessageBox>

const QString StudyFileReader::MinSupportedStudyVersion = "1.0";

StudyFileReader::StudyFileReader( Dictionary* aDict ):
    m_dict( aDict ), m_cardPack( NULL )
    {
    settings = StudySettings::inst();
    }

bool StudyFileReader::read( QIODevice* aDevice )
    {
    setDevice( aDevice );
    while( !atEnd() )
        {
        readNext();
        if( isStartElement() )
            {
            if( name() == "study" )
                {
                readStudy();
                return !error();    // readStudy() processes all situations
                }
            else
                raiseError( Dictionary::tr("The file is not a study file.") );
            }
        }
    return !error();
    }

void StudyFileReader::readStudy()
    {
    Q_ASSERT( isStartElement() && name() == "study" );

    m_studyVersion = attributes().value( "version" ).toString();
    if(m_studyVersion >= MinSupportedStudyVersion)
        readStudyCurrentVersion();
    else
        QMessageBox::warning( NULL, Dictionary::tr("Unsupported format"),
            Dictionary::tr("The study file uses unsupported format %1.\n"
                        "The minimum supported version is %2" )
                        .arg( m_studyVersion )
                        .arg( MinSupportedStudyVersion ) );
    }

void StudyFileReader::readUnknownElement()
    {
    Q_ASSERT( isStartElement() );
     while( !atEnd() )
        {
        readNext();
        if( isEndElement() )
            break;
        if( isStartElement() )
            readUnknownElement();
        }
    }

void StudyFileReader::readStudyCurrentVersion()
    {
    Q_ASSERT( isStartElement() && name() == "study" );

    while( !atEnd() )
        {
        readNext();
        if( isEndElement() )
            break;
        if( isStartElement() )
            {
            if( name() == "pack" )
                readPack();
            else
                readUnknownElement();
            }
        }
    if(m_studyVersion != STUDY_VERSION)
        m_dict->setStudyModified();
    }

void StudyFileReader::readPack()
    {
    Q_ASSERT( isStartElement() && name() == "pack" );

    QString id = attributes().value( "id" ).toString();
    m_cardPack = m_dict->cardPack( id );
    if( !m_cardPack )
    {
        skipCurrentElement();
        return;
    }
    m_cardPack->setReadingStudyFile(true);

    while( !atEnd() )
        {
        readNext();
        if( isEndElement() )
            break;
        if( isStartElement() )
            {
            if( name() == "cur-card" )
                {
                Q_ASSERT( m_cardPack->curCardName().isEmpty() );
                QString curCard = attributes().value( "id" ).toString();
                if( !curCard.isEmpty() )
                    m_cardPack->setCurCard( curCard );
                readNext(); // read end of 'cur-card' element
                Q_ASSERT( isEndElement() );
                }
            else if( name() == "c" )
                readC();
            else
                readUnknownElement();
            }
        }

    m_cardPack->setReadingStudyFile(false);
    }

void StudyFileReader::readC()
    {
    Q_ASSERT( isStartElement() && name() == "c" && m_cardPack );

    m_cardId = attributes().value( "id" ).toString();

    while( !atEnd() )
        {
        readNext();
        if( isEndElement() )
            break;
        if( isStartElement() )
            {
            if( name() == "r" )
                readR();
            else
                readUnknownElement();
            }
        }
    }

void StudyFileReader::readR()
    {
    Q_ASSERT( isStartElement() && name() == "r" );

    StudyRecord study;
    bool ok;    // for number conversions

    QString date = attributes().value( "d" ).toString();
    study.date = QDateTime::fromString( date, Qt::ISODate );

    QString grade = attributes().value( "g" ).toString();
    study.grade = grade.toInt( &ok );
    if(m_studyVersion < "1.4" && study.grade < 3)
        study.grade++;

    QString easiness = attributes().value( "e" ).toString();
    study.easiness = easiness.toFloat( &ok );
    
    QString time;
    time = attributes().value( "t" ).toString();    // obsolete parameter
    if(!time.isEmpty())
        {
        study.recallTime = time.toFloat( &ok );
        Q_ASSERT( ok );
        }

    time = attributes().value( "rt" ).toString();
    if(!time.isEmpty())
        {
        study.recallTime = time.toFloat( &ok );
        if( study.recallTime > StudyRecord::MaxAnswerTime )
            study.recallTime = StudyRecord::MaxAnswerTime;
        Q_ASSERT( ok );
        }

    time = attributes().value( "at" ).toString();
    if(!time.isEmpty())
        {
        study.answerTime = time.toFloat( &ok );
        if( study.answerTime > StudyRecord::MaxAnswerTime )
            study.answerTime = StudyRecord::MaxAnswerTime;
        Q_ASSERT( ok );
        }
    
    QString interval = attributes().value( "i" ).toString();
    QString level = attributes().value( "l" ).toString();
    if(!interval.isEmpty() && interval != "0")
        {
        study.interval = interval.toFloat();
        if(!level.isEmpty())
            study.level = level.toInt();
        else
            study.level = StudyRecord::Repeating;
        if(m_studyVersion == "1.4")
            fixupIncorrectGradeIn1_4(study);
        }
    else    // delayed card
        {
        QString cardsStr = attributes().value( "c" ).toString();
        if( !cardsStr.isEmpty() )
            {
            int cardInterval = cardsStr.toInt();
            if(cardInterval <= 7)
                study.interval = settings->unknownInterval;
            else
                study.interval = settings->incorrectInterval;
            }
        else
            study.interval = 0;
        study.level = StudyRecord::ShortLearning;
        }
    
    m_cardPack->addStudyRecord( m_cardId, study );

    readElementText();  // read until element end
    }

void StudyFileReader::fixupIncorrectGradeIn1_4(StudyRecord& study)
{
    if(!(study.level == StudyRecord::ShortLearning &&
            study.grade <= 3))
        return;
    if(equalDouble(study.interval, settings->unknownInterval))
        study.grade = StudyRecord::Unknown;
    else if(equalDouble(study.interval, settings->incorrectInterval))
        study.grade = StudyRecord::Incorrect;
}

bool StudyFileReader::equalDouble(double a, double b)
{
    static const double Dif = 0.0001;
    return fabs(a - b) < Dif;
}