summaryrefslogtreecommitdiffstats
path: root/src/scanviewer.cpp
blob: ec8dbc185801767820d8b511bac9fb1cade9f282 (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
/*
 * Copyright (C) 2004 Robert Hogan <robert at roberthogan dot net>
 */

#include "scanviewer.h"
#include "klamav.h"
#include "freshklam.h"
#include "directorylist.h"
#include "dbviewer.h"
#include "collectiondb.h"

#include <config.h>
#include "klamavconfig.h"


#include <tdelocale.h>
#include <kiconloader.h>
#include <tdemenubar.h>
#include <kstatusbar.h>
#include <tdeio/netaccess.h>
#include <knotifyclient.h>
#include <kprocio.h>
#include <tqlayout.h>
#include <tdecmdlineargs.h>
#include <tdemessagebox.h>
#include <kstandarddirs.h>
#include <ksystemtray.h>
#include <kprogress.h>

#include <tqtimer.h>
#include <tqpushbutton.h>
#include <tqcolor.h>
#include <tqtooltip.h> //TQToolTip::palette()
#include <kdebug.h>

static int counter = 0;

ScanViewer::ScanViewer(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{

    scanInProgress = TRUE;
    multiScan = FALSE;

    //TQGridLayout *layout = new TQGridLayout(this, 6, 3, 10, 4);
    layout = new TQGridLayout(this, 6, 3, 10, 4);
    layout->setColStretch(0, 10);
    layout->addColSpacing(1, 10);
    layout->setColStretch(1, 0);
    layout->setColStretch(2, 1);
    layout->addRowSpacing(1, 10);
    layout->setRowStretch(1, 0);
    layout->setRowStretch(2, 10);
    layout->addRowSpacing(4, 10);
    layout->setRowStretch(4, 0);



    resultview = new TQListView(this);
    resultview->setShowSortIndicator(true);

    TQFontMetrics rb_fm(resultview->fontMetrics());
    //resultview->setMinimumSize(rb_fm.width("0")*55,
    //            rb_fm.lineSpacing()*15);
    resultview->addColumn( i18n( "Name of File" ),(resultview->width()/3));
        resultview->addColumn( i18n( "Name of Problem Found" ),(resultview->width()/3));
    resultview->addColumn( i18n( "Status" ),(resultview->width()/3));
    resultview->setResizeMode(TQListView::AllColumns);
    resultview->setSelectionMode( TQListView::Extended );
    resultview->setAllColumnsShowFocus(true);
    layout->addMultiCellWidget(resultview, 2, 2, 0, 2);

    connect( resultview, SIGNAL(onItem ( TQListViewItem * )),
        SLOT(slotOnItem ( TQListViewItem * )));
    connect( resultview, SIGNAL(onViewport (  )),
        SLOT(slotOffItem ( )));

    menu = new TQPopupMenu( resultview );

        connect(resultview, SIGNAL( contextMenuRequested( TQListViewItem *, const TQPoint& , int ) ),
        this, SLOT( slotRMB( TQListViewItem *, const TQPoint &, int ) ) );

    status_frame = new TQFrame(this);
    status_frame->setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
    TQBoxLayout *status_layout = new TQHBoxLayout(status_frame, 2);


    status_label = new TQLabel("", status_frame);
    status_layout->addWidget(status_label, 10);

    //matches_label = new TQLabel(status_frame);
    //TQFontMetrics ml_fm(matches_label->fontMetrics());
    //matches_label->setFixedWidth(ml_fm.width(i18n("9999 viruses/errors found")));
    //matches_label->setFixedHeight(ml_fm.lineSpacing());
    //status_layout->addWidget(matches_label, 0);


    status_layout->activate();
    status_frame->adjustSize();
    status_frame->setMinimumSize(status_frame->size());
    layout->addMultiCellWidget(status_frame, 3, 3, 0, 2);

    status2_frame = new TQFrame(this);
    status2_frame->setFrameStyle(TQFrame::Panel | TQFrame::Sunken);
    status2_layout = new TQHBoxLayout(status2_frame, 2);

    status2_label = new TQLabel(i18n("Files scanned: 0"), status2_frame);
    status2_layout->addWidget(status2_label, 10);
    status2_label->hide();

    prog = new KProgress(status2_frame);
    progosd = new K3bJobProgressOSD();
    progosd->setText( i18n("Scanning..") );
    progosd->readSettings(TDEGlobal::config());

    status2_layout->addWidget(prog, 10);
    prog->adjustSize();
    prog->hide();
    progosd->hide();

     scan_time = new TQPushButton( status2_frame );
    scan_time->setText(i18n("Calculating Scan Time... (Click To By-Pass)"));
    status2_layout->addWidget(scan_time,10);
     scan_time->adjustSize();
    scan_time->show();

    //status_frame->setPaletteBackgroundColor(TQColor::Yellow);
    connect( scan_time, SIGNAL(clicked()),
        SLOT(slotCancelScanTime()) );



    matches2_label = new TQLabel(status2_frame);
    TQFontMetrics ml_fm2(matches2_label->fontMetrics());
    matches2_label->setFixedWidth(ml_fm2.width(i18n("9999 viruses/problems found")));
    matches2_label->setFixedHeight(ml_fm2.lineSpacing());
    status2_layout->addWidget(matches2_label, 0);

    status2_layout->activate();
    status2_frame->adjustSize();
    status2_frame->setMinimumSize(status2_frame->size());

    layout->addMultiCellWidget(status2_frame, 4, 4, 0, 2);


    layout->activate();


}

ScanViewer::~ScanViewer()
{

    scanCancelled = TRUE;

    if ((childproc) && (scanInProgress)){
        delete childproc;
        childproc = 0;
    }
}



void ScanViewer::processOutput()
{
    int pos;
    TQString item2;

    if (!(childproc))
        return;

    if ((showProgress) && prog->isHidden()){
        status2_label->hide();
        scan_time->hide();
        prog->show();
        progosd->show();
    }


    //buf.replace("//","/"); // don't know why they're getting two slashes
    while ((childproc) && ((pos = (childproc->readln(item2,true))) != -1)) {
            TQListViewItem* tm;
            //item2 = (buf.section('\n',j,j)).stripWhiteSpace();
            item2 = item2.stripWhiteSpace();
            int fnameStartPoint = 0;
            int fnameEndPoint = item2.findRev(":");
            TQString tmpFName = item2.mid(fnameStartPoint,(fnameEndPoint - fnameStartPoint));
            tmpFName = i18n(tmpFName.ascii());
            if ((pos = (item2.find(" FOUND"))) != -1){
            //if ((pos = buf.section('\n',j,j).find("FOUND")) != -1){
                TQString tmpVirusName = item2.mid((fnameEndPoint+1),(item2.length() - (fnameEndPoint+1)));
                TQDate today = TQDate::currentDate();
                TQTime now = TQTime::currentTime();
                TQString suffix = TQString(":%1 %2")
                    .arg(today.toString("ddd MMMM d yyyy"))
                    .arg(now.toString("hh-mm-ss-zzz ap"));

                if ((tmpVirusName.find("FOUND")) != -1){
                    tmpVirusName.replace("FOUND","");
                    tm = new TQListViewItem( resultview, tmpFName, tmpVirusName.stripWhiteSpace(), i18n("Loose") );
                    tm->setPixmap( 0, SmallIcon("klamav_virus") );
                    //resultview->insertItem(buf.section('\n',j,j));
                    QuarantineList.append(tmpFName+":"+tmpVirusName+suffix);
                CollectionDB::instance()->insertEvent("Virus Found",tmpVirusName,tmpFName);

                }
                filesscanned++;
                if (!(showProgress))
                    status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));

            //}else if ((pos = buf.section('\n',j,j).find("ERROR:")) != -1){
            }else if ((pos = (item2.find("ERROR:"))) != -1){
                TQString tmpVirusName = item2.mid((fnameEndPoint+1),(item2.length() - (fnameEndPoint+1)));
                tm = new TQListViewItem( resultview, tmpFName, tmpVirusName.stripWhiteSpace(), i18n("Not a Virus") );
                tm->setPixmap( 0, SmallIcon("klamav_error") );
                //resultview->insertItem(buf.section('\n',j,j));
                CollectionDB::instance()->insertEvent("Error Found",tmpVirusName,tmpFName);

                errorsEncountered = TRUE;
                filesscanned++;
                if (!(showProgress))
                    status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));
            //}else if ((pos = buf.section('\n',j,j).find("Scanning ")) != -1){
            }else if ((pos = (item2.find("Scanning"))) != -1){
                if (status_label->palette() != TQToolTip::palette())
                    status_label->setText(item2);
            //}else if ((pos = buf.section('\n',j,j).find(": OK")) != -1){
            }else if ((pos = (item2.find(": OK"))) != -1){
                filesscanned++;
                if (!(showProgress))
                    status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));

            //}else if ((pos = buf.section('\n',j,j).find(": Access denied")) != -1){
            }else if ((pos = (item2.find(": Access denied"))) != -1){
                filesscanned++;
                if (!(showProgress))
                    status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));
                KNotifyClient::event(tdemain->_tray->winId(),"ScanAccessDenied", TQString("Can't scan %1 "
                    "- Access Denied!").arg(tmpFName));
                CollectionDB::instance()->insertEvent("Error Found","Access Denied",tmpFName);
            //}else if ((pos = buf.section('\n',j,j).find(": Can't open")) != -1){

            }else if ((pos = (item2.find(": Can't open"))) != -1){
                //status2_label->setText(i18n("Files scanned: %1").arg(++filesscanned));
                filesscanned++;
                if (!(showProgress))
                    status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));
                KNotifyClient::event(tdemain->_tray->winId(),"ScanAccessDenied", TQString("Can't scan %1 "
                   "- Access Denied!").arg(tmpFName));
                CollectionDB::instance()->insertEvent("Error Found","Access Denied",tmpFName);
            }else if ((pos = (item2.find(": Empty file"))) != -1){
                //status2_label->setText(i18n("Files scanned: %1").arg(++filesscanned));
                filesscanned++;
                if (!(showProgress))
                        status2_label->setText(i18n("Files scanned: %1").arg(filesscanned));
            //KNotifyClient::event(tdemain->_tray->winId(),"ScanAccessDenied", TQString("Can't scan %1 "
            //            "- Empty File!").arg(tmpFName));
                CollectionDB::instance()->insertEvent("Error Found","Empty File",tmpFName);
            }
            item2 = "";
    }

    TQString str;
    str.setNum(resultview->childCount());

    str += i18n(" viruses/problems found");
    matches2_label->setText(str);

    if (showProgress){
        prog->setProgress (filesscanned);
        progosd->setProgress ((int)(100 * (float)filesscanned/cnt));
    }


}


