~kubuntu-members/kfourinline/4.11

« back to all changes in this revision

Viewing changes to src/kgamedialogconfig.cpp

  • Committer: Albert Astals Cid
  • Date: 2012-12-24 18:16:20 UTC
  • Revision ID: git-v1:39cf736b6624c4cb0283a0f602f02c3af8a3307c
kfourinline only uses the network config, kill the rest

Show diffs side-by-side

added added

removed removed

Lines of Context:
255
255
 d->mConnect->setName(name);
256
256
}
257
257
 
258
 
/////////////////////////// KGameDialogGeneralConfig /////////////////////////
259
 
class KGameDialogGeneralConfigPrivate
260
 
{
261
 
public:
262
 
        KGameDialogGeneralConfigPrivate()
263
 
        {
264
 
                mTopLayout = 0;
265
 
                mName = 0;
266
 
        }
267
 
 
268
 
        KLineEdit* mName;
269
 
 
270
 
        QVBoxLayout* mTopLayout;
271
 
};
272
 
 
273
 
KGameDialogGeneralConfig::KGameDialogGeneralConfig(QWidget* parent, bool initializeGUI)
274
 
                : KGameDialogConfig(parent)
275
 
{
276
 
// kDebug(11001) << ": this=" << this;
277
 
 d = new KGameDialogGeneralConfigPrivate;
278
 
 
279
 
 if (initializeGUI) {
280
 
        d->mTopLayout = new QVBoxLayout(this);
281
 
        d->mTopLayout->setMargin( KDialog::marginHint() );
282
 
        d->mTopLayout->setSpacing( KDialog::spacingHint() );
283
 
 
284
 
        QWidget* nameWidget = new QWidget(this);
285
 
        d->mTopLayout->addWidget(nameWidget);
286
 
        QHBoxLayout* l = new QHBoxLayout(nameWidget);
287
 
        QLabel* nameLabel = new QLabel(i18n("Your name:"), nameWidget);
288
 
        l->addWidget(nameLabel);
289
 
        d->mName = new KLineEdit(nameWidget);
290
 
        l->addWidget(d->mName);
291
 
 }
292
 
}
293
 
 
294
 
KGameDialogGeneralConfig::~KGameDialogGeneralConfig()
295
 
{
296
 
 kDebug(11001) ;
297
 
 delete d;
298
 
}
299
 
 
300
 
void KGameDialogGeneralConfig::setPlayerName(const QString& name)
301
 
{
302
 
 if (d->mName) {
303
 
        d->mName->setText(name);
304
 
 }
305
 
}
306
 
 
307
 
QString KGameDialogGeneralConfig::playerName() const
308
 
{
309
 
 return d->mName ? d->mName->text() : QString();
310
 
}
311
 
 
312
 
void KGameDialogGeneralConfig::setOwner(KPlayer* p)
313
 
{
314
 
 if (owner()) {
315
 
        owner()->disconnect(this);
316
 
 }
317
 
 KGameDialogConfig::setOwner(p);
318
 
 if (!owner()) {
319
 
        // can this config be used at all?
320
 
        // maybe call hide()
321
 
        return;
322
 
 }
323
 
 connect(owner(), SIGNAL(signalPropertyChanged(KGamePropertyBase*,KPlayer*)),
324
 
                this, SLOT(slotPropertyChanged(KGamePropertyBase*,KPlayer*)));
325
 
 setPlayerName(p->name());
326
 
 //TODO: connect signalPropertyChanged and check for playername changes!
327
 
}
328
 
 
329
 
void KGameDialogGeneralConfig::setKGame(KGame* g)
330
 
{
331
 
 KGameDialogConfig::setKGame(g);
332
 
 if (!g) {
333
 
        // TODO
334
 
        // can this config be used at all?
335
 
        // maybe call hide()
336
 
        return;
337
 
 }
338
 
}
339
 
 
340
 
void KGameDialogGeneralConfig::setAdmin(bool admin)
341
 
{
342
 
 KGameDialogConfig::setAdmin(admin);
343
 
// enable/disable widgets
344
 
 
345
 
}
346
 
 
347
 
void KGameDialogGeneralConfig::submitToKGame(KGame* g, KPlayer* p)
348
 
{
349
 
//FIXME
350
 
 if (p) {
351
 
        p->setName(playerName());
352
 
 }
353
 
 if (g) {
354
 
 }
355
 
}
356
 
 
357
 
void KGameDialogGeneralConfig::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* p)
358
 
