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();
41 inResult = xml.readNextStartElement();
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;
77 qDebug() << " Skip this";
78 xml.skipCurrentElement();
80 inResult = xml.readNextStartElement();
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);
89 qDebug() << "Parsing finished";
93 void EveSkillTraining::fetchInfo()
95 if (m_character == NULL || m_account == NULL)
97 if (cachedUntil.isValid()) {
98 if (cachedUntil > QDateTime::currentDateTime().toUTC() ) {
99 qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
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()));
111 void EveSkillTraining::infoReady()
113 if (m_reply->error()) {
114 qDebug() << "Failed! " << m_reply->errorString();
117 qDebug() << "Skill reply";
118 QByteArray reply = m_reply->readAll();
119 qDebug() << "Reply ready";
121 QXmlStreamReader reader(reply);
123 m_reply->deleteLater();