Fully implement www server list via model
authorunc0rr
Sat, 15 Dec 2007 18:58:55 +0000
changeset 664 f0af2401f981
parent 663 d1982b62d52a
child 665 5c7bfc8bac6a
Fully implement www server list via model
QTfrontend/hwform.cpp
QTfrontend/netserverslist.cpp
QTfrontend/netserverslist.h
QTfrontend/netudpwidget.cpp
QTfrontend/netwwwwidget.cpp
QTfrontend/netwwwwidget.h
--- a/QTfrontend/hwform.cpp	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/hwform.cpp	Sat Dec 15 18:58:55 2007 +0000
@@ -344,7 +344,7 @@
 
 void HWForm::NetConnectServer()
 {
-  QListWidgetItem * curritem = ui.pageNet->netServersWidget->serversList->currentItem();
+/*  QListWidgetItem * curritem = ui.pageNet->netServersWidget->serversList->currentItem();
   if (!curritem) {
     QMessageBox::critical(this,
 			  tr("Error"),
@@ -352,7 +352,7 @@
 			  tr("OK"));
     return ;
   }
-  _NetConnect(curritem->text(), 46631, ui.pageOptions->editNetNick->text());
+  _NetConnect(curritem->text(), 46631, ui.pageOptions->editNetNick->text());*/
 }
 
 void HWForm::_NetConnect(const QString & hostName, quint16 port, const QString & nick)
--- a/QTfrontend/netserverslist.cpp	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/netserverslist.cpp	Sat Dec 15 18:58:55 2007 +0000
@@ -24,12 +24,12 @@
 HWNetServersWidget::HWNetServersWidget(QWidget* parent) :
   QWidget(parent), mainLayout(this)
 {
-	serversList = new QListWidget(this);
+	serversList = new QTableView(this);
 	mainLayout.setMargin(0);
 	mainLayout.addWidget(serversList);
 }
 
 void HWNetServersWidget::updateList()
 {
-	serversList->clear();
+
 }
--- a/QTfrontend/netserverslist.h	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/netserverslist.h	Sat Dec 15 18:58:55 2007 +0000
@@ -21,7 +21,7 @@
 
 #include <QWidget>
 #include <QVBoxLayout>
-#include <QListWidget>
+#include <QTableView>
 
 class HWNetServersWidget : public QWidget
 {
@@ -30,7 +30,7 @@
 public:
 	HWNetServersWidget (QWidget *parent = 0);
 
-	QListWidget* serversList;
+	QTableView* serversList;
 
 public slots:
 	virtual void updateList();
--- a/QTfrontend/netudpwidget.cpp	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/netudpwidget.cpp	Sat Dec 15 18:58:55 2007 +0000
@@ -32,7 +32,7 @@
 
 void HWNetUdpWidget::updateList()
 {
-  serversList->clear();
+//  serversList->clear();
   pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631);
 }
 
@@ -45,7 +45,7 @@
     quint16 clientPort;
     pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort);
     if(QString("%1").arg(datagram.data())==QString("hedgewars server")) {
-      serversList->addItem(clientAddr.toString());
+//      serversList->addItem(clientAddr.toString());
     }
   }
 }
--- a/QTfrontend/netwwwwidget.cpp	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/netwwwwidget.cpp	Sat Dec 15 18:58:55 2007 +0000
@@ -30,59 +30,14 @@
 HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
   HWNetServersWidget(parent)
 {
-	http = new QHttp(this);
-	http->setHost("www.hedgewars.org", 80);
-	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
+	serversList->setModel(new HWNetWwwModel);
 }
 
 void HWNetWwwWidget::updateList()
 {
-	QString request = QString("protocol_version=%1")
-			.arg(*cProtoVer);
-	http->post("/games/list_games", request.toUtf8());
-
-	serversList->clear();
+	static_cast<HWNetWwwModel *>(serversList->model())->updateList();
 }
 
