summaryrefslogtreecommitdiffstats
path: root/kbackgammon/engines/fibs/kbgfibschat.cpp
blob: c1eca59df2a403aced49218f2d769662c6b3422e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828

/* Yo Emacs, this -*- C++ -*-

  Copyright (C) 1999-2001 Jens Hoefkens
  jens@hoefkens.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

  $Id$

*/


#include "kbgfibschat.h"
#include "kbgfibschat.moc"

#include <tqstring.h>

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpopupmenu.h>
#include <tqregexp.h>
#include <tqfont.h>
#include <tqwhatsthis.h>
#include <tqdatetime.h>
#include <tqclipboard.h>
#include <tqsimplerichtext.h>
#include <tqregion.h>
#include <tqpalette.h>
#include <tqpainter.h>
#include <tqpoint.h>
#include <tqlistbox.h>
#include <tqiconset.h>
#include <tqstringlist.h>
#include <tqdict.h>

#include <klocale.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kstdaction.h>
#include <ktabctl.h>
#include <kaction.h>
#include <kiconloader.h>

#include "clip.h"
#include "version.h"


/*
 * Private utility class that might become more generally useful in
 * the future. Basically, it implements rich text TQListBox items.
 */
class KLBT : public TQListBoxText
{

public:

    /*
     * Constructor
     */
    KLBT(TQWidget *tqparent, const TQString &text = TQString(), const TQString &player = TQString())
        : TQListBoxText(text)
    {
        w = tqparent;
        n = new TQString(player);
        t = new TQSimpleRichText(text, w->font());

        // FIXME: this is not yet perfect
        t->setWidth(w->width()-20);
    }

    /*
     * Destructor
     */
    virtual ~KLBT()
    {
        delete t;
        delete n;
    }

    /*
     * Overloaded required members returning height
     */
    virtual int height(const TQListBox *) const
    {
        return (1+t->height());
    }

    /*
     * Overloaded required members returning width
     */
    virtual int width(const TQListBox *) const
    {
        return t->width();
    }

    /*
     * The context menu needs the name of the player. It's easier
     * than extracting it from the text.
     */
    TQString player() const
    {
        return *n;
    }

protected:

    /*
     * Required overloaded member to paint the text on the painter p.
     */
    virtual void paint(TQPainter *p)
    {
        t->draw(p, 1, 1, TQRegion(p->viewport()), w->tqcolorGroup());
    }

private:

    TQSimpleRichText *t;
    TQWidget *w;
    TQString *n;

};


class KBgChatPrivate
{
public:

    /*
     * Name of the users
     */
    TQString mName[2];

    /*
     * Hold and assemble info text
     */
    TQString mText;

    /*
     * Numbers of the private action list.
     */
    enum Privact {Inquire, InviteD, Invite1, Invite2, Invite3, Invite4,
                  Invite5, Invite6, Invite7, InviteR, InviteU, Silent,
                  Talk, Gag, Ungag, Cleargag, Copy, Clear, Close, MaxAction};

    /*
     * Available actions
     */
    KAction *mAct[MaxAction];

    /*
     * Context menu and invitation menu
     */
    TQPopupMenu *mChat, *mInvt;

    /*
     * list of users we do not want to hear shouting
     */
    TQStringList mGag;

    /*
     * Listbox needed by the setup dialog
     */
    TQListBox *mLb;

    /*
     * Internal ID to name mapping
     */
    TQDict<int> *mName2ID;

};


// == constructor, destructor ==================================================

/*
 * Constructor of the chat window.
 */
