summaryrefslogtreecommitdiffstats
path: root/kbiff/kbiff.cpp
blob: 185a8f087fa01c4e966ab1262054bb977ae1be59 (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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
 * kbiff.cpp
 * Copyright (C) 1999-2008 Kurt Granroth <granroth@kde.org>
 *
 * This file contains the implementation of the main KBiff
 * widget
 */
#include "kbiff.h"
#include "kbiff.moc"
#include <ntqmovie.h>
#include <ntqtooltip.h>

#include <kaudioplayer.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>
#include <kprocess.h>
#include <krun.h>
#include <twin.h>

#include "setupdlg.h"
#include "notify.h"
#include "status.h"
#include "led.h"

#include <unistd.h>

#include <dcopclient.h>

KBiff::KBiff(DCOPClient *client_, TQWidget *parent_)
    : DCOPObjectProxy(client_),
      TQLabel(parent_),
      statusTimer(0),
      status(0),
      statusChanged(true),
      mled( new Led("mled") )
{
    setBackgroundMode(X11ParentRelative);

    setAutoResize(true);
    setMargin(0);
    setAlignment(AlignLeft | AlignTop);

    // enable the session management stuff
    connect(kapp, SIGNAL(saveYourself()), this, SLOT(saveYourself()));

    // nuke the list stuff when removed
    monitorList.setAutoDelete(true);
    notifyList.setAutoDelete(true);
    statusList.setAutoDelete(true);

    // register with DCOP
    registerMe(client_);

    reset();
}

KBiff::~KBiff()
{
    monitorList.clear();
    notifyList.clear();
    statusList.clear();
    delete mled;

    // we no longer want to be registered
    DCOPClient *client = kapp->dcopClient();
    TQCString proxy = TQCString("kbiff-") + TQCString().setNum(getpid());
    if (client->isApplicationRegistered(proxy) == true)
    {
        TQByteArray params;
        TQDataStream ds(params, IO_WriteOnly);
        ds << proxy;
        client->send("kbiff", "kbiff", "proxyDeregister(TQString)", params);
    }
    client->detach();
}

void KBiff::processSetup(const KBiffSetup* setup_, bool run_)
{
    // General settings
    isSecure    = setup_->getSecure();
    profile     = setup_->getProfile();
    mailClient  = setup_->getMailClient();
    sessions    = setup_->getSessionManagement();
    skipcheck   = setup_->getCheckStartup();
    noMailIcon  = setup_->getNoMailIcon();
    newMailIcon = setup_->getNewMailIcon();
    oldMailIcon = setup_->getOldMailIcon();
    noConnIcon  = setup_->getNoConnIcon();
    stoppedIcon = setup_->getStoppedIcon();

    // New mail
    systemBeep     = setup_->getSystemBeep();
    runCommand     = setup_->getRunCommand();
    runCommandPath = setup_->getRunCommandPath();
    runResetCommand     = setup_->getRunResetCommand();
    runResetCommandPath = setup_->getRunResetCommandPath();
    playSound      = setup_->getPlaySound();
    playSoundPath  = setup_->getPlaySoundPath();
    notify         = setup_->getNotify();
    dostatus       = setup_->getStatus();

    // if we aren't going the status route, we should at least
    // provide a tooltip!
    if (dostatus == false)
        TQToolTip::add(this, profile);
    else
        TQToolTip::remove(this);

    // set all the new mailboxes
    setMailboxList(setup_->getMailboxList(), setup_->getPoll());

    // change the dock state if necessary
    if (docked != setup_->getDock())
        dock();

    if (run_ && !skipcheck)
        start();
    skipcheck = false;

    // handle session management disabling
    if (sessions == false)
    {
      disconnect(this, SLOT(saveYourself()));
      kapp->disableSessionManagement();
    }

    // if we are going to be doing status, we might as well create
    // one now
    if ( dostatus )
    {
      statusList.clear();
      KBiffMonitor *monitor;
      for (monitor = monitorList.first(); monitor; monitor = monitorList.next())
      {
        statusList.append(new KBiffStatusItem(monitor->getMailboxKey(),
                                              monitor->newMessages(),
                                              monitor->curMessages()));
      }
      if (status)
      {
        status->hide();
        delete status;
        status = 0;
      }
      status = new KBiffStatus(this, profile, statusList);
    }

    delete setup_;
}

void KBiff::setMailboxList(const TQList<KBiffMailbox>& mailbox_list, unsigned int poll)
{
    TQList<KBiffMailbox> tmp_list = mailbox_list;

    myMUTEX = true;
    if (isRunning())
        stop();
    monitorList.clear();
    
    KBiffMailbox *mbox;
    for (mbox = tmp_list.first(); mbox != 0; mbox = tmp_list.next())
    {
        KBiffURL *url = &(mbox->url);
        KBiffMonitor *monitor = new KBiffMonitor();
        monitor->setMailbox(*url);
        monitor->setPollInterval(poll);
        monitor->setMailboxKey(mbox->key);
        connect(monitor, SIGNAL(signal_newMail(const int, const TQString&)),
                this, SLOT(haveNewMail(const int, const TQString&)));
        connect(monitor, SIGNAL(signal_currentStatus(const int, const TQString&, const KBiffMailState)),
                this, SLOT(currentStatus(const int, const TQString&, const KBiffMailState)));
        connect(monitor, SIGNAL(signal_noMail()), this, SLOT(displayPixmap()));
        connect(monitor, SIGNAL(signal_noMail()),
                this, SLOT(haveNoNewMail()));
        connect(monitor, SIGNAL(signal_oldMail()), this, SLOT(displayPixmap()));
        connect(monitor, SIGNAL(signal_oldMail()),
                this, SLOT(haveNoNewMail()));
        connect(monitor, SIGNAL(signal_noConn()), this, SLOT(displayPixmap()));
        connect(monitor, SIGNAL(signal_noConn()),
                this, SLOT(haveNoNewMail()));
        connect(monitor, SIGNAL(signal_invalidLogin(const TQString&)),
                this, SLOT(invalidLogin(const TQString&)));
        connect(monitor, SIGNAL(signal_fetchMail(const TQString&)),
                this, SLOT(slotLaunchFetchClient(const TQString&)));
        monitorList.append(monitor);
    }
    myMUTEX = false;
}

bool KBiff::isDocked() const
{
    return docked;
}

void KBiff::readSessionConfig()
{
    TDEConfig *config = kapp->sessionConfig();

    config->setGroup("KBiff");

    profile = config->readEntry("Profile", "Inbox");
    docked = config->readBoolEntry("IsDocked", false);
    bool run = config->readBoolEntry("IsRunning", true);

    KBiffSetup *setup_dlg = new KBiffSetup(profile);
    processSetup(setup_dlg, run);
}

///////////////////////////////////////////////////////////////////////////
// Protected Virtuals
///////////////////////////////////////////////////////////////////////////
void KBiff::mousePressEvent(TQMouseEvent *e)
{
    // regardless of which button, get rid of the status box
    if (status)
        status->hide();

    // also, ditch the timer
    if (statusTimer)
    {
        statusTimer->stop();
        delete statusTimer;
        statusTimer = 0;
    }

    // check if this is a right click
    if(e->button() == RightButton)
    {
        // popup the context menu
        popupMenu();
    }
    else
    {
        // execute the command
        slotLaunchMailClient();

        readPop3MailNow();
    }
}

void KBiff::enterEvent(TQEvent *e)
{
    TQLabel::enterEvent(e);

    // return now if the user doesn't want this feature.
    // *sniff*.. the ingrate.. I worked so hard on this, too... *sob*
    if (dostatus == false)
        return;

    // don't do anything if we already have a timer
    if (statusTimer)
        return;

    // popup the status in one second
    statusTimer = new TQTimer(this);
    connect(statusTimer, SIGNAL(timeout()), this, SLOT(popupStatus()));

    statusTimer->start(1000, true);
}

void KBiff::leaveEvent(TQEvent *e)
{
    TQLabel::leaveEvent(e);

    // stop the timer if it is going
    if (statusTimer)
    {
        statusTimer->stop();
        delete statusTimer;
        statusTimer = 0;
    }

    // get rid of the status box if it is activated
    if (status)
        status->hide();
}

void KBiff::popupStatus()
{
    // if we don't get rid of the timer, then the very next
    // time we float over the icon, the status box will
    // *not* be activated!
    if (statusTimer)
    {
        statusTimer->stop();
        delete statusTimer;
        statusTimer = 0;
    }

    if (statusChanged)
    {
        statusList.clear();
        KBiffMonitor *monitor;
        for(monitor = monitorList.first(); monitor; monitor = monitorList.next())
        {
            statusList.append(new KBiffStatusItem(monitor->getMailboxKey(), monitor->newMessages(), monitor->curMessages()));
        }
        statusChanged = false;
    }

    status->updateListView(statusList);
    status->popup(TQCursor::pos());
}

bool KBiff::isGIF8x(const TQString& file_name)
{

    /* The first test checks if we can open the file */
    TQFile gif8x(file_name);
    if (gif8x.open(IO_ReadOnly) == false)
        return false;

    /**
     * The GIF89 format specifies that the first five bytes of
     * the file shall have the characters 'G' 'I' 'F' '8' '9'.
     * The GIF87a format specifies that the first six bytes
     * shall read 'G' 'I' 'F' '8' '7' 'a'.  Knowing that, we
     * shall read in the first six bytes and test away.
     */
    char header[6];
    int bytes_read = gif8x.readBlock(header, 6);

    /* Close the file just to be nice */
    gif8x.close();

    /* If we read less than 6 bytes, then its definitely not GIF8x */
    if (bytes_read < 6)
        return false;

    /* Now test for the magical GIF8(9|7a) */
    if (header[0] == 'G' &&
        header[1] == 'I' &&
        header[2] == 'F' &&
        header[3] == '8' &&
       (header[4] == '9' || (header[4] == '7' &&
                             header[5] == 'a')))
    {
        /* Success! */
        return true;
    }

    /* Apparently not GIF8(9|7a) */
    return false;
}

///////////////////////////////////////////////////////////////////////////
// Protected Slots
///////////////////////////////////////////////////////////////////////////
void KBiff::saveYourself()
{
    if (sessions)
    {
        TDEConfig *config = kapp->sessionConfig();
        config->setGroup("KBiff");

        config->writeEntry("Profile", profile);
        config->writeEntry("IsDocked", docked);
        config->writeEntry("IsRunning", isRunning());

        config->sync();

    }
}

void KBiff::invokeHelp()
{
    kapp->invokeHelp();
}

void KBiff::displayPixmap()
{
    if (myMUTEX)
        return;

    // we will try to deduce the pixmap (or gif) name now.  it will
    // vary depending on the dock and mail state
    TQString pixmap_name;
    bool has_new = false, has_old = false, has_no = true, has_noconn = false;
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0 && has_new == false;
          monitor = monitorList.next())
    {
        switch (monitor->getMailState())
        {
            case NoMail:
                has_no = true;
                break;
            case NewMail:
                has_new = true;
                break;
            case OldMail:
                has_old = true;
                break;
            case NoConn:
                has_noconn = true;
                break;
            default:
                has_no = true;
                break;
        }
    }

    if ( !isRunning() )
    {
        pixmap_name = stoppedIcon;
        mled->Off();
    }
    else if (has_new)
    {
        pixmap_name = newMailIcon;
        // turn on led for new mail, otherwise turn off
        mled->On();
    }
    else if (has_old)
    {
        pixmap_name = oldMailIcon;
        mled->Off();
    }
    else if (has_noconn)
    {
        pixmap_name = noConnIcon;
        mled->Off();
    }
    else
    {
        pixmap_name = noMailIcon;
        mled->Off();
    }

    if (docked)
    {
        // we need to check if this has path info encoded into it
        TQFileInfo info(pixmap_name);

        // if info.fileName() returns pixmap_name, then we no there
        // isn't any paths attached and we can just prepend our 'mini'
        if (info.fileName() == pixmap_name)
            pixmap_name.prepend("mini-");
        else
        {
            // so we have some path junk on it.  we get the filename
            // by itself, prepend our 'mini' and tack it onto the end
            // of the original dirpath.  simple
            TQString filename(info.fileName());
            filename.prepend("mini-");

            // we aren't guaranteed that the dirpath will end in a /
            // so we add one (an extra one won't hurt, in any case
            pixmap_name = info.dirPath() + "/" + filename;
        }
    }
    TQString filename = TDEGlobal::iconLoader()->iconPath( pixmap_name, TDEIcon::User );
    TQFileInfo file(filename);

    // at this point, we have the file to display.  so display it
    if (isGIF8x(file.absFilePath()))
        setMovie(TQMovie(file.absFilePath()));
    else
        setPixmap(TQPixmap(file.absFilePath()));
    adjustSize();
}

void KBiff::currentStatus(const int num, const TQString& the_mailbox, const KBiffMailState the_state)
{
  statusChanged = true;
  // iterate through all saved notify dialogs to see if "our" one is
  // currently being displayed
  KBiffNotify *notifyptr;
  for (notifyptr = notifyList.first();
       notifyptr != 0;
       notifyptr = notifyList.next())
  {
    // if this one is not visible, delete it from the list.  the only
    // way it will again become visible is if the haveNewMail slot
    // gets triggered
    if (notifyptr->isVisible() == false)
    {
      notifyList.remove();
    }
    else
    {
      // if this box is visible (active), we see if it is the one
      // we are looking for
      if (notifyptr->getMailbox() == the_mailbox)
      {
        // yep.  now, if there is new mail, we set the new number in
        // the dialog.  if it is any other state, we remove this
        // dialog from the list
        switch (the_state)
        {
          case NewMail:
            notifyptr->setNew(num);
            break;
          case OldMail:
          case NoMail:
          case NoConn:
          default:
            notifyList.remove();
            break;
        }
      }
    }
  }
}

void KBiff::haveNewMail(const int num, const TQString& the_mailbox)
{
    displayPixmap();

    // beep if we are allowed to
    if (systemBeep)
    {
        kapp->beep();
    }

    // run a command if we have to
    if (runCommand)
    {
        // make sure the command exists
        if (!runCommandPath.isEmpty())
        {
            executeCommand(replaceCommandArgs(runCommandPath));
        }
    }

    // play a sound if we have to
    if (playSound)
        slotPlaySound(playSoundPath);

    // notify if we must
    if (notify)
    {
        KBiffNotify *notify_dlg = new KBiffNotify(this, num, the_mailbox);
        connect(notify_dlg, SIGNAL(signalLaunchMailClient()),
                this, SLOT(slotLaunchMailClient()));
        notifyList.append(notify_dlg);
        notify_dlg->show();

        // half-hearted attempt to center this
        int x_pos = (TDEApplication::desktop()->width() - notify_dlg->width()) / 2;
        int y_pos = (TDEApplication::desktop()->height() - notify_dlg->height()) / 2;
        notify_dlg->move(x_pos, y_pos);
    }
}

