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