414999fa091395136250d1f5cdc5727403a7415c
[evehomescreen] / src / eveaccount.cpp
1 #include "eveaccount.h"
2 #include "QXmlStreamReader"
3 #include <QNetworkAccessManager>
4 #include <QNetworkRequest>
5 #include <QNetworkReply>
6 #include <QtDebug>
7 EveCharacter::EveCharacter(QObject *parent) :
8     QObject(parent),
9     characterId(0),
10     corpId(0),
11     characterIcon(NULL)
12 {
13 }
14
15 EveCharacter::EveCharacter(const EveCharacter &aOther):
16         QObject(aOther.parent()),
17         name(aOther.name),
18         corpName(aOther.corpName),
19         characterId(aOther.characterId),
20         corpId(aOther.corpId),
21         characterIcon(NULL)
22 {
23     if (aOther.characterIcon) {
24         characterIcon = new QPixmap(*(aOther.characterIcon));
25     }
26 }
27
28 EveCharacter::~EveCharacter()
29 {
30     if (characterIcon)
31         delete characterIcon;
32     characterIcon = NULL;
33 }
34
35 EveCharacter &EveCharacter::operator =(const EveCharacter &other)
36 {
37     name = other.name;
38     characterId = other.characterId;
39     corpId = other.corpId;
40     corpName = other.corpName;
41     qDebug() << "Assignment, characterIcon " << characterIcon;
42     if (characterIcon != NULL) {
43         delete characterIcon;
44         characterIcon = NULL;
45     }
46     qDebug() << " after deletion";
47     qDebug() << " other icon" << other.characterIcon;
48     if (other.characterIcon != NULL)
49         characterIcon = new QPixmap(*(other.characterIcon));
50     qDebug() << "Assignment done";
51     return *this;
52 }
53
54 bool EveCharacter::fromXml(QXmlStreamReader &xml)
55 {
56     if (xml.name() != "row")
57         return false;
58     name = xml.attributes().value("","name").toString();
59     characterId = xml.attributes().value("","characterID").toString().toInt();
60     corpName = xml.attributes().value("","corporationName").toString();
61     corpId = xml.attributes().value("","corporationID").toString().toInt();
62     return true;
63 }
64
65 bool EveCharacter::fetchImage()
66 {
67     if (characterId == 0) {
68         qDebug() << "No character";
69         return false;
70     }
71     if (characterIcon != NULL) {
72         return true;
73
74     }
75     qDebug() << "Requesting image";
76     QNetworkRequest req(QUrl(QString("http://img.eve.is/serv.asp?c=%1&s=64").arg(characterId)));
77     m_reply = mgr.get(req);
78     connect(m_reply,SIGNAL(finished()),this,SLOT(imageReady()));
79     return true;
80 }
81
82 // Construct the pixmap from the reply data
83 // and delete the reply
84 void EveCharacter::imageReady()
85 {
86     qDebug() << "Image ready";
87     characterIcon = new QPixmap;
88     if (!characterIcon->loadFromData(m_reply->readAll())) {
89         delete characterIcon;
90         characterIcon = NULL;
91         qDebug() << "Cannot load image";
92         return;
93     }
94     m_reply->deleteLater();
95     emit imageLoaded();
96 }