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 |
QVariant HWNetWwwModel::headerData(int section,
|
|
49 |
Qt::Orientation orientation, int role) const
|
|
50 |
{
|
664
|
51 |
if (role != Qt::DisplayRole)
|
|
52 |
return QVariant();
|
662
|
53 |
|
664
|
54 |
if (orientation == Qt::Horizontal)
|
|
55 |
{
|
|
56 |
switch (section)
|
|
57 |
{
|
|
58 |
case 0: return tr("Title");
|
|
59 |
case 1: return tr("IP");
|
|
60 |
case 2: return tr("Port");
|
|
61 |
default: return QVariant();
|
|
62 |
}
|
|
63 |
} else
|
|
64 |
return QString("%1").arg(section + 1);
|
662
|
65 |
}
|
|
66 |
|
|
67 |
int HWNetWwwModel::rowCount(const QModelIndex &parent) const
|
|
68 |
{
|
|
69 |
if (parent.isValid())
|
|
70 |
return 0;
|
|
71 |
else
|
|
72 |
return games.size();
|
|
73 |
}
|
|
74 |
|
|
75 |
int HWNetWwwModel::columnCount(const QModelIndex & parent) const
|
|
76 |
{
|
|
77 |
if (parent.isValid())
|
|
78 |
return 0;
|
|
79 |
else
|
|
80 |
return 3;
|
|
81 |
}
|
|
82 |
|
|
83 |
void HWNetWwwModel::updateList()
|
|
84 |
{
|
|
85 |
QString request = QString("protocol_version=%1")
|
|
86 |
.arg(*cProtoVer);
|
|
87 |
http->post("/games/list_games", request.toUtf8());
|
|
88 |
|
|
89 |
games.clear();
|
664
|
90 |
|
|
91 |
reset();
|
662
|
92 |
}
|
|
93 |
|
|
94 |
void HWNetWwwModel::onClientRead(int id, bool error)
|
|
95 |
{
|
|
96 |
if (error)
|
|
97 |
{
|
|
98 |
qWarning() << "Error" << http->errorString();
|
|
99 |
return;
|
|
100 |
}
|
|
101 |
games.clear();
|
|
102 |
|
|
103 |
QDomDocument doc;
|
|
104 |
if (!doc.setContent(http->readAll())) return;
|
|
105 |
|
|
106 |
QDomElement docElem = doc.documentElement();
|
|
107 |
|
|
108 |
QDomNode n = docElem.firstChild();
|
|
109 |
while (!n.isNull())
|
|
110 |
{
|
664
|
111 |
QDomElement game = n.toElement(); // try to convert the node to an element.
|
|
112 |
|
|
113 |
if(!game.isNull())
|
645
|
114 |
{
|
664
|
115 |
QDomNode p = game.firstChild();
|
|
116 |
QStringList sl;
|
|
117 |
sl << "-" << "-" << "-";
|
|
118 |
while (!p.isNull())
|
|
119 |
{
|
|
120 |
QDomElement e = p.toElement();
|
|
121 |
|
|
122 |
if(!p.isNull())
|
|
123 |
{
|
|
124 |
int i = -1;
|
|
125 |
if (e.tagName() == "title") i = 0;
|
|
126 |
else if (e.tagName() == "ip") i = 1;
|
|
127 |
else if (e.tagName() == "port") i = 2;
|
|
128 |
|
|
129 |
QDomText t = e.firstChild().toText();
|
|
130 |
if(!t.isNull() && (i >= 0))
|
|
131 |
sl[i] = t.data();
|
|
132 |
}
|
|
133 |
p = p.nextSibling();
|
|
134 |
}
|
|
135 |
games.append(sl);
|
645
|
136 |
}
|
|
137 |
n = n.nextSibling();
|
|
138 |
}
|
664
|
139 |
|
|
140 |
reset();
|
625
|
141 |
}
|