QTfrontend/netwwwwidget.cpp
author unc0rr
Wed, 12 Dec 2007 19:49:13 +0000
changeset 645 c71fe8b942ee
parent 638 e1cc516488f2
child 662 b2f914786d87
permissions -rw-r--r--
Extract IP from XML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     1
/*
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a worms-like game
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com>
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     4
 *
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     8
 *
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    13
 *
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    17
 */
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    18
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    19
#include <QHttp>
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    20
#include <QListWidget>
638
e1cc516488f2 Fix some bugs
unc0rr
parents: 635
diff changeset
    21
#include <QDebug>
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    22
#include <QDomDocument>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    23
#include <QDomElement>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    24
#include <QDomNode>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    25
#include <QDomText>
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    26
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    27
#include "netwwwwidget.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    28
#include "hwconsts.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    29
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    30
HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
634
b9db5ff5cfdb Fix some bugs
unc0rr
parents: 632
diff changeset
    31
  HWNetServersWidget(parent)
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    32
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    33
	http = new QHttp(this);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    34
	http->setHost("www.hedgewars.org", 80);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    35
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    36
}
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 634
diff changeset
    37
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    38
void HWNetWwwWidget::updateList()
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    39
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    40
	QString request = QString("protocol_version=%1")
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    41
			.arg(*cProtoVer);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    42
	http->post("/games/list_games", request.toUtf8());
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    43
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    44
	serversList->clear();
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    45
}
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    46
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    47
void HWNetWwwWidget::onClientRead(int id, bool error)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    48
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    49
	if (error)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    50
	{
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    51
		qWarning() << "Error" << http->errorString();
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    52
		return;
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    53
	}
638
e1cc516488f2 Fix some bugs
unc0rr
parents: 635
diff changeset
    54
	serversList->clear();
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    55
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    56
	QDomDocument doc;
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    57
	if (!doc.setContent(http->readAll())) return;
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    58
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    59
	QDomElement docElem = doc.documentElement();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    60
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    61
	QDomNode n = docElem.firstChild();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    62
	while (!n.isNull())
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    63
	{
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    64
		QDomElement e = n.toElement(); // try to convert the node to an element.
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    65
		if(!e.isNull() && (e.tagName() == "ip"))
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    66
		{
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    67
			QDomText t = e.firstChild().toText();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    68
			if(!t.isNull())
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    69
				serversList->addItem(t.data());
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    70
		}
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    71
		n = n.nextSibling();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    72
	}
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    73
}