kgameconnectdialog.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
00004     Copyright (C) 2001 Martin Heni (martin@heni-online.de)
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License version 2 as published by the Free Software Foundation.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 
00022 #include "kgameconnectdialog.h"
00023 
00024 #include <knuminput.h>
00025 #include <klocale.h>
00026 
00027 #include <qlineedit.h>
00028 #include <qcombobox.h>
00029 #include <qvbuttongroup.h>
00030 #include <qlayout.h>
00031 #include <qradiobutton.h>
00032 #include <qlabel.h>
00033 #include <dnssd/servicebrowser.h>
00034 #include <qpushbutton.h>
00035 #include <qgrid.h>
00036 
00037 class KGameConnectWidgetPrivate
00038 {
00039  public:
00040         KGameConnectWidgetPrivate()
00041         {
00042                 mPort = 0;
00043                 mHost = 0;
00044                 mButtonGroup = 0;
00045                 mBrowser = 0;
00046         }
00047 
00048         KIntNumInput* mPort;
00049         QLineEdit* mHost; //KLineEdit?
00050         QVButtonGroup* mButtonGroup;
00051         QComboBox *mClientName;
00052         QLabel *mClientNameLabel;
00053         DNSSD::ServiceBrowser *mBrowser;
00054         QLabel *mServerNameLabel;
00055         QLineEdit *mServerName;
00056         QString mType;
00057 };
00058 
00059 KGameConnectWidget::KGameConnectWidget(QWidget* parent) : QWidget(parent)
00060 {
00061  d = new KGameConnectWidgetPrivate;
00062 
00063  QVBoxLayout* vb = new QVBoxLayout(this, KDialog::spacingHint());
00064  d->mButtonGroup = new QVButtonGroup(this);
00065  vb->addWidget(d->mButtonGroup);
00066  connect(d->mButtonGroup, SIGNAL(clicked(int)), this, SLOT(slotTypeChanged(int)));
00067  (void)new QRadioButton(i18n("Create a network game"), d->mButtonGroup);
00068  (void)new QRadioButton(i18n("Join a network game"), d->mButtonGroup);
00069 
00070  QGrid* g = new QGrid(2, this);
00071  vb->addWidget(g);
00072  g->setSpacing(KDialog::spacingHint());
00073  d->mServerNameLabel = new QLabel(i18n("Game name:"), g);
00074  d->mServerName = new QLineEdit(g);
00075  d->mClientNameLabel = new QLabel(i18n("Network games:"), g);
00076  d->mClientName = new QComboBox(g);
00077  connect(d->mClientName,SIGNAL(activated(int)),SLOT(slotGameSelected(int)));
00078  (void)new QLabel(i18n("Port to connect to:"), g);
00079  d->mPort = new KIntNumInput(g);
00080  (void)new QLabel(i18n("Host to connect to:"), g);
00081  d->mHost = new QLineEdit(g); 
00082 
00083  QPushButton *button=new QPushButton(i18n("&Start Network"), this);
00084  connect(button, SIGNAL(clicked()), this, SIGNAL(signalNetworkSetup()));
00085  vb->addWidget(button);
00086  // Hide until type is set
00087  d->mClientName->hide();
00088  d->mClientNameLabel->hide();
00089  d->mServerName->hide();
00090  d->mServerNameLabel->hide();
00091 }
00092 
00093 void KGameConnectWidget::showDnssdControls() 
00094 {
00095  if (!d->mBrowser) return;
00096  if (d->mHost->isEnabled()) {      // client    
00097   d->mClientName->show();
00098   d->mClientNameLabel->show();
00099   d->mServerName->hide();
00100   d->mServerNameLabel->hide();
00101   slotGameSelected(d->mClientName->currentItem());
00102  } else {
00103   d->mClientName->hide();
00104   d->mClientNameLabel->hide();
00105   d->mServerName->show();
00106   d->mServerNameLabel->show();
00107  }
00108 }
00109 
00110 void KGameConnectWidget::setType(const QString& type) 
00111 {
00112  d->mType = type;
00113  delete d->mBrowser;
00114  d->mBrowser = new DNSSD::ServiceBrowser(type);
00115  connect(d->mBrowser,SIGNAL(finished()),SLOT(slotGamesFound()));
00116  d->mBrowser->startBrowse();
00117  showDnssdControls();
00118 }
00119 
00120 void KGameConnectWidget::slotGamesFound()
00121 {
00122  bool autoselect=false;
00123  if (!d->mClientName->count()) autoselect=true;
00124  d->mClientName->clear();
00125  QStringList names;
00126  QValueList<DNSSD::RemoteService::Ptr>::ConstIterator itEnd = d->mBrowser->services().end();
00127  for (QValueList<DNSSD::RemoteService::Ptr>::ConstIterator it = d->mBrowser->services().begin();
00128   it!=itEnd; ++it) names << (*it)->serviceName();
00129  d->mClientName->insertStringList(names);
00130  if (autoselect && d->mClientName->count()) slotGameSelected(0);
00131 }
00132 
00133 void KGameConnectWidget::setName(const QString& name) 
00134 {
00135  d->mServerName->setText(name);
00136 }
00137 
00138 QString KGameConnectWidget::gameName() const
00139 {
00140  return d->mServerName->text();
00141 }
00142 
00143 QString KGameConnectWidget::type() const
00144 {
00145  return d->mType;
00146 }
00147 
00148 void KGameConnectWidget::slotGameSelected(int nr)
00149 {
00150  if (nr>=(d->mBrowser->services().count()) || nr<0) return;
00151  if (!d->mHost->isEnabled()) return; // this is server mode, do not overwrite host and port controls
00152  DNSSD::RemoteService::Ptr srv = d->mBrowser->services()[nr];
00153  if (!srv->isResolved() && !srv->resolve()) return;
00154  d->mHost->setText(srv->hostName());
00155  d->mPort->setValue(srv->port());
00156 }
00157 KGameConnectWidget::~KGameConnectWidget()
00158 {
00159  delete d->mBrowser;
00160  delete d;
00161 }
00162 
00163 QString KGameConnectWidget::host() const
00164 { 
00165  if (d->mHost->isEnabled()) {
00166         return d->mHost->text();
00167  } else {
00168         return QString::null;
00169  }
00170 }
00171 
00172 unsigned short int KGameConnectWidget::port() const
00173 {
00174  return d->mPort->value(); 
00175 }
00176 
00177 void KGameConnectWidget::setHost(const QString& host)
00178 { 
00179  d->mHost->setText(host); 
00180 }
00181 
00182 void KGameConnectWidget::setPort(unsigned short int port)
00183 {
00184  d->mPort->setValue(port); 
00185 }
00186 
00187 void KGameConnectWidget::setDefault(int state)
00188 {
00189  d->mButtonGroup->setButton(state); 
00190  slotTypeChanged(state); 
00191 }
00192 
00193 void KGameConnectWidget::slotTypeChanged(int t)
00194 {
00195  if (t == 0) {
00196         d->mHost->setEnabled(false);
00197  } else if (t == 1) {
00198         d->mHost->setEnabled(true);
00199  }
00200  showDnssdControls();
00201  emit signalServerTypeChanged(t);
00202 }
00203 
00204 class KGameConnectDialogPrivate
00205 {
00206  public:
00207         KGameConnectDialogPrivate()
00208         {
00209                 mConnect = 0;
00210         }
00211 
00212         KGameConnectWidget* mConnect;
00213 };
00214 
00215 // buttonmask =Ok|Cancel
00216 KGameConnectDialog::KGameConnectDialog(QWidget* parent,int buttonmask) : KDialogBase(Plain,
00217                 i18n("Network Game"),buttonmask , Ok, parent, 0, true, buttonmask!=0)
00218 {
00219  d = new KGameConnectDialogPrivate;
00220  QVBoxLayout* vb = new QVBoxLayout(plainPage(), spacingHint());
00221  d->mConnect = new KGameConnectWidget(plainPage());
00222  vb->addWidget(d->mConnect);
00223 }
00224 
00225 KGameConnectDialog::~KGameConnectDialog()
00226 {
00227  delete d;
00228 }
00229 
00230 int KGameConnectDialog::initConnection( unsigned short int& port,
00231                 QString& host, QWidget* parent, bool server)
00232 {
00233  KGameConnectDialog d(parent);
00234  d.setHost(host);
00235  d.setPort(port);
00236  if (server) {
00237         d.setDefault(0);
00238  } else {
00239         d.setDefault(1);
00240  }
00241 
00242  int result = d.exec();
00243  if (result == QDialog::Accepted) {
00244         host = d.host();
00245         port = d.port();
00246  }
00247  return result;
00248 }
00249 
00250 QString KGameConnectDialog::host() const
00251 {
00252  return d->mConnect->host();
00253 }
00254 
00255 unsigned short int KGameConnectDialog::port() const
00256 {
00257  return d->mConnect->port();
00258 }
00259 
00260 void KGameConnectDialog::setHost(const QString& host)
00261 {
00262  d->mConnect->setHost(host);
00263 }
00264 
00265 void KGameConnectDialog::setPort(unsigned short int port)
00266 {
00267  d->mConnect->setPort(port);
00268 }
00269 
00270 void KGameConnectDialog::setDefault(int state)
00271 {
00272  d->mConnect->setDefault(state);
00273 }
00274 
00275 
00276 
00277 #include "kgameconnectdialog.moc"
00278 

Generated on Wed Aug 23 18:04:17 2006 for libkdegames by  doxygen 1.4.6