summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_diroperator.cpp
blob: a7f1bbe5885a4c659b7b608a2a06f0038eb46161 (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
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
/***************************************************************************
                          sq_diroperator.cpp  -  description
                             -------------------
    begin                : Mon Mar 15 2004
    copyright            : (C) 2004 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqtimer.h>
#include <tqlabel.h>
#include <tqfileinfo.h>
#include <tqapplication.h>
#include <tqtooltip.h>
#include <tqclipboard.h>

#include <tdeapplication.h>
#include <kstringhandler.h>
#include <tdelocale.h>
#include <kprogress.h>
#include <tdeglobal.h>
#include <tdestdaccel.h>
#include <kdiskfreesp.h>
#include <konq_operations.h>
#include <kiconeffect.h>

#include "ksquirrel.h"
#include "sq_archivehandler.h"
#include "sq_diroperator.h"
#include "sq_fileiconview.h"
#include "sq_filethumbview.h"
#include "sq_filethumbviewitem.h"
#include "sq_filedetailview.h"
#include "sq_widgetstack.h"
#include "sq_config.h"
#include "sq_glview.h"
#include "sq_glwidget.h"
#include "sq_thumbnailsize.h"
#include "sq_pixmapcache.h"
#include "sq_libraryhandler.h"
#include "sq_externaltool.h"
#include "sq_popupmenu.h"
#include "sq_navigatordropmenu.h"
#include "sq_previewwidget.h"
#include "sq_treeview.h"
#include "sq_progress.h"
#include "sq_downloader.h"
#include "sq_hloptions.h"

#include "sq_categoriesview.h"
#include "sq_categorybrowsermenu.h"
#include "sq_imagebasket.h"
#include "sq_converter.h"
#include "sq_directorybasket.h"

static const int  SQ_MAX_WORD_LENGTH = 50;

SQ_DirOperator::SQ_DirOperator(const KURL &url, ViewT type_, TQWidget *parent, const char *name)
    : KDirOperator(url, parent, name), type(type_)
{
    usenew = false;

    totalSize = static_cast<TDEIO::filesize_t>(0);

    // create and insert new actions in context menu
    setupActionsMy();

    connect(this, TQT_SIGNAL(finishedLoading()), this, TQT_SLOT(slotFinishedLoading()));
    connect(this, TQT_SIGNAL(updateInformation(int,int)), this, TQT_SLOT(slotUpdateInformation(int,int)));
    connect(this, TQT_SIGNAL(urlEntered(const KURL&)), this, TQT_SLOT(slotUrlEntered(const KURL&)));
    connect(this, TQT_SIGNAL(dropped(const KFileItem *, TQDropEvent*, const KURL::List&)),
            this, TQT_SLOT(slotDropped(const KFileItem *, TQDropEvent*, const KURL::List&)));
    connect(this, TQT_SIGNAL(fileSelected(const KFileItem *)), this, TQT_SLOT(slotExecutedConst(const KFileItem *)));

    connect(dirLister(), TQT_SIGNAL(deleteItem(KFileItem *)), this, TQT_SLOT(slotItemDeleted(KFileItem *)));
    connect(dirLister(), TQT_SIGNAL(newItems(const KFileItemList &)), this, TQT_SLOT(slotNewItems(const KFileItemList &)));
    connect(dirLister(), TQT_SIGNAL(refreshItems(const KFileItemList &)), this, TQT_SLOT(slotRefreshItems(const KFileItemList &)));

    timer_preview = new TQTimer(this);
    connect(timer_preview, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotPreview()));

    setAcceptDrops(true);

    fileview = 0;
    oldCurrentItem = 0;

    fireDiskSize(url);

    down = new SQ_Downloader(this, "SQ_Downloader [dirop]");
    connect(down, TQT_SIGNAL(result(const KURL &)), this, TQT_SLOT(slotDownloaderResult(const KURL &)));
    connect(down, TQT_SIGNAL(percents(int)), this, TQT_SLOT(slotDownloadPercents(int)));

    connect(SQ_ArchiveHandler::instance(), TQT_SIGNAL(unpack(const KURL &)), this, TQT_SLOT(slotSetURL(const KURL &)));

    // read sorting settings
    SQ_Config::instance()->setGroup("Fileview");
    int sorting = 0;

    if(SQ_Config::instance()->readBoolEntry("sorting_name", true))      sorting |= TQDir::Name;
    if(SQ_Config::instance()->readBoolEntry("sorting_time", false))     sorting |= TQDir::Time;
    if(SQ_Config::instance()->readBoolEntry("sorting_size", false))     sorting |= TQDir::Size;
    if(SQ_Config::instance()->readBoolEntry("sorting_dirs", true))      sorting |= TQDir::DirsFirst;
    if(SQ_Config::instance()->readBoolEntry("sorting_reverse", false))  sorting |= TQDir::Reversed;
    if(SQ_Config::instance()->readBoolEntry("sorting_ignore", false))   sorting |= TQDir::IgnoreCase;

    actionHidden = dynamic_cast<TDEToggleAction *>(actionCollection()->action("show hidden"));

    if(SQ_Config::instance()->readBoolEntry("show hidden", false) && actionHidden)
    {
        actionHidden->setChecked(false);
        actionHidden->activate();
    }

    setSorting(static_cast<TQDir::SortSpec>(sorting));
    setEnableDirHighlighting(true);
}

SQ_DirOperator::~SQ_DirOperator()
{}

void SQ_DirOperator::slotUrlEntered(const KURL &url)
{
    usenew = false;

    totalSize = static_cast<TDEIO::filesize_t>(0);

    clearListers();

    SQ_WidgetStack::instance()->setURLForCurrent(url);

    fireDiskSize(url);
}

void SQ_DirOperator::fireDiskSize(const KURL &url)
{
    if(url.isLocalFile())
    {
        KDiskFreeSp *sp = KDiskFreeSp::findUsageInfo(url.path());

        connect(sp, TQT_SIGNAL(foundMountPoint(const unsigned long&,
                                        const unsigned long&,
                                        const unsigned long&,
                                        const TQString&)),
                                        this, TQT_SLOT(slotFoundMountPoint(const unsigned long&,
                                        const unsigned long&,
                                        const unsigned long&,
                                        const TQString&)));
    }
    else
    {
        KSquirrel::app()->diskProgress()->flush();
        KSquirrel::app()->diskProgress()->setTotalSteps(1);
        KSquirrel::app()->diskProgress()->setIndicator(0);
        KSquirrel::app()->diskProgress()->update();
        KSquirrel::app()->sbarWidget("diskSpace")->setText(i18n("remote filesystem"));
        TQToolTip::remove(KSquirrel::app()->sbarWidget("diskSpace"));
    }
}

void SQ_DirOperator::slotFoundMountPoint(const unsigned long &kbSize,
                    const unsigned long &kbUsed,
                    const unsigned long &kbAvail,
                    const TQString &mount)
{
    // clear any previous drawing
    KSquirrel::app()->diskProgress()->flush();

    KSquirrel::app()->diskProgress()->setTotalSteps(kbSize);
    KSquirrel::app()->diskProgress()->setIndicator(kbSize-kbAvail); // kbSize != kbAvail+kbUsed
    KSquirrel::app()->sbarWidget("diskSpace")->setText(TQString::fromLatin1("%1: %2/%3")
                                                        .arg(mount)
                                                        .arg(TDEIO::convertSizeFromKB(kbAvail))
                                                        .arg(TDEIO::convertSizeFromKB(kbSize)));

    TQToolTip::add(KSquirrel::app()->sbarWidget("diskSpace"), i18n("<table><tr><td>Mount point:</td><td>%1</td></tr><tr><td>Total size:</td><td>%2</td></tr><tr><td>Used:</td><td>%3</td></tr><tr><td>Available:</td><td>%4</td></tr></table>")
                                                                .arg(mount)
                                                                .arg(TDEIO::convertSizeFromKB(kbSize))
                                                                .arg(TDEIO::convertSizeFromKB(kbUsed))
                                                                .arg(TDEIO::convertSizeFromKB(kbAvail))
                                                                );
}

void SQ_DirOperator::slotExecutedConst(const KFileItem *fi)
{
    execute(const_cast<KFileItem *>(fi));
}

void SQ_DirOperator::execute(KFileItem *fi)
{
    down->kill();

    SQ_GLWidget::window()->setOriginalURL(fi->url());

    if(fi->url().isLocalFile())
        executePrivate(fi);
    else if(SQ_LibraryHandler::instance()->maybeSupported(fi->url(), fi->mimetype()) != SQ_LibraryHandler::No)
        down->start(fi);
}

void SQ_DirOperator::slotDownloaderResult(const KURL &url)
{
    SQ_GLWidget::window()->setDownloadPercents(-1);

    if(url.isEmpty())
        return;    

    KFileItem fi(KFileItem::Unknown, KFileItem::Unknown, url);

    executePrivate(&fi);
}

void SQ_DirOperator::prepareView(ViewT t)
{
    type = t;

    if(fileview)
    {
        oldSelected = *fileview->selectedItems();
        oldCurrentItem = fileview->currentFileItem();
    }

    switch(type)
    {
        case  SQ_DirOperator::TypeDetailed:
        {
            SQ_FileDetailView *dv;
            fileview = dv = new SQ_FileDetailView(this, "detail view");

            connect(dv, TQT_SIGNAL(currentChanged(TQListViewItem *)), this, TQT_SLOT(slotCurrentChanged(TQListViewItem *)));
            connect(dv, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));
            connect(dv, TQT_SIGNAL(invokeBrowser()), this, TQT_SLOT(slotInvokeBrowser()));
            connect(KSquirrel::app(), TQT_SIGNAL(resetToolTip()), dv, TQT_SLOT(slotResetToolTip()));
        }
        break;

        case  SQ_DirOperator::TypeList:
        case  SQ_DirOperator::TypeIcons:
        {
            SQ_FileIconView *iv;
            fileview = iv = new SQ_FileIconView(this, (type == SQ_DirOperator::TypeIcons) ? "icon view":"list view");

            connect(iv, TQT_SIGNAL(currentChanged(TQIconViewItem *)), this, TQT_SLOT(slotCurrentChanged(TQIconViewItem *)));
            connect(iv, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));
            connect(iv, TQT_SIGNAL(invokeBrowser()), this, TQT_SLOT(slotInvokeBrowser()));
            connect(KSquirrel::app(), TQT_SIGNAL(resetToolTip()), iv, TQT_SLOT(slotResetToolTip()));

            disableSpecificActions(iv);
        }
        break;

        case  SQ_DirOperator::TypeThumbs:
        {
            SQ_FileThumbView *tv;
            fileview = tv = new SQ_FileThumbView(this, "thumb view");

            connect(tv, TQT_SIGNAL(currentChanged(TQIconViewItem *)), this, TQT_SLOT(slotCurrentChanged(TQIconViewItem *)));
            connect(tv, TQT_SIGNAL(selectionChanged()), this, TQT_SLOT(slotSelectionChanged()));
            connect(tv, TQT_SIGNAL(invokeBrowser()), this, TQT_SLOT(slotInvokeBrowser()));
            connect(dirLister(), TQT_SIGNAL(canceled()), tv, TQT_SLOT(stopThumbnailUpdate()));
            connect(KSquirrel::app(), TQT_SIGNAL(resetToolTip()), tv, TQT_SLOT(slotResetToolTip()));

            disableSpecificActions(tv);
        }
        break;
    }
}

void SQ_DirOperator::slotCurrentChanged(TQListViewItem *)
{
    timer_preview->start(SQ_PreviewWidget::instance()->delay(), true);
}

void SQ_DirOperator::slotCurrentChanged(TQIconViewItem *)
{
    timer_preview->start(SQ_PreviewWidget::instance()->delay(), true);
}

void SQ_DirOperator::slotSelectionChanged()
{
    TQString str = i18n("no files selected");
    TQPixmap px;
    TDEIO::filesize_t sz = static_cast<TDEIO::filesize_t>(0);
    int add = 0;
    KFileItem *fi = 0;

    // which items are selected ?
    for(KFileItem *it = fileview->firstFileItem();it;it = fileview->nextItem(it))
    {
        if(fileview->isSelected(it))
        {
            // _current_ fileitem and currently _selected_ fileitem
            // may differ, so lets count only first _selected_ item
            if(!fi)
                fi = it;

            add++;

            // count only files
            if(it->isFile())
                sz += it->size();
        }
    }

    if(fi)
    {
        // determine pixmap
        px = fi->pixmap(TDEIcon::SizeSmall);

        // costruct name and size
        str = TQString("%1 %2 %3")
                .arg(KStringHandler::csqueeze(fi->name(), SQ_MAX_WORD_LENGTH))
                .arg(TDEIO::convertSize(fi->size()))
                .arg(add > 1 ? TQString("<b>[%1/%2]</b>").arg(TDEIO::convertSize(sz)).arg(add):"");
    }

    // update statusbar
    KSquirrel::app()->sbarWidget("fileIcon")->setPixmap(px);
    KSquirrel::app()->sbarWidget("fileName")->setText(str);
}

void SQ_DirOperator::setPreparedView()
{
    setView(fileview);

    fileview->setOnlyDoubleClickSelectsFiles(true);
}

void SQ_DirOperator::slotDropped(const KFileItem *i, TQDropEvent*, const KURL::List &urls)
{
    KURL u = i ? i->url() : url();

    // setup and show popup menu with actions
    SQ_NavigatorDropMenu::instance()->setupFiles(urls, u);
    SQ_NavigatorDropMenu::instance()->exec(TQCursor::pos());
}

// Insert new actions in context menu.
void SQ_DirOperator::setupActionsMy()
{
    new TDEAction(i18n("Edit file type"), 0, 0, this, TQT_SLOT(slotEditMime()), actionCollection(), "dirop_edit_mime");

    if(SQ_HLOptions::instance()->have_imagebasket)
        new TDEAction(i18n("Add to Basket"), "folder_image", CTRL+TQt::Key_B, this, TQT_SLOT(slotAddToBasket()), actionCollection(), "dirop_tobasket");

    if(SQ_HLOptions::instance()->have_directorybasket)
        new TDEAction(i18n("Add to Folder Basket"), "folder_image", CTRL+TQt::Key_D, this, TQT_SLOT(slotAddToDirectoryBasket()), actionCollection(), "dirop_todirbasket");

    TDEActionMenu *file = new TDEActionMenu(i18n("File actions"), actionCollection(), "dirop_file_menu");
    TDEAction *prop = actionCollection()->action("properties");

    TDEAction *reload = actionCollection()->action("mkdir");
    if(reload && reload->shortcut().isNull()) reload->setShortcut(TDEShortcut(CTRL+TQt::Key_N));
    reload = actionCollection()->action("reload");

    connect(prop, TQT_SIGNAL(enabled(bool)), this, TQT_SLOT(slotEnableFileActions(bool)));

    TDEAction *sep = actionCollection()->action("separator");

    file->insert(new TDEAction(i18n("Copy"), "edit-copy", TDEStdAccel::copy(), SQ_WidgetStack::instance(), TQT_SLOT(slotFileCopy()), actionCollection(), "dirop_copy"));
    file->insert(new TDEAction(i18n("Cut"), "edit-cut", TDEStdAccel::cut(), SQ_WidgetStack::instance(), TQT_SLOT(slotFileCut()), actionCollection(), "dirop_cut"));
    file->insert(new TDEAction(i18n("Paste"), "edit-paste", TDEStdAccel::paste(), SQ_WidgetStack::instance(), TQT_SLOT(slotFilePaste()), actionCollection(), "dirop_paste"));
    file->insert(sep);
    file->insert(new TDEAction(i18n("Copy to..."), 0, TQt::Key_F5, SQ_WidgetStack::instance(), TQT_SLOT(slotFileCopyTo()), actionCollection(), "dirop_copyto"));
    file->insert(new TDEAction(i18n("Move to..."), 0, TQt::Key_F6, SQ_WidgetStack::instance(), TQT_SLOT(slotFileMoveTo()),actionCollection(), "dirop_cutto"));
    file->insert(new TDEAction(i18n("Link to..."), 0, TQt::Key_F7, SQ_WidgetStack::instance(), TQT_SLOT(slotFileLinkTo()), actionCollection(), "dirop_linkto"));
    file->insert(sep);
    file->insert(new TDEAction(i18n("Run"), "launch", CTRL+Key_J, SQ_WidgetStack::instance(), TQT_SLOT(slotRunSeparately()), actionCollection(), "dirop_runsepar"));
    file->insert(new TDEAction(i18n("Repeat (nothing to repeat)"), "go-down", TQt::Key_F10, KSquirrel::app(), TQT_SLOT(slotRepeat()), actionCollection(), "dirop_repeat"));
    file->insert(new TDEAction(i18n("Convert..."), 0, CTRL+TQt::Key_K, SQ_Converter::instance(), TQT_SLOT(slotStartEdit()), actionCollection(), "dirop_convert"));

    file->insert(sep);
    file->insert(new TDEAction(i18n("Recreate selected thumbnails"), "reload", CTRL+TQt::Key_R, SQ_WidgetStack::instance(),
        TQT_SLOT(slotRecreateThumbnail()), actionCollection(), "dirop_recreate_thumbnails"));
    file->insert(sep);
    file->insert(new TDEAction(i18n("Copy file path"), 0, TQt::Key_F8, this, TQT_SLOT(slotCopyPath()), actionCollection(), "dirop_copypath"));
    file->insert(new TDEAction(i18n("Copy file url"), 0, TQt::Key_F9, this, TQT_SLOT(slotCopyURL()), actionCollection(), "dirop_copyurl"));

    if(reload) reload->setShortcut(CTRL+TQt::Key_F5);
}

void SQ_DirOperator::slotEnableFileActions(bool e)
{
    actionCollection()->action("dirop_tobasket")->setEnabled(e);
    actionCollection()->action("dirop_todirbasket")->setEnabled(e);

    actionCollection()->action("dirop_copy")->setEnabled(e);
    actionCollection()->action("dirop_cut")->setEnabled(e);
    actionCollection()->action("dirop_copyto")->setEnabled(e);
    actionCollection()->action("dirop_cutto")->setEnabled(e);
    actionCollection()->action("dirop_linkto")->setEnabled(e);

    actionCollection()->action("dirop_runsepar")->setEnabled(e);
    actionCollection()->action("dirop_convert")->setEnabled(e);

    actionCollection()->action("dirop_copypath")->setEnabled(e);
    actionCollection()->action("dirop_copyurl")->setEnabled(e);

    actionCollection()->action("dirop_recreate_thumbnails")->setEnabled(type == SQ_DirOperator::TypeThumbs && e);
    SQ_ExternalTool::instance()->constPopupMenu()->setEnabled(e);

    if(SQ_HLOptions::instance()->have_categories)
        SQ_CategoriesBox::instance()->popupMenu()->setEnabled(e);
}

void SQ_DirOperator::slotCopyPath()
{
    KFileItem *f = fileview->currentFileItem();

    if(f)
        TQApplication::clipboard()->setText(f->url().path());
}

void SQ_DirOperator::slotCopyURL()
{
    KFileItem *f = fileview->currentFileItem();

    if(f)
        TQApplication::clipboard()->setText(f->url().prettyURL());
}

void SQ_DirOperator::slotEditMime()
{
    KFileItem *f = fileview->currentFileItem();

    if(f)
        KonqOperations::editMimeType(f->mimetype());
}

void SQ_DirOperator::slotAddToBasket()
{
    SQ_ImageBasket::instance()->add(*selectedItems());
}

void SQ_DirOperator::slotAddToDirectoryBasket()
{
    SQ_DirectoryBasket::instance()->add(*selectedItems());
}

void SQ_DirOperator::slotFinishedLoading()
{
    // force sort if invisible
    if(!isVisible())
    {
        // derived from KFileDetailView
        if(type ==  SQ_DirOperator::TypeDetailed)
        {
            KFileDetailView *dv = dynamic_cast<KFileDetailView *>(fileview);
            if(dv) dv->sort();
        }
        // derived from KFileIconView
        else
        {
            KFileIconView *tv = dynamic_cast<KFileIconView *>(fileview);
            if(tv) tv->sort(!tv->isReversed());
        }
    }

    int dirs = numDirs(), files = numFiles();
    int total = dirs+files;

    slotUpdateInformation(files, dirs);

    if(lasturl.equals(url(), true))
        return;
    else
    {
        lasturl = url();
        usenew = true;
    }

    // clear TQLabels in statusbar, if current directory has
    // no files/directories
    if(!total)
    {
        lasturl = KURL();
        usenew = false;
        m_pending = TQString();
        KSquirrel::app()->sbarWidget("fileIcon")->clear();
        KSquirrel::app()->sbarWidget("fileName")->clear();
        return;
    }

    // setting current file directly doesn't work. let
    // events to be processed and only then set current file
    TQTimer::singleShot(0, this, TQT_SLOT(slotDelayedFinishedLoading()));
}

void SQ_DirOperator::slotDelayedFinishedLoading()
{
    // select pending file if any
    if(!m_pending.isEmpty())
    {
        if(fileview)
        {
            fileview->clearSelection();
            fileview->setCurrentItem(m_pending);
            setCurrentItem(fileview->currentFileItem());
        }

        m_pending = TQString();
        startOrNotThumbnailUpdate();

        return;
    }

    KURL up_url = url().upURL();

    KFileItem *first = fileview->firstFileItem();

    if(!first) return;

    // ignore ".." item
    if(up_url.equals(first->url(), true))
        first = fileview->nextItem(first);

    // hell!
    if(!first) return;

    SQ_Config::instance()->setGroup("Fileview");

    bool goto_first = false;

    // SQ_WidgetStack will select first supported image for us
    if(SQ_Config::instance()->readBoolEntry("tofirst", true))
        goto_first = SQ_WidgetStack::instance()->moveTo(SQ_WidgetStack::Next, first) == SQ_WidgetStack::moveFailed;
    else
        goto_first = true;

    if(goto_first)
    {
        KFileItem *fi = fileview->currentFileItem();

        if(fi && !up_url.equals(fi->url(), true))
            setCurrentItem(fileview->currentFileItem());
        else
            setCurrentItem(first);
    }

    startOrNotThumbnailUpdate();
}

void SQ_DirOperator::slotUpdateInformation(int files, int dirs)
{
    int total = dirs + files;

    SQ_Config::instance()->setGroup("Fileview");

    TQString str = i18n("Total %1 in %2 (%3, %4)")
                    .arg(TDEIO::convertSize(totalSize))
                    .arg(i18n("1 item", "%n items", total))
                    .arg(i18n("1 folder", "%n folders", dirs))
                    .arg(i18n("1 file", "%n files", files));

    KSquirrel::app()->sbarWidget("dirInfo")->setText(str);
}

/*
 *  Deselect all items, set current item, select this item,
 *  and ensure it visible.
 */
void SQ_DirOperator::setCurrentItem(KFileItem *item)
{
    // for safety...
    if(!item)
        return;

    // clear selection
    fileview->clearSelection();

    // set current item and select it
    fileview->setCurrentItem(item);
    fileview->setSelected(item, true);

    // make this item visible
    fileview->ensureItemVisible(item);
}

/*
 *  Change thumbnail size. KSquirrel will emit thumbSizeChanged() signal, and
 *  diroperator will catch it. Only diroperator, which manages SQ_FileThumbView
 *  won't ignore this signal. See also SQ_WidgetStack::raiseWidget().
 */
void SQ_DirOperator::slotSetThumbSize(const TQString &size)
{
    // for safety...
    if(type != SQ_DirOperator::TypeThumbs)
        return;

    SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

    SQ_ThumbnailSize::instance()->setPixelSize(size);

    tv->rebuildCachedPixmaps();

    // reinsert files
    removeCdUpItem();
    smartUpdate();

    // finally, update thumbnails
    startOrNotThumbnailUpdate();
}

/*
 *  Smart update. Store all file items, reset view,
 *  and transfer all items back.
 *
 *  Taken from tdelibs-3.2.3/tdeio/tdefile/tdediroperator.cpp
 */
void SQ_DirOperator::smartUpdate()
{
    // store old file items
    KFileItemList items = *fileview->items();
    oldSelected = *fileview->selectedItems();
    oldCurrentItem = fileview->currentFileItem();

    // heh, 'items' now contains "..", we should remove it!
    removeCdUpItem();

    // clear view and add items (see SQ_FileThumbView::insertItem() for more)
    fileview->clear();

    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_Config::instance()->setGroup("Thumbnails");

        int newgrid = SQ_ThumbnailSize::instance()->extended() ?
                        SQ_ThumbnailSize::instance()->extendedSize().width()
                        : SQ_ThumbnailSize::instance()->pixelSize();

        newgrid += (SQ_Config::instance()->readNumEntry("margin", 2) + 2);

        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        tv->setGridX(newgrid);
        tv->arrangeItemsInGrid();
    }

    fileview->addItemList(items);

    selectOld();
}