KBgChat::KBgChat(TQWidget *tqparent, const char *name)
    : KChat(tqparent, false)
{
    d = new KBgChatPrivate();
    KActionCollection* actions = new KActionCollection(this);

    d->mName[0] = TQString();
    d->mChat = 0;
    d->mInvt = new TQPopupMenu();

    setAutoAddMessages(false); // we get an echo from FIBS
    setFromNickname(i18n("%1 user").tqarg(PROG_NAME));

    if (!addSendingEntry(i18n("Kibitz to watchers and players"), CLIP_YOU_KIBITZ))
        kdDebug(10500) << "adding kibitz" << endl;
    if (!addSendingEntry(i18n("Whisper to watchers only"), CLIP_YOU_WHISPER))
        kdDebug(10500) << "adding whisper" << endl;

    connect(this, TQT_SIGNAL(rightButtonClicked(TQListBoxItem *, const TQPoint &)),
            this, TQT_SLOT(contextMenu(TQListBoxItem *, const TQPoint &)));
    connect(this, TQT_SIGNAL(signalSendMessage(int, const TQString &)),
            this, TQT_SLOT(handleCommand(int, const TQString &)));

    d->mName2ID = new TQDict<int>(17, true);
    d->mName2ID->setAutoDelete(true);

    /*
     * some eye candy :)
     */
    setIcon(kapp->miniIcon());
    setCaption(i18n("Chat Window"));

    TQWhatsThis::add(this, i18n("This is the chat window.\n\n"
                               "The text in this window is colored depending on whether "
                               "it is directed at you personally, shouted to the general "
                               "FIBS population, has been said by you, or is of general "
                               "interest. If you select the name of a player, the context "
                               "contains entries specifically geared towards that player."));
    /*
     * Define set of available actions
     */
    d->mAct[KBgChatPrivate::Inquire] = new KAction(i18n("Info On"),
                                                   TQIconSet(kapp->iconLoader()->loadIcon(
                                                                "help.xpm", KIcon::Small)),
                                                   0, TQT_TQOBJECT(this), TQT_SLOT(slotInquire()), actions);
    d->mAct[KBgChatPrivate::Talk]    = new KAction(i18n("Talk To"),
                                                   TQIconSet(kapp->iconLoader()->loadIcon(
                                                                PROG_NAME "-chat.png", KIcon::Small)),
                                                   0, TQT_TQOBJECT(this), TQT_SLOT(slotTalk()), actions);

    d->mAct[KBgChatPrivate::InviteD]  = new KAction(i18n("Use Dialog"),    0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInviteD()),  actions);
    d->mAct[KBgChatPrivate::Invite1]  = new KAction(i18n("1 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite1()),  actions);
    d->mAct[KBgChatPrivate::Invite2]  = new KAction(i18n("2 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite2()),  actions);
    d->mAct[KBgChatPrivate::Invite3]  = new KAction(i18n("3 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite3()),  actions);
    d->mAct[KBgChatPrivate::Invite4]  = new KAction(i18n("4 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite4()),  actions);
    d->mAct[KBgChatPrivate::Invite5]  = new KAction(i18n("5 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite5()),  actions);
    d->mAct[KBgChatPrivate::Invite6]  = new KAction(i18n("6 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite6()),  actions);
    d->mAct[KBgChatPrivate::Invite7]  = new KAction(i18n("7 Point Match"), 0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInvite7()),  actions);
    d->mAct[KBgChatPrivate::InviteU]  = new KAction(i18n("Unlimited"),     0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInviteU()),  actions);
    d->mAct[KBgChatPrivate::InviteR]  = new KAction(i18n("Resume"),        0, TQT_TQOBJECT(this),
                                                    TQT_SLOT(slotInviteR()),  actions);

    d->mAct[KBgChatPrivate::InviteD]->plug(d->mInvt);

    d->mInvt->insertSeparator();

    d->mAct[KBgChatPrivate::Invite1]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite2]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite3]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite4]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite5]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite6]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::Invite7]->plug(d->mInvt);

    d->mInvt->insertSeparator();

    d->mAct[KBgChatPrivate::InviteU]->plug(d->mInvt);
    d->mAct[KBgChatPrivate::InviteR]->plug(d->mInvt);

    d->mAct[KBgChatPrivate::Gag]      = new KAction(i18n("Gag"),   0, TQT_TQOBJECT(this), TQT_SLOT(slotGag()),     actions);
    d->mAct[KBgChatPrivate::Ungag]    = new KAction(i18n("Ungag"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotUngag()),   actions);
    d->mAct[KBgChatPrivate::Cleargag] = new KAction(i18n("Clear Gag List"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotCleargag()), actions);
    d->mAct[KBgChatPrivate::Copy]     = KStdAction::copy(TQT_TQOBJECT(this), TQT_SLOT(slotCopy()), actions);
    d->mAct[KBgChatPrivate::Clear]    = new KAction(i18n("Clear"),        0, TQT_TQOBJECT(this), TQT_SLOT(slotClear()), actions);
    d->mAct[KBgChatPrivate::Close]    = KStdAction::close(TQT_TQOBJECT(this), TQT_SLOT(hide()), actions);
    d->mAct[KBgChatPrivate::Silent]   = new KToggleAction(i18n("Silent"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotSilent()), actions);
}


/*
 * Destructor
 */
KBgChat::~KBgChat()
{
    delete d->mName2ID;
    delete d->mChat; // save to delete NULL pointers
    delete d->mInvt;
    delete d;
}


// == configuration handling ===================================================

/*
 * Restore the previously stored settings
 */
void KBgChat::readConfig()
{
    KConfig* config = kapp->config();
    config->setGroup("chat window");

    TQPoint pos(10, 10);

    pos = config->readPointEntry("ori", &pos);
    setGeometry(pos.x(), pos.y(), config->readNumEntry("wdt",460), config->readNumEntry("hgt",200));

    config->readBoolEntry("vis", false) ? show() : hide();

    ((KToggleAction *)d->mAct[KBgChatPrivate::Silent])->setChecked(config->readBoolEntry("sil", false));

    d->mGag = config->readListEntry("gag");
}

/*
 * Save the current settings to disk
 */
void KBgChat::saveConfig()
{
    KConfig* config = kapp->config();
    config->setGroup("chat window");

    config->writeEntry("ori", pos());
    config->writeEntry("hgt", height());
    config->writeEntry("wdt", width());
    config->writeEntry("vis", isVisible());

    config->writeEntry("sil", ((KToggleAction *)d->mAct[KBgChatPrivate::Silent])->isChecked());

    config->writeEntry("gag", d->mGag);
}


/*
 * Setup dialog page of the player list - allow the user to select the
 * columns to show
 *
 * FIXME: need to be able to set font here KChatBase::setBothFont(const TQFont& font)
 */
void KBgChat::getSetupPages(KTabCtl *nb, int space)
{
    /*
     * Main Widget
     * ===========
     */
    TQWidget *w = new TQWidget(nb);
    TQGridLayout *gl = new TQGridLayout(w, 2, 1, space);

    d->mLb = new TQListBox(w);
    d->mLb->setMultiSelection(true);

    d->mLb->insertStringList(d->mGag);

    TQLabel *info = new TQLabel(w);
    info->setText(i18n("Select users to be removed from the gag list."));

    TQWhatsThis::add(w, i18n("Select all the users you want "
                            "to remove from the gag list "
                            "and then click OK. Afterwards "
                            "you will again hear what they shout."));

    gl->addWidget(d->mLb, 0, 0);
    gl->addWidget(info, 1, 0);

    /*
     * put in the page
     * ===============
     */
    gl->activate();
    w->adjustSize();
    w->setMinimumSize(w->size());
    nb->addTab(w, i18n("&Gag List"));
}

/*
 * Remove all the selected entries from the gag list
 */
void KBgChat::setupOk()
{
    for (uint i = 0; i < d->mLb->count(); ++i) {
        if (d->mLb->isSelected(i))
            d->mGag.remove(d->mLb->text(i));
    }
    d->mLb->clear();
    d->mLb->insertStringList(d->mGag);
}

/*
 * Don't do anything
 */
void KBgChat::setupCancel()
{
    // empty
}

/*
 * By default, all players stay in the gag list
 */
void KBgChat::setupDefault()
{
    d->mLb->clearSelection();
}


// == various slots and functions ==============================================

/*
 * Overloaded member to create a TQListBoxItem for the chat window.
 */
TQListBoxItem* KBgChat::tqlayoutMessage(const TQString& fromName, const TQString& text)
{
    TQListBoxText* message = new KLBT(this, text, fromName);
    return message;
}

/*
 * Catch hide events, so the engine's menu can be update.
 */
void KBgChat::showEvent(TQShowEvent *e)
{
    TQFrame::showEvent(e);
    emit windowVisible(true);
}

/*
 * Catch hide events, so the engine's menu can be update.
 */
void KBgChat::hideEvent(TQHideEvent *e)
{
    emit windowVisible(false);
    TQFrame::hideEvent(e);
}

/*
 * At the beginning of a game, add the name to the list and switch to
 * kibitz mode.
 */
void KBgChat::startGame(const TQString &name)
{
    int *id = d->mName2ID->tqfind(d->mName[1] = name);
    if (!id) {
        id = new int(nextId());
        d->mName2ID->insert(name, id);
        addSendingEntry(i18n("Talk to %1").tqarg(name), *id);
    }
    setSendingEntry(CLIP_YOU_KIBITZ);
}

/*
 * At the end of a game, we switch to talk mode.
 */
void KBgChat::endGame()
{
    int *id = d->mName2ID->tqfind(d->mName[1]);
    if (id)
        setSendingEntry(*id);
    else
        setSendingEntry(SendToAll);
}

/*
 * Set the chat window ready to talk to name
 */
void KBgChat::fibsTalk(const TQString &name)
{
    int *id = d->mName2ID->tqfind(name);
    if (!id) {
        id = new int(nextId());
        d->mName2ID->insert(name, id);
        addSendingEntry(i18n("Talk to %1").tqarg(name), *id);
    }
    setSendingEntry(*id);
}

/*
 * Remove the player from the combo box when he/she logs out.
 */
void KBgChat::deletePlayer(const TQString &name)
{
    int *id = d->mName2ID->tqfind(name);
    if (id) {
        removeSendingEntry(*id);
        d->mName2ID->remove(name);
    }
}

/*
 * Take action when the user presses return in the line edit control.
 */
void KBgChat::handleCommand(int id, const TQString& msg)
{
    int realID = sendingEntry();

    switch (realID) {
    case SendToAll:
        emit fibsCommand("shout " + msg);
        break;
    case CLIP_YOU_KIBITZ:
        emit fibsCommand("kibitz " + msg);
        break;
    case CLIP_YOU_WHISPER:
        emit fibsCommand("whisper " + msg);
        break;
    default:
        TQDictIterator<int> it(*d->mName2ID);
        while (it.current()) {
            if (*it.current() == realID) {
                emit fibsCommand("tell " + it.currentKey() + " " + msg);
                return;
            }
            ++it;
        }
        kdDebug(10500) << "unrecognized ID in KBgChat::handleCommand" << endl;
    }
}


// == handle strings from the server ===========================================

/*
 * A message from the server that should be handled by us. If necessary,
 * we replace the CLIP number by a string and put the line into the window.
 *
 * This function emits the string in rich text format with the signal
 * personalMessage - again: the string contains rich text!
 */
void KBgChat::handleData(const TQString &msg)
{
    TQString clip = msg.left(msg.tqfind(' ')), user, cMsg = msg;
    TQDateTime date;

    bool flag = false;
    int cmd = clip.toInt(&flag);

    if (flag) {
        cMsg.tqreplace(0, cMsg.tqfind(' ')+1, "");

        user = cMsg.left(cMsg.tqfind(' '));

        switch (cmd) {
        case CLIP_SAYS:
            if (!d->mGag.tqcontains(user)) {
                cMsg = i18n("<u>%1 tells you:</u> %2").tqarg(user).tqarg(cMsg.tqreplace(TQRegExp("^" + user), ""));
                cMsg = "<font color=\"red\">" + cMsg + "</font>";
                emit personalMessage(cMsg);
            } else
                cMsg = "";
            break;

        case CLIP_SHOUTS:
            if ((!((KToggleAction *)d->mAct[KBgChatPrivate::Silent])->isChecked()) && (!d->mGag.tqcontains(user))) {
                cMsg = i18n("<u>%1 shouts:</u> %2").tqarg(user).tqarg(cMsg.tqreplace(TQRegExp("^" + user), ""));
                cMsg = "<font color=\"black\">" + cMsg + "</font>";
            } else
                cMsg = "";
            break;

        case CLIP_WHISPERS:
            if (!d->mGag.tqcontains(user)) {
                cMsg = i18n("<u>%1 whispers:</u> %2").tqarg(user).tqarg(cMsg.tqreplace(TQRegExp("^" + user), ""));
                cMsg = "<font color=\"red\">" + cMsg + "</font>";
                emit personalMessage(cMsg);
            } else
                cMsg = "";
            break;

        case CLIP_KIBITZES:
            if (!d->mGag.tqcontains(user)) {
                cMsg = i18n("<u>%1 kibitzes:</u> %2").tqarg(user).tqarg(cMsg.tqreplace(TQRegExp("^" + user), ""));
                cMsg = "<font color=\"red\">" + cMsg + "</font>";
                emit personalMessage(cMsg);
            } else
                cMsg = "";
            break;

        case CLIP_YOU_SAY:
            cMsg = i18n("<u>You tell %1:</u> %2").tqarg(user).tqarg(cMsg.tqreplace(TQRegExp("^" + user), ""));
            cMsg = "<font color=\"darkgreen\">" + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_YOU_SHOUT:
            cMsg = i18n("<u>You shout:</u> %1").tqarg(cMsg);
            cMsg = "<font color=\"darkgreen\">" + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_YOU_WHISPER:
            cMsg = i18n("<u>You whisper:</u> %1").tqarg(cMsg);
            cMsg = "<font color=\"darkgreen\">" + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_YOU_KIBITZ:
            cMsg = i18n("<u>You kibitz:</u> %1").tqarg(cMsg);
            cMsg = "<font color=\"darkgreen\">" + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_MESSAGE:
            user = cMsg.left(cMsg.tqfind(' ')+1);
            cMsg.remove(0, cMsg.tqfind(' ')+1);
            date.setTime_t(cMsg.left(cMsg.tqfind(' ')+1).toUInt());
            cMsg.remove(0, cMsg.tqfind(' '));
            cMsg = i18n("<u>User %1 left a message at %2</u>: %3").tqarg(user).tqarg(date.toString()).tqarg(cMsg);
            cMsg = "<font color=\"red\">" + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_MESSAGE_DELIVERED:
            cMsg = i18n("Your message for %1 has been delivered.").tqarg(user);
            cMsg = TQString("<font color=\"darkgreen\">") + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        case CLIP_MESSAGE_SAVED:
            cMsg = i18n("Your message for %1 has been saved.").tqarg(user);
            cMsg = TQString("<font color=\"darkgreen\">") + cMsg + "</font>";
            emit personalMessage(cMsg);
            user = TQString();
            break;

        default: // ignore the message
            return;
        }

    } else {

        /*
         * Special treatment for non-CLIP messages
         */
        if (cMsg.tqcontains(TQRegExp("^You say to yourself: "))) {
            cMsg.tqreplace(TQRegExp("^You say to yourself: "),
                         i18n("<u>You say to yourself:</u> "));
        } else {
            kdDebug(user.isNull(), 10500) << "KBgChat::handleData unhandled message: "
                                                  << cMsg.latin1() << endl;
            return;
        }
    }

    if (!cMsg.isEmpty())
        addMessage(user, cMsg);
}


// == context menu and related slots ===========================================

/*
 * RMB opens a context menu.
 */
void KBgChat::contextMenu(TQListBoxItem *i, const TQPoint &p)
{
    /*
     * Even if i is non-null, user might still be TQString()
     */
    d->mName[0] = (i == 0) ? TQString() : ((KLBT *)i)->player();
    d->mText = (i == 0) ? TQString() : ((KLBT *)i)->text();

    /*
     * Get a new context menu every time. Safe to delete the 0
     * pointer.
     */
    delete d->mChat; d->mChat = new TQPopupMenu();

    /*
     * Fill the context menu with actions
     */
    if (!d->mName[0].isNull()) {

        d->mAct[KBgChatPrivate::Talk]->setText(i18n("Talk to %1").tqarg(d->mName[0]));
        d->mAct[KBgChatPrivate::Talk]->plug(d->mChat);

        d->mAct[KBgChatPrivate::Inquire]->setText(i18n("Info on %1").tqarg(d->mName[0]));
        d->mAct[KBgChatPrivate::Inquire]->plug(d->mChat);

        // invite menu is always the same
        d->mChat->insertItem(i18n("Invite %1").tqarg(d->mName[0]), d->mInvt);

        d->mChat->insertSeparator();

        if (d->mGag.tqcontains(d->mName[0]) <= 0) {
            d->mAct[KBgChatPrivate::Gag]->setText(i18n("Gag %1").tqarg(d->mName[0]));
            d->mAct[KBgChatPrivate::Gag]->plug(d->mChat);
        } else {
            d->mAct[KBgChatPrivate::Ungag]->setText(i18n("Ungag %1").tqarg(d->mName[0]));
            d->mAct[KBgChatPrivate::Ungag]->plug(d->mChat);
        }
    }
    if (d->mGag.count() > 0)
        d->mAct[KBgChatPrivate::Cleargag]->plug(d->mChat);

    if ((d->mGag.count() > 0) || (!d->mName[0].isNull()))
        d->mChat->insertSeparator();

    d->mAct[KBgChatPrivate::Silent]->plug(d->mChat);

    d->mChat->insertSeparator();

    d->mAct[KBgChatPrivate::Copy ]->plug(d->mChat);
    d->mAct[KBgChatPrivate::Clear]->plug(d->mChat);
    d->mAct[KBgChatPrivate::Close]->plug(d->mChat);

    d->mChat->popup(p);
}

/*
 * Clear the gag list
 */
void KBgChat::slotCleargag()
{
    d->mGag.clear();

    TQString msg("<font color=\"blue\">");
    msg += i18n("The gag list is now empty.");
    msg += "</font>";

    addMessage(TQString(), msg);
}

/*
 * Gag the selected user
 */
void KBgChat::slotGag()
{
    d->mGag.append(d->mName[0]);

    TQString msg("<font color=\"blue\">");
    msg += i18n("You won't hear what %1 says and shouts.").tqarg(d->mName[0]);
    msg += "</font>";

    addMessage(TQString(), msg);
}

/*
 * Simple interface to the actual talk slot
 */
void KBgChat::slotTalk()
{
    fibsTalk(d->mName[0]);
}

/*
 * Remove selected user from gag list
 */
void KBgChat::slotUngag()
{
    d->mGag.remove(d->mName[0]);

    TQString msg("<font color=\"blue\">");
    msg += i18n("You will again hear what %1 says and shouts.").tqarg(d->mName[0]);
    msg += "</font>";

    addMessage(TQString(), msg);
}

/*
 * Get information on selected user
 */
void KBgChat::slotInquire()
{
    kdDebug(d->mName[0].isNull(), 10500) << "KBgChat::slotInquire: user == null" << endl;
    emit fibsCommand("whois " + d->mName[0]);
}

/*
 * Block all shouts from the chat window
 */
void KBgChat::slotSilent()
{
    TQString msg;
    if (((KToggleAction *)d->mAct[KBgChatPrivate::Silent])->isChecked())
        msg = "<font color=\"blue\">" + i18n("You will not hear what people shout.") + "</font>";
    else
        msg = "<font color=\"blue\">" + i18n("You will hear what people shout.") + "</font>";
    addMessage(TQString(), msg);
}

/*
 * Copy the selected line to the clipboard. Strip the additional HTML
 * from the text before copying.
 */
void KBgChat::slotCopy()
{
    d->mText.tqreplace(TQRegExp("<u>"), "");
    d->mText.tqreplace(TQRegExp("</u>"), "");
    d->mText.tqreplace(TQRegExp("</font>"), "");
    d->mText.tqreplace(TQRegExp("^.*\">"), "");

    kapp->clipboard()->setText(d->mText);
}

/*
 * Invite the selected player.
 */
void KBgChat::slotInviteD()
{
    kdDebug(d->mName[0].isNull(), 10500) << "KBgChat::slotInvite: user == null" << endl;
    emit fibsRequestInvitation(d->mName[0]);
}
void KBgChat::slotInvite1() { emit fibsCommand("invite " + d->mName[0] + " 1"); }
void KBgChat::slotInvite2() { emit fibsCommand("invite " + d->mName[0] + " 2"); }
void KBgChat::slotInvite3() { emit fibsCommand("invite " + d->mName[0] + " 3"); }
void KBgChat::slotInvite4() { emit fibsCommand("invite " + d->mName[0] + " 4"); }
void KBgChat::slotInvite5() { emit fibsCommand("invite " + d->mName[0] + " 5"); }
void KBgChat::slotInvite6() { emit fibsCommand("invite " + d->mName[0] + " 6"); }
void KBgChat::slotInvite7() { emit fibsCommand("invite " + d->mName[0] + " 7"); }

void KBgChat::slotInviteU() { emit fibsCommand("invite " + d->mName[0] + " unlimited"); }
void KBgChat::slotInviteR() { emit fibsCommand("invite " + d->mName[0]); }


// EOF