QTfrontend/netwwwwidget.cpp
author unc0rr
Sat, 01 Dec 2007 12:38:15 +0000
changeset 625 373353eaa742
child 632 5e09ae25729f
permissions -rw-r--r--
Sample WWW server list class
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>
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    21
#include <QMessageBox>
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    22
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    23
#include "netwwwwidget.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    24
#include "hwconsts.h"
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    25
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    26
HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    27
  QWidget(parent), mainLayout(this)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    28
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    29
	serversList = new QListWidget(this);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    30
	mainLayout.setMargin(0);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    31
	mainLayout.addWidget(serversList);
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
	updateList();
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    37
}
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    38
// http://hedgewars.org/games/create
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    39
// http://www.hedgewars.org/games/update_game?id=1&key=pufidzuk
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    40
// http://www.hedgewars.org/games/destroy_game?id=5&key=wrdeough
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    41
void HWNetWwwWidget::updateList()
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    42
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    43
	http->abort();
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    44
// example for adding game to server list
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    45
/*	QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4")
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    46
			.arg("hedgewarsserver")
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    47
			.arg(46631)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    48
			.arg(false ? "true" : "false")
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    49
			.arg(*cProtoVer);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    50
	http->post("/games/create", request.toUtf8());
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    51
*/
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    52
// query game list
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    53
	QString request = QString("protocol_version=%1")
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    54
			.arg(*cProtoVer);
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    55
	http->post("/games/list_games", request.toUtf8());
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    56
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    57
	serversList->clear();
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    58
}
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    59
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    60
void HWNetWwwWidget::onClientRead(int id, bool error)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    61
{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    62
	if (error)
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    63
	{
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    64
		QMessageBox::critical(this, tr("Error"), http->errorString());
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    65
		return;
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    66
	}
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    67
	serversList->addItem(http->readAll());
373353eaa742 Sample WWW server list class
unc0rr
parents:
diff changeset
    68
}