kgamedialogconfig.cpp

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