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) :
22 bool EveSkillTraining::fromXml(QXmlStreamReader &xml)
24 xml.readNextStartElement();
25 if (xml.name() != "eveapi")
27 xml.readNextStartElement();
28 if (xml.name() != "currentTime") {
32 currentTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");
33 currentTime.setTimeSpec(Qt::UTC);
34 //xml.readNextStartElement(); // end currentTime element
35 bool inResult = xml.readNextStartElement(); // start result element
36 if (xml.name() != "result") {
37 qDebug() << "Wrong element:" << xml.name();
40 inResult = xml.readNextStartElement();
41 if (!inResult || xml.name() != "rowset" ) {
42 qDebug() << "No rowset element";
45 inResult = xml.readNextStartElement();
46 if (xml.name() == "row") {
48 m_trainingQueue.clear();
50 while (xml.name() == "row") {
53 entry.typeId = xml.attributes().value("","typeID").toString().toInt();
54 entry.destSkillpoints = xml.attributes().value("","endSP").toString().toInt();
55 entry.startSkillpoints = xml.attributes().value("","startSP").toString().toInt();
56 entry.level = xml.attributes().value("","level").toString().toInt();
57 if (xml.attributes().hasAttribute("","startTime")) {
58 entry.startTime = QDateTime::fromString(xml.attributes().value("","startTime").toString(),
59 "yyyy-MM-dd hh:mm:ss");
60 entry.startTime.setTimeSpec(Qt::UTC);
62 if (xml.attributes().hasAttribute("","endTime")) {
63 entry.endTime = QDateTime::fromString(xml.attributes().value("","endTime").toString(),
64 "yyyy-MM-dd hh:mm:ss");
65 entry.endTime.setTimeSpec(Qt::UTC);
68 m_trainingQueue.append(entry);
69 if (!xml.readNextStartElement() ) // end row element
70 xml.readNextStartElement(); // start next row element
72 if (xml.name() == "cachedUntil" ) {
73 cachedUntil = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");
74 cachedUntil.setTimeSpec(Qt::UTC);
75 qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
77 qDebug() << "Parsing finished";
78 qDebug() << firstTraining();
82 void EveSkillTraining::fetchInfo()
84 if (m_character == NULL || m_account == NULL)
86 if (cachedUntil.isValid()) {
87 if (cachedUntil > QDateTime::currentDateTime().toUTC() ) {
88 qDebug() << "Cached until " << cachedUntil.toString(Qt::SystemLocaleShortDate);
92 QNetworkRequest req(QUrl(QString("http://api.eveonline.com/char/SkillQueue.xml.aspx?apiKey=%1&userID=%2&characterID=%3")
93 .arg(m_account->apiKey())
94 .arg(m_account->userId())
95 .arg(m_character->characterId)));
96 m_reply = m_mgr.get(req);
97 connect(m_reply,SIGNAL(finished()),this,SLOT(infoReady()));
100 void EveSkillTraining::infoReady()
102 if (m_reply->error()) {
103 qDebug() << "Failed! " << m_reply->errorString();
106 qDebug() << "Skill reply";
107 QByteArray reply = m_reply->readAll();
108 qDebug() << "Reply ready";
110 QXmlStreamReader reader(reply);
112 m_reply->deleteLater();
116 QDebug operator<<(QDebug dbg, const TrainingEntry &e)
118 dbg.nospace() << e.typeId << " to " << e.level << e.startTime << " -> " << e.endTime;