-void HWNetWwwWidget::onClientRead(int id, bool error)
-{
-	if (error)
-	{
-		qWarning() << "Error" << http->errorString();
-		return;
-	}
-	serversList->clear();
-
-	QDomDocument doc;
-	if (!doc.setContent(http->readAll())) return;
-
-	QDomElement docElem = doc.documentElement();
-
-	QDomNode n = docElem.firstChild();
-	while (!n.isNull())
-	{
-		QDomElement game = n.toElement(); // try to convert the node to an element.
-
-		if(!game.isNull())
-		{
-			QDomNode p = game.firstChild();
-			while (!p.isNull())
-			{
-				QDomElement e = p.toElement();
-				if(!p.isNull())
-				{
-					QDomText t = e.firstChild().toText();
-					if(!t.isNull())
-						serversList->addItem(t.data());
-				}
-				p = p.nextSibling();
-			}
-		}
-		n = n.nextSibling();
-	}
-}
-
-
 
 HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
 {
@@ -94,29 +49,31 @@
 QVariant HWNetWwwModel::data(const QModelIndex &index,
                              int role) const
 {
-    if (!index.isValid() || index.row() < 0
-            || index.row() >= games.size() - 1
-            || role != Qt::DisplayRole)
-        return QVariant();
+	if (!index.isValid() || index.row() < 0
+		|| index.row() >= games.size()
+		|| role != Qt::DisplayRole)
+	return QVariant();
 
-    return QVariant(QString("test"));
+	return games[index.row()][index.column()];
 }
 
 QVariant HWNetWwwModel::headerData(int section,
             Qt::Orientation orientation, int role) const
 {
-    if (role != Qt::DisplayRole)
-        return QVariant();
+	if (role != Qt::DisplayRole)
+		return QVariant();
 
-    if (orientation == Qt::Horizontal) {
-        switch (section) {
-        case 0: return tr("Title");
-        case 1: return tr("IP");
-        case 2: return tr("Port");
-        default: return QVariant();
-        }
-    } else
-        return QString("%1").arg(section + 1);
+	if (orientation == Qt::Horizontal)
+	{
+		switch (section)
+		{
+			case 0: return tr("Title");
+			case 1: return tr("IP");
+			case 2: return tr("Port");
+			default: return QVariant();
+		}
+	} else
+		return QString("%1").arg(section + 1);
 }
 
 int HWNetWwwModel::rowCount(const QModelIndex &parent) const
@@ -142,6 +99,8 @@
 	http->post("/games/list_games", request.toUtf8());
 
 	games.clear();
+
+	reset();
 }
 
 void HWNetWwwModel::onClientRead(int id, bool error)
@@ -161,13 +120,34 @@
 	QDomNode n = docElem.firstChild();
 	while (!n.isNull())
 	{
-		QDomElement e = n.toElement(); // try to convert the node to an element.
-		if(!e.isNull() && (e.tagName() == "ip"))
+		QDomElement game = n.toElement(); // try to convert the node to an element.
+
+		if(!game.isNull())
 		{
-			QDomText t = e.firstChild().toText();
-			if(!t.isNull())
-				games.append(QStringList() << t.data());
+			QDomNode p = game.firstChild();
+			QStringList sl;
+			sl << "-" << "-" << "-";
+			while (!p.isNull())
+			{
+				QDomElement e = p.toElement();
+
+				if(!p.isNull())
+				{
+					int i = -1;
+					if (e.tagName() == "title") i = 0;
+					else if (e.tagName() == "ip") i = 1;
+					else if (e.tagName() == "port") i = 2;
+
+					QDomText t = e.firstChild().toText();
+					if(!t.isNull() && (i >= 0))
+						sl[i] = t.data();
+				}
+				p = p.nextSibling();
+			}
+			games.append(sl);
 		}
 		n = n.nextSibling();
 	}
+
+	reset();
 }
--- a/QTfrontend/netwwwwidget.h	Sat Dec 15 18:04:56 2007 +0000
+++ b/QTfrontend/netwwwwidget.h	Sat Dec 15 18:58:55 2007 +0000
@@ -37,12 +37,6 @@
 
 public slots:
 	void updateList();
-
-private slots:
-	void onClientRead(int id, bool error);
-
-private:
-	QHttp * http;
 };
 
 class HWNetWwwModel : public QAbstractTableModel