1 /* |
1 /* |
2 * Hedgewars, a worms-like game |
2 * Hedgewars, a worms-like game |
3 * Copyright (c) 2007 Ulyanov Igor <iulyanov@gmail.com> |
3 * Copyright (c) 2007 Ulyanov Igor <iulyanov@gmail.com> |
|
4 * Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com> |
4 * |
5 * |
5 * This program is free software; you can redistribute it and/or modify |
6 * This program is free software; you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License as published by |
7 * it under the terms of the GNU General Public License as published by |
7 * the Free Software Foundation; version 2 of the License |
8 * the Free Software Foundation; version 2 of the License |
8 * |
9 * |
15 * along with this program; if not, write to the Free Software |
16 * along with this program; if not, write to the Free Software |
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
17 */ |
18 */ |
18 |
19 |
19 #include <QUdpSocket> |
20 #include <QUdpSocket> |
20 #include <QListWidget> |
|
21 |
21 |
22 #include "netudpwidget.h" |
22 #include "netudpwidget.h" |
23 |
23 |
24 /*HWNetUdpWidget::HWNetUdpWidget(QWidget* parent) : |
24 HWNetUdpModel::HWNetUdpModel(QObject* parent) : |
25 HWNetServersWidget(parent) |
25 HWNetServersModel(parent) |
26 { |
26 { |
27 pUdpSocket = new QUdpSocket(this); |
27 pUdpSocket = new QUdpSocket(this); |
28 |
28 |
29 pUdpSocket->bind(); |
29 pUdpSocket->bind(); |
30 connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
30 connect(pUdpSocket, SIGNAL(readyRead()), this, SLOT(onClientRead())); |
31 } |
31 } |
32 |
32 |
33 void HWNetUdpWidget::updateList() |
33 void HWNetUdpModel::updateList() |
34 { |
34 { |
35 // serversList->clear(); |
35 games.clear(); |
36 pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631); |
36 |
|
37 reset(); |
|
38 |
|
39 pUdpSocket->writeDatagram("hedgewars client", QHostAddress::Broadcast, 46631); |
37 } |
40 } |
38 |
41 |
39 void HWNetUdpWidget::onClientRead() |
42 void HWNetUdpModel::onClientRead() |
40 { |
43 { |
41 while (pUdpSocket->hasPendingDatagrams()) { |
44 while (pUdpSocket->hasPendingDatagrams()) { |
42 QByteArray datagram; |
45 QByteArray datagram; |
43 datagram.resize(pUdpSocket->pendingDatagramSize()); |
46 datagram.resize(pUdpSocket->pendingDatagramSize()); |
44 QHostAddress clientAddr; |
47 QHostAddress clientAddr; |
45 quint16 clientPort; |
48 quint16 clientPort; |
46 pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort); |
49 |
47 if(QString("%1").arg(datagram.data())==QString("hedgewars server")) { |
50 pUdpSocket->readDatagram(datagram.data(), datagram.size(), &clientAddr, &clientPort); |
48 // serversList->addItem(clientAddr.toString()); |
51 |
49 } |
52 if(QString("%1").arg(datagram.data())==QString("hedgewars server")) { |
50 } |
53 QStringList sl; |
|
54 sl << "-" << clientAddr.toString() << "46631"; |
|
55 games.append(sl); |
|
56 } |
|
57 } |
|
58 |
|
59 reset(); |
51 } |
60 } |
52 */ |
61 |
|
62 QVariant HWNetUdpModel::data(const QModelIndex &index, |
|
63 int role) const |
|
64 { |
|
65 if (!index.isValid() || index.row() < 0 |
|
66 || index.row() >= games.size() |
|
67 || role != Qt::DisplayRole) |
|
68 return QVariant(); |
|
69 |
|
70 return games[index.row()][index.column()]; |
|
71 } |
|
72 |
|
73 QVariant HWNetUdpModel::headerData(int section, |
|
74 Qt::Orientation orientation, int role) const |
|
75 { |
|
76 if (role != Qt::DisplayRole) |
|
77 return QVariant(); |
|
78 |
|
79 if (orientation == Qt::Horizontal) |
|
80 { |
|
81 switch (section) |
|
82 { |
|
83 case 0: return tr("Title"); |
|
84 case 1: return tr("IP"); |
|
85 case 2: return tr("Port"); |
|
86 default: return QVariant(); |
|
87 } |
|
88 } else |
|
89 return QString("%1").arg(section + 1); |
|
90 } |
|
91 |
|
92 int HWNetUdpModel::rowCount(const QModelIndex &parent) const |
|
93 { |
|
94 if (parent.isValid()) |
|
95 return 0; |
|
96 else |
|
97 return games.size(); |
|
98 } |
|
99 |
|
100 int HWNetUdpModel::columnCount(const QModelIndex & parent) const |
|
101 { |
|
102 if (parent.isValid()) |
|
103 return 0; |
|
104 else |
|
105 return 3; |
|
106 } |