void ScanViewer::slotScan(const TQStringList & filepattern, int mode, bool recursive, bool dcopscan)
{

    //KMessageBox::information (this, filepattern);
    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

    if(( args->isSet( "scanthis" ) ) || (dcopscan)) {
        calculateTime = FALSE;
        showProgress = FALSE;
        scan_time->hide();
        status2_label->show();

    }else{
        calculateTime = TRUE;
        showProgress = TRUE;
        scan_time->show();
        status2_label->hide();
    }

    scanCancelled = FALSE;
    scanInProgress = FALSE;

    m_mode = mode;
    m_filepattern = filepattern;
    m_recursive = recursive;


    //kdDebug() << "m_filepattern *" << m_filepattern << "*" << endl;

    prog->hide();
    progosd->hide();


    matches2_label->setText("");
    status_label->setText( i18n("Preparing To Scan ") + m_filepattern.join(" "));
    status2_label->setText(i18n("Files scanned:")+" 0");
    prog->setProgress(0);
    //progosd->setProgress(0);

    kdDebug() << filepattern << endl;
    TQStringList tmpfilepattern = filepattern;

    cnt = 0;
    //TQStringList temp = TQStringList::split(" ", tmpfilepattern.replace("\"",""));
    //kdDebug() << temp << endl;
    if (calculateTime){
        for ( TQStringList::Iterator it = tmpfilepattern.begin(); it != tmpfilepattern.end(); ++it ){
            TQDir d( TQString((*it).latin1()).stripWhiteSpace() );
            kdDebug() << "dir " << TQString((*it).latin1()).stripWhiteSpace() << endl;
            counter = 0;
            cnt = cnt + countFiles(d);
            if (scanCancelled)
            break;
        }
    }
    //kdDebug() << scanCancelled << endl;

    calculateTime = FALSE;
    if (scanCancelled)
        return;

    //kdDebug() << filepattern << endl;

    prog->setTotalSteps(cnt);
    kdDebug() << "COUNT" << cnt << endl;

    config = TDEGlobal::config();

    config->setGroup("KlamavBackend");
    clamdscan = config->readBoolEntry("ScannerClamdscan", false);

    slotClear();
    QuarantineList.clear();

    errorsEncountered = FALSE;
    filesscanned = 0;
    //status2_label->setText(i18n("Scan in Progress..."));

    TQString db;

    if(!( args->isSet( "scanthis" ) ) )
        db = tdemain->freshklam->getCurrentDBDir();
    else{
        config->setGroup("Freshklam");
        TQStringList lastDownloadPaths = config->readListEntry("lastDownloadPaths");
        for (TQStringList::Iterator ita = lastDownloadPaths.begin(); ita == lastDownloadPaths.begin() ; ita++){
            db = *ita;
        }
    }

    ////kdDebug() << "here 2" << endl;
    TQString dbpath;
    TQString excludes;
    TQString options; // can not be used for clamdscan

    if (!(db.isEmpty()))
        dbpath = TQString(" -d %1 ").arg(db);

    config->setGroup("Klamscan");

    if (config->readEntry("ExcludeQuarantine") == "Yes"){
        config->setGroup("Kuarantine");
        TQStringList lastQuarLocations = config->readListEntry("KuarantineLocations");
        TQString quarloc;
        for (TQStringList::Iterator ita = lastQuarLocations.begin(); ita == lastQuarLocations.begin() ; ita++){
                quarloc = *ita;
        }
        excludes += TQString(" --exclude-dir=%1 ").arg(quarloc);
    }


    //if ((recursive_box->isChecked() && !(multiScan)) || (multi_recursive && multiScan))
    if (recursive)
        options += " -r ";

    //config->setGroup("Klamscan");


    if (KlamavConfig::noFilesToExtract() > 0)
        options += "--max-files=" + TQString("%1").arg(KlamavConfig::noFilesToExtract()) + " ";

//     if (KlamavConfig::mBsToExtract() > 0)
//         options += "--max-space=" + TQString("%1").arg(KlamavConfig::mBsToExtract()) + " ";
//
//     if (KlamavConfig::compressionRatio() > 0)
//         options += "--max-ratio=" + TQString("%1").arg(KlamavConfig::compressionRatio()) + " ";

    if (KlamavConfig::recursionLevel() > 0)
        options += "--max-recursion=" + TQString("%1").arg(KlamavConfig::recursionLevel()) + " ";


    if (KlamavConfig::maxFileSize() > 0)
        options += "--max-filesize=" + TQString("%1").arg(KlamavConfig::maxFileSize()) + "M ";

    if (KlamavConfig::maxScanSize() > 0)
        options += "--max-scansize=" + TQString("%1").arg(KlamavConfig::maxScanSize()) + "M ";

    //config->setGroup("Klamscan");
//     if (KlamavConfig::virusLimitsExceeded())
//         options += "--block-max ";

    if (!(KlamavConfig::scanArchives()))
        options += "--scan-archive=no ";

    if (KlamavConfig::virusEncrypted())
        options += "--alert-encrypted ";

    if (!(KlamavConfig::scanMail()))
        options += "--scan-mail=no ";

    if (!(KlamavConfig::scanHTML()))
        options += "--scan-html=no ";

    if (!(KlamavConfig::scanPE()))
        options += "--scan-pe=no ";

    if (!(KlamavConfig::scanMacros()))
        options += "--scan-ole2=no ";

    if(!(KlamavConfig::scanELF()))
        options += "--scan-elf=no ";

    if(!(KlamavConfig::scanPDF()))
        options += "--scan-pdf=no ";

    if(!(KlamavConfig::scanHWP3()))
        options += "--scan-hwp3=no ";

    if(!(KlamavConfig::scanSWF()))
        options += "--scan-pdf=no ";

    if(!(KlamavConfig::scanXML()))
        options += "--scan-xmldocs=no ";

    if (KlamavConfig::virusBroken())
        options += "--alert-broken ";


    if(clamdscan) {
        // make sure that clamd is running
        FILE* clamdpid;
        if( clamdpid = fopen("/run/clamav/clamd.pid","r") )
            fclose(clamdpid); // it's ok
        else {
            int choice = KMessageBox::warningYesNoCancel(
                this,
                i18n("The ClamAV daemon does not seem to be running on this system. Do you really want to continue with this scan or would you like to launch a standalone scan instead?'"),
                i18n("Clamd not running"),
                i18n("Continue this scan"),
                i18n("Launch standalone scan"),
                i18n("Do not ask me again")
            );

            switch(choice) {
                case 2: // cancel
                    return;
                    break;
                case 4: // launch standalone scan
                    clamdscan = false;
                    break;
            }
        }
    }

    childproc = new KProcIO();
    childproc->setUseShell(TRUE);
    childproc->setUsePty (KProcIO::Stdout,TRUE);

    if(clamdscan) {
	config->setGroup("KlamavBackend");
	TQString multiscanOption = config->readBoolEntry("ClamdMultiscan", true) ? " -m " : "";

        kdDebug() << "clamdscan -v --fdpass "
                  << multiscanOption
                  << "'" + m_filepattern.join("' '") + "'" << endl;

        *childproc << "clamdscan -v ";
	*childproc << multiscanOption;
    } else {
        kdDebug() << "clamscan -v "
                  << excludes << " "
                  << dbpath << " "
                  << options << " "
                  << "'" + m_filepattern.join("' '") + "'" << endl;

        *childproc << "clamscan -v ";
        *childproc << excludes << " ";
        *childproc << dbpath << " ";
        *childproc << options << " ";
    }

    *childproc << "'" + m_filepattern.join("' '") + "'";


/*    connect( childproc, SIGNAL(processExited(TDEProcess *)),
        SLOT(childExited()) );
    connect( childproc, SIGNAL(receivedStdout(TDEProcess *, char *, int)),
        SLOT(receivedOutput(TDEProcess *, char *, int)) );
    childproc->start(TDEProcess::NotifyOnExit, TDEProcess::Stdout);*/

    connect( childproc, SIGNAL(readReady(KProcIO *)),
        SLOT(receivedOutput(KProcIO *)) );
    childproc->start(KProcIO::NotifyOnExit);
    connect( childproc, SIGNAL(processExited(TDEProcess *)),
            SLOT(childExited()) );

    scanInProgress = TRUE;

    menu->setEnabled(FALSE);


}