void SQ_DirOperator::selectOld()
{
    // set current item...
    fileview->setCurrentItem(oldCurrentItem);

    // restore selection
    KFileItemListIterator it(oldSelected);

    fileview->ensureItemVisible(it.current());

    for(; it.current();++it)
        fileview->setSelected(it.current(), true);

    oldSelected.clear();

    stopPreview();
}

/*
 *  Remove ".." from currently listed items
 */
void SQ_DirOperator::removeCdUpItem()
{
    // for safety...
    if(!fileview)
        return;

    KFileItemList m_items = *fileview->items();
    KFileItemListIterator it_s(m_items);
    KURL up_url = url().upURL();

    for(; it_s.current();++it_s)
    {
        if(it_s.current()->url() == up_url)
        {
            fileview->removeItem(it_s.current());
            break;
        }
    }
}

void SQ_DirOperator::startOrNotThumbnailUpdate()
{
    // start delayed thumbnail update, if needed
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        // start thumbnail update only when this
        // diroperator is visible
        if(tv)
        {
            if(tv->isVisible())
                tv->startThumbnailUpdate();
            else
                tv->waitForShowEvent();
        }
    }
}

void SQ_DirOperator::clearListers()
{
    SQ_Listers::iterator itEnd = listers.end();

    // delete all listers
    for(SQ_Listers::iterator it = listers.begin();it != itEnd;++it)
        delete it.data();

    listers.clear();
}

