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>
|
|
20 |
#include <QListWidget>
|
638
|
21 |
#include <QDebug>
|
645
|
22 |
#include <QDomDocument>
|
|
23 |
#include <QDomElement>
|
|
24 |
#include <QDomNode>
|
|
25 |
#include <QDomText>
|
625
|
26 |
|
|
27 |
#include "netwwwwidget.h"
|
|
28 |
#include "hwconsts.h"
|
|
29 |
|
|
30 |
HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
|
634
|
31 |
HWNetServersWidget(parent)
|
625
|
32 |
{
|
|
33 |
http = new QHttp(this);
|
|
34 |
http->setHost("www.hedgewars.org", 80);
|
|
35 |
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
|
|
36 |
}
|
635
|
37 |
|
625
|
38 |
void HWNetWwwWidget::updateList()
|
|
39 |
{
|
|
40 |
QString request = QString("protocol_version=%1")
|
|
41 |
.arg(*cProtoVer);
|
|
42 |
http->post("/games/list_games", request.toUtf8());
|
|
43 |
|
|
44 |
serversList->clear();
|
|
45 |
}
|
|
46 |
|
|
47 |
void HWNetWwwWidget::onClientRead(int id, bool error)
|
|
48 |
{
|
|
49 |
if (error)
|
|
50 |
{
|
645
|
51 |
qWarning() << "Error" << http->errorString();
|
625
|
52 |
return;
|
|
53 |
}
|
638
|
54 |
serversList->clear();
|
645
|
55 |
|
|
56 |
QDomDocument doc;
|
|
57 |
if (!doc.setContent(http->readAll())) return;
|
|
58 |
|
|
59 |
QDomElement docElem = doc.documentElement();
|
|
60 |
|
|
61 |
QDomNode n = docElem.firstChild();
|
|
62 |
while (!n.isNull())
|
|
63 |
{
|
662
|
64 |
QDomElement game = n.toElement(); // try to convert the node to an element.
|
|
65 |
|
|
66 |
if(!game.isNull())
|
|
67 |
{
|
|
68 |
QDomNode p = game.firstChild();
|
|
69 |
while (!p.isNull())
|
|
70 |
{
|
|
71 |
QDomElement e = p.toElement();
|
|
72 |
if(!p.isNull())
|
|
73 |
{
|
|
74 |
QDomText t = e.firstChild().toText();
|
|
75 |
if(!t.isNull())
|
|
76 |
serversList->addItem(t.data());
|
|
77 |
}
|
|
78 |
p = p.nextSibling();
|
|
79 |
}
|
|
80 |
}
|
|
81 |
n = n.nextSibling();
|
|
82 |
}
|
|
83 |
}
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
|
|
88 |
{
|
|
89 |
http = new QHttp(this);
|
|
90 |
http->setHost("www.hedgewars.org", 80);
|
|
91 |
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
|
|
92 |
}
|
|
93 |
|
|
94 |
QVariant HWNetWwwModel::data(const QModelIndex &index,
|
|
95 |
int role) const
|
|
96 |
{
|
|
97 |
if (!index.isValid() || index.row() < 0
|
|
98 |
|| index.row() >= games.size() - 1
|
|
99 |
|| role != Qt::DisplayRole)
|
|
100 |
return QVariant();
|
|
101 |
|
|
102 |
return QVariant(QString("test"));
|
|
103 |
}
|
|
104 |
|
|
105 |
QVariant HWNetWwwModel::headerData(int section,
|
|
106 |
Qt::Orientation orientation, int role) const
|
|
107 |
{
|
|
108 |
if (role != Qt::DisplayRole)
|
|
109 |
return QVariant();
|
|
110 |
|
|
111 |
if (orientation == Qt::Horizontal) {
|
|
112 |
switch (section) {
|
|
113 |
case 0: return tr("Title");
|
|
114 |
case 1: return tr("IP");
|
|
115 |
case 2: return tr("Port");
|
|
116 |
default: return QVariant();
|
|
117 |
}
|
|
118 |
} else
|
|
119 |
return QString("%1").arg(section + 1);
|
|
120 |
}
|
|
121 |
|
|
122 |
int HWNetWwwModel::rowCount(const QModelIndex &parent) const
|
|
123 |
{
|
|
124 |
if (parent.isValid())
|
|
125 |
return 0;
|
|
126 |
else
|
|
127 |
return games.size();
|
|
128 |
}
|
|
129 |
|
|
130 |
int HWNetWwwModel::columnCount(const QModelIndex & parent) const
|
|
131 |
{
|
|
132 |
if (parent.isValid())
|
|
133 |
return 0;
|
|
134 |
else
|
|
135 |
return 3;
|
|
136 |
}
|
|
137 |
|
|
138 |
void HWNetWwwModel::updateList()
|
|
139 |
{
|
|
140 |
QString request = QString("protocol_version=%1")
|
|
141 |
.arg(*cProtoVer);
|
|
142 |
http->post("/games/list_games", request.toUtf8());
|
|
143 |
|
|
144 |
games.clear();
|
|
145 |
}
|
|
146 |
|
|
147 |
void HWNetWwwModel::onClientRead(int id, bool error)
|
|
148 |
{
|
|
149 |
if (error)
|
|
150 |
{
|
|
151 |
qWarning() << "Error" << http->errorString();
|
|
152 |
return;
|
|
153 |
}
|
|
154 |
games.clear();
|
|
155 |
|
|
156 |
QDomDocument doc;
|
|
157 |
if (!doc.setContent(http->readAll())) return;
|
|
158 |
|
|
159 |
QDomElement docElem = doc.documentElement();
|
|
160 |
|
|
161 |
QDomNode n = docElem.firstChild();
|
|
162 |
while (!n.isNull())
|
|
163 |
{
|
645
|
164 |
QDomElement e = n.toElement(); // try to convert the node to an element.
|
|
165 |
if(!e.isNull() && (e.tagName() == "ip"))
|
|
166 |
{
|
|
167 |
QDomText t = e.firstChild().toText();
|
|
168 |
if(!t.isNull())
|
662
|
169 |
games.append(QStringList() << t.data());
|
645
|
170 |
}
|
|
171 |
n = n.nextSibling();
|
|
172 |
}
|
625
|
173 |
}
|