QTfrontend/netwwwwidget.cpp
author displacer
Sat, 12 Jan 2008 22:41:03 +0000
changeset 703 424297e3165a
parent 668 0d7683a66d61
child 883 07a568ba44e0
permissions -rw-r--r--
weapon names transmitted over network
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>
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    20
#include <QDomDocument>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    21
#include <QDomElement>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    22
#include <QDomNode>
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    23
#include <QDomText>
665
5c7bfc8bac6a Start move from custom widgets to custom models
unc0rr
parents: 664
diff changeset
    24
#include <QDebug>
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    25
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    26
#include "netwwwwidget.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    27
#include "hwconsts.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    28
635
0fb6d55ecd66 Register in www works
unc0rr
parents: 634
diff changeset
    29
665
5c7bfc8bac6a Start move from custom widgets to custom models
unc0rr
parents: 664
diff changeset
    30
HWNetWwwModel::HWNetWwwModel(QObject *parent) : HWNetServersModel(parent)
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    31
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    32
	http = new QHttp(this);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    33
	http->setHost("www.hedgewars.org", 80);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    34
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    35
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    36
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    37
QVariant HWNetWwwModel::data(const QModelIndex &index,
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    38
                             int role) const
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    39
{
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    40
	if (!index.isValid() || index.row() < 0
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    41
		|| index.row() >= games.size()
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    42
		|| role != Qt::DisplayRole)
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    43
	return QVariant();
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    44
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    45
	return games[index.row()][index.column()];
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    46
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    47
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    48
void HWNetWwwModel::updateList()
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    49
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    50
	QString request = QString("protocol_version=%1")
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    51
			.arg(*cProtoVer);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    52
	http->post("/games/list_games", request.toUtf8());
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    53
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    54
	games.clear();
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    55
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    56
	reset();
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    57
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    58
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    59
void HWNetWwwModel::onClientRead(int id, bool error)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    60
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    61
	if (error)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    62
	{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    63
		qWarning() << "Error" << http->errorString();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    64
		return;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    65
	}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    66
	games.clear();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    67
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    68
	QDomDocument doc;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    69
	if (!doc.setContent(http->readAll())) return;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    70
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    71
	QDomElement docElem = doc.documentElement();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    72
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    73
	QDomNode n = docElem.firstChild();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    74
	while (!n.isNull())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    75
	{
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    76
		QDomElement game = n.toElement(); // try to convert the node to an element.
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    77
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    78
		if(!game.isNull())
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
    79
		{
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    80
			QDomNode p = game.firstChild();
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    81
			QStringList sl;
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    82
			sl << "-" << "-" << "-";
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    83
			while (!p.isNull())
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    84
			{
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    85
				QDomElement e = p.toElement();
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    86
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    87
				if(!p.isNull())
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    88
				{
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    89
					int i = -1;
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    90
					if (e.tagName() == "title") i = 0;
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    91
					else if (e.tagName() == "ip") i = 1;
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    92
					else if (e.tagName() == "port") i = 2;
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    93
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    94
					QDomText t = e.firstChild().toText();
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    95
					if(!t.isNull() && (i >= 0))
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    96
						sl[i] = t.data();
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    97
				}
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    98
				p = p.nextSibling();
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
    99
			}
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
   100
			games.append(sl);
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   101
		}
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   102
		n = n.nextSibling();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   103
	}
664
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
   104
f0af2401f981 Fully implement www server list via model
unc0rr
parents: 662
diff changeset
   105
	reset();
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
   106
}