void SQ_DirOperator::urlAdded(const KURL &_url)
{
    // avoid duplications
    if(listers.contains(_url) || _url.equals(url(), true))
        return;

    KDirLister *lister = new KDirLister(true);

    lister->setMainWindow(dirLister()->mainWindow());

    // redirect all signals to dirLister() - nice hack
    connect(lister, TQT_SIGNAL(refreshItems(const KFileItemList &)), dirLister(), TQT_SIGNAL(refreshItems(const KFileItemList &)));
    connect(lister, TQT_SIGNAL(deleteItem(KFileItem *)), dirLister(), TQT_SIGNAL(deleteItem(KFileItem *)));
    connect(lister, TQT_SIGNAL(newItems(const KFileItemList &)), dirLister(), TQT_SIGNAL(newItems(const KFileItemList &)));

    connect(lister, TQT_SIGNAL(started(const KURL &)), dirLister(), TQT_SIGNAL(started(const KURL &)));
    connect(lister, TQT_SIGNAL(completed()), dirLister(), TQT_SIGNAL(completed()));
    connect(lister, TQT_SIGNAL(canceled()), dirLister(), TQT_SIGNAL(canceled()));

    // save this lister for us
    listers[_url] = lister;

    lister->openURL(_url);
}

void SQ_DirOperator::urlRemoved(const KURL &url)
{
    // for safety
    SQ_Listers::iterator it = listers.find(url);

    if(it == listers.end())
        return;

    KDirLister *lister = it.data();

    KFileItemList list = lister->items();

    // start delayed thumbnail update, if needed
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv && tv->updateRunning())
            tv->itemsRemoved(list);
    }

    SQ_Config::instance()->setGroup("Fileview");
    bool c = SQ_Config::instance()->readBoolEntry("calculate", true);

    for(KFileItem *itit_tvoyu_mats = list.first();itit_tvoyu_mats;itit_tvoyu_mats = list.next())
    {
        itemDeleted(itit_tvoyu_mats);

        if(c) totalSize -= itit_tvoyu_mats->size();
    }

    slotUpdateInformation(numFiles(), numDirs());

    // remove this lister from map
    listers.remove(url);

    delete lister;

    // update view
    KFileIconView *vi = dynamic_cast<KFileIconView *>(fileview);

    if(vi)
       vi->arrangeItemsInGrid();
}