{
359
 
 if (!prop || !p || p != owner()) {
360
 
        return;
361
 
 }
362
 
 switch (prop->id()) {
363
 
        case KGamePropertyBase::IdName:
364
 
                setPlayerName(p->name());
365
 
                break;
366
 
        default:
367
 
                break;
368
 
 }
369
 
}
370
 
 
371
 
class KGameDialogMsgServerConfigPrivate
372
 
{
373
 
public:
374
 
        KGameDialogMsgServerConfigPrivate()
375
 
        {
376
 
                senderLayout = 0;
377
 
                localLayout = 0;
378
 
 
379
 
                changeMaxClients = 0;
380
 
                changeAdmin= 0;
381
 
                removeClient= 0;
382
 
                noAdmin = 0;
383
 
 
384
 
                noMaster = 0;
385
 
        }
386
 
 
387
 
        QVBoxLayout* senderLayout;
388
 
        QHBoxLayout* localLayout;
389
 
 
390
 
        QPushButton* changeMaxClients;
391
 
        QPushButton* changeAdmin;
392
 
        QPushButton* removeClient;
393
 
        QLabel* noAdmin;
394
 
 
395
 
        QLabel* noMaster;
396
 
};
397
 
 
398
 
 
399
 
// TODO: change ADMIN ID, remove CLIENTS, change MAXCLIENTS
400
 
// we do everything here with QPushButtons as we want to wait a moment before
401
 
// continuing - the message must be sent over network first
402
 
KGameDialogMsgServerConfig::KGameDialogMsgServerConfig(QWidget* parent)
403
 
                : KGameDialogConfig(parent)
404
 
{
405
 
 d = new KGameDialogMsgServerConfigPrivate;
406
 
 
407
 
 QVBoxLayout* topLayout = new QVBoxLayout(this);
408
 
 topLayout->setMargin( KDialog::marginHint() );
409
 
 topLayout->setSpacing( KDialog::spacingHint() );
410
 
 d->senderLayout = new QVBoxLayout;
411
 
 d->localLayout = new QHBoxLayout;
412
 
 topLayout->addLayout( d->senderLayout );
413
 
 topLayout->addLayout( d->localLayout );
414
 
}
415
 
 
416
 
KGameDialogMsgServerConfig::~KGameDialogMsgServerConfig()
417
 
{
418
 
 kDebug(11001) ;
419
 
 delete d;
420
 
}
421
 
 
422
 
void KGameDialogMsgServerConfig::setKGame(KGame* g)
423
 
{
424
 
 KGameDialogConfig::setKGame(g);
425
 
 //TODO display the ID of the admin if we aren't
426
 
 // connect(g, SIGNAL(signalAdminChanged(int)), this, SLOT(slotChangeIsAdmin(int)));//TODO
427
 
 if (!game()) {
428
 
        // we cannot do anything without a KGame object!
429
 
        setAdmin(false);
430
 
        return;
431
 
 }
432
 
 setAdmin(game()->isAdmin());
433
 
 setHasMsgServer(game()->messageServer());
434
 
}
435
 
 
436
 
 
437
 
void KGameDialogMsgServerConfig::slotChangeMaxClients()
438
 
{
439
 
 if (!game()) {
440
 
        kError(11001) << ": no valid game object available!";
441
 
        return;
442
 
 }
443
 
 if (!game()->isAdmin()) {
444
 
        kError(11001) << ": only ADMIN is allowed to call this!";
445
 
        return;
446
 
 }
447
 
 int max;
448
 
// edit->setText(QString::number()); // current max clients! //TODO
449
 
 
450
 
 QDialog* dialog = new QDialog();
451
 
 dialog->setWindowTitle(i18n("Maximal Number of Clients"));
452
 
 QHBoxLayout* l = new QHBoxLayout(dialog);
453
 
 l->setMargin( KDialog::marginHint() );
454
 
 l->setSpacing( KDialog::spacingHint() );
455
 
 
456
 
 l->addWidget(new QLabel(i18n("Maximal number of clients (-1 = infinite):"), dialog));
457
 
 KLineEdit* edit = new KLineEdit(dialog);//TODO: use KIntNumInput
458
 
 l->addWidget(edit);
459
 
// edit->setText(QString::number(max)); // current max clients! //TODO
460
 
 if (dialog->exec() == QDialog::Accepted) {
461
 
        bool ok;
462
 
        max = edit->text().toInt(&ok);
463
 
        if (ok) {
464
 
                game()->setMaxClients(max);
465
 
        }
466
 
 }
467
 
 
468
 
}
469
 
 
470
 
void KGameDialogMsgServerConfig::slotRemoveClient()
471
 
{
472
 
}
473
 
 
474
 
void KGameDialogMsgServerConfig::slotChangeAdmin()
475
 