void ScanViewer::finish()
{
//    search_button->setEnabled(true);
//    cancel_button->setEnabled(false);


    if ((childproc) && (scanInProgress)){
        //processOutput();
        kdDebug() << "deleting childproc" << endl;
        delete childproc;
        childproc = 0;
        kdDebug() << "deleted childproc" << endl;
    }

    scanInProgress = FALSE;

    status_label->setText(i18n( "If viruses were found, you can right-click to quarantine selected files." ));
    menu->setEnabled(TRUE);
    //dir_combo->setEnabled(TRUE);

    scan_time->hide();
    prog->hide();
    progosd->hide();
    progosd->saveSettings(TDEGlobal::config());
    status2_label->show();

    multiScan = FALSE;
    calculateTime = FALSE;

    emit scanFinished(this);

}


void ScanViewer::slotCancel()
{

    scanCancelled = TRUE;
    finish();

    //status2_frame->adjustSize();
    status2_label->setText(i18n("Cancelled"));

    CollectionDB::instance()->insertEvent("Manual Scan",TQString("Scan Cancelled"),m_filepattern.join(" "));

}

void ScanViewer::resetSysTray()
{
    tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_on_acc_disabled"));
}

void ScanViewer::childExited()
{
    int status = childproc->exitStatus();

    int result;

    status2_label->setText( i18n("Scan Complete") );
    CollectionDB::instance()->insertEvent("Manual Scan",TQString("Scan Complete"),m_filepattern.join(" "));

    if (status == 0){
    	tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_scan_safe"));
        TQTimer::singleShot( 10000, this, SLOT(resetSysTray()) );
        if (!(errorsEncountered))
            KNotifyClient::event(tdemain->_tray->winId(),"Scan Complete - No Virus Found", i18n("Scan Complete - No Viruses Found!"));
        else
            KNotifyClient::event(tdemain->_tray->winId(),"Scan Complete - No Virus But Errors", i18n("Scan Complete - No Viruses Found But Some Errors Encountered!"));

    }else if(status == 1){
    	tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_scan_found"));
        switch (m_mode) {
        case 0:
            progosd->setText("Problems found!");
            result = KMessageBox::warningContinueCancelList(this, i18n( "I'm going to quarantine this lot, you can restore them later if you want. If you don't want to quarantine, just press cancel."),QuarantineList,i18n( "Quarantine Infected Files" ),i18n( "Quarantine" ));
            switch (result) {
                case 2 :KNotifyClient::event(tdemain->_tray->winId(),"ScanAccessDenied", TQString("Suspicious Items Not "
                        "Quarantined!")); break;
                case 5 : Quarantine(); break;
            }
            break;
        case 1:
            Quarantine(); break;
        default:
            KMessageBox::information (this,i18n( "Scan Complete - Viruses Found!") );break;
    }
       }else if (status ==40){ KMessageBox::information (this,i18n( "Unknown option passed.") );

       }else if (status ==50){ KMessageBox::information (this,i18n( "Database initialization error.") );

       }else if (status ==52){ KMessageBox::information (this,i18n( "Not supported file type.") );

       }else if (status ==53){ KMessageBox::information (this,i18n( "Can't open directory.") );

       }else if (status ==54){ KMessageBox::information (this,i18n( "Can't open file. (ofm)") );

       }else if (status ==55){ KMessageBox::information (this,i18n( "Error reading file. (ofm)") );

       }else if (status ==56){ KMessageBox::information (this,i18n( "Can't stat input file / directory.") );

       }else if (status ==57){ KMessageBox::information (this,i18n( "Can't get absolute path name of current working directory." ));

       }else if (status ==58){ KMessageBox::information (this,i18n( "I/O error, please check your filesystem." ));

       }else if (status ==59){ KMessageBox::information (this,i18n( "Can't get information about current user from /etc/passwd.") );

       }else if (status ==60){ KMessageBox::information (this,i18n( "Can't get information about user 'clamav' (default name) from /etc/passwd.") );

       }else if (status ==61){ KMessageBox::information (this,i18n( "Can't fork.") );

       }else if (status ==63){ KMessageBox::information (this, i18n("Can't create temporary files/directories (check permissions)."));

       }else if (status ==64){ KMessageBox::information (this, i18n("Can't write to temporary directory (please specify another one)."));

       }else if (status ==70){ KMessageBox::information (this, i18n("Can't allocate and clear memory (calloc)."));

       }else if (status ==71){ KMessageBox::information (this, i18n("Can't allocate memory (malloc)."));

       }else if (status ==71){KMessageBox::information (this, i18n("Unspecified Error!"));
    }

    status2_label->setText( i18n("Files scanned: %1").arg(filesscanned) );

    finish();

    //if (status != 0)
          //matches_label->setText("");

}