void SQ_DirOperator::executePrivate(KFileItem *fi)
{
    KURL url = fi->url();
    TQString fullpath = url.path();
    TQFileInfo fm(fullpath);

    // Ok, this file is image file. Let's try to load it
    if(SQ_LibraryHandler::instance()->libraryForFile(fullpath))
        SQ_GLWidget::window()->startDecoding(fullpath);
    else
    {
        SQ_Config::instance()->setGroup("Fileview");

        // archive ?
        if(!SQ_ArchiveHandler::instance()->findProtocolByFile(fi).isEmpty())
            if(SQ_Config::instance()->readBoolEntry("archives", true))
                SQ_ArchiveHandler::instance()->tryUnpack(fi);
            else;
        // not image, not archive. Read settings and run this file separately
        // with default application (if needed).
        else if(SQ_Config::instance()->readBoolEntry("run unknown", false))
            emit runSeparately(fi);
    }
}

void SQ_DirOperator::disableSpecificActions(KFileIconView *v)
{
    TDEAction *a;

    a = v->actionCollection()->action("zoomIn");
    if(a) a->setEnabled(false);

    a = v->actionCollection()->action("zoomOut");
    if(a) a->setEnabled(false);

    a = v->actionCollection()->action("show previews");
    if(a) a->setEnabled(false);
}