{
476
 
 if (!game()) {
477
 
        kError(11001) << ": no valid game object available!";
478
 
        return;
479
 
 }
480
 
 if (!admin()) {
481
 
        kError(11001) << ": only ADMIN is allowed to call this!";
482
 
        return;
483
 
 }
484
 
 //TODO
485
 
 quint32 newAdmin = 0;
486
 
// newAdmin = ;
487
 
 game()->electAdmin(newAdmin);
488
 
}
489
 
 
490
 
void KGameDialogMsgServerConfig::removeClient(quint32 /*id*/)
491
 
{
492
 
//TODO
493
 
}
494
 
 
495
 
void KGameDialogMsgServerConfig::setAdmin(bool a)
496
 
{
497
 
 if (admin() == a) {
498
 
        // no need to do anything
499
 
        return;
500
 
 }
501
 
 KGameDialogConfig::setAdmin(a);
502
 
 if (admin()) {
503
 
        delete d->noAdmin;
504
 
        d->noAdmin = 0;
505
 
        d->changeMaxClients = new QPushButton(i18n("Change Maximal Number of Clients"), this);
506
 
        connect(d->changeMaxClients, SIGNAL(pressed()), this, SLOT(slotChangeMaxClients()));
507
 
        d->changeAdmin = new QPushButton(i18n("Change Admin"), this);
508
 
        connect(d->changeAdmin, SIGNAL(pressed()), this, SLOT(slotChangeAdmin()));
509
 
        d->removeClient = new QPushButton(i18n("Remove Client with All Players"), this);
510
 
        connect(d->removeClient, SIGNAL(pressed()), this, SLOT(slotRemoveClient()));
511
 
        d->senderLayout->addWidget(d->changeMaxClients);
512
 
        d->senderLayout->addWidget(d->changeAdmin);
513
 
        d->senderLayout->addWidget(d->removeClient);
514
 
 } else {
515
 
        delete d->changeMaxClients;
516
 
        d->changeMaxClients = 0;
517
 
        delete d->changeAdmin;
518
 
        d->changeAdmin = 0;
519
 
        delete d->removeClient;
520
 
        d->removeClient = 0;
521
 
        d->noAdmin = new QLabel(i18n("Only the admin can configure the message server!"), this);
522
 
        d->senderLayout->addWidget(d->noAdmin);
523
 
 }
524
 
}
525
 
 
526
 
 
527
 
void KGameDialogMsgServerConfig::setHasMsgServer(bool has)
528
 
{
529
 
 if (!has) {
530
 
        // delete all inputs
531
 
        if (!d->noMaster) {
532
 
                d->noMaster = new QLabel(i18n("You do not own the message server"), this);
533
 
                d->localLayout->addWidget(d->noMaster);
534
 
        }
535
 
        return;
536
 
 }
537
 
 if (d->noMaster) {
538
 
        delete d->noMaster;
539
 
        d->noMaster = 0;
540
 
 }
541
 
 //TODO
542
 
 // list all connections, data (max clients) and so on
543
 
 // cannot be done above (together with QPushButtons) as it is possible that
544
 
 // this client is ADMIN but not MASTER (i.e. doesn't own the messageserver)
545
 
}
546
 
 
547
 
 
548
 
class KGameDialogChatConfigPrivate
549
 
{
550
 
public:
551
 
        KGameDialogChatConfigPrivate()
552
 
        {
553
 
                mChat = 0;
554
 
        }
555
 
 
556
 
        KGameChat* mChat;
557
 
};
558
 
 
559
 
KGameDialogChatConfig::KGameDialogChatConfig(int chatMsgId, QWidget* parent)
560
 
                : KGameDialogConfig(parent)
561
 
{
562
 
 d = new KGameDialogChatConfigPrivate;
563
 
 QVBoxLayout* topLayout = new QVBoxLayout(this);
564
 
 topLayout->setMargin( KDialog::marginHint() );
565
 
 topLayout->setSpacing( KDialog::spacingHint() );
566
 
 QGroupBox* b = new QGroupBox(i18n("Chat"), this);
567
 
 topLayout->addWidget(b);
568
 
 QHBoxLayout* gboxLay = new QHBoxLayout(b);
569
 
 d->mChat = new KGameChat(0, chatMsgId, b);
570
 
 gboxLay->addWidget(d->mChat);
571
 
}
572
 
 
573
 
KGameDialogChatConfig::~KGameDialogChatConfig()
574
 
{
575
 
 kDebug(11001) ;
576
 
 delete d;
577
 
}
578
 
 
579
 
void KGameDialogChatConfig::setKGame(KGame* g)
580
 
