• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kgame

kgamedialogconfig.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 (kde at 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 #include "kgamedialogconfig.h"
00022 
00023 #include "kgame.h"
00024 #include "kplayer.h"
00025 #include "kgamechat.h"
00026 #include "kgameconnectdialog.h"
00027 #include "kgameproperty.h"
00028 
00029 #include <klistwidget.h>
00030 #include <klocale.h>
00031 #include <knuminput.h>
00032 #include <kdialog.h>
00033 #include <kmessagebox.h>
00034 
00035 #include <QLayout>
00036 #include <QLabel>
00037 #include <QPushButton>
00038 #include <QLineEdit>
00039 #include <QGroupBox>
00040 
00041 #include "kgamedialogconfig.moc"
00042 
00043 class KGameDialogConfigPrivate
00044 {
00045 public:
00046     KGameDialogConfigPrivate()
00047     {
00048         mOwner = 0;
00049         mGame = 0;
00050 
00051         mAdmin = false;
00052     }
00053 
00054     bool mAdmin;
00055     KGame* mGame;
00056     KPlayer* mOwner;
00057 };
00058 
00059 KGameDialogConfig::KGameDialogConfig(QWidget* parent) 
00060     : QWidget( parent ),
00061       d( new KGameDialogConfigPrivate )
00062 {
00063 }
00064 
00065 KGameDialogConfig::~KGameDialogConfig()
00066 {
00067  kDebug(11001) ;
00068  delete d;
00069 }
00070 
00071 void KGameDialogConfig::setKGame(KGame* g)
00072 {
00073  d->mGame = g;
00074 }
00075 
00076 void KGameDialogConfig::setOwner(KPlayer* p)
00077 {
00078  d->mOwner = p;
00079 }
00080 
00081 void KGameDialogConfig::setAdmin(bool a)
00082 {
00083  d->mAdmin = a;
00084 }
00085 
00086 KGame* KGameDialogConfig::game() const
00087 { return d->mGame; }
00088 bool KGameDialogConfig::admin() const
00089 { return d->mAdmin; }
00090 KPlayer* KGameDialogConfig::owner() const
00091 { return d->mOwner; }
00092 
00094 class KGameDialogNetworkConfigPrivate
00095 {
00096 public:
00097     KGameDialogNetworkConfigPrivate()
00098     {
00099         mInitConnection = 0;
00100         mNetworkLabel = 0;
00101         mDisconnectButton = 0;
00102         mConnect = 0;
00103         mDefaultServer=true;
00104 
00105     }
00106 
00107     // QPushButton* mInitConnection;
00108     QGroupBox* mInitConnection;
00109     QLabel* mNetworkLabel;
00110     QPushButton *mDisconnectButton;
00111 
00112     bool mDefaultServer;
00113     QString mDefaultHost;
00114     unsigned short int mDefaultPort;
00115     KGameConnectWidget *mConnect;
00116 };
00117 
00118 
00119 KGameDialogNetworkConfig::KGameDialogNetworkConfig(QWidget* parent)
00120         : KGameDialogConfig(parent)
00121 {
00122 // kDebug(11001) << ": this=" << this;
00123  d = new KGameDialogNetworkConfigPrivate();
00124 
00125  QVBoxLayout* topLayout = new QVBoxLayout(this);
00126  topLayout->setMargin( KDialog::marginHint() );
00127  topLayout->setSpacing( KDialog::spacingHint() );
00128 
00129  QHBoxLayout *hb = new QHBoxLayout;
00130  hb->setSpacing( KDialog::spacingHint() );
00131  topLayout->addLayout(hb);
00132 
00133  d->mNetworkLabel = new QLabel(this);
00134  hb->addWidget(d->mNetworkLabel);
00135 
00136  d->mDisconnectButton=new QPushButton(i18n("Disconnect"),this);
00137  connect(d->mDisconnectButton, SIGNAL(clicked()), this, SLOT(slotExitConnection()));
00138  hb->addWidget(d->mDisconnectButton);
00139 
00140  d->mInitConnection = new QGroupBox(i18n("Network Configuration"), this);
00141  QHBoxLayout* gboxLay = new QHBoxLayout(d->mInitConnection);
00142  topLayout->addWidget(d->mInitConnection);
00143 
00144  d->mConnect = new KGameConnectWidget(d->mInitConnection);
00145  gboxLay->addWidget(d->mConnect);
00146  connect(d->mConnect, SIGNAL(signalNetworkSetup()), this, SLOT(slotInitConnection()));
00147  connect(d->mConnect, SIGNAL(signalServerTypeChanged(int)),
00148          this, SIGNAL(signalServerTypeChanged(int)));
00149 
00150  // Needs to be AFTER the creation of the dialogs
00151  setConnected(false);
00152  setDefaultNetworkInfo("localhost", 7654,true);
00153 }
00154 
00155 KGameDialogNetworkConfig::~KGameDialogNetworkConfig()
00156 {
00157  kDebug(11001) ;
00158  delete d;
00159 }
00160 
00161 void KGameDialogNetworkConfig::slotExitConnection()
00162 {
00163  kDebug(11001) << " !!!!!!!!!!!!!!!!!!!!!!!";
00164   if (game()) game()->disconnect();
00165   setConnected(false,false);
00166 }
00167 
00168 void KGameDialogNetworkConfig::slotInitConnection()
00169 {
00170  kDebug(11001) ;
00171  bool connected = false;
00172  bool master = true;
00173  unsigned short int port = d->mConnect->port();
00174  QString host = d->mConnect->host();
00175 
00176  if (host.isNull()) {
00177     master = true;
00178     if (game()) {
00179         game()->setDiscoveryInfo(d->mConnect->type(),d->mConnect->gameName());
00180         connected = game()->offerConnections(port);
00181     }
00182  } else {
00183     master = false;
00184     if (game()) {
00185         connected = game()->connectToServer(host, port);
00186     }
00187   // We need to learn about failed connections
00188   if (game()) {
00189      connect(game(), SIGNAL(signalConnectionBroken()),
00190       this, SLOT(slotConnectionBroken()));
00191   }
00192  }
00193  setConnected(connected, master);
00194 }
00195 
00196 void KGameDialogNetworkConfig::slotConnectionBroken()
00197 {
00198   kDebug(11001) ;
00199   setConnected(false,false);
00200   KMessageBox::error(this, i18n("Cannot connect to the network"));
00201 }
00202 
00203 void KGameDialogNetworkConfig::setConnected(bool connected, bool master)
00204 {
00205  if (!connected) {
00206     d->mNetworkLabel->setText(i18n("Network status: No Network"));
00207     d->mInitConnection->setEnabled(true);
00208   d->mDisconnectButton->setEnabled(false);
00209     return;
00210  }
00211  if (master) {
00212     d->mNetworkLabel->setText(i18n("Network status: You are MASTER"));
00213  } else {
00214     d->mNetworkLabel->setText(i18n("Network status: You are connected"));
00215  }
00216  d->mInitConnection->setEnabled(false);
00217  d->mDisconnectButton->setEnabled(true);
00218 }
00219 
00220 void KGameDialogNetworkConfig::submitToKGame(KGame* , KPlayer* )
00221 {
00222 }
00223 
00224 void KGameDialogNetworkConfig::setKGame(KGame* g)
00225 {
00226  KGameDialogConfig::setKGame(g);
00227  if (!game()) {
00228     setConnected(false);
00229     return;
00230  }
00231  setConnected(game()->isNetwork(), game()->isMaster());
00232 }
00233 
00234 void KGameDialogNetworkConfig::setDefaultNetworkInfo(const QString& host, unsigned short int port,bool server)
00235 {
00236  d->mDefaultPort = port;
00237  d->mDefaultHost = host;
00238  d->mDefaultServer = server;
00239 
00240  d->mConnect->setHost(host);
00241  d->mConnect->setPort(port);
00242  if (server) {
00243     d->mConnect->setDefault(0);
00244  } else {
00245     d->mConnect->setDefault(1);
00246  }
00247 }
00248 
00249 void KGameDialogNetworkConfig::setDiscoveryInfo(const QString& type, const QString& name)
00250 {
00251  d->mConnect->setType(type);
00252  d->mConnect->setName(name);
00253 }
00254 
00256 class KGameDialogGeneralConfigPrivate
00257 {
00258 public:
00259     KGameDialogGeneralConfigPrivate()
00260     {
00261         mTopLayout = 0;
00262         mName = 0;
00263     }
00264 
00265     QLineEdit* mName;
00266 
00267     QVBoxLayout* mTopLayout;
00268 };
00269 
00270 KGameDialogGeneralConfig::KGameDialogGeneralConfig(QWidget* parent, bool initializeGUI)
00271         : KGameDialogConfig(parent)
00272 {
00273 // kDebug(11001) << ": this=" << this;
00274  d = new KGameDialogGeneralConfigPrivate;
00275 
00276  if (initializeGUI) {
00277     d->mTopLayout = new QVBoxLayout(this);
00278     d->mTopLayout->setMargin( KDialog::marginHint() );
00279     d->mTopLayout->setSpacing( KDialog::spacingHint() );
00280 
00281     QWidget* nameWidget = new QWidget(this);
00282         d->mTopLayout->addWidget(nameWidget);
00283     QHBoxLayout* l = new QHBoxLayout(nameWidget);
00284     QLabel* nameLabel = new QLabel(i18n("Your name:"), nameWidget);
00285     l->addWidget(nameLabel);
00286     d->mName = new QLineEdit(nameWidget);
00287     l->addWidget(d->mName);
00288  }
00289 }
00290 
00291 KGameDialogGeneralConfig::~KGameDialogGeneralConfig()
00292 {
00293  kDebug(11001) ;
00294  delete d;
00295 }
00296 
00297 void KGameDialogGeneralConfig::setPlayerName(const QString& name)
00298 {
00299  if (d->mName) {
00300     d->mName->setText(name);
00301  }
00302 }
00303 
00304 QString KGameDialogGeneralConfig::playerName() const
00305 {
00306  return d->mName ? d->mName->text() : QString();
00307 }
00308 
00309 void KGameDialogGeneralConfig::setOwner(KPlayer* p)
00310 {
00311  if (owner()) {
00312     owner()->disconnect(this);
00313  }
00314  KGameDialogConfig::setOwner(p);
00315  if (!owner()) {
00316     // can this config be used at all?
00317     // maybe call hide()
00318     return;
00319  }
00320  connect(owner(), SIGNAL(signalPropertyChanged(KGamePropertyBase*, KPlayer*)),
00321         this, SLOT(slotPropertyChanged(KGamePropertyBase*, KPlayer*)));
00322  setPlayerName(p->name());
00323  //TODO: connect signalPropertyChanged and check for playername changes!
00324 }
00325 
00326 void KGameDialogGeneralConfig::setKGame(KGame* g)
00327 {
00328  KGameDialogConfig::setKGame(g);
00329  if (!g) {
00330     // TODO
00331     // can this config be used at all?
00332     // maybe call hide()
00333     return;
00334  }
00335 }
00336 
00337 void KGameDialogGeneralConfig::setAdmin(bool admin)
00338 {
00339  KGameDialogConfig::setAdmin(admin);
00340 // enable/disable widgets
00341 
00342 }
00343 
00344 void KGameDialogGeneralConfig::submitToKGame(KGame* g, KPlayer* p)
00345 {
00346 //FIXME
00347  if (p) {
00348     p->setName(playerName());
00349  }
00350  if (g) {
00351  }
00352 }
00353 
00354 void KGameDialogGeneralConfig::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* p)
00355 {
00356  if (!prop || !p || p != owner()) {
00357     return;
00358  }
00359  switch (prop->id()) {
00360     case KGamePropertyBase::IdName:
00361         setPlayerName(p->name());
00362         break;
00363     default:
00364         break;
00365  }
00366 }
00367 
00368 class KGameDialogMsgServerConfigPrivate
00369 {
00370 public:
00371     KGameDialogMsgServerConfigPrivate()
00372     {
00373         senderLayout = 0;
00374         localLayout = 0;
00375 
00376         changeMaxClients = 0;
00377         changeAdmin= 0;
00378         removeClient= 0;
00379         noAdmin = 0;
00380 
00381         noMaster = 0;
00382     }
00383 
00384     QVBoxLayout* senderLayout;
00385     QHBoxLayout* localLayout;
00386 
00387     QPushButton* changeMaxClients;
00388     QPushButton* changeAdmin;
00389     QPushButton* removeClient;
00390     QLabel* noAdmin;
00391 
00392     QLabel* noMaster;
00393 };
00394 
00395 
00396 // TODO: change ADMIN ID, remove CLIENTS, change MAXCLIENTS
00397 // we do everything here with QPushButtons as we want to wait a moment before
00398 // continuing - the message must be sent over network first
00399 KGameDialogMsgServerConfig::KGameDialogMsgServerConfig(QWidget* parent)
00400         : KGameDialogConfig(parent)
00401 {
00402  d = new KGameDialogMsgServerConfigPrivate;
00403 
00404  QVBoxLayout* topLayout = new QVBoxLayout(this);
00405  topLayout->setMargin( KDialog::marginHint() );
00406  topLayout->setSpacing( KDialog::spacingHint() );
00407  d->senderLayout = new QVBoxLayout;
00408  d->localLayout = new QHBoxLayout;
00409  topLayout->addLayout( d->senderLayout );
00410  topLayout->addLayout( d->localLayout );
00411 }
00412 
00413 KGameDialogMsgServerConfig::~KGameDialogMsgServerConfig()
00414 {
00415  kDebug(11001) ;
00416  delete d;
00417 }
00418 
00419 void KGameDialogMsgServerConfig::setKGame(KGame* g)
00420 {
00421  KGameDialogConfig::setKGame(g);
00422  //TODO display the ID of the admin if we aren't
00423  // connect(g, SIGNAL(signalAdminChanged(int)), this, SLOT(slotChangeIsAdmin(int)));//TODO
00424  if (!game()) {
00425     // we cannot do anything without a KGame object!
00426     setAdmin(false);
00427     return;
00428  }
00429  setAdmin(game()->isAdmin());
00430  setHasMsgServer(game()->messageServer());
00431 }
00432 
00433 
00434 void KGameDialogMsgServerConfig::slotChangeMaxClients()
00435 {
00436  if (!game()) {
00437     kError(11001) << ": no valid game object available!";
00438     return;
00439  }
00440  if (!game()->isAdmin()) {
00441     kError(11001) << ": only ADMIN is allowed to call this!";
00442     return;
00443  }
00444  int max;
00445 // edit->setText(QString::number()); // current max clients! //TODO
00446 
00447  QDialog* dialog = new QDialog();
00448  dialog->setWindowTitle(i18n("Maximal Number of Clients"));
00449  QHBoxLayout* l = new QHBoxLayout(dialog);
00450  l->setMargin( KDialog::marginHint() );
00451  l->setSpacing( KDialog::spacingHint() );
00452 
00453  l->addWidget(new QLabel(i18n("Maximal number of clients (-1 = infinite):"), dialog));
00454  QLineEdit* edit = new QLineEdit(dialog);//TODO: use KIntNumInput
00455  l->addWidget(edit);
00456 // edit->setText(QString::number(max)); // current max clients! //TODO
00457  if (dialog->exec() == QDialog::Accepted) {
00458     bool ok;
00459     max = edit->text().toInt(&ok);
00460     if (ok) {
00461         game()->setMaxClients(max);
00462     }
00463  }
00464 
00465 }
00466 
00467 void KGameDialogMsgServerConfig::slotRemoveClient()
00468 {
00469 }
00470 
00471 void KGameDialogMsgServerConfig::slotChangeAdmin()
00472 {
00473  if (!game()) {
00474     kError(11001) << ": no valid game object available!";
00475     return;
00476  }
00477  if (!admin()) {
00478     kError(11001) << ": only ADMIN is allowed to call this!";
00479     return;
00480  }
00481  //TODO
00482  quint32 newAdmin = 0;
00483 // newAdmin = ;
00484  game()->electAdmin(newAdmin);
00485 }
00486 
00487 void KGameDialogMsgServerConfig::removeClient(quint32 /*id*/)
00488 {
00489 //TODO
00490 }
00491 
00492 void KGameDialogMsgServerConfig::setAdmin(bool a)
00493 {
00494  if (admin() == a) {
00495     // no need to do anything
00496     return;
00497  }
00498  KGameDialogConfig::setAdmin(a);
00499  if (admin()) {
00500     if (d->noAdmin) {
00501         delete d->noAdmin;
00502         d->noAdmin = 0;
00503     }
00504     d->changeMaxClients = new QPushButton(i18n("Change Maximal Number of Clients"), this);
00505     connect(d->changeMaxClients, SIGNAL(pressed()), this, SLOT(slotChangeMaxClients()));
00506     d->changeAdmin = new QPushButton(i18n("Change Admin"), this);
00507     connect(d->changeAdmin, SIGNAL(pressed()), this, SLOT(slotChangeAdmin()));
00508     d->removeClient = new QPushButton(i18n("Remove Client with All Players"), this);
00509     connect(d->removeClient, SIGNAL(pressed()), this, SLOT(slotRemoveClient()));
00510     d->senderLayout->addWidget(d->changeMaxClients);
00511     d->senderLayout->addWidget(d->changeAdmin);
00512     d->senderLayout->addWidget(d->removeClient);
00513  } else {
00514     if (d->changeMaxClients) {
00515         delete d->changeMaxClients;
00516         d->changeMaxClients = 0;
00517     }
00518     if (d->changeAdmin) {
00519         delete d->changeAdmin;
00520         d->changeAdmin = 0;
00521     }
00522     if (d->removeClient) {
00523         delete d->removeClient;
00524         d->removeClient = 0;
00525     }
00526     d->noAdmin = new QLabel(i18n("Only the admin can configure the message server!"), this);
00527     d->senderLayout->addWidget(d->noAdmin);
00528  }
00529 }
00530 
00531 
00532 void KGameDialogMsgServerConfig::setHasMsgServer(bool has)
00533 {
00534  if (!has) {
00535     // delete all inputs
00536     if (!d->noMaster) {
00537         d->noMaster = new QLabel(i18n("You do not own the message server"), this);
00538         d->localLayout->addWidget(d->noMaster);
00539     }
00540     return;
00541  }
00542  if (d->noMaster) {
00543     delete d->noMaster;
00544     d->noMaster = 0;
00545  }
00546  //TODO
00547  // list all connections, data (max clients) and so on
00548  // cannot be done above (together with QPushButtons) as it is possible that
00549  // this client is ADMIN but not MASTER (i.e. doesn't own the messageserver)
00550 }
00551 
00552 
00553 class KGameDialogChatConfigPrivate
00554 {
00555 public:
00556     KGameDialogChatConfigPrivate()
00557     {
00558         mChat = 0;
00559     }
00560 
00561     KGameChat* mChat;
00562 };
00563 
00564 KGameDialogChatConfig::KGameDialogChatConfig(int chatMsgId, QWidget* parent)
00565         : KGameDialogConfig(parent)
00566 {
00567  d = new KGameDialogChatConfigPrivate;
00568  QVBoxLayout* topLayout = new QVBoxLayout(this);
00569  topLayout->setMargin( KDialog::marginHint() );
00570  topLayout->setSpacing( KDialog::spacingHint() );
00571  QGroupBox* b = new QGroupBox(i18n("Chat"), this);
00572  topLayout->addWidget(b);
00573  QHBoxLayout* gboxLay = new QHBoxLayout(b);
00574  d->mChat = new KGameChat(0, chatMsgId, b);
00575  gboxLay->addWidget(d->mChat);
00576 }
00577 
00578 KGameDialogChatConfig::~KGameDialogChatConfig()
00579 {
00580  kDebug(11001) ;
00581  delete d;
00582 }
00583 
00584 void KGameDialogChatConfig::setKGame(KGame* g)
00585 {
00586  KGameDialogConfig::setKGame(g);
00587  d->mChat->setKGame(game());
00588  if (!game()) {
00589     hide();
00590  } else {
00591     show();
00592  }
00593 }
00594 
00595 void KGameDialogChatConfig::setOwner(KPlayer* p)
00596 {
00597  KGameDialogConfig::setOwner(p);
00598  if (!owner()) {
00599     hide();
00600     return;
00601  }
00602  d->mChat->setFromPlayer(owner());
00603  show();
00604 }
00605 
00606 
00607 
00608 class KGameDialogConnectionConfigPrivate
00609 {
00610 public:
00611     KGameDialogConnectionConfigPrivate()
00612     {
00613         mPlayerBox = 0;
00614     }
00615 
00616     QHash<QListWidgetItem*, KPlayer*> mItem2Player;
00617     KListWidget* mPlayerBox;
00618 };
00619 
00620 KGameDialogConnectionConfig::KGameDialogConnectionConfig(QWidget* parent)
00621         : KGameDialogConfig(parent)
00622 {
00623  //TODO: prevent player to ban himself
00624  d = new KGameDialogConnectionConfigPrivate;
00625  QVBoxLayout* topLayout = new QVBoxLayout(this);
00626  topLayout->setMargin( KDialog::marginHint() );
00627  topLayout->setSpacing( KDialog::spacingHint() );
00628  QGroupBox* b = new QGroupBox(i18n("Connected Players"), this);
00629  topLayout->addWidget(b);
00630  QHBoxLayout* gboxLay = new QHBoxLayout(b);
00631  d->mPlayerBox = new KListWidget(b);
00632  gboxLay->addWidget(d->mPlayerBox);
00633  setMinimumHeight(100);
00634 }
00635 
00636 KGameDialogConnectionConfig::~KGameDialogConnectionConfig()
00637 {
00638  kDebug(11001) ;
00639  // d->mIem2Player.clear();
00640  delete d;
00641 }
00642 
00643 void KGameDialogConnectionConfig::setKGame(KGame* g)
00644 {
00645  if (game()) {
00646     disconnect(game(), 0, this, 0);
00647  }
00648  KGameDialogConfig::setKGame(g);
00649  slotClearPlayers();
00650  if (game()) {
00651 // react to changes in KGame::playerList()
00652     connect(game(), SIGNAL(signalPlayerJoinedGame(KPlayer*)),
00653             this, SLOT(slotPlayerJoinedGame(KPlayer*)));
00654     connect(game(), SIGNAL(signalPlayerLeftGame(KPlayer*)),
00655             this, SLOT(slotPlayerLeftGame(KPlayer*)));
00656 
00657   for ( QList<KPlayer*>::const_iterator it = game()->playerList()->begin(); it!=game()->playerList()->end();it++ )
00658   {
00659         slotPlayerJoinedGame(*it);
00660     }
00661  }
00662 }
00663 
00664 void KGameDialogConnectionConfig::setOwner(KPlayer* p)
00665 {
00666  KGameDialogConfig::setOwner(p);
00667 }
00668 
00669 void KGameDialogConnectionConfig::setAdmin(bool a)
00670 {
00671  if (!game()) {// not possible... in theory
00672     return;
00673  }
00674  if (admin()) {
00675     disconnect(game(), SIGNAL(executed(QListWidgetItem*)), this, 0);
00676  }
00677  KGameDialogConfig::setAdmin(a);
00678  if (admin()) {
00679     connect(d->mPlayerBox, SIGNAL(executed(QListWidgetItem*)), this,
00680             SLOT(slotKickPlayerOut(QListWidgetItem*)));
00681  }
00682 }
00683 
00684 QListWidgetItem* KGameDialogConnectionConfig::item(KPlayer* p) const
00685 {
00686  QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
00687  it = d->mItem2Player.constBegin();
00688  itEnd = d->mItem2Player.constEnd();
00689  for ( ; it != itEnd; ++it ) {
00690     if (it.value() == p) return it.key();
00691  }
00692  return 0;
00693 }
00694 
00695 void KGameDialogConnectionConfig::slotClearPlayers()
00696 {
00697  QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
00698  it = d->mItem2Player.constBegin();
00699  itEnd = d->mItem2Player.constEnd();
00700  for ( ; it != itEnd; ++it ) {
00701     slotPlayerLeftGame(it.value());
00702  }
00703 
00704  if (d->mItem2Player.count() > 0) {
00705     kWarning(11001) << ": itemList wasn't cleared properly";
00706     d->mItem2Player.clear();
00707  }
00708  if (d->mPlayerBox->count() > 0) {
00709     kWarning(11001) << ": listBox wasn't cleared properly";
00710     d->mPlayerBox->clear();
00711  }
00712 
00713 }
00714 
00715 void KGameDialogConnectionConfig::slotPlayerJoinedGame(KPlayer* p)
00716 {
00717  if (!p) {
00718     kError(11001) << ": Cannot add NULL player";
00719         return;
00720  }
00721  bool playerFound = false;
00722  QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
00723  it = d->mItem2Player.constBegin();
00724  itEnd = d->mItem2Player.constEnd();
00725  for ( ; !playerFound && it != itEnd; ++it ) playerFound = it.value() == p;
00726  if (playerFound) {
00727     kError(11001) << ": attempt to double add player";
00728     return;
00729  }
00730  kDebug(11001) << ": add player" << p->id();
00731  QListWidgetItem* t = new QListWidgetItem(p->name(), d->mPlayerBox);
00732  d->mItem2Player.insert(t, p);
00733 
00734  connect(p, SIGNAL(signalPropertyChanged(KGamePropertyBase*, KPlayer*)),
00735         this, SLOT(slotPropertyChanged(KGamePropertyBase*, KPlayer*)));
00736 
00737 }
00738 
00739 void KGameDialogConnectionConfig::slotPlayerLeftGame(KPlayer* p)
00740 {
00741  // disconnect first
00742  this->disconnect(p);
00743  if (!item(p)) {
00744     kError(11001) << ": cannot find" << p->id()
00745             << "in list";
00746     return;
00747  }
00748  d->mPlayerBox->takeItem(d->mPlayerBox->row(item(p)));
00749 
00750 }
00751 
00752 void KGameDialogConnectionConfig::slotKickPlayerOut(QListWidgetItem* item)
00753 {
00754  kDebug(11001) << "kick player out";
00755  KPlayer* p = d->mItem2Player[item];
00756  if (!p) {
00757     kError(11001) << "invalid item selected - no player found";
00758     return;
00759  }
00760  if (!game()) {
00761     kWarning(11001) << "no game set";
00762     return;
00763  }
00764  if (!admin()) {
00765     kDebug(11001) << "Only the ADMIN can kick players";
00766     return;
00767  }
00768  if (p == owner()) { // you wanna ban the ADMIN ??
00769     kDebug(11001) << "you cannot kick the ADMIN";
00770     return;
00771  }
00772 
00773  if (KMessageBox::questionYesNo(this, i18n("Do you want to ban player \"%1\" from the game?",
00774         p->name()), QString(), KGuiItem (i18n("Ban Player")), KGuiItem (i18n("Do Not Ban"))) == KMessageBox::Yes) {
00775     kDebug(11001) << "will remove player" << p;
00776     game()->removePlayer(p);
00777 //  d->mPlayerBox->removeItem(d->mPlayerBox->index(item)); // should be done by signalPlayerLeftGame
00778  } else {
00779     kDebug(11001) << "will NOT remove player" << p;
00780  }
00781 }
00782 
00783 void KGameDialogConnectionConfig::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* player)
00784 {
00785  if(prop->id() == KGamePropertyBase::IdName) {
00786     QListWidgetItem* old = item(player);
00787     QListWidgetItem* t = new QListWidgetItem(player->name());
00788     int row = d->mPlayerBox->row(old);
00789     d->mPlayerBox->takeItem( row );
00790     d->mPlayerBox->insertItem(row, t);
00791     d->mItem2Player.remove(old);
00792     d->mItem2Player.insert(t, player);
00793  }
00794 }
00795 

libkdegames/kgame

Skip menu "libkdegames/kgame"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal