21 #include "kgamedialogconfig.h" 25 #include "kgamechat.h" 26 #include "kgameconnectdialog.h" 28 #include <tdelocale.h> 29 #include <knuminput.h> 31 #include <tdelistbox.h> 32 #include <tdemessagebox.h> 35 #include <tqhgroupbox.h> 37 #include <tqpushbutton.h> 38 #include <tqlineedit.h> 40 #include <tqptrdict.h> 42 #include "kgamedialogconfig.moc" 44 class KGameDialogConfigPrivate
47 KGameDialogConfigPrivate()
60 KGameDialogConfig::KGameDialogConfig(TQWidget* parent) : TQWidget(parent)
62 d =
new KGameDialogConfigPrivate;
65 KGameDialogConfig::~KGameDialogConfig()
67 kdDebug(11001) << k_funcinfo << endl;
94 class KGameDialogNetworkConfigPrivate
97 KGameDialogNetworkConfigPrivate()
101 mDisconnectButton = 0;
108 TQHGroupBox* mInitConnection;
109 TQLabel* mNetworkLabel;
110 TQPushButton *mDisconnectButton;
113 TQString mDefaultHost;
114 unsigned short int mDefaultPort;
115 KGameConnectWidget *mConnect;
119 KGameDialogNetworkConfig::KGameDialogNetworkConfig(TQWidget* parent)
123 d =
new KGameDialogNetworkConfigPrivate();
125 TQVBoxLayout* topLayout =
new TQVBoxLayout(
this, KDialog::marginHint(), KDialog::spacingHint(),
"toplayout");
127 TQHBoxLayout *hb =
new TQHBoxLayout(topLayout, KDialog::spacingHint());
129 d->mNetworkLabel =
new TQLabel(
this);
130 hb->addWidget(d->mNetworkLabel);
132 d->mDisconnectButton=
new TQPushButton(i18n(
"Disconnect"),
this);
133 connect(d->mDisconnectButton, TQ_SIGNAL(clicked()),
this, TQ_SLOT(slotExitConnection()));
134 hb->addWidget(d->mDisconnectButton);
136 d->mInitConnection =
new TQHGroupBox(i18n(
"Network Configuration"),
this);
137 topLayout->addWidget(d->mInitConnection);
139 d->mConnect =
new KGameConnectWidget(d->mInitConnection);
140 connect(d->mConnect, TQ_SIGNAL(signalNetworkSetup()),
this, TQ_SLOT(slotInitConnection()));
141 connect(d->mConnect, TQ_SIGNAL(signalServerTypeChanged(
int)),
142 this, TQ_SIGNAL(signalServerTypeChanged(
int)));
146 setDefaultNetworkInfo(
"localhost", 7654,
true);
149 KGameDialogNetworkConfig::~KGameDialogNetworkConfig()
151 kdDebug(11001) << k_funcinfo << endl;
155 void KGameDialogNetworkConfig::slotExitConnection()
157 kdDebug(11001) << k_funcinfo <<
" !!!!!!!!!!!!!!!!!!!!!!!" << endl;
158 if (game()) game()->disconnect();
159 setConnected(
false,
false);
162 void KGameDialogNetworkConfig::slotInitConnection()
164 kdDebug(11001) << k_funcinfo << endl;
165 bool connected =
false;
167 unsigned short int port = d->mConnect->port();
168 TQString host = d->mConnect->host();
173 game()->setDiscoveryInfo(d->mConnect->type(),d->mConnect->gameName());
174 connected = game()->offerConnections(port);
179 connected = game()->connectToServer(host, port);
183 connect(game(), TQ_SIGNAL(signalConnectionBroken()),
184 this, TQ_SLOT(slotConnectionBroken()));
187 setConnected(connected, master);
190 void KGameDialogNetworkConfig::slotConnectionBroken()
192 kdDebug(11001) << k_funcinfo << endl;
193 setConnected(
false,
false);
194 KMessageBox::error(
this, i18n(
"Cannot connect to the network"));
197 void KGameDialogNetworkConfig::setConnected(
bool connected,
bool master)
200 d->mNetworkLabel->setText(i18n(
"Network status: No Network"));
201 d->mInitConnection->setEnabled(
true);
202 d->mDisconnectButton->setEnabled(
false);
206 d->mNetworkLabel->setText(i18n(
"Network status: You are MASTER"));
208 d->mNetworkLabel->setText(i18n(
"Network status: You are connected"));
210 d->mInitConnection->setEnabled(
false);
211 d->mDisconnectButton->setEnabled(
true);
214 void KGameDialogNetworkConfig::submitToKGame(
KGame* ,
KPlayer* )
218 void KGameDialogNetworkConfig::setKGame(
KGame* g)
225 setConnected(game()->isNetwork(), game()->isMaster());
228 void KGameDialogNetworkConfig::setDefaultNetworkInfo(
const TQString& host,
unsigned short int port,
bool server)
230 d->mDefaultPort = port;
231 d->mDefaultHost = host;
232 d->mDefaultServer = server;
234 d->mConnect->setHost(host);
235 d->mConnect->setPort(port);
237 d->mConnect->setDefault(0);
239 d->mConnect->setDefault(1);
243 void KGameDialogNetworkConfig::setDiscoveryInfo(
const TQString& type,
const TQString& name)
245 d->mConnect->setType(type);
246 d->mConnect->setName(name);
250 class KGameDialogGeneralConfigPrivate
253 KGameDialogGeneralConfigPrivate()
261 TQVBoxLayout* mTopLayout;
264 KGameDialogGeneralConfig::KGameDialogGeneralConfig(TQWidget* parent,
bool initializeGUI)
268 d =
new KGameDialogGeneralConfigPrivate;
271 d->mTopLayout =
new TQVBoxLayout(
this, KDialog::marginHint(), KDialog::spacingHint());
272 d->mTopLayout->setAutoAdd(
true);
274 TQWidget* nameWidget =
new TQWidget(
this);
275 TQHBoxLayout* l =
new TQHBoxLayout(nameWidget);
276 TQLabel* nameLabel =
new TQLabel(i18n(
"Your name:"), nameWidget);
277 l->addWidget(nameLabel);
278 d->mName =
new TQLineEdit(nameWidget);
279 l->addWidget(d->mName);
283 KGameDialogGeneralConfig::~KGameDialogGeneralConfig()
285 kdDebug(11001) << k_funcinfo << endl;
289 void KGameDialogGeneralConfig::setPlayerName(
const TQString& name)
292 d->mName->setText(name);
296 TQString KGameDialogGeneralConfig::playerName()
const 298 return d->mName ? d->mName->text() : TQString();
301 void KGameDialogGeneralConfig::setOwner(
KPlayer* p)
304 owner()->disconnect(
this);
314 setPlayerName(p->
name());
318 void KGameDialogGeneralConfig::setKGame(
KGame* g)
329 void KGameDialogGeneralConfig::setAdmin(
bool admin)
336 void KGameDialogGeneralConfig::submitToKGame(
KGame* g,
KPlayer* p)
348 if (!prop || !p || p != owner()) {
351 switch (prop->
id()) {
352 case KGamePropertyBase::IdName:
353 setPlayerName(p->
name());
360 class KGameDialogMsgServerConfigPrivate
363 KGameDialogMsgServerConfigPrivate()
368 changeMaxClients = 0;
376 TQVBoxLayout* senderLayout;
377 TQHBoxLayout* localLayout;
379 TQPushButton* changeMaxClients;
380 TQPushButton* changeAdmin;
381 TQPushButton* removeClient;
391 KGameDialogMsgServerConfig::KGameDialogMsgServerConfig(TQWidget* parent)
394 d =
new KGameDialogMsgServerConfigPrivate;
396 TQVBoxLayout* topLayout =
new TQVBoxLayout(
this, KDialog::marginHint(), KDialog::spacingHint());
397 d->senderLayout =
new TQVBoxLayout(topLayout);
398 d->localLayout =
new TQHBoxLayout(topLayout);
401 KGameDialogMsgServerConfig::~KGameDialogMsgServerConfig()
403 kdDebug(11001) << k_funcinfo << endl;
407 void KGameDialogMsgServerConfig::setKGame(
KGame* g)
418 setHasMsgServer(game()->messageServer());
422 void KGameDialogMsgServerConfig::slotChangeMaxClients()
425 kdError(11001) << k_funcinfo <<
": no valid game object available!" << endl;
428 if (!game()->isAdmin()) {
429 kdError(11001) << k_funcinfo <<
": only ADMIN is allowed to call this!" << endl;
435 TQDialog* dialog =
new TQDialog();
436 dialog->setCaption(i18n(
"Maximal Number of Clients"));
437 TQHBoxLayout* l =
new TQHBoxLayout(dialog, KDialog::marginHint(), KDialog::spacingHint());
440 (void)
new TQLabel(i18n(
"Maximal number of clients (-1 = infinite):"), dialog);
441 TQLineEdit* edit =
new TQLineEdit(dialog);
443 if (dialog->exec() == TQDialog::Accepted) {
445 max = edit->text().toInt(&ok);
447 game()->setMaxClients(max);
453 void KGameDialogMsgServerConfig::slotRemoveClient()
457 void KGameDialogMsgServerConfig::slotChangeAdmin()
460 kdError(11001) << k_funcinfo <<
": no valid game object available!" << endl;
464 kdError(11001) << k_funcinfo <<
": only ADMIN is allowed to call this!" << endl;
468 TQ_UINT32 newAdmin = 0;
470 game()->electAdmin(newAdmin);
473 void KGameDialogMsgServerConfig::removeClient(TQ_UINT32 )
478 void KGameDialogMsgServerConfig::setAdmin(
bool a)
490 d->changeMaxClients =
new TQPushButton(i18n(
"Change Maximal Number of Clients"),
this);
491 connect(d->changeMaxClients, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotChangeMaxClients()));
492 d->changeAdmin =
new TQPushButton(i18n(
"Change Admin"),
this);
493 connect(d->changeAdmin, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotChangeAdmin()));
494 d->removeClient =
new TQPushButton(i18n(
"Remove Client with All Players"),
this);
495 connect(d->removeClient, TQ_SIGNAL(pressed()),
this, TQ_SLOT(slotRemoveClient()));
496 d->senderLayout->addWidget(d->changeMaxClients);
497 d->senderLayout->addWidget(d->changeAdmin);
498 d->senderLayout->addWidget(d->removeClient);
500 if (d->changeMaxClients) {
501 delete d->changeMaxClients;
502 d->changeMaxClients = 0;
504 if (d->changeAdmin) {
505 delete d->changeAdmin;
508 if (d->removeClient) {
509 delete d->removeClient;
512 d->noAdmin =
new TQLabel(i18n(
"Only the admin can configure the message server!"),
this);
513 d->senderLayout->addWidget(d->noAdmin);
518 void KGameDialogMsgServerConfig::setHasMsgServer(
bool has)
523 d->noMaster =
new TQLabel(i18n(
"You don't own the message server"),
this);
524 d->localLayout->addWidget(d->noMaster);
539 class KGameDialogChatConfigPrivate
542 KGameDialogChatConfigPrivate()
550 KGameDialogChatConfig::KGameDialogChatConfig(
int chatMsgId, TQWidget* parent)
553 d =
new KGameDialogChatConfigPrivate;
554 TQVBoxLayout* topLayout =
new TQVBoxLayout(
this, KDialog::marginHint(), KDialog::spacingHint());
555 topLayout->setAutoAdd(
true);
556 TQHGroupBox* b =
new TQHGroupBox(i18n(
"Chat"),
this);
557 d->mChat =
new KGameChat(0, chatMsgId, b);
560 KGameDialogChatConfig::~KGameDialogChatConfig()
562 kdDebug(11001) << k_funcinfo << endl;
569 d->mChat->setKGame(game());
584 d->mChat->setFromPlayer(owner());
590 class KGameDialogConnectionConfigPrivate
593 KGameDialogConnectionConfigPrivate()
598 TQPtrDict<KPlayer> mItem2Player;
599 TDEListBox* mPlayerBox;
602 KGameDialogConnectionConfig::KGameDialogConnectionConfig(TQWidget* parent)
606 d =
new KGameDialogConnectionConfigPrivate;
607 TQVBoxLayout* topLayout =
new TQVBoxLayout(
this, KDialog::marginHint(), KDialog::spacingHint());
608 topLayout->setAutoAdd(
true);
609 TQHGroupBox* b =
new TQHGroupBox(i18n(
"Connected Players"),
this);
610 d->mPlayerBox =
new TDEListBox(b);
611 setMinimumHeight(100);
614 KGameDialogConnectionConfig::~KGameDialogConnectionConfig()
616 kdDebug(11001) << k_funcinfo << endl;
621 void KGameDialogConnectionConfig::setKGame(
KGame* g)
624 disconnect(game(), 0,
this, 0);
630 connect(game(), TQ_SIGNAL(signalPlayerJoinedGame(
KPlayer*)),
631 this, TQ_SLOT(slotPlayerJoinedGame(
KPlayer*)));
632 connect(game(), TQ_SIGNAL(signalPlayerLeftGame(
KPlayer*)),
633 this, TQ_SLOT(slotPlayerLeftGame(
KPlayer*)));
635 KGame::KGamePlayerList l = *game()->playerList();
636 for (
KPlayer* p = l.first(); p; p = l.next()) {
637 slotPlayerJoinedGame(p);
642 void KGameDialogConnectionConfig::setOwner(
KPlayer* p)
647 void KGameDialogConnectionConfig::setAdmin(
bool a)
653 disconnect(game(), TQ_SIGNAL(executed(TQListBoxItem*)),
this, 0);
657 connect(d->mPlayerBox, TQ_SIGNAL(executed(TQListBoxItem*)),
this,
658 TQ_SLOT(slotKickPlayerOut(TQListBoxItem*)));
662 TQListBoxItem* KGameDialogConnectionConfig::item(
KPlayer* p)
const 664 TQPtrDictIterator<KPlayer> it(d->mItem2Player);
665 while (it.current()) {
666 if (it.current() == p) {
667 return (TQListBoxItem*)it.currentKey();
674 void KGameDialogConnectionConfig::slotClearPlayers()
676 TQPtrDictIterator<KPlayer> it(d->mItem2Player);
677 while (it.current()) {
678 slotPlayerLeftGame(it.current());
682 if (d->mItem2Player.count() > 0) {
683 kdWarning(11001) << k_funcinfo <<
": itemList wasn't cleared properly" << endl;
684 d->mItem2Player.clear();
686 if (d->mPlayerBox->count() > 0) {
687 kdWarning(11001) << k_funcinfo <<
": listBox wasn't cleared properly" << endl;
688 d->mPlayerBox->clear();
693 void KGameDialogConnectionConfig::slotPlayerJoinedGame(
KPlayer* p)
696 kdError(11001) << k_funcinfo <<
": Cannot add NULL player" << endl;
698 if (d->mItem2Player[p]) {
699 kdError(11001) << k_funcinfo <<
": attempt to double add player" << endl;
702 kdDebug(11001) << k_funcinfo <<
": add player " << p->
id() << endl;
703 TQListBoxText* t =
new TQListBoxText(p->
name());
704 d->mItem2Player.insert(t, p);
705 d->mPlayerBox->insertItem(t);
712 void KGameDialogConnectionConfig::slotPlayerLeftGame(
KPlayer* p)
717 kdError(11001) << k_funcinfo <<
": cannot find " << p->
id()
718 <<
" in list" << endl;
721 d->mPlayerBox->removeItem(d->mPlayerBox->index(item(p)));
725 void KGameDialogConnectionConfig::slotKickPlayerOut(TQListBoxItem* item)
727 kdDebug(11001) <<
"kick player out" << endl;
728 KPlayer* p = d->mItem2Player[item];
730 kdError(11001) <<
"invalid item selected - no player found" << endl;
734 kdWarning(11001) <<
"no game set" << endl;
738 kdDebug(11001) <<
"Only the ADMIN can kick players" << endl;
742 kdDebug(11001) <<
"you cannot kick the ADMIN" << endl;
746 if (KMessageBox::questionYesNo(
this, i18n(
"Do you want to ban player \"%1\" from the game?").arg(
747 p->
name()), TQString(), i18n(
"Ban Player"), i18n(
"Do Not Ban")) == KMessageBox::Yes) {
748 kdDebug(11001) <<
"will remove player " << p << endl;
749 game()->removePlayer(p);
752 kdDebug(11001) <<
"will NOT remove player " << p << endl;
758 if(prop->
id() == KGamePropertyBase::IdName) {
759 TQListBoxText* old = 0;
760 TQPtrDictIterator<KPlayer> it(d->mItem2Player);
761 while (it.current() && !old) {
762 if (it.current() == player) {
763 old = (TQListBoxText*)it.currentKey();
767 TQListBoxText* t =
new TQListBoxText(player->
name());
768 d->mPlayerBox->changeItem(t, d->mPlayerBox->index(old));
769 d->mItem2Player.remove(old);
770 d->mItem2Player.insert(t, player);
virtual const TQString & name() const
TQ_UINT32 id() const
Returns the id of the player.
Base class for a game player.
void setName(const TQString &name)
Sets the name of the player.
Base class of KGameProperty.
The main KDE game object.
A Chat widget for KGame-based games.