void ScanViewer::receivedOutput(KProcIO *)
{
    //buf += TQCString(buffer, buflen+1);
    processOutput();
       //childproc->ackRead();

}


void ScanViewer::slotClear()
{
    //finish();
    resultview->clear();

//    status2_label->setText(i18n("Ready"));
    matches2_label->setText(i18n("0 viruses/problems found"));

}


// void  ScanViewer::setDirName(TQString dir){
// //    dir_combo->setEditText(dir);
//     dir_combo->setCurrentText(dir);
// }

void  ScanViewer::Quarantine(){
    bool allQuarantined=TRUE;
    config->setGroup("Kuarantine");
    TQStringList lastQuarLocations = config->readListEntry("KuarantineLocations");

    tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_quarantining"));

    TQString quarloc;
    for (TQStringList::Iterator it = lastQuarLocations.begin(); it == lastQuarLocations.begin() ; it++){
            quarloc = *it;
    }
    lastQuarItems = config->readListEntry(TQString("Items %1").arg(quarloc));

    for (TQStringList::Iterator it = QuarantineList.begin(); it != QuarantineList.end(); it++ ){
        if (lastQuarItems.contains(*it) != 0) {
            lastQuarItems.remove(*it);
        }
        TQString item2 = (*it).stripWhiteSpace();
        int fnameStartPoint = 0;
        int dtStartPoint = item2.findRev(":");
        int fnameEndPoint = item2.findRev(":", (signed int)-((item2.length() - dtStartPoint)+1));
        TQString fname = item2.mid(fnameStartPoint,(fnameEndPoint - fnameStartPoint));
        TQString itemName = item2.mid((fnameEndPoint+1),((dtStartPoint+1) - (fnameEndPoint+2)));
        TQString when = item2.mid((dtStartPoint+1),(item2.length() - (dtStartPoint+1)));
        if (!(fname.isEmpty())){
            TQStringList tokens = TQStringList::split ( "/", fname, FALSE );
            TQString qname = tokens.last();
            qname.prepend("/");
            qname.prepend(quarloc);
            qname.append(":"+when);
            if (TDEIO::NetAccess::file_move(fname,qname)){
                if (lastQuarItems.contains(item2))
                    lastQuarItems.remove(item2);
                lastQuarItems.prepend(item2);
                (resultview->findItem(fname,0))->setText(2,i18n("Quarantined"));
                (resultview->findItem(fname,0))->setPixmap( 0, SmallIcon("klamav") );
                chmod(qname.ascii(),0400);
                CollectionDB::instance()->insertEvent("Quarantine",TQString("Quarantined"),fname);

            }else{
                KMessageBox::information (this,i18n("<p>There was a problem quarantining <b>%1</b>. Check your diskspace, the permissions on your quarantine location and whether a file with the same name already exists in the quarantine. </p>").arg(fname));
            }


        }
    }
    if (allQuarantined)
        tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_on_acc_disabled"));
    else
        tdemain->_tray->setPixmap(KSystemTray::loadIcon("klamav_scan_found"));
    config->writeEntry(TQString("Items %1").arg(quarloc), lastQuarItems);
    config->sync();

    //tdemain->kuarantine->refresh();



}