void KBiff::haveNoNewMail()
{
    displayPixmap();

    // run a command if we have to
    if (runResetCommand)
    {
        // make sure the command exists
        if (!runResetCommandPath.isEmpty())
        {
            executeCommand(runResetCommandPath);
        }
    }
}

TQString KBiff::getURLWithNewMail()
{
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
            monitor != 0;
            monitor = monitorList.next())
    {
        if(monitor->getMailState() == NewMail)
            return monitor->getMailbox();
    }

    return monitorList.first()->getMailbox();
}

TQString KBiff::getMailBoxWithNewMail()
{
    TQString url(getURLWithNewMail());
    
    int slashPos = url.find('/');
    if(slashPos == -1)
      return url.mid(slashPos + 1);
    else
      return url.mid(url.find(':') + 1);
}

TQString KBiff::replaceCommandArgs(TQString cmdStr)
{
    bool expand = false;
    for(unsigned int i = 0; i < cmdStr.length(); i++)
    {
        if(expand)
        {
            expand = false;
            if(cmdStr[i] == 'm')
                cmdStr.replace(i - 1, 2, getMailBoxWithNewMail());
            else if(cmdStr[i] == 'u')
                cmdStr.replace(i - 1, 2, getURLWithNewMail());
            else if(cmdStr[i] == '%')
                cmdStr.replace(i - 1, 2, "%");

            continue;
        }

        if(cmdStr[i] == '%')
            expand = true;
    }

    return cmdStr;
}

