QTfrontend/netwwwwidget.cpp
changeset 664 f0af2401f981
parent 662 b2f914786d87
child 665 5c7bfc8bac6a
equal deleted inserted replaced
663:d1982b62d52a 664:f0af2401f981
    28 #include "hwconsts.h"
    28 #include "hwconsts.h"
    29 
    29 
    30 HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
    30 HWNetWwwWidget::HWNetWwwWidget(QWidget* parent) :
    31   HWNetServersWidget(parent)
    31   HWNetServersWidget(parent)
    32 {
    32 {
    33 	http = new QHttp(this);
    33 	serversList->setModel(new HWNetWwwModel);
    34 	http->setHost("www.hedgewars.org", 80);
       
    35 	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
       
    36 }
    34 }
    37 
    35 
    38 void HWNetWwwWidget::updateList()
    36 void HWNetWwwWidget::updateList()
    39 {
    37 {
    40 	QString request = QString("protocol_version=%1")
    38 	static_cast<HWNetWwwModel *>(serversList->model())->updateList();
    41 			.arg(*cProtoVer);
       
    42 	http->post("/games/list_games", request.toUtf8());
       
    43 
       
    44 	serversList->clear();
       
    45 }
    39 }
    46 
       
    47 void HWNetWwwWidget::onClientRead(int id, bool error)
       
    48 {
       
    49 	if (error)
       
    50 	{
       
    51 		qWarning() << "Error" << http->errorString();
       
    52 		return;
       
    53 	}
       
    54 	serversList->clear();
       
    55 
       
    56 	QDomDocument doc;
       
    57 	if (!doc.setContent(http->readAll())) return;
       
    58 
       
    59 	QDomElement docElem = doc.documentElement();
       
    60 
       
    61 	QDomNode n = docElem.firstChild();
       
    62 	while (!n.isNull())
       
    63 	{
       
    64 		QDomElement game = n.toElement(); // try to convert the node to an element.
       
    65 
       
    66 		if(!game.isNull())
       
    67 		{
       
    68 			QDomNode p = game.firstChild();
       
    69 			while (!p.isNull())
       
    70 			{
       
    71 				QDomElement e = p.toElement();
       
    72 				if(!p.isNull())
       
    73 				{
       
    74 					QDomText t = e.firstChild().toText();
       
    75 					if(!t.isNull())
       
    76 						serversList->addItem(t.data());
       
    77 				}
       
    78 				p = p.nextSibling();
       
    79 			}
       
    80 		}
       
    81 		n = n.nextSibling();
       
    82 	}
       
    83 }
       
    84 
       
    85 
    40 
    86 
    41 
    87 HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
    42 HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
    88 {
    43 {
    89 	http = new QHttp(this);
    44 	http = new QHttp(this);
    92 }
    47 }
    93 
    48 
    94 QVariant HWNetWwwModel::data(const QModelIndex &index,
    49 QVariant HWNetWwwModel::data(const QModelIndex &index,
    95                              int role) const
    50                              int role) const
    96 {
    51 {
    97     if (!index.isValid() || index.row() < 0
    52 	if (!index.isValid() || index.row() < 0
    98             || index.row() >= games.size() - 1
    53 		|| index.row() >= games.size()
    99             || role != Qt::DisplayRole)
    54 		|| role != Qt::DisplayRole)
   100         return QVariant();
    55 	return QVariant();
   101 
    56 
   102     return QVariant(QString("test"));
    57 	return games[index.row()][index.column()];
   103 }
    58 }
   104 
    59 
   105 QVariant HWNetWwwModel::headerData(int section,
    60 QVariant HWNetWwwModel::headerData(int section,
   106             Qt::Orientation orientation, int role) const
    61             Qt::Orientation orientation, int role) const
   107 {
    62 {
   108     if (role != Qt::DisplayRole)
    63 	if (role != Qt::DisplayRole)
   109         return QVariant();
    64 		return QVariant();
   110 
    65 
   111     if (orientation == Qt::Horizontal) {
    66 	if (orientation == Qt::Horizontal)
   112         switch (section) {
    67 	{
   113         case 0: return tr("Title");
    68 		switch (section)
   114         case 1: return tr("IP");
    69 		{
   115         case 2: return tr("Port");
    70 			case 0: return tr("Title");
   116         default: return QVariant();
    71 			case 1: return tr("IP");
   117         }
    72 			case 2: return tr("Port");
   118     } else
    73 			default: return QVariant();
   119         return QString("%1").arg(section + 1);
    74 		}
       
    75 	} else
       
    76 		return QString("%1").arg(section + 1);
   120 }
    77 }
   121 
    78 
   122 int HWNetWwwModel::rowCount(const QModelIndex &parent) const
    79 int HWNetWwwModel::rowCount(const QModelIndex &parent) const
   123 {
    80 {
   124 	if (parent.isValid())
    81 	if (parent.isValid())
   140 	QString request = QString("protocol_version=%1")
    97 	QString request = QString("protocol_version=%1")
   141 			.arg(*cProtoVer);
    98 			.arg(*cProtoVer);
   142 	http->post("/games/list_games", request.toUtf8());
    99 	http->post("/games/list_games", request.toUtf8());
   143 
   100 
   144 	games.clear();
   101 	games.clear();
       
   102 
       
   103 	reset();
   145 }
   104 }
   146 
   105 
   147 void HWNetWwwModel::onClientRead(int id, bool error)
   106 void HWNetWwwModel::onClientRead(int id, bool error)
   148 {
   107 {
   149 	if (error)
   108 	if (error)
   159 	QDomElement docElem = doc.documentElement();
   118 	QDomElement docElem = doc.documentElement();
   160 
   119 
   161 	QDomNode n = docElem.firstChild();
   120 	QDomNode n = docElem.firstChild();
   162 	while (!n.isNull())
   121 	while (!n.isNull())
   163 	{
   122 	{
   164 		QDomElement e = n.toElement(); // try to convert the node to an element.
   123 		QDomElement game = n.toElement(); // try to convert the node to an element.
   165 		if(!e.isNull() && (e.tagName() == "ip"))
   124 
       
   125 		if(!game.isNull())
   166 		{
   126 		{
   167 			QDomText t = e.firstChild().toText();
   127 			QDomNode p = game.firstChild();
   168 			if(!t.isNull())
   128 			QStringList sl;
   169 				games.append(QStringList() << t.data());
   129 			sl << "-" << "-" << "-";
       
   130 			while (!p.isNull())
       
   131 			{
       
   132 				QDomElement e = p.toElement();
       
   133 
       
   134 				if(!p.isNull())
       
   135 				{
       
   136 					int i = -1;
       
   137 					if (e.tagName() == "title") i = 0;
       
   138 					else if (e.tagName() == "ip") i = 1;
       
   139 					else if (e.tagName() == "port") i = 2;
       
   140 
       
   141 					QDomText t = e.firstChild().toText();
       
   142 					if(!t.isNull() && (i >= 0))
       
   143 						sl[i] = t.data();
       
   144 				}
       
   145 				p = p.nextSibling();
       
   146 			}
       
   147 			games.append(sl);
   170 		}
   148 		}
   171 		n = n.nextSibling();
   149 		n = n.nextSibling();
   172 	}
   150 	}
       
   151 
       
   152 	reset();
   173 }
   153 }