{
581
 
 KGameDialogConfig::setKGame(g);
582
 
 d->mChat->setKGame(game());
583
 
 if (!game()) {
584
 
        hide();
585
 
 } else {
586
 
        show();
587
 
 }
588
 
}
589
 
 
590
 
void KGameDialogChatConfig::setOwner(KPlayer* p)
591
 
{
592
 
 KGameDialogConfig::setOwner(p);
593
 
 if (!owner()) {
594
 
        hide();
595
 
        return;
596
 
 }
597
 
 d->mChat->setFromPlayer(owner());
598
 
 show();
599
 
}
600
 
 
601
 
 
602
 
 
603
 
class KGameDialogConnectionConfigPrivate
604
 
{
605
 
public:
606
 
        KGameDialogConnectionConfigPrivate()
607
 
        {
608
 
                mPlayerBox = 0;
609
 
        }
610
 
 
611
 
        QHash<QListWidgetItem*, KPlayer*> mItem2Player;
612
 
        KListWidget* mPlayerBox;
613
 
};
614
 
 
615
 
KGameDialogConnectionConfig::KGameDialogConnectionConfig(QWidget* parent)
616
 
                : KGameDialogConfig(parent)
617
 
{
618
 
 //TODO: prevent player to ban himself
619
 
 d = new KGameDialogConnectionConfigPrivate;
620
 
 QVBoxLayout* topLayout = new QVBoxLayout(this);
621
 
 topLayout->setMargin( KDialog::marginHint() );
622
 
 topLayout->setSpacing( KDialog::spacingHint() );
623
 
 QGroupBox* b = new QGroupBox(i18n("Connected Players"), this);
624
 
 topLayout->addWidget(b);
625
 
 QHBoxLayout* gboxLay = new QHBoxLayout(b);
626
 
 d->mPlayerBox = new KListWidget(b);
627
 
 gboxLay->addWidget(d->mPlayerBox);
628
 
 setMinimumHeight(100);
629
 
}
630
 
 
631
 
KGameDialogConnectionConfig::~KGameDialogConnectionConfig()
632
 
{
633
 
 kDebug(11001) ;
634
 
 // d->mIem2Player.clear();
635
 
 delete d;
636
 
}
637
 
 
638
 
void KGameDialogConnectionConfig::setKGame(KGame* g)
639
 
{
640
 
 if (game()) {
641
 
        disconnect(game(), 0, this, 0);
642
 
 }
643
 
 KGameDialogConfig::setKGame(g);
644
 
 slotClearPlayers();
645
 
 if (game()) {
646
 
// react to changes in KGame::playerList()
647
 
        connect(game(), SIGNAL(signalPlayerJoinedGame(KPlayer*)),
648
 
                        this, SLOT(slotPlayerJoinedGame(KPlayer*)));
649
 
        connect(game(), SIGNAL(signalPlayerLeftGame(KPlayer*)),
650
 
                        this, SLOT(slotPlayerLeftGame(KPlayer*)));
651
 
 
652
 
  const QList<KPlayer*> *playerList = game()->playerList();
653
 
  for ( QList<KPlayer*>::const_iterator it = playerList->begin(); it!=playerList->end();++it )
654
 
  {
655
 
                slotPlayerJoinedGame(*it);
656
 
        }
657
 
 }
658
 
}
659
 
 
660
 
void KGameDialogConnectionConfig::setOwner(KPlayer* p)
661
 
{
662
 
 KGameDialogConfig::setOwner(p);
663
 
}
664
 
 
665
 
void KGameDialogConnectionConfig::setAdmin(bool a)
666
 
{
667
 
 if (!game()) {// not possible... in theory
668
 
        return;
669
 
 }
670
 
 if (admin()) {
671
 
        disconnect(game(), SIGNAL(executed(QListWidgetItem*)), this, 0);
672
 
 }
673
 
 KGameDialogConfig::setAdmin(a);
674
 
 if (admin()) {
675
 
        connect(d->mPlayerBox, SIGNAL(executed(QListWidgetItem*)), this,
676
 
                        SLOT(slotKickPlayerOut(QListWidgetItem*)));
677
 
 }
678
 
}
679
 
 
680
 
QListWidgetItem* KGameDialogConnectionConfig::item(KPlayer* p) const
681
 
{
682
 
 QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
683
 
 it = d->mItem2Player.constBegin();
684
 
 itEnd = d->mItem2Player.constEnd();
685
 
 for ( ; it != itEnd; ++it ) {
686
 
        if (it.value() == p) return it.key();
687
 
 }
688
 
 return 0;
689
 
}
690
 
 
691
 
void KGameDialogConnectionConfig::slotClearPlayers()
692
 
