QTfrontend/netwwwwidget.cpp
author unc0rr
Sat, 15 Dec 2007 16:44:13 +0000
changeset 662 b2f914786d87
parent 645 c71fe8b942ee
child 664 f0af2401f981
permissions -rw-r--r--
- Parse new XML from www (now valid) - Start attemp to implement model for www servers list
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
	{
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    64
		QDomElement game = n.toElement(); // try to convert the node to an element.
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
		if(!game.isNull())
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
			QDomNode p = game.firstChild();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    69
			while (!p.isNull())
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 e = p.toElement();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    72
				if(!p.isNull())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    73
				{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    74
					QDomText t = e.firstChild().toText();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    75
					if(!t.isNull())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    76
						serversList->addItem(t.data());
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
				p = p.nextSibling();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    79
			}
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
		n = n.nextSibling();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    82
	}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    83
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    84
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
HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
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
	http = new QHttp(this);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    90
	http->setHost("www.hedgewars.org", 80);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    91
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    92
}
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
QVariant HWNetWwwModel::data(const QModelIndex &index,
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    95
                             int role) const
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
    if (!index.isValid() || index.row() < 0
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    98
            || index.row() >= games.size() - 1
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
    99
            || role != Qt::DisplayRole)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   100
        return QVariant();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   101
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   102
    return QVariant(QString("test"));
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   103
}
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
QVariant HWNetWwwModel::headerData(int section,
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   106
            Qt::Orientation orientation, int role) const
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 (role != Qt::DisplayRole)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   109
        return QVariant();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   110
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   111
    if (orientation == Qt::Horizontal) {
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   112
        switch (section) {
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   113
        case 0: return tr("Title");
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   114
        case 1: return tr("IP");
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   115
        case 2: return tr("Port");
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   116
        default: return QVariant();
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
    } else
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   119
        return QString("%1").arg(section + 1);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   120
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   121
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   122
int HWNetWwwModel::rowCount(const QModelIndex &parent) const
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   123
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   124
	if (parent.isValid())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   125
		return 0;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   126
	else
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   127
		return games.size();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   128
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   129
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   130
int HWNetWwwModel::columnCount(const QModelIndex & parent) const
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   131
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   132
	if (parent.isValid())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   133
		return 0;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   134
	else
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   135
		return 3;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   136
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   137
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   138
void HWNetWwwModel::updateList()
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   139
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   140
	QString request = QString("protocol_version=%1")
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   141
			.arg(*cProtoVer);
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   142
	http->post("/games/list_games", request.toUtf8());
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   143
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   144
	games.clear();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   145
}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   146
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   147
void HWNetWwwModel::onClientRead(int id, bool error)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   148
{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   149
	if (error)
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   150
	{
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   151
		qWarning() << "Error" << http->errorString();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   152
		return;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   153
	}
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   154
	games.clear();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   155
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   156
	QDomDocument doc;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   157
	if (!doc.setContent(http->readAll())) return;
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   158
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   159
	QDomElement docElem = doc.documentElement();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   160
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   161
	QDomNode n = docElem.firstChild();
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   162
	while (!n.isNull())
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   163
	{
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   164
		QDomElement e = n.toElement(); // try to convert the node to an element.
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   165
		if(!e.isNull() && (e.tagName() == "ip"))
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   166
		{
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   167
			QDomText t = e.firstChild().toText();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   168
			if(!t.isNull())
662
b2f914786d87 - Parse new XML from www (now valid)
unc0rr
parents: 645
diff changeset
   169
				games.append(QStringList() << t.data());
645
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   170
		}
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   171
		n = n.nextSibling();
c71fe8b942ee Extract IP from XML
unc0rr
parents: 638
diff changeset
   172
	}
625
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
   173
}