void SQ_DirOperator::slotPreview()
{
    KFileItem *fi = fileview ? fileview->currentFileItem() : 0;

    if(fi) SQ_PreviewWidget::instance()->load(fi->url());
}

void SQ_DirOperator::slotNewItems(const KFileItemList &list)
{
    SQ_Config::instance()->setGroup("Fileview");

    if(SQ_Config::instance()->readBoolEntry("calculate", true))
    {
        KFileItemListIterator it(list);
        KFileItem *fi;

        while((fi = it.current()) != 0)
        {
            ++it;
            if(fi->isFile()) totalSize += fi->size();
        }
    }

    // start delayed thumbnail update, if needed
    if(type == SQ_DirOperator::TypeThumbs && usenew)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv)
            tv->addItemsToJob(list);
    }
}

void SQ_DirOperator::slotRefreshItems(const KFileItemList &list)
{
    KFileItemListIterator it(list);
    KFileItem *fci = fileview ? fileview->currentFileItem() : 0;
    KFileItem *fi;

    // refresh image been previewed
    while((fi = it.current()))
    {
        if(fi == fci)
        {
            timer_preview->start(SQ_PreviewWidget::instance()->delay(), true);
            break;
        }

        ++it;
    }

    // start delayed thumbnail update, if needed
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv)
        {
            KFileItemListIterator it(list);
            SQ_FileThumbViewItem *tfi;

            if(tv->updateRunning())
                tv->itemsRemoved(list);

            while((fi = it.current()))
            {
                // force thumbnail to be updated by setting listed = false
                tfi = reinterpret_cast<SQ_FileThumbViewItem *>(fi->extraData(tv));

                if(tfi)
                    tfi->setListed(false);

                // remove thumbnail from pixmap cache and from disk
                SQ_PixmapCache::instance()->removeEntryFull(fi->url().path());
                ++it;
            }

            tv->addItemsToJob(list, false);
        }
    }
}