void ScanViewer::slotRMB( TQListViewItem* Item, const TQPoint & point, int )
{

    if( Item ){
    TQPixmap gicon;
    TQPixmap vicon;
    TQPixmap vlicon;
    TQPixmap ticon;

        TQString iconPath = locate("cache", KMimeType::favIconForURL("http://www.viruspool.net")+".png");
        if ( iconPath.isEmpty() )
          vicon = SmallIcon("edit-find");
        else
          vicon = TQPixmap( iconPath );

        iconPath = locate("cache", KMimeType::favIconForURL("http://www.google.com")+".png");
        if ( iconPath.isEmpty() )
          gicon = SmallIcon("edit-find");
        else
          gicon = TQPixmap( iconPath );

        iconPath = locate("cache", KMimeType::favIconForURL("http://www.trendmicro.com")+".png");
        if ( iconPath.isEmpty() )
          ticon = SmallIcon("edit-find");
        else
          ticon = TQPixmap( iconPath );

        iconPath = locate("cache", KMimeType::favIconForURL("http://www.viruslist.com")+".png");
        if ( iconPath.isEmpty() )
          vlicon = SmallIcon("edit-find");
        else
          vlicon = TQPixmap( iconPath );

    menu->clear();
        menu->insertItem( "Quarantine Selected", this,SLOT(slotQuarantineSelected()) );

        menu->insertItem( ticon,i18n("Search for %1 with Trend Micro").arg(Item->text(1)), this, SLOT(slotTrendMicro()) );

        menu->insertItem( gicon,i18n("Search for %1 with Google").arg(Item->text(1)), this, SLOT(slotGoogle()) );
        menu->popup( point );
     }
}

