Add copying and licence information
[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     if (xml.name() != "result") {
29         qDebug() << "Wrong element:" << xml.name();
30         return false;
31     }
32     qDebug() << "passed, element is result";
33     bool inResult = true;
34     while (inResult) {
35         inResult = xml.readNextStartElement();
36         qDebug() << "element:" << xml.name();
37         if (xml.name() == "trainingEndTime") {
38             qDebug() << "Parse end time";
39             endTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");//2008-08-17 06:43:00
40             qDebug()<< endTime;
41
42         } else if (xml.name() == "trainingStartTime") {
43             qDebug() << "Parse start time";
44             startTime = QDateTime::fromString(xml.readElementText(),"yyyy-MM-dd hh:mm:ss");//2008-08-17 06:43:00
45             qDebug() << startTime;
46
47         } else if (xml.name() == "trainingTypeID") {
48             typeId = xml.readElementText().toInt();
49
50         } else if (xml.name() == "trainingStartSP"){
51             startSkillpoints = xml.readElementText().toInt();
52
53         } else if (xml.name() == "trainingDestinationSP"){
54             destSkillpoints = xml.readElementText().toInt();
55
56         } else if (xml.name() == "trainingToLevel"){
57             level = xml.readElementText().toInt();
58         } else
59             // Noop
60             xml.skipCurrentElement();
61
62     } // while
63     qDebug() << "Parsing finished";
64     return true;
65 }
66
67 void EveSkillTraining::fetchInfo()
68 {
69     if (m_character == NULL || m_account == NULL)
70         return;
71     QNetworkRequest req(QUrl(QString("http://api.eveonline.com/char/SkillInTraining.xml.aspx?apiKey=%1&userID=%2&characterID=%3")
72                              .arg(m_account->apiKey())
73                              .arg(m_account->userId())
74                              .arg(m_character->characterId)));
75     m_reply = m_mgr.get(req);
76     connect(m_reply,SIGNAL(finished()),this,SLOT(infoReady()));
77 }
78
79 void EveSkillTraining::infoReady()
80 {
81     if (m_reply->error()) {
82         qDebug() << "Failed! " << m_reply->errorString();
83         return;
84     }
85     qDebug() << "Skill reply";
86     QByteArray reply = m_reply->readAll();
87     qDebug() << "Reply ready";
88     qDebug() << reply;
89     QXmlStreamReader reader(reply);
90
91     reader.readNextStartElement();
92     if (reader.name() != "eveapi")
93         return;
94     reader.readNextStartElement();
95     if (reader.name() != "currentTime")
96         return;
97     reader.readNextStartElement(); // end currentTime element
98     reader.readNextStartElement(); // start result element
99     fromXml(reader);
100     emit finished();
101 }