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