{
693
 
 QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
694
 
 it = d->mItem2Player.constBegin();
695
 
 itEnd = d->mItem2Player.constEnd();
696
 
 for ( ; it != itEnd; ++it ) {
697
 
        slotPlayerLeftGame(it.value());
698
 
 }
699
 
 
700
 
 if (d->mItem2Player.count() > 0) {
701
 
        kWarning(11001) << ": itemList wasn't cleared properly";
702
 
        d->mItem2Player.clear();
703
 
 }
704
 
 if (d->mPlayerBox->count() > 0) {
705
 
        kWarning(11001) << ": listBox wasn't cleared properly";
706
 
        d->mPlayerBox->clear();
707
 
 }
708
 
 
709
 
}
710
 
 
711
 
void KGameDialogConnectionConfig::slotPlayerJoinedGame(KPlayer* p)
712
 
{
713
 
 if (!p) {
714
 
        kError(11001) << ": Cannot add NULL player";
715
 
        return;
716
 
 }
717
 
 bool playerFound = false;
718
 
 QHash<QListWidgetItem*, KPlayer*>::const_iterator it, itEnd;
719
 
 it = d->mItem2Player.constBegin();
720
 
 itEnd = d->mItem2Player.constEnd();
721
 
 for ( ; !playerFound && it != itEnd; ++it ) playerFound = it.value() == p;
722
 
 if (playerFound) {
723
 
        kError(11001) << ": attempt to double add player";
724
 
        return;
725
 
 }
726
 
 kDebug(11001) << ": add player" << p->id();
727
 
 QListWidgetItem* t = new QListWidgetItem(p->name(), d->mPlayerBox);
728
 
 d->mItem2Player.insert(t, p);
729
 
 
730
 
 connect(p, SIGNAL(signalPropertyChanged(KGamePropertyBase*,KPlayer*)),
731
 
                this, SLOT(slotPropertyChanged(KGamePropertyBase*,KPlayer*)));
732
 
 
733
 
}
734
 
 
735
 
void KGameDialogConnectionConfig::slotPlayerLeftGame(KPlayer* p)
736
 
{
737
 
 // disconnect first
738
 
 this->disconnect(p);
739
 
 if (!item(p)) {
740
 
        kError(11001) << ": cannot find" << p->id()
741
 
                        << "in list";
742
 
        return;
743
 
 }
744
 
 d->mPlayerBox->takeItem(d->mPlayerBox->row(item(p)));
745
 
 
746
 
}
747
 
 
748
 
void KGameDialogConnectionConfig::slotKickPlayerOut(QListWidgetItem* item)
749
 
{
750
 
 kDebug(11001) << "kick player out";
751
 
 KPlayer* p = d->mItem2Player[item];
752
 
 if (!p) {
753
 
        kError(11001) << "invalid item selected - no player found";
754
 
        return;
755
 
 }
756
 
 if (!game()) {
757
 
        kWarning(11001) << "no game set";
758
 
        return;
759
 
 }
760
 
 if (!admin()) {
761
 
        kDebug(11001) << "Only the ADMIN can kick players";
762
 
        return;
763
 
 }
764
 
 if (p == owner()) { // you wanna ban the ADMIN ??
765
 
        kDebug(11001) << "you cannot kick the ADMIN";
766
 
        return;
767
 
 }
768
 
 
769
 
 if (KMessageBox::questionYesNo(this, i18n("Do you want to ban player \"%1\" from the game?",
770
 
                p->name()), QString(), KGuiItem (i18n("Ban Player")), KGuiItem (i18n("Do Not Ban"))) == KMessageBox::Yes) {
771
 
        kDebug(11001) << "will remove player" << p;
772
 
        game()->removePlayer(p);
773
 
//      d->mPlayerBox->removeItem(d->mPlayerBox->index(item)); // should be done by signalPlayerLeftGame
774
 
 } else {
775
 
        kDebug(11001) << "will NOT remove player" << p;
776
 
 }
777
 
}
778
 
 
779
 
void KGameDialogConnectionConfig::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* player)
780
 
{
781
 
 if(prop->id() == KGamePropertyBase::IdName) {
782
 
        QListWidgetItem* old = item(player);
783
 
        QListWidgetItem* t = new QListWidgetItem(player->name());
784
 
        int row = d->mPlayerBox->row(old);
785
 
        d->mPlayerBox->takeItem( row );
786
 
        d->mPlayerBox->insertItem(row, t);
787
 
        d->mItem2Player.remove(old);
788
 
        d->mItem2Player.insert(t, player);
789
 
 }
790
 
}
 
258
 
 
259
 
791
260