libkdegames Library API Documentation

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