/*
 *  Invoked, when some item has been deleted. We should
 *  remove appropriate thumbnail from pixmap cache.
 */
void SQ_DirOperator::slotItemDeleted(KFileItem *item)
{
    if(!item) return;

    itemKill(item);

    // remove thumbnail from cache,
    SQ_PixmapCache::instance()->removeEntryFull(item->url());
}

void SQ_DirOperator::itemKill(KFileItem *item)
{
    if(!item) return;

    SQ_Config::instance()->setGroup("Fileview");
    if(SQ_Config::instance()->readBoolEntry("calculate", true))
        totalSize -= item->size();

    slotUpdateInformation(numFiles(), numDirs());

    // start delayed thumbnail update, if needed
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv && tv->updateRunning())
            tv->itemRemoved(item);
    }
}

void SQ_DirOperator::stopPreview()
{
    timer_preview->stop();
}

void SQ_DirOperator::activatedMenu(const KFileItem *, const TQPoint &pos)
{
    // remove "View" submenu, since we will insert our's one.
    setupMenu(KDirOperator::AllActions ^ KDirOperator::ViewActions);
    updateSelectionDependentActions();

    TDEActionMenu *pADirOperatorMenu = dynamic_cast<TDEActionMenu *>(actionCollection()->action("popupMenu"));

    pADirOperatorMenu->insert(actionCollection()->action("dirop_edit_mime"));
    pADirOperatorMenu->insert(actionCollection()->action("separator"));

    if(SQ_HLOptions::instance()->have_categories)
        pADirOperatorMenu->popupMenu()->insertItem(i18n("Add to &Category"), SQ_CategoriesBox::instance()->popupMenu());

    if(SQ_HLOptions::instance()->have_imagebasket)
        pADirOperatorMenu->insert(actionCollection()->action("dirop_tobasket"));

    if(SQ_HLOptions::instance()->have_directorybasket)
        pADirOperatorMenu->insert(actionCollection()->action("dirop_todirbasket"));

    pADirOperatorMenu->popupMenu()->insertItem(i18n("File actions"), dynamic_cast<TDEActionMenu *>(actionCollection()->action("dirop_file_menu"))->popupMenu(), -1, 0);
    pADirOperatorMenu->popupMenu()->insertItem(i18n("&External tools"), SQ_ExternalTool::instance()->constPopupMenu(), -1, 1);
    pADirOperatorMenu->popupMenu()->insertSeparator(2);

    KFileItemList *items = const_cast<KFileItemList *>(selectedItems());

    if(items)
        SQ_ExternalTool::instance()->setItems(*items);

    pADirOperatorMenu->popup(pos);
}