void KBiff::dock()
{
    // destroy the old window
    if (this->isVisible())
    {
        this->hide();
        this->destroy(true, true);
        this->create(0, true, false);
        kapp->setMainWidget(this);

        // we don't want a "real" top widget if we are _going_ to
        // be docked.
        if (docked)
            kapp->setTopWidget(this);
        else
            kapp->setTopWidget(new TQWidget);
    }

    if (docked == false)
    {
        docked = true;

        // enable docking
        KWin::setSystemTrayWindowFor(this->winId(), 0);
    }
    else
        docked = false;

    // (un)dock it!
    this->show();
    TQTimer::singleShot(1000, this, SLOT(displayPixmap()));
}

void KBiff::setup()
{
    KBiffSetup* setup_dlg = new KBiffSetup(profile);

    if (setup_dlg->exec())
        processSetup(setup_dlg, true);
    else
        delete setup_dlg;
}

void KBiff::checkMailNow()
{
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0;
          monitor = monitorList.next())
    {
        monitor->checkMailNow();
    }
}

void KBiff::readMailNow()
{
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0;
          monitor = monitorList.next())
    {
        monitor->setMailboxIsRead();
    }
}

void KBiff::readPop3MailNow()
{
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0;
          monitor = monitorList.next())
    {
        if (monitor->getProtocol() == "pop3")
            monitor->setMailboxIsRead();
    }
}

