625
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include <QHttp>
|
645
|
20 |
#include <QDomDocument>
|
|
21 |
#include <QDomElement>
|
|
22 |
#include <QDomNode>
|
|
23 |
#include <QDomText>
|
665
|
24 |
#include <QDebug>
|
625
|
25 |
|
|
26 |
#include "netwwwwidget.h"
|
|
27 |
#include "hwconsts.h"
|
|
28 |
|
635
|
29 |
|
665
|
30 |
HWNetWwwModel::HWNetWwwModel(QObject *parent) : HWNetServersModel(parent)
|
662
|
31 |
{
|
|
32 |
http = new QHttp(this);
|
|
33 |
http->setHost("www.hedgewars.org", 80);
|
|
34 |
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
|
|
35 |
}
|
|
36 |
|
|
37 |
QVariant HWNetWwwModel::data(const QModelIndex &index,
|
|
38 |
int role) const
|
|
39 |
{
|
664
|
40 |
if (!index.isValid() || index.row() < 0
|
|
41 |
|| index.row() >= games.size()
|
|
42 |
|| role != Qt::DisplayRole)
|
|
43 |
return QVariant();
|
662
|
44 |
|
664
|
45 |
return games[index.row()][index.column()];
|
662
|
46 |
}
|
|
47 |
|
|
48 |
void HWNetWwwModel::updateList()
|
|
49 |
{
|
|
50 |
QString request = QString("protocol_version=%1")
|
|
51 |
.arg(*cProtoVer);
|
|
52 |
http->post("/games/list_games", request.toUtf8());
|
|
53 |
|
|
54 |
games.clear();
|
664
|
55 |
|
|
56 |
reset();
|
662
|
57 |
}
|
|
58 |
|
|
59 |
void HWNetWwwModel::onClientRead(int id, bool error)
|
|
60 |
{
|
|
61 |
if (error)
|
|
62 |
{
|
|
63 |
qWarning() << "Error" << http->errorString();
|
|
64 |
return;
|
|
65 |
}
|
|
66 |
games.clear();
|
|
67 |
|
|
68 |
QDomDocument doc;
|
|
69 |
if (!doc.setContent(http->readAll())) return;
|
|
70 |
|
|
71 |
QDomElement docElem = doc.documentElement();
|
|
72 |
|
|
73 |
QDomNode n = docElem.firstChild();
|
|
74 |
while (!n.isNull())
|
|
75 |
{
|
664
|
76 |
QDomElement game = n.toElement(); // try to convert the node to an element.
|
|
77 |
|
|
78 |
if(!game.isNull())
|
645
|
79 |
{
|
664
|
80 |
QDomNode p = game.firstChild();
|
|
81 |
QStringList sl;
|
|
82 |
sl << "-" << "-" << "-";
|
|
83 |
while (!p.isNull())
|
|
84 |
{
|
|
85 |
QDomElement e = p.toElement();
|
|
86 |
|
|
87 |
if(!p.isNull())
|
|
88 |
{
|
|
89 |
int i = -1;
|
|
90 |
if (e.tagName() == "title") i = 0;
|
|
91 |
else if (e.tagName() == "ip") i = 1;
|
|
92 |
else if (e.tagName() == "port") i = 2;
|
|
93 |
|
|
94 |
QDomText t = e.firstChild().toText();
|
|
95 |
if(!t.isNull() && (i >= 0))
|
|
96 |
sl[i] = t.data();
|
|
97 |
}
|
|
98 |
p = p.nextSibling();
|
|
99 |
}
|
|
100 |
games.append(sl);
|
645
|
101 |
}
|
|
102 |
n = n.nextSibling();
|
|
103 |
}
|
664
|
104 |
|
|
105 |
reset();
|
625
|
106 |
}
|