void ScanViewer::slotQuarantineSelected()
{

    TQPtrList<TQListViewItem> list;
    TQListViewItemIterator it( resultview, TQListViewItemIterator::Selected );

    QuarantineList = "";
    while ( it.current() ) {
      TQListViewItem* tItem = it.current();

      TQDate today = TQDate::currentDate();
      TQTime now = TQTime::currentTime();
      TQString suffix = TQString(":%1 %2")
          .arg(today.toString("ddd MMMM d yyyy"))
          .arg(now.toString("hh-mm-ss ap"));

      QuarantineList.append(tItem->text(0)+":"+tItem->text(1)+suffix);
      ++it;
    }

    Quarantine();
}



void ScanViewer::slotGoogle()
{
    TQString name = resultview->currentItem()->text(1);
    tdemain->klamdb->slotExternal(name, "Google");
}

void ScanViewer::slotTrendMicro()
{
    TQString name = resultview->currentItem()->text(1);
    tdemain->klamdb->slotExternal(name, "TrendMicro");
}

void ScanViewer::slotStartAgain()
{
    calculateTime = TRUE;
    emit scanStartingAgain(this);
    slotScan(m_filepattern, m_mode, m_recursive,false);


}

bool ScanViewer::scanGoingOn()
{
    return (scanInProgress || calculateTime);
}

