Add copying and licence information
[evehomescreen] / src / skilltree.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 "skilltree.h"
5 #include <QFile>
6 #include <QDataStream>
7 #include <QNetworkAccessManager>
8 #include <QNetworkReply>
9 #include <QNetworkRequest>
10 #include <QtDebug>
11 #include <QXmlStreamReader>
12 #include <assert.h>
13
14 SkillTree::SkillTree(QObject *parent) :
15     QObject(parent),
16     mgr(0),
17     reply(0)
18 {
19     QFile cacheFile("/var/tmp/skillCache");
20     if (cacheFile.exists()) {
21         QDataStream input(&cacheFile);
22         input >> skillNames;
23     } else {
24         // load from network
25
26     }
27
28 }
29
30 void SkillTree::loadSkills()
31 {
32     QFile cacheFile("/var/tmp/skillCache");
33     if (cacheFile.exists()) {
34         cacheFile.open(QIODevice::ReadOnly);
35         qDebug() << "Load from file";
36         QDataStream input(&cacheFile);
37         input >> skillNames;
38         emit skillsLoaded();
39     } else {
40         // load from network
41         qDebug() << "load skills from network";
42         mgr = new QNetworkAccessManager(this);
43         QNetworkRequest req(QUrl("http://api.eveonline.com//eve/SkillTree.xml.aspx"));
44         reply = mgr->get(req);
45         connect(reply,SIGNAL(finished()),this,SLOT(replyReady()));
46     }
47 }
48
49 void SkillTree::replyReady()
50 {
51     qDebug() << "Reply ready";
52     if (!reply->error()) {
53         QByteArray replyContent = reply->readAll();
54         qDebug() << replyContent;
55         fromXml(replyContent);
56         emit skillsLoaded();
57     } else {
58         qDebug() << "fetch error:" << reply->errorString();
59     }
60 }
61
62 void SkillTree::fromXml(QByteArray &content)
63 {
64     QXmlStreamReader xml(content);
65     xml.readNextStartElement(); //eveapi
66     //assert(xml.name() == "eveapi");
67     xml.readNextStartElement(); //currentTime
68     xml.skipCurrentElement();
69     //assert(xml.name() == "currentTime");
70     xml.readNextStartElement(); //result
71     xml.readNextStartElement(); // rowset for skill groups
72     bool groupsLeft = xml.readNextStartElement(); // row for skill group
73     while (groupsLeft) {
74         xml.readNextStartElement(); // rowset for skills
75         bool skillsLeft = xml.readNextStartElement(); // row for skill
76         while (skillsLeft) {
77             assert(xml.name() == "row");
78             QString name = xml.attributes().value("","typeName").toString();
79             int typeId = xml.attributes().value("","typeID").toString().toInt();
80             qDebug() << "Found skill " << name << ":" << typeId;
81             skillNames[typeId] = name;
82
83             xml.skipCurrentElement(); // description
84             skillsLeft = xml.readNextStartElement(); // next skill row
85         }
86         xml.readNextStartElement(); // rowset ends
87         groupsLeft = xml.readNextStartElement(); // next skill group row
88     }
89     int len = 0;
90     QString name;
91     QString longestName;
92     foreach (name,skillNames) {
93         if (name.length() > len) {
94             longestName = name;
95             len = name.length();
96         }
97     }
98
99     qDebug() << skillNames;
100     qDebug() << longestName;
101 }
102
103 void SkillTree::save()
104 {
105     QFile cache("/var/tmp/skillCache");
106     cache.open(QIODevice::WriteOnly);
107     QDataStream output(&cache);
108     output << skillNames;
109     cache.flush();
110     cache.close();
111 }
112