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