void KBiff::stop()
{
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0;
          monitor = monitorList.next())
    {
        monitor->stop();
    }
    displayPixmap();
}

void KBiff::start()
{
    myMUTEX = true;
    KBiffMonitor *monitor;
    for (unsigned int i = 0; i < monitorList.count(); i++)
    {
        monitor = monitorList.at(i);
        monitor->start();
    }
    myMUTEX = false;
    displayPixmap();
}

///////////////////////////////////////////////////////////////////////////
// Protected Functions
///////////////////////////////////////////////////////////////////////////
void KBiff::popupMenu()
{
    TDEPopupMenu *popup = new TDEPopupMenu(0, "popup");
    popup->insertTitle(kapp->miniIcon(), profile);

    // if secure, disable everything but exit
    if (isSecure == false)
    {
        if (docked)
            popup->insertItem(i18n("&UnDock"), this, SLOT(dock()));
        else
            popup->insertItem(i18n("&Dock"), this, SLOT(dock()));
        popup->insertItem(i18n("&Setup..."), this, SLOT(setup()));
        popup->insertSeparator();
        popup->insertItem(i18n("&Help..."), this, SLOT(invokeHelp()));
        popup->insertSeparator();

        int check_id;
        check_id = popup->insertItem(i18n("&Check Mail Now"), this, SLOT(checkMailNow()));
        int read_id;
        read_id = popup->insertItem(i18n("&Read Mail Now"), this, SLOT(readMailNow()));

        if (isRunning())
        {
            popup->setItemEnabled(check_id, true);
            popup->setItemEnabled(read_id, true);
            popup->insertItem(i18n("&Stop"), this, SLOT(stop()));
        }
        else
        {
            popup->setItemEnabled(check_id, false);
            popup->setItemEnabled(read_id, false);
            popup->insertItem(i18n("&Start"), this, SLOT(start()));
        }
        popup->insertSeparator();
    }

    popup->insertItem(i18n("E&xit"), kapp, SLOT(quit()));

    popup->popup(TQCursor::pos());
}

void KBiff::reset()
{
    // reset all the member variables
    systemBeep     = true;
    runCommand     = false;
    runCommandPath = "";
    playSound      = false;
    playSoundPath  = "";
    notify         = true;
    dostatus       = true;

    noMailIcon  = "nomail";
    newMailIcon = "newmail";
    oldMailIcon = "oldmail";
    noConnIcon  = "noconn";
    stoppedIcon = "stopped";

    docked    = false;
    isSecure  = false;

    mailClient  = "xmutt -f +%m";

    myMUTEX = false;
}

bool KBiff::isRunning()
{
    bool is_running = false;
    KBiffMonitor *monitor;
    for (monitor = monitorList.first();
         monitor != 0;
          monitor = monitorList.next())
    {
        if (monitor->isRunning())
        {
            is_running = true;
            break;
        }
    }
    return is_running;
}

void KBiff::executeCommand(const TQString& command)
{
    KRun::runCommand(command);
}

void KBiff::slotLaunchFetchClient(const TQString& fetchClient)
{
    if (!fetchClient.isEmpty())
        executeCommand(fetchClient);
}

void KBiff::slotLaunchMailClient()
{
    if (!mailClient.isEmpty())
        executeCommand(replaceCommandArgs(mailClient));
}

void KBiff::slotPlaySound(const TQString& play_sound)
{
    // make sure something is specified
    if (!play_sound.isNull())
        KAudioPlayer::play(play_sound);
}