void ScanViewer::slotCancelScanTime()
{
    calculateTime = FALSE;
    showProgress = FALSE;
    status2_label->show();
    scan_time->hide();

}


// void ScanViewer::startProgress()
// {
//
//     cnt = 0;
//     //kdDebug() << "m_filepattern" << m_filepattern << endl;
//     TQDir d( m_filepattern );
// //    int num = countFiles(d);
// //     if (countFiles(d) > 0)
// //        //kdDebug() << "count" << num << endl;
//
//     progress = new KProgressDialog (this, "progress", i18n( "Loading .." ), i18n( "Loading..." ), true);
//     progress->setAllowCancel(false);
//     prog = progress->progressBar();
//     progress->setLabel(i18n( "Loading lots and lots and lots of virus information" ));
//     //int cnt = countFiles(d);
//     prog->setTotalSteps(countFiles(d));
//     //kdDebug() << "COUNT" << countFiles(d) << endl;
//
// }

int ScanViewer::countFiles( TQDir & root)
{

    kapp->processEvents();

    if (!(calculateTime) || (scanCancelled)){
//         kapp->processEvents();
        return 0;
    }
    TQStringList entries = root.entryList( TQDir::Dirs | TQDir::Files | TQDir::Hidden);


    ////kdDebug() << "count" << counter << endl;
    for (TQStringList::size_type j = 0; j < entries.size(); j++ )
    {
        TQString entry = entries[j];

        if( entry == "." || entry == "..")
            continue;


        TQFileInfo fi(root, entry );
        entry = fi.absFilePath();
        entry = TQDir::convertSeparators(entry);

        if ((fi.isFile()) && !(fi.isSymLink())){
            counter++;
        }else if ((fi.isDir()) && !(fi.isSymLink())){
            TQDir temp(entry);
            countFiles(temp);
        }
    }
    return counter;
}

