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