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"
6 #include "eveaccount.h"
8 #include <QXmlStreamReader>
9 #include <QNetworkAccessManager>
10 #include <QNetworkRequest>
11 #include <QNetworkReply>
13 EveSkillTraining::EveSkillTraining(QObject *parent) :
26 bool EveSkillTraining::fromXml(QXmlStreamReader &xml)
28 xml.readNextStartElement();
29 if (xml.name() != "eveapi")
31 xml.readNextStartElement();
32 if (xml.name() != "currentTime")
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();
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);
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;
56 } else if (xml.name() == "trainingTypeID") {
57 typeId = xml.readElementText().toInt();
59 } else if (xml.name() == "trainingStartSP"){
60 startSkillpoints = xml.readElementText().toInt();
62 } else if (xml.name() == "trainingDestinationSP"){
63 destSkillpoints = xml.readElementText().toInt();
65 } else if (xml.name() == "trainingToLevel"){
66 level = xml.readElementText().toInt();
67 } else if (xml.name() == "skillInTraining" ) {
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);
81 qDebug() << " Skip this";
82 xml.skipCurrentElement();
84 inResult = xml.readNextStartElement();
88 qDebug() << "Parsing finished";
92 void EveSkillTraining::fetchInfo()
94 if (m_character == NULL || m_account == NULL)
96 if (cachedUntil.isValid()) {
97 if (cachedUntil > QDateTime::currentDateTime().toUTC() ) {
98 qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
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()));
110 void EveSkillTraining::infoReady()
112 if (m_reply->error()) {
113 qDebug() << "Failed! " << m_reply->errorString();
116 qDebug() << "Skill reply";
117 QByteArray reply = m_reply->readAll();
118 qDebug() << "Reply ready";
120 QXmlStreamReader reader(reply);
122 m_reply->deleteLater();