QTfrontend/netwwwwidget.cpp
changeset 662 b2f914786d87
parent 645 c71fe8b942ee
child 664 f0af2401f981
equal deleted inserted replaced
661:0523b15353a2 662:b2f914786d87
    59 	QDomElement docElem = doc.documentElement();
    59 	QDomElement docElem = doc.documentElement();
    60 
    60 
    61 	QDomNode n = docElem.firstChild();
    61 	QDomNode n = docElem.firstChild();
    62 	while (!n.isNull())
    62 	while (!n.isNull())
    63 	{
    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 
       
    86 
       
    87 HWNetWwwModel::HWNetWwwModel(QObject *parent) : QAbstractTableModel(parent)
       
    88 {
       
    89 	http = new QHttp(this);
       
    90 	http->setHost("www.hedgewars.org", 80);
       
    91 	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
       
    92 }
       
    93 
       
    94 QVariant HWNetWwwModel::data(const QModelIndex &index,
       
    95                              int role) const
       
    96 {
       
    97     if (!index.isValid() || index.row() < 0
       
    98             || index.row() >= games.size() - 1
       
    99             || role != Qt::DisplayRole)
       
   100         return QVariant();
       
   101 
       
   102     return QVariant(QString("test"));
       
   103 }
       
   104 
       
   105 QVariant HWNetWwwModel::headerData(int section,
       
   106             Qt::Orientation orientation, int role) const
       
   107 {
       
   108     if (role != Qt::DisplayRole)
       
   109         return QVariant();
       
   110 
       
   111     if (orientation == Qt::Horizontal) {
       
   112         switch (section) {
       
   113         case 0: return tr("Title");
       
   114         case 1: return tr("IP");
       
   115         case 2: return tr("Port");
       
   116         default: return QVariant();
       
   117         }
       
   118     } else
       
   119         return QString("%1").arg(section + 1);
       
   120 }
       
   121 
       
   122 int HWNetWwwModel::rowCount(const QModelIndex &parent) const
       
   123 {
       
   124 	if (parent.isValid())
       
   125 		return 0;
       
   126 	else
       
   127 		return games.size();
       
   128 }
       
   129 
       
   130 int HWNetWwwModel::columnCount(const QModelIndex & parent) const
       
   131 {
       
   132 	if (parent.isValid())
       
   133 		return 0;
       
   134 	else
       
   135 		return 3;
       
   136 }
       
   137 
       
   138 void HWNetWwwModel::updateList()
       
   139 {
       
   140 	QString request = QString("protocol_version=%1")
       
   141 			.arg(*cProtoVer);
       
   142 	http->post("/games/list_games", request.toUtf8());
       
   143 
       
   144 	games.clear();
       
   145 }
       
   146 
       
   147 void HWNetWwwModel::onClientRead(int id, bool error)
       
   148 {
       
   149 	if (error)
       
   150 	{
       
   151 		qWarning() << "Error" << http->errorString();
       
   152 		return;
       
   153 	}
       
   154 	games.clear();
       
   155 
       
   156 	QDomDocument doc;
       
   157 	if (!doc.setContent(http->readAll())) return;
       
   158 
       
   159 	QDomElement docElem = doc.documentElement();
       
   160 
       
   161 	QDomNode n = docElem.firstChild();
       
   162 	while (!n.isNull())
       
   163 	{
    64 		QDomElement e = n.toElement(); // try to convert the node to an element.
   164 		QDomElement e = n.toElement(); // try to convert the node to an element.
    65 		if(!e.isNull() && (e.tagName() == "ip"))
   165 		if(!e.isNull() && (e.tagName() == "ip"))
    66 		{
   166 		{
    67 			QDomText t = e.firstChild().toText();
   167 			QDomText t = e.firstChild().toText();
    68 			if(!t.isNull())
   168 			if(!t.isNull())
    69 				serversList->addItem(t.data());
   169 				games.append(QStringList() << t.data());
    70 		}
   170 		}
    71 		n = n.nextSibling();
   171 		n = n.nextSibling();
    72 	}
   172 	}
    73 }
   173 }