- Parse new XML from www (now valid)
authorunc0rr
Sat, 15 Dec 2007 16:44:13 +0000
changeset 662 b2f914786d87
parent 661 0523b15353a2
child 663 d1982b62d52a
- Parse new XML from www (now valid) - Start attemp to implement model for www servers list
QTfrontend/hwform.h
QTfrontend/netwwwwidget.cpp
QTfrontend/netwwwwidget.h
--- a/QTfrontend/hwform.h	Fri Dec 14 16:18:42 2007 +0000
+++ b/QTfrontend/hwform.h	Sat Dec 15 16:44:13 2007 +0000
@@ -79,8 +79,6 @@
 	void ForcedDisconnect();
 	void ShowErrorMessage(const QString &);
 	void GetRecord(bool isDemo, const QByteArray & record);
-
-public slots:
 	void CreateNetGame();
 
 private:
--- a/QTfrontend/netwwwwidget.cpp	Fri Dec 14 16:18:42 2007 +0000
+++ b/QTfrontend/netwwwwidget.cpp	Sat Dec 15 16:44:13 2007 +0000
@@ -61,12 +61,112 @@
 	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)
+{
+	http = new QHttp(this);
+	http->setHost("www.hedgewars.org", 80);
+	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
+}
+
+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();
+
+    return QVariant(QString("test"));
+}
+
+QVariant HWNetWwwModel::headerData(int section,
+            Qt::Orientation orientation, int role) const
+{
+    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);
+}
+
+int HWNetWwwModel::rowCount(const QModelIndex &parent) const
+{
+	if (parent.isValid())
+		return 0;
+	else
+		return games.size();
+}
+
+int HWNetWwwModel::columnCount(const QModelIndex & parent) const
+{
+	if (parent.isValid())
+		return 0;
+	else
+		return 3;
+}
+
+void HWNetWwwModel::updateList()
+{
+	QString request = QString("protocol_version=%1")
+			.arg(*cProtoVer);
+	http->post("/games/list_games", request.toUtf8());
+
+	games.clear();
+}
+
+void HWNetWwwModel::onClientRead(int id, bool error)
+{
+	if (error)
+	{
+		qWarning() << "Error" << http->errorString();
+		return;
+	}
+	games.clear();
+
+	QDomDocument doc;
+	if (!doc.setContent(http->readAll())) return;
+
+	QDomElement docElem = doc.documentElement();
+
+	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"))
 		{
 			QDomText t = e.firstChild().toText();
 			if(!t.isNull())
-				serversList->addItem(t.data());
+				games.append(QStringList() << t.data());
 		}
 		n = n.nextSibling();
 	}
--- a/QTfrontend/netwwwwidget.h	Fri Dec 14 16:18:42 2007 +0000
+++ b/QTfrontend/netwwwwidget.h	Sat Dec 15 16:44:13 2007 +0000
@@ -19,6 +19,10 @@
 #ifndef _NET_WWWWIDGET_INCLUDED
 #define _NET_WWWWIDGET_INCLUDED
 
+#include <QAbstractTableModel>
+#include <QFile>
+#include <QDateTime>
+#include <QVector>
 #include "netserverslist.h"
 
 class QListWidget;
@@ -41,4 +45,27 @@
 	QHttp * http;
 };
 
+class HWNetWwwModel : public QAbstractTableModel
+{
+	Q_OBJECT
+
+public:
+	HWNetWwwModel(QObject *parent = 0);
+
+	QVariant data(const QModelIndex &index, int role) const;
+	QVariant headerData(int section, Qt::Orientation orientation, int role) const;
+	int rowCount(const QModelIndex & parent) const;
+	int columnCount(const QModelIndex & parent) const;
+
+private:
+	QHttp * http;
+	QList<QStringList> games;
+
+private slots:
+	void onClientRead(int id, bool error);
+
+public slots:
+	void updateList();
+};
+
 #endif // _NET_WWWWIDGET_INCLUDED