• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • kgame
  • dialogs
kgameconnectdialog.cpp
1/*
2 This file is part of the TDE games library
3 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
4 Copyright (C) 2001 Martin Heni (martin@heni-online.de)
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21
22#include "kgameconnectdialog.h"
23
24#include <knuminput.h>
25#include <tdelocale.h>
26
27#include <tqlineedit.h>
28#include <tqcombobox.h>
29#include <tqvbuttongroup.h>
30#include <tqlayout.h>
31#include <tqradiobutton.h>
32#include <tqlabel.h>
33#include <dnssd/servicebrowser.h>
34#include <tqpushbutton.h>
35#include <tqgrid.h>
36
37class KGameConnectWidgetPrivate
38{
39 public:
40 KGameConnectWidgetPrivate()
41 {
42 mPort = 0;
43 mHost = 0;
44 mButtonGroup = 0;
45 mBrowser = 0;
46 }
47
48 KIntNumInput* mPort;
49 TQLineEdit* mHost; //KLineEdit?
50 TQVButtonGroup* mButtonGroup;
51 TQComboBox *mClientName;
52 TQLabel *mClientNameLabel;
53 DNSSD::ServiceBrowser *mBrowser;
54 TQLabel *mServerNameLabel;
55 TQLineEdit *mServerName;
56 TQString mType;
57};
58
59KGameConnectWidget::KGameConnectWidget(TQWidget* parent) : TQWidget(parent)
60{
61 d = new KGameConnectWidgetPrivate;
62
63 TQVBoxLayout* vb = new TQVBoxLayout(this, KDialog::spacingHint());
64 d->mButtonGroup = new TQVButtonGroup(this);
65 vb->addWidget(d->mButtonGroup);
66 connect(d->mButtonGroup, TQ_SIGNAL(clicked(int)), this, TQ_SLOT(slotTypeChanged(int)));
67 (void)new TQRadioButton(i18n("Create a network game"), d->mButtonGroup);
68 (void)new TQRadioButton(i18n("Join a network game"), d->mButtonGroup);
69
70 TQGrid* g = new TQGrid(2, this);
71 vb->addWidget(g);
72 g->setSpacing(KDialog::spacingHint());
73 d->mServerNameLabel = new TQLabel(i18n("Game name:"), g);
74 d->mServerName = new TQLineEdit(g);
75 d->mClientNameLabel = new TQLabel(i18n("Network games:"), g);
76 d->mClientName = new TQComboBox(g);
77 connect(d->mClientName,TQ_SIGNAL(activated(int)),TQ_SLOT(slotGameSelected(int)));
78 (void)new TQLabel(i18n("Port to connect to:"), g);
79 d->mPort = new KIntNumInput(g);
80 (void)new TQLabel(i18n("Host to connect to:"), g);
81 d->mHost = new TQLineEdit(g);
82
83 TQPushButton *button=new TQPushButton(i18n("&Start Network"), this);
84 connect(button, TQ_SIGNAL(clicked()), this, TQ_SIGNAL(signalNetworkSetup()));
85 vb->addWidget(button);
86 // Hide until type is set
87 d->mClientName->hide();
88 d->mClientNameLabel->hide();
89 d->mServerName->hide();
90 d->mServerNameLabel->hide();
91}
92
93void KGameConnectWidget::showDnssdControls()
94{
95 if (!d->mBrowser) return;
96 if (d->mHost->isEnabled()) { // client
97 d->mClientName->show();
98 d->mClientNameLabel->show();
99 d->mServerName->hide();
100 d->mServerNameLabel->hide();
101 slotGameSelected(d->mClientName->currentItem());
102 } else {
103 d->mClientName->hide();
104 d->mClientNameLabel->hide();
105 d->mServerName->show();
106 d->mServerNameLabel->show();
107 }
108}
109
110void KGameConnectWidget::setType(const TQString& type)
111{
112 d->mType = type;
113 delete d->mBrowser;
114 d->mBrowser = new DNSSD::ServiceBrowser(type);
115 connect(d->mBrowser,TQ_SIGNAL(finished()),TQ_SLOT(slotGamesFound()));
116 d->mBrowser->startBrowse();
117 showDnssdControls();
118}
119
120void KGameConnectWidget::slotGamesFound()
121{
122 bool autoselect=false;
123 if (!d->mClientName->count()) autoselect=true;
124 d->mClientName->clear();
125 TQStringList names;
126 TQValueList<DNSSD::RemoteService::Ptr>::ConstIterator itEnd = d->mBrowser->services().end();
127 for (TQValueList<DNSSD::RemoteService::Ptr>::ConstIterator it = d->mBrowser->services().begin();
128 it!=itEnd; ++it) names << (*it)->serviceName();
129 d->mClientName->insertStringList(names);
130 if (autoselect && d->mClientName->count()) slotGameSelected(0);
131}
132
133void KGameConnectWidget::setName(const TQString& name)
134{
135 d->mServerName->setText(name);
136}
137
138TQString KGameConnectWidget::gameName() const
139{
140 return d->mServerName->text();
141}
142
143TQString KGameConnectWidget::type() const
144{
145 return d->mType;
146}
147
148void KGameConnectWidget::slotGameSelected(int nr)
149{
150 if (nr>=(d->mBrowser->services().count()) || nr<0) return;
151 if (!d->mHost->isEnabled()) return; // this is server mode, do not overwrite host and port controls
152 DNSSD::RemoteService::Ptr srv = d->mBrowser->services()[nr];
153 if (!srv->isResolved() && !srv->resolve()) return;
154 d->mHost->setText(srv->hostName());
155 d->mPort->setValue(srv->port());
156}
157KGameConnectWidget::~KGameConnectWidget()
158{
159 delete d->mBrowser;
160 delete d;
161}
162
163TQString KGameConnectWidget::host() const
164{
165 if (d->mHost->isEnabled()) {
166 return d->mHost->text();
167 } else {
168 return TQString();
169 }
170}
171
172unsigned short int KGameConnectWidget::port() const
173{
174 return d->mPort->value();
175}
176
177void KGameConnectWidget::setHost(const TQString& host)
178{
179 d->mHost->setText(host);
180}
181
182void KGameConnectWidget::setPort(unsigned short int port)
183{
184 d->mPort->setValue(port);
185}
186
187void KGameConnectWidget::setDefault(int state)
188{
189 d->mButtonGroup->setButton(state);
190 slotTypeChanged(state);
191}
192
193void KGameConnectWidget::slotTypeChanged(int t)
194{
195 if (t == 0) {
196 d->mHost->setEnabled(false);
197 } else if (t == 1) {
198 d->mHost->setEnabled(true);
199 }
200 showDnssdControls();
201 emit signalServerTypeChanged(t);
202}
203
204class KGameConnectDialogPrivate
205{
206 public:
207 KGameConnectDialogPrivate()
208 {
209 mConnect = 0;
210 }
211
212 KGameConnectWidget* mConnect;
213};
214
215// buttonmask =Ok|Cancel
216KGameConnectDialog::KGameConnectDialog(TQWidget* parent,int buttonmask) : KDialogBase(Plain,
217 i18n("Network Game"),buttonmask , Ok, parent, 0, true, buttonmask!=0)
218{
219 d = new KGameConnectDialogPrivate;
220 TQVBoxLayout* vb = new TQVBoxLayout(plainPage(), spacingHint());
221 d->mConnect = new KGameConnectWidget(plainPage());
222 vb->addWidget(d->mConnect);
223}
224
225KGameConnectDialog::~KGameConnectDialog()
226{
227 delete d;
228}
229
230int KGameConnectDialog::initConnection( unsigned short int& port,
231 TQString& host, TQWidget* parent, bool server)
232{
233 KGameConnectDialog d(parent);
234 d.setHost(host);
235 d.setPort(port);
236 if (server) {
237 d.setDefault(0);
238 } else {
239 d.setDefault(1);
240 }
241
242 int result = d.exec();
243 if (result == TQDialog::Accepted) {
244 host = d.host();
245 port = d.port();
246 }
247 return result;
248}
249
250TQString KGameConnectDialog::host() const
251{
252 return d->mConnect->host();
253}
254
255unsigned short int KGameConnectDialog::port() const
256{
257 return d->mConnect->port();
258}
259
260void KGameConnectDialog::setHost(const TQString& host)
261{
262 d->mConnect->setHost(host);
263}
264
265void KGameConnectDialog::setPort(unsigned short int port)
266{
267 d->mConnect->setPort(port);
268}
269
270void KGameConnectDialog::setDefault(int state)
271{
272 d->mConnect->setDefault(state);
273}
274
275
276
277#include "kgameconnectdialog.moc"
278
KGameConnectDialog
Dialog to ask for host and port.
Definition: kgameconnectdialog.h:118
KGameConnectDialog::initConnection
static int initConnection(unsigned short int &port, TQString &host, TQWidget *parent, bool server=false)
Shows a dialog to either connect to an existing game or to create a server game, depending on user's ...
Definition: kgameconnectdialog.cpp:230
KGameConnectDialog::host
TQString host() const
Definition: kgameconnectdialog.cpp:250
KGameConnectDialog::setHost
void setHost(const TQString &host)
Definition: kgameconnectdialog.cpp:260
KGameConnectDialog::port
unsigned short int port() const
Definition: kgameconnectdialog.cpp:255
KGameConnectDialog::setDefault
void setDefault(int state)
Specifies which state is the default (0 = server game; 1 = join game)
Definition: kgameconnectdialog.cpp:270
KGameConnectDialog::setPort
void setPort(unsigned short int port)
Definition: kgameconnectdialog.cpp:265

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.4
This website is maintained by Timothy Pearson.