void SQ_DirOperator::slotSetURL(const KURL &url)
{
    setURL(url, true);
}

void SQ_DirOperator::calcTotalSize()
{
    totalSize = static_cast<TDEIO::filesize_t>(0);

    KFileItemList *list = const_cast<KFileItemList *>(fileview->items());

    if(!list) return;

    KFileItemListIterator it(*list);
    KFileItem *fi;

    while((fi = it.current()) != 0)
    {
        ++it;

        if(fi->isFile())
            totalSize += fi->size();
    }

    slotUpdateInformation(numFiles(), numDirs());
}

void SQ_DirOperator::saveConfig()
{
    TQDir::SortSpec sort = sorting();

    SQ_Config::instance()->writeEntry("sorting_name", KFile::isSortByName(sort));
    SQ_Config::instance()->writeEntry("sorting_time", KFile::isSortByDate(sort));
    SQ_Config::instance()->writeEntry("sorting_size", KFile::isSortBySize(sort));
    SQ_Config::instance()->writeEntry("sorting_dirs", KFile::isSortDirsFirst(sort));
    SQ_Config::instance()->writeEntry("sorting_reverse", (sort & TQDir::Reversed) == TQDir::Reversed);
    SQ_Config::instance()->writeEntry("sorting_ignore", KFile::isSortCaseInsensitive(sort));

    SQ_Config::instance()->writeEntry("show hidden", showHiddenFiles());
}

void SQ_DirOperator::setLazy(bool l, int delay, int rows)
{
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv)
        {
            if(tv->lazy() != l)
                tv->setLazy(l, delay);

            tv->setAddRows(rows);

            if(tv->updateRunning())
                tv->startThumbnailUpdate();
        }
    }
}

void SQ_DirOperator::slotInvokeBrowser()
{
    kapp->invokeBrowser(url().prettyURL());
}

void SQ_DirOperator::enableThumbnailActions(bool enable)
{
    actionCollection()->action("dirop_recreate_thumbnails")->setEnabled(enable);
}

void SQ_DirOperator::stopThumbnailUpdate()
{
    if(type == SQ_DirOperator::TypeThumbs)
    {
        SQ_FileThumbView *tv = dynamic_cast<SQ_FileThumbView *>(fileview);

        if(tv && tv->updateRunning())
            tv->stopThumbnailUpdate();
    }
}

void SQ_DirOperator::slotDownloadPercents(int p)
{
    SQ_GLWidget::window()->setDownloadPercents(p);
}

#include "sq_diroperator.moc"