bool KBiff::process(const TQCString&, const TQCString& function,
                    const TQByteArray& data, TQCString& replyType,
                    TQByteArray &replyData)
{
    TQDataStream args(data, IO_ReadOnly);
    TQDataStream reply(replyData, IO_WriteOnly);
    TQString proxy;
    if (function == "proxyRegister(TQString)")
    {
        args >> proxy;
        proxyList.append(proxy);
        replyType = "void";
        return true;
    }

    else if (function == "proxyDeregister(TQString)")
    {
        args >> proxy;
        proxyList.remove(proxy);
        replyType = "void";
        return true;
    }

    else if (function == "hasMailbox(TQString)")
    {
        TQString mailbox;
        args >> mailbox;

        reply << (bool) findMailbox(mailbox, proxy);
        replyType = "bool";
        return true;
    }

    else if (function == "mailCount(TQString)")
    {
        reply << -1;
        replyType = "int";
        return true;
    }

    else if (function == "newMailCount(TQString)")
    {
        TQString mailbox;
        args >> mailbox;

        reply << newMailCount(mailbox);
        replyType = "int";
        return true;
    }


    return false;
}

int KBiff::newMailCount(const TQString& url)
{
    int newmail = -1;

    TQString proxy;
    if (findMailbox(url, proxy) == true)
    {
        if (proxy != TQString::null)
        {
            TQByteArray data;
            TQDataStream ds(data, IO_WriteOnly);
            ds << url;

            TQByteArray reply_data;
            TQCString reply_type;
            TQDataStream reply(reply_data, IO_ReadOnly);

            DCOPClient *dcc = kapp->dcopClient();
            if (dcc->call(proxy.ascii(), "kbiff",
                          "newMailCount(TQString)", data, reply_type,
                          reply_data) == true)
            {
                reply >> newmail;
            }
        }
        else
        {
            KBiffMonitor *monitor;
            for(monitor = monitorList.first(); monitor;
                monitor = monitorList.next())
            {
                if (monitor->getMailbox() == url)
                {
                    newmail = monitor->newMessages();
                    break;
                }
            }
        }
    }

    return newmail;
}

bool KBiff::findMailbox(const TQString& url, TQString& proxy)
{
    bool has_mailbox = false;
    KBiffMonitor *monitor;
    for(monitor = monitorList.first(); monitor; monitor = monitorList.next())
    {
        if (monitor->getMailbox() == url)
        {
            has_mailbox = true;
            break;
        }
    }
    if (has_mailbox == false)
    {
        TQByteArray data, replyData;
        TQCString replyType;
        TQDataStream ds(data, IO_WriteOnly);
        ds << url;
        // okay, now try to iterate through our proxies
        TQStringList::Iterator it = proxyList.begin();
        for ( ; it != proxyList.end(); it++)
        {
            DCOPClient *dcc = kapp->dcopClient();
            if (dcc->call(TQCString((*it).ascii()), "kbiff",
                        "hasMailbox(TQString)", data, replyType,
                        replyData) == true)
            {
                has_mailbox = true;
                proxy = *it;
                break;
            }
        }
    }

    return has_mailbox;
}

void KBiff::registerMe(DCOPClient *client)
{
    // we need to attach our client before doing anything
    client->attach();

    // if we aren't registered yet, then we will do so.. and be
    // responsible for all *other* kbiff requests, too!
    if (client->isApplicationRegistered("kbiff") == false)
        client->registerAs("kbiff");
    else
    {
        // okay, there is a running kbiff already.  we will let it
        // know that we are active and let it feed us requests
        TQCString proxy = TQCString("kbiff-") + TQCString().setNum(getpid());
        TQByteArray params, reply;
        TQCString reply_type;
        TQDataStream ds(params, IO_WriteOnly);
        ds << proxy;
        client->send("kbiff", "kbiff", "proxyRegister(TQString)", params);
        client->registerAs(TQCString(proxy));
    }
}

void KBiff::invalidLogin(const TQString& mailbox)
{
  TQString title(i18n("Invalid Login to %1").arg(mailbox));
  KMessageBox::sorry(0,
    i18n("I was not able to login to the remote server.\n"
         "This means that either the server is down or you have "
         "entered an incorrect username or password.\n"
         "Please make sure that you have entered the correct settings."),
    title);
}