void ScanViewer::startProgressDialog( const TQString & text )
{
    //if ( progressDialog )
    //    delete progressDialog;

    progressDialog = new KProgressDialog( this, "progress_dialog", TQString::null, text, false );

    progressDialog->setAllowCancel( true );
    progressDialog->showCancelButton( true );
    progressDialog->setPlainCaption( i18n( "Please Wait" ) );

    progressDialog->progressBar()->setTotalSteps( 0 );
    progressDialog->progressBar()->setPercentageVisible( false );

    progressDialog->setMinimumDuration( 500 );
    progressDialog->show();

    timer = new TQTimer( this );
    connect( timer, SIGNAL( timeout() ), this, SLOT( slotProg() ) );

    timer->start( 200, FALSE );
}

void ScanViewer::slotProg()
{

    if (progressDialog)
        progressDialog->progressBar()->setProgress(progressDialog->progressBar()->progress() + 4 );
}

void ScanViewer::slotOnItem( TQListViewItem * lineitem)
{


    status_label->setPalette(TQToolTip::palette());

    TQString problem = lineitem->text(1).stripWhiteSpace();
    TQString message = problem + " is probably a virus. Right-click and select a service to research it.";

    TQString path( lineitem->text(0).stripWhiteSpace() );
    TQString file = path.section( '/', -1 );

    if ((problem.contains("ExceededFile")) ||
       (problem == "Archive.ExceededRecursionLimit") ||
       (problem == "Oversized.Zip") ||
       (problem == "Oversized.RAR"))
        message = problem +": " + i18n("Attempts to scan ") + file + i18n(" resulted in exceeding a limit you set in 'Archive Limits'.");
    else if (problem == "ClamAV-Test-File")
        message = problem +": "  + file + i18n(" contains the ClamAV test signature. It's not a virus.");
    else if (problem == "Broken.Executable")
        message = problem +": " + file + i18n(" is a damaged exectuable. Some viruses use this to conceal themselves.");
    else if (problem == "Suspect.zip")
        message = problem +": " + file + i18n(" has a form of zip compression sometimes used by viruses.");
    else if (problem == "Encrypted.zip")
        message = problem +": " + file + i18n(" is an encrypted zip file.");
    else if (problem == "Encrypted.RAR")
        message = problem +": " + file + i18n(" is an encrypted RAR file.");
    else if (problem == "Exploit.Zip.ModifiedHeaders")
        message = problem +": " + file + i18n(" is mis-formatted in a way sometimes used by viruses.");


     status_label->setText( message);

}

void ScanViewer::slotOffItem( )
{

    status_label->unsetPalette();
    status_label->setText(i18n("Hover over each entry for more info. Right-click on entries for more options."));

}
#include "scanviewer.moc"