e86fc0894810974614f959d729ebd9baaa774bdf
[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
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         } if (xml.name() == "cachedUntil" ) {
76             cachedUntil = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");
77             cachedUntil.setTimeSpec(Qt::UTC);
78             qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
79         } else {
80             // Noop
81             qDebug() << "  Skip this";
82             xml.skipCurrentElement();
83         }
84         inResult = xml.readNextStartElement();
85
86     } // while
87
88     qDebug() << "Parsing finished";
89     return true;
90 }
91
92 void EveSkillTraining::fetchInfo()
93 {
94     if (m_character == NULL || m_account == NULL)
95         return;
96     if (cachedUntil.isValid()) {
97         if (cachedUntil > QDateTime::currentDateTime().toUTC() ) {
98             qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
99             return;
100         }
101     }
102     QNetworkRequest req(QUrl(QString("http://api.eveonline.com/char/SkillInTraining.xml.aspx?apiKey=%1&userID=%2&characterID=%3")
103                              .arg(m_account->apiKey())
104                              .arg(m_account->userId())
105                              .arg(m_character->characterId)));
106     m_reply = m_mgr.get(req);
107     connect(m_reply,SIGNAL(finished()),this,SLOT(infoReady()));
108 }
109
110 void EveSkillTraining::infoReady()
111 {
112     if (m_reply->error()) {
113         qDebug() << "Failed! " << m_reply->errorString();
114         return;
115     }
116     qDebug() << "Skill reply";
117     QByteArray reply = m_reply->readAll();
118     qDebug() << "Reply ready";
119     qDebug() << reply;
120     QXmlStreamReader reader(reply);
121     fromXml(reader);
122     m_reply->deleteLater();
123     emit finished();
124 }