Make cachedUntil public
[evehomescreen] / src / eveskilltraining.cpp
1 // Copyright (C) 2010 Jaakko Kyro <jkyro@korjaussarja.net>
2 // This file is licenced under GPL, see COPYING
3 // for full licence information
4 #include "eveskilltraining.h"
5 #include "evemodel.h"
6 #include "eveaccount.h"
7
8 #include <QXmlStreamReader>
9 #include <QNetworkAccessManager>
10 #include <QNetworkRequest>
11 #include <QNetworkReply>
12 #include <QtDebug>
13 EveSkillTraining::EveSkillTraining(QObject *parent) :
14     QObject(parent),
15     training(false),
16     typeId(0),
17     startSkillpoints(0),
18     destSkillpoints(0),
19     level(0),
20     m_character(NULL),
21     m_account(NULL),
22     m_reply(NULL)
23 {
24 }
25
26 bool EveSkillTraining::fromXml(QXmlStreamReader &xml)
27 {
28     xml.readNextStartElement();
29     if (xml.name() != "eveapi")
30         return false;
31     xml.readNextStartElement();
32     if (xml.name() != "currentTime")
33         return false;
34
35     xml.readNextStartElement(); // end currentTime element
36     bool inResult = xml.readNextStartElement(); // start result element
37     if (xml.name() != "result") {
38         qDebug() << "Wrong element:" << xml.name();
39         return false;
40     }
41     inResult = xml.readNextStartElement();
42     while (inResult) {
43         qDebug() << "element:" << xml.name();
44         if (xml.name() == "trainingEndTime") {
45             qDebug() << "Parse end time";
46             endTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");//2008-08-17 06:43:00
47             startTime.setTimeSpec(Qt::UTC);
48             qDebug()<< endTime;
49
50         } else if (xml.name() == "trainingStartTime") {
51             qDebug() << "Parse start time";
52             startTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");//2008-08-17 06:43:00
53             startTime.setTimeSpec(Qt::UTC);
54             qDebug() << startTime;
55
56         } else if (xml.name() == "trainingTypeID") {
57             typeId = xml.readElementText().toInt();
58
59         } else if (xml.name() == "trainingStartSP"){
60             startSkillpoints = xml.readElementText().toInt();
61
62         } else if (xml.name() == "trainingDestinationSP"){
63             destSkillpoints = xml.readElementText().toInt();
64
65         } else if (xml.name() == "trainingToLevel"){
66             level = xml.readElementText().toInt();
67         } else if (xml.name() == "skillInTraining" ) {
68
69             training = xml.readElementText().toInt() == 1;
70             qDebug() << "Training?" << training;
71         } else if (xml.name() == "currentTQTime" ) {
72             currentTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");
73             startTime.setTimeSpec(Qt::UTC);
74             qDebug() << "Current: " << currentTime;
75         }  else {
76             // Noop
77             qDebug() << "  Skip this";
78             xml.skipCurrentElement();
79         }
80         inResult = xml.readNextStartElement();
81
82     } // while
83     inResult = xml.readNextStartElement();
84     if (xml.name() == "cachedUntil" ) {
85         cachedUntil = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");
86         cachedUntil.setTimeSpec(Qt::UTC);
87         qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
88     }
89     qDebug() << "Parsing finished";
90     return true;
91 }
92
93 void EveSkillTraining::fetchInfo()
94 {
95     if (m_character == NULL || m_account == NULL)
96         return;
97     if (cachedUntil.isValid()) {
98         if (cachedUntil > QDateTime::currentDateTime().toUTC() ) {
99             qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
100             return;
101         }
102     }
103     QNetworkRequest req(QUrl(QString("http://api.eveonline.com/char/SkillInTraining.xml.aspx?apiKey=%1&userID=%2&characterID=%3")
104                              .arg(m_account->apiKey())
105                              .arg(m_account->userId())
106                              .arg(m_character->characterId)));
107     m_reply = m_mgr.get(req);
108     connect(m_reply,SIGNAL(finished()),this,SLOT(infoReady()));
109 }
110
111 void EveSkillTraining::infoReady()
112 {
113     if (m_reply->error()) {
114         qDebug() << "Failed! " << m_reply->errorString();
115         return;
116     }
117     qDebug() << "Skill reply";
118     QByteArray reply = m_reply->readAll();
119     qDebug() << "Reply ready";
120     qDebug() << reply;
121     QXmlStreamReader reader(reply);
122     fromXml(reader);
123     m_reply->deleteLater();
124     emit finished();
125 }