summaryrefslogtreecommitdiff
path: root/core/launcher/launcherview.cpp
blob: 6275fcb36355596687d4e21c02e2c6e6f7ef16f5 (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
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
/*
                             This file is part of the Opie Project
              =.             (C) 2000-2002 Trolltech AS
            .=l.             (C) 2002-2005 The Opie Team <opie-devel@handhelds.org>
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
    :`=1 )Y*s>-.--   :       the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; version 2 of the License.
     ._= =}       :
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
    :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/
#include "launcherview.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oresource.h>
#include <qpe/config.h>
#include <qtopia/qpeapplication.h>
#include <qtopia/private/categories.h>
#include <qtopia/categoryselect.h>
#include <qtopia/mimetype.h>
using namespace Opie::Core;

#include <qpe/qcopenvelope_qws.h>

/* QT */
#include <qtimer.h>
#include <qfileinfo.h>
#include <qiconview.h>
#include <qobjectlist.h>


// These define how the busy icon is animated and highlighted
#define BRIGHTEN_BUSY_ICON
//#define ALPHA_FADE_BUSY_ICON
//#define USE_ANIMATED_BUSY_ICON_OVERLAY
#define BOUNCE_BUSY_ICON

typedef QMap<QString,QPixmap>::Iterator pixiter;

class BgPixmap
{
public:
    BgPixmap( const QPixmap &p ) : pm(p), ref(1) {}
    QPixmap pm;
    int ref;
};


static QMap<QString,BgPixmap*> *bgCache = 0;

static void cleanup_cache()
{
    QMap<QString,BgPixmap*>::Iterator it = bgCache->begin();
    while ( it != bgCache->end() ) {
    QMap<QString,BgPixmap*>::Iterator curr = it;
    ++it;
    delete (*curr);
    bgCache->remove( curr );
    }
    delete bgCache;
    bgCache = 0;
}


class LauncherItem : public QIconViewItem
{
public:
    enum iconstate_t {
        BASE_ICON,
        WAITING_ICON,
        EYE_ICON
    };

    LauncherItem( QIconView *parent, AppLnk* applnk, bool bigIcon=TRUE );
    ~LauncherItem();

    AppLnk *appLnk() const { return app; }
    AppLnk *takeAppLnk() { AppLnk* r=app; app=0; return r; }

    void animateIcon();
    void resetIcon();
    bool isEyeImage()const{return m_EyeImage;}

    virtual int compare ( QIconViewItem * i ) const;
    void paintItem( QPainter *p, const QColorGroup &cg );

    void setBusyIndicatorType ( BusyIndicatorType t ) { busyType = t; }
    void setEyePixmap(const QPixmap&aIcon);
    virtual QPixmap*pixmap()const;

protected:
    bool isBigIcon;
    int iteration;
    AppLnk* app;

private:
    void paintAnimatedIcon( QPainter *p );
    BusyIndicatorType busyType;
    int psize;
    bool m_EyeImage;
    iconstate_t m_EyeImageSet;
};

static bool s_IgnoreNextPix = false;

LauncherItem::LauncherItem( QIconView *parent, AppLnk *applnk, bool bigIcon )
    : QIconViewItem( parent, applnk->name(),
           bigIcon ? applnk->bigPixmap() :applnk->pixmap() ),
    isBigIcon( bigIcon ),
    iteration(0),
    app(applnk), // Takes ownership
    psize( (bigIcon ? applnk->bigPixmap().width() :applnk->pixmap().width() ) ),
    m_EyeImage(false),
    m_EyeImageSet(BASE_ICON)
{
    if (applnk->type().lower().startsWith("image/") && applnk->exec().contains("opie-eye",false)) {
        m_EyeImage = true;
        QMap<QString,QPixmap>::Iterator it = LauncherIconView::sm_EyeCache->find(applnk->file());
        if (it != LauncherIconView::sm_EyeCache->end()) {
            m_EyeImageSet = EYE_ICON;
            setPixmap(*it);
        }
    }
}

LauncherItem::~LauncherItem()
{
    LauncherIconView* liv = (LauncherIconView*)iconView();
    if ( liv->busyItem() == this )
        liv->setBusy(FALSE);
    delete app;
}

QPixmap*LauncherItem::pixmap()const
{
    if (m_EyeImage && m_EyeImageSet == BASE_ICON && s_IgnoreNextPix==false) {
        LauncherIconView* liv = (LauncherIconView*)iconView();
        liv->requestEyePix(this);
    }
    return QIconViewItem::pixmap();
}

int LauncherItem::compare ( QIconViewItem * i ) const
{
    LauncherIconView* view = (LauncherIconView*)iconView();
    return view->compare(app,((LauncherItem *)i)->appLnk());
}

void LauncherItem::paintItem( QPainter *p, const QColorGroup &cg )
{
    LauncherIconView* liv = (LauncherIconView*)iconView();
    QBrush oldBrush( liv->itemTextBackground() );
    QColorGroup mycg( cg );
    if ( liv->currentItem() == this ) {
        liv->setItemTextBackground( cg.brush( QColorGroup::Highlight ) );
        mycg.setColor( QColorGroup::Text, cg.color( QColorGroup::HighlightedText ) );
    }

    QIconViewItem::paintItem(p,mycg);

    // Paint animation overlay
    if ( liv->busyItem() == this )
        paintAnimatedIcon(p);

    if ( liv->currentItem() == this )
        liv->setItemTextBackground( oldBrush );
}

void LauncherItem::paintAnimatedIcon( QPainter *p )
{
    LauncherIconView* liv = (LauncherIconView*)iconView();
    int pic = iteration % 16;
    int w = pixmap()->width(), h = pixmap()->height();
    QPixmap dblBuf( w, h + 4 );
    QPainter p2( &dblBuf );
    int x1, y1;
    if ( liv->itemTextPos() == QIconView::Bottom ) {
    x1 = x() + (width() - w) / 2 - liv->contentsX();
    y1 = y() - liv->contentsY();
    } else {
    x1 = x() - liv->contentsX();
    y1 = y() + (height() - h) / 2 - liv->contentsY();
    }
    y1 -= 2;
    p2.translate(-x1,-y1);
    liv->drawBackground( &p2, QRect(x1,y1,w,h+4) );
    int bounceY = 2;
#ifdef BOUNCE_BUSY_ICON
    if ( busyType == BIT_Animated ) {
       bounceY = 4 - ((iteration+2)%8);
       bounceY = bounceY < 0 ? -bounceY : bounceY;
    }
#endif
    p2.drawPixmap( x1, y1 + bounceY, *pixmap() );
#ifdef USE_ANIMATED_BUSY_ICON_OVERLAY
    p2.drawPixmap( x1, y1 + bounceY, liv->busyPixmap(), w * pic, 0, w, h );
#else
    Q_UNUSED( pic )
#endif
    p->drawPixmap( x1, y1, dblBuf );
}

void LauncherItem::animateIcon()
{
    LauncherIconView* liv = (LauncherIconView*)iconView();

    if ( liv->busyItem() != this || !app )
    return;

    // Highlight the icon
    if ( iteration == 0 ) {
        QPixmap src;
        pixiter it;
        if (isEyeImage() && (it=LauncherIconView::sm_EyeCache->find(appLnk()->file()))!=LauncherIconView::sm_EyeCache->end()) {
            src = (*it);
        } else {
            src = ((isBigIcon ? app->bigPixmap() : app->pixmap()));
        }
        QImage img = src.convertToImage();
        QRgb *rgb;
        int count;
        if ( img.depth() == 32 ) {
            rgb = (QRgb*)img.bits();
            count = img.bytesPerLine()/sizeof(QRgb)*img.height();
        } else {
            rgb = img.colorTable();
            count = img.numColors();
        }
        for ( int r = 0; r < count; r++, rgb++ ) {
#if defined(BRIGHTEN_BUSY_ICON)
            QColor c(*rgb);
            int h, s, v;
            c.hsv(&h,&s,&v);
            c.setHsv(h,QMAX(s-24,0),QMIN(v+48,255));
            *rgb = qRgba(c.red(),c.green(),c.blue(),qAlpha(*rgb));
#elif defined(ALPHA_FADE_BUSY_ICON)
            *rgb = qRgba(qRed(*rgb),qGreen(*rgb),qBlue(*rgb),qAlpha(*rgb)/2);
#endif
        }
        src.convertFromImage( img );
        setPixmap( src );
    }

    iteration++;

    // Paint animation overlay
    QPainter p( liv->viewport() );
    paintAnimatedIcon( &p );
}

void LauncherItem::resetIcon()
{
    iteration = 0;
    if (isEyeImage()) {
        QMap<QString,QPixmap>::Iterator it = LauncherIconView::sm_EyeCache->find(appLnk()->file());
        if (it != LauncherIconView::sm_EyeCache->end()) {
            setPixmap(*it);
            return;
        }
    }
    setPixmap(isBigIcon ? app->bigPixmap() : app->pixmap());
}

void LauncherItem::setEyePixmap(const QPixmap&aIcon)
{
    if (!isEyeImage()) return;
    setPixmap(aIcon);
    m_EyeImageSet = EYE_ICON;
}

//===========================================================================
// Implemantation of LauncherIconview start
//===========================================================================

QMap<QString,QPixmap>* LauncherIconView::sm_EyeCache=0;

LauncherIconView::LauncherIconView( QWidget* parent, const char* name )
    : QIconView(parent,name),tf(""),cf(0),bsy(0),busyTimer(0),bigIcns(TRUE),bgColor(white),numColumns(0)
{
    m_EyeCallBack = 0;
    if (!sm_EyeCache) sm_EyeCache = new QMap<QString,QPixmap>();
    sortmeth = Name;
    hidden.setAutoDelete(TRUE);
    ike = FALSE;
    calculateGrid( Bottom );
    connect(&m_eyeTimer,SIGNAL(timeout()),this,SLOT(stopEyeTimer()));
    Config config( "Launcher" );
    config.setGroup( "GUI" );
    setStaticBackgroundPicture( config.readBoolEntry( "StaticBackground", true ) );
}

LauncherIconView::~LauncherIconView()
{
    odebug << "LauncherIconView::~LauncherIconView()" << oendl;
#if 0 // debuggery
    QListIterator<AppLnk> it(hidden);
    AppLnk* l;
    while ((l=it.current())) {
        ++it;
        //odebug << "" << l << ": hidden (should remove)" << oendl;
    }
#endif
}

void LauncherIconView::unsetPalette()
{
    s_IgnoreNextPix = true;
    QIconView::unsetPalette();
    s_IgnoreNextPix = false;
}

void LauncherIconView::setPalette(const QPalette & palette)
{
    s_IgnoreNextPix = true;
    QIconView::setPalette(palette);
    s_IgnoreNextPix = false;
}

void LauncherIconView::setStaticBackgroundPicture( bool enable )
{
    staticBackground = enable;
    if ( staticBackground )
    {
        setStaticBackground( true );
        verticalScrollBar()->setTracking( false );
    }
    else
    {
        setStaticBackground( false );
        verticalScrollBar()->setTracking( true );
    }
}

int LauncherIconView::compare(const AppLnk* a, const AppLnk* b)
{
    switch (sortmeth) {
    case Name:
        return a->name().lower().compare(b->name().lower());
    case Date: {
        QFileInfo fa(a->linkFileKnown() ? a->linkFile() : a->file());
        QFileInfo fb(b->linkFileKnown() ? b->linkFile() : b->file());
        return fa.lastModified().secsTo(fb.lastModified());
    }
    case Type:
        return a->type().compare(b->type());
    }
    return 0;
}

void LauncherIconView::setSortMethod( SortMethod m )
{
    if ( sortmeth != m ) {
        sortmeth = m;
        sort();
    }
}

void LauncherIconView::setCategoryFilter( int catfilter, bool resort )
{
        if ( catfilter == -2 )
            cf = 0;
        else
            cf = catfilter;
        hideOrShowItems(resort);
}

void LauncherIconView::setTypeFilter(const QString& typefilter, bool resort)
{
    tf = QRegExp(typefilter,FALSE,TRUE);
    hideOrShowItems(resort);
}

void LauncherIconView::setItemTextPos( ItemTextPos pos )
{
    calculateGrid( pos );
    QIconView::setItemTextPos( pos );
}

void LauncherIconView::drawBackground( QPainter *p, const QRect &r )
{
    if ( bgPixmap.isNull() )
    {
        p->fillRect( r, bgColor );
    }
    else
    {
        if ( staticBackground )
        {
            p->drawTiledPixmap( r, bgPixmap, QPoint( r.x() % bgPixmap.width(), r.y() % bgPixmap.height() ) );
        }
        else
        {
        p->drawTiledPixmap( r, bgPixmap, QPoint( (r.x() + contentsX()) % bgPixmap.width(),
                                                 (r.y() + contentsY()) % bgPixmap.height() ) );
        }
    }
}

void LauncherIconView::addCatsAndMimes(AppLnk* app)
{
    //  QStringList c = app->categories();
    //  for (QStringList::ConstIterator cit=c.begin(); cit!=c.end(); ++cit) {
    //      cats.replace(*cit,(void*)1);
    //  }
    QString maj=app->type();
    int sl=maj.find('/');
    if (sl>=0) {
        QString k;
        k = maj.left(12) == "application/" ? maj : maj.left(sl);
        mimes.replace(k,(void*)1);
    }
}

void LauncherIconView::setBusy(bool on)
{
#ifdef USE_ANIMATED_BUSY_ICON_OVERLAY
    if ( busyPix.isNull() ) {
        int size = ( bigIcns ) ? AppLnk::bigIconSize() : AppLnk::smallIconSize();
        busyPix.convertFromImage( OResource::loadImage( "busy", OResource::NoScale ).smoothScale( size * 16, size ) );
    }
#endif

    if ( on ) {
        busyTimer = startTimer( 100 );
    } else {
        if ( busyTimer ) {
            killTimer( busyTimer );
        busyTimer = 0;
        }
    }

    LauncherItem *c = on ? (LauncherItem*)currentItem() : 0;

    if ( bsy != c ) {
        LauncherItem *oldBusy = bsy;
        bsy = c;
        if ( oldBusy )  {
        oldBusy->resetIcon();
                    }
        if ( bsy )   {
                                bsy->setBusyIndicatorType( busyType ) ;
        bsy->animateIcon();
                    }
    }
}

void LauncherIconView::clear()
{
    mimes.clear();
    cats.clear();
    QIconView::clear();
    hidden.clear();
}

QStringList LauncherIconView::mimeTypes() const
{
    QStringList r;
    QDictIterator<void> it(mimes);
    while (it.current()) {
        r.append(it.currentKey());
        ++it;
    }
    r.sort();
    return r;
}

LauncherItem*LauncherIconView::findDocItem(const QString&fname)
{
    LauncherItem* item = (LauncherItem*)firstItem();
    while (item) {
        if (item->appLnk()->file()==fname) {
            break;
        }
        item = (LauncherItem*)item->nextItem();
    }
    return item;
}

void LauncherIconView::setEyePixmap(const QPixmap&aPixmap,const QString&aFile,int width)
{
    int s = ( bigIcns ) ? AppLnk::bigIconSize() : AppLnk::smallIconSize();
    if (s!=width) return;
    LauncherItem*item = findDocItem(aFile);
    if (!item||!item->isEyeImage()) return;
    (*sm_EyeCache)[aFile]=aPixmap;
    item->setEyePixmap(aPixmap);
}

void LauncherIconView::checkCallback()
{
    if (!m_EyeCallBack) {
        m_EyeCallBack = new LauncherThumbReceiver();
        connect(m_EyeCallBack,SIGNAL(sig_Thumbnail(const QPixmap&,const QString&,int)),
            this,SLOT(setEyePixmap(const QPixmap&,const QString&,int)));
        m_eyeTimer.changeInterval(600000);
    }
}

void LauncherIconView::addCheckItem(AppLnk* app)
{
    LauncherItem*item = new LauncherItem( this, app, bigIcns );
    if (item->isEyeImage()) {
        checkCallback();
    }
}

void LauncherIconView::requestEyePix(const LauncherItem*item)
{
    if (!item) return;
    if (item->isEyeImage()) {
        checkCallback();
        int s = ( bigIcns ) ? AppLnk::bigIconSize() : AppLnk::smallIconSize();
        m_EyeCallBack->requestThumb(item->appLnk()->file(),s,s);
    }
}

void LauncherIconView::stopEyeTimer()
{
    if (m_EyeCallBack) {
        disconnect(m_EyeCallBack,SIGNAL(sig_Thumbnail(const QPixmap&,const QString&,int)),
            this,SLOT(setEyePixmap(const QPixmap&,const QString&,int)));
        delete m_EyeCallBack;
        m_EyeCallBack=0;
    }
    m_eyeTimer.stop();
}

void LauncherIconView::updateCategoriesAndMimeTypes()
{
    mimes.clear();
    cats.clear();
    LauncherItem* item = (LauncherItem*)firstItem();
    while (item) {
        addCatsAndMimes(item->appLnk());
        item = (LauncherItem*)item->nextItem();
    }
    QListIterator<AppLnk> it(hidden);
    AppLnk* l;
    while ((l=it.current())) {
        addCatsAndMimes(l);
        ++it;
    }
}

void LauncherIconView::hideOrShowItems(bool resort)
{
    viewport()->setUpdatesEnabled( FALSE );
    hidden.setAutoDelete(FALSE);
    QList<AppLnk> links=hidden;
    hidden.clear();
    hidden.setAutoDelete(TRUE);
    LauncherItem* item = (LauncherItem*)firstItem();
    while (item) {
        links.append(item->takeAppLnk());
        item = (LauncherItem*)item->nextItem();
    }
    clear();
    QListIterator<AppLnk> it(links);
    AppLnk* l;
    while ((l=it.current())) {
        addItem(l,FALSE);
        ++it;
    }
    if ( resort && !autoArrange() )
    sort();
    viewport()->setUpdatesEnabled( TRUE );
}

bool LauncherIconView::removeLink(const QString& linkfile,bool removeCache)
{
    LauncherItem* item = (LauncherItem*)firstItem();
    AppLnk* l;
    bool did = FALSE;
    DocLnk dl(linkfile);
    while (item) {
        l = item->appLnk();
        LauncherItem *nextItem = (LauncherItem *)item->nextItem();
        if (  l->linkFileKnown() && l->linkFile() == linkfile || l->fileKnown() &&
                 ( l->file() == linkfile || dl.isValid() && dl.file() == l->file() ) ) {
            if (removeCache) sm_EyeCache->remove(l->file());
            delete item;
            did = TRUE;
        }
        item = nextItem;
    }
    QListIterator<AppLnk> it(hidden);
    while ((l=it.current())) {
        ++it;
        if ( l->linkFileKnown() && l->linkFile() == linkfile
                || l->file() == linkfile
                || dl.isValid() && dl.file() == l->file() ) {
            hidden.removeRef(l);
           did = TRUE;
        }
    }
    return did;
}

void LauncherIconView::addItem(AppLnk* app, bool resort)
{
    addCatsAndMimes(app);
    if ( (tf.isEmpty() || tf.match(app->type()) >= 0)
     && (cf == 0 || app->categories().contains(cf)
         || cf == -1 && app->categories().count() == 0 ) ) {
        addCheckItem(app);
    } else {
        hidden.append(app);
    }
    if ( resort ){
        sort();
    }
}

void LauncherIconView::changeItem(const AppLnk&old,AppLnk*nlink)
{
    QString oldfile = old.file();
    QString newfile = nlink->file();

    if (newfile != oldfile) {
        QMap<QString,QPixmap>::Iterator it = sm_EyeCache->find(oldfile);
        if (it != sm_EyeCache->end()) {
            (*sm_EyeCache)[newfile]=(*it);
        }
        removeLink(old.linkFile());
    } else {
        removeLink(old.linkFile(),false);
    }
    addItem(nlink,false);
}

void LauncherIconView::timerEvent( QTimerEvent *te )
{
    if ( te->timerId() == busyTimer ) {
        if ( bsy )
        bsy->animateIcon();
    } else {
        QIconView::timerEvent( te );
    }
}

void LauncherIconView::setBigIcons( bool bi )
{
    sm_EyeCache->clear();
    bigIcns = bi;
#ifdef USE_ANIMATED_BUSY_ICON_OVERLAY
    busyPix.resize(0,0);
#endif
}

QIconViewItem* LauncherIconView::busyItem() const
{
    return bsy;
}

void LauncherIconView::setBusyIndicatorType ( BusyIndicatorType t ) { busyType = t; }

void LauncherIconView::calculateGrid( ItemTextPos pos )
{
        int dw = QApplication::desktop()->width();
        int viewerWidth = dw-style().scrollBarExtent().width();
        if ( pos == Bottom ) {
            if( !numColumns ) {
                if ( viewerWidth <= 200 ) numColumns = 2;
                else if ( viewerWidth >= 400 ) numColumns = viewerWidth/96;
                else numColumns = 3;
            }
            setSpacing( 4 );
            setGridX( (viewerWidth-(numColumns+1)*spacing())/numColumns );
            setGridY( fontMetrics().height()*2+24 );
        } else {
            if( !numColumns ) {
                if ( viewerWidth < 150 ) numColumns = 1;
                else if ( viewerWidth >= 400 ) numColumns = viewerWidth/150;
                else numColumns = 2;
            }
            setSpacing( 2 );
            setGridX( (viewerWidth-(numColumns+1)*spacing())/numColumns );
            setGridY( fontMetrics().height()+2 );
        }
}

void LauncherIconView::styleChange( QStyle &old )
{
    QIconView::styleChange( old );
    calculateGrid( itemTextPos() );
}

void LauncherIconView::keyPressEvent(QKeyEvent* e)
{
    ike = TRUE;
    if ( e->key() == Key_F33 /* OK button */ || e->key() == Key_Space ) {
        if ( (e->state() & ShiftButton) )
            emit mouseButtonPressed(ShiftButton, currentItem(), QPoint() );
         else
            returnPressed(currentItem());
    }

    QIconView::keyPressEvent(e);
    ike = FALSE;
}

//===========================================================================
// Implemantation of LauncherIconview end
//===========================================================================


//===========================================================================
LauncherView::LauncherView( QWidget* parent, const char* name, WFlags fl )
    : QVBox( parent, name, fl )
{
    catmb = 0;
    icons = new LauncherIconView( this );
    setFocusProxy(icons);
    QPEApplication::setStylusOperation( icons->viewport(), QPEApplication::RightOnHold );

    icons->setItemsMovable( FALSE );
    icons->setAutoArrange( TRUE );
    icons->setSorting( TRUE );
    icons->setFrameStyle( QFrame::NoFrame );
    icons->setMargin( 0 );
    icons->setSelectionMode( QIconView::NoSelection );
    icons->setBackgroundMode( PaletteBase );
    icons->setResizeMode( QIconView::Adjust );
    vmode = (ViewMode)-1;
    setViewMode( Icon );

    connect( icons, SIGNAL(mouseButtonClicked(int,QIconViewItem*,const QPoint&)),
           SLOT(itemClicked(int,QIconViewItem*)) );
    connect( icons, SIGNAL(selectionChanged()),
           SLOT(selectionChanged()) );
    connect( icons, SIGNAL(returnPressed(QIconViewItem*)),
           SLOT(returnPressed(QIconViewItem*)) );
    connect( icons, SIGNAL(mouseButtonPressed(int,QIconViewItem*,const QPoint&)),
           SLOT(itemPressed(int,QIconViewItem*)) );

    tools = 0;
    setBackgroundType( Ruled, QString::null );
}

LauncherView::~LauncherView()
{
    if ( bgCache && bgCache->contains( bgName ) )
    (*bgCache)[bgName]->ref--;
}


bool LauncherView::bsy=FALSE;

void LauncherView::setBusy(bool on)
{
    icons->setBusy(on);
}

void LauncherView::setBusyIndicatorType( const QString& type ) {
    if ( type. lower ( ) == "animated" )
    icons->setBusyIndicatorType(  BIT_Animated  ) ;
    else
    icons->setBusyIndicatorType(  BIT_Normal  ) ;
}

void LauncherView::hideIcons()
{
    icons->hide();
}

void LauncherView::setToolsEnabled(bool y)
{
    if ( !y != !tools ) {
        if ( y ) {
            tools = new QHBox(this);
            // Type filter
            typemb = new QComboBox(tools);
            QSizePolicy p = typemb->sizePolicy();
            p.setHorData(QSizePolicy::Expanding);
            typemb->setSizePolicy(p);
            // Category filter
            updateTools();
            tools->show();
        } else {
            delete tools;
            tools = 0;
        }
    }
}

void LauncherView::updateTools()
{
    disconnect( typemb, SIGNAL(activated(int)),
            this, SLOT(showType(int)) );
    if ( catmb ) {
        disconnect( catmb, SIGNAL(signalSelected(int)),this,SLOT(showCategory(int)));
    }

    // ### I want to remove this
    icons->updateCategoriesAndMimeTypes();

    QString prev;

    // Type filter
    QStringList types;
    typelist = icons->mimeTypes();
    for (QStringList::ConstIterator it = typelist.begin(); it!=typelist.end(); ++it) {
        QString t = *it;
        if ( t.left(12) == "application/" ) {
            MimeType mt(t);
            const AppLnk* app = mt.application();
            if ( app )
                t = app->name();
            else
                t = t.mid(12);
        } else {
            t[0] = t[0].upper();
        }
        types += t;
    }
    types << tr("All types");
    prev = typemb->currentText();
    typemb->clear();
    typemb->insertStringList(types);
    for (int i=0; i<typemb->count(); i++) {
        if ( typemb->text(i) == prev ) {
            typemb->setCurrentItem(i);
            break;
        }
    }
    if ( prev.isNull() ) {
        typemb->setCurrentItem(typemb->count()-1);
    }

    int pcat = catmb ? catmb->currentCategory() : -2;
    if ( !catmb ) {
        catmb = new CategorySelect(tools);
    } else if (pcat!=-2) {

    }
    Categories cats( 0 );
    cats.load( categoryFileName() );
    QArray<int> vl( 0 );
    catmb->setCategories( vl, "Document View", // No tr
        tr("Document View") );
    catmb->setRemoveCategoryEdit( TRUE );
    catmb->setAllCategories( TRUE );
    catmb->setCurrentCategory(pcat);

    // if type has changed we need to redisplay
    if ( typemb->currentText() != prev )
        showType( typemb->currentItem() );

    connect(typemb, SIGNAL(activated(int)), this, SLOT(showType(int)));
    connect(catmb, SIGNAL(signalSelected(int)), this, SLOT(showCategory(int)));
}

void LauncherView::sortBy(int s)
{
    icons->setSortMethod((LauncherIconView::SortMethod)s);
}

void LauncherView::showType(int t)
{
    if ( t >= (int)typelist.count() ) {
        icons->setTypeFilter("",TRUE);
    } else {
        QString ty = typelist[t];
        if ( !ty.contains('/') )
            ty += "/*";
        icons->setTypeFilter(ty,TRUE);
    }
}

void LauncherView::showCategory( int c )
{
    icons->setCategoryFilter( c, TRUE );
}

void LauncherView::setViewMode( ViewMode m )
{
    odebug << "LauncherView::setViewMode( ViewMode m )" << oendl;
    if ( vmode != m ) {
        bool bigIcons = m == Icon;
        icons->viewport()->setUpdatesEnabled( FALSE );
        icons->setBigIcons( bigIcons );
        switch ( m ) {
            case List:
            icons->setItemTextPos( QIconView::Right );
        break;
        case Icon:
            icons->setItemTextPos( QIconView::Bottom );
        break;
        }
        icons->hideOrShowItems( FALSE );
        icons->viewport()->setUpdatesEnabled( TRUE );
        vmode = m;
    }
}

//
// User images may require scaling.
//
QImage LauncherView::loadBackgroundImage(QString &bgName)
{
    QImageIO imgio;
    QSize   ds = qApp->desktop()->size();   // should be launcher, not desktop
    bool    further_scaling = TRUE;

    imgio.setFileName( bgName );
    imgio.setParameters("GetHeaderInformation");

    if (imgio.read() == FALSE) {
    return imgio.image();
    }

    if (imgio.image().width() < ds.width() &&
        imgio.image().height() < ds.height()) {
    further_scaling = FALSE;
    }

    if (!imgio.image().bits()) {
    //
    // Scale and load.  Note we don't scale up.
    //
    QString param( "Scale( %1, %2, ScaleMin )" ); // No tr
    imgio.setParameters(further_scaling ?
        param.arg(ds.width()).arg(ds.height()).latin1() :
        "");
    imgio.read();
    } else {
    if (further_scaling) {
        int t1 = imgio.image().width() * ds.height();
        int t2 = imgio.image().height() * ds.width();
        int dsth = ds.height();
        int dstw = ds.width();

        if (t1 > t2) {
        dsth = t2 / imgio.image().width();
        } else {
        dstw = t1 / imgio.image().height();
        }

        //
        // Loader didn't scale for us.  Do it manually.
        //
        return imgio.image().smoothScale(dstw, dsth);
    }
    }

    return imgio.image();
}

void LauncherView::setBackgroundType( BackgroundType t, const QString &val )
{
    if ( !bgCache ) {
    bgCache = new QMap<QString,BgPixmap*>;
    qAddPostRoutine( cleanup_cache );
    }

    if ( bgCache->contains( bgName ) )
    (*bgCache)[bgName]->ref--;
    bgName = "";

    QPixmap bg;

    switch ( t ) {
    case Ruled: {
        bgName = QString("Ruled_%1").arg(colorGroup().background().name()); // No tr
        if ( bgCache->contains( bgName ) ) {
        (*bgCache)[bgName]->ref++;
        bg = (*bgCache)[bgName]->pm;
        } else {
        bg.resize( width(), 9 );
        QPainter painter( &bg );
        for ( int i = 0; i < 3; i++ ) {
            painter.setPen( white );
            painter.drawLine( 0, i*3, width()-1, i*3 );
            painter.drawLine( 0, i*3+1, width()-1, i*3+1 );
            painter.setPen( colorGroup().background().light(105) );
            painter.drawLine( 0, i*3+2, width()-1, i*3+2 );
        }
        painter.end();
        bgCache->insert( bgName, new BgPixmap(bg) );
        }
        break;
    }

    case Image:
        if (!val.isEmpty()) {
        bgName = val;
        if ( bgCache->contains( bgName ) ) {
            (*bgCache)[bgName]->ref++;
            bg = (*bgCache)[bgName]->pm;
        } else {
            QString imgFile = bgName;
            bool tile = FALSE;
            if ( imgFile[0]!='/' || !QFile::exists(imgFile) ) {
            imgFile = OResource::findPixmap( imgFile );
            tile = TRUE;
            }
            QImage img = loadBackgroundImage(imgFile);


            if ( img.depth() == 1 )
            img = img.convertDepth(8);
            img.setAlphaBuffer(FALSE);
            bg.convertFromImage(img);
            bgCache->insert( bgName, new BgPixmap(bg) );
        }
        }
        break;

    case SolidColor:
    default:
        break;
    }

    const QObjectList *list = queryList( "QWidget", 0, FALSE );
    QObject *obj;
    for ( QObjectListIt it( *list ); (obj=it.current()); ++it ) {
    if ( obj->isWidgetType() ) {
        QWidget *w = (QWidget*)obj;
        w->setBackgroundPixmap( bg );
        if ( bgName.isEmpty() ) {
        // Solid Color
        if ( val.isEmpty() )
            w->setBackgroundColor( colorGroup().base() );
        else
            w->setBackgroundColor( val );
        } else {
        // Ruled or Image pixmap
        w->setBackgroundOrigin( ParentOrigin );
        }
    }
    }
    delete list;

    bgType = t;
    icons->viewport()->update();

    QTimer::singleShot( 1000, this, SLOT(flushBgCache()) );
}

void LauncherView::setColNumber( int num )
{
    icons->setColNumber( num );
}

void LauncherIconView::setColNumber( int num )
{
    numColumns = num;
    calculateGrid( Bottom );
}

void LauncherView::setTextColor( const QColor &tc )
{
    textCol = tc;
    QColorGroup cg = icons->colorGroup();
    cg.setColor( QColorGroup::Text, tc );
    icons->setPalette( QPalette(cg,cg,cg) );
    icons->viewport()->update();
}

void LauncherView::setViewFont( const QFont &f )
{
    icons->setFont( f );
    icons->hideOrShowItems( FALSE );
}

void LauncherView::clearViewFont()
{
    icons->unsetFont();
    icons->hideOrShowItems( FALSE );
}

void LauncherView::resizeEvent(QResizeEvent *e)
{
//  qDebug("LauncherView resize event");
    QVBox::resizeEvent( e );
// commented out for launcherview and qt/e 2.3.8 problems, probably needs real fixing somewhere...
//    if ( e->size().width() != e->oldSize().width() )
    sort();
}

void LauncherView::selectionChanged()
{
    QIconViewItem* item = icons->currentItem();
    if ( item && item->isSelected() ) {
    AppLnk *appLnk = ((LauncherItem *)item)->appLnk();
    if ( icons->inKeyEvent() ) // not for mouse press
        emit clicked( appLnk );
    item->setSelected(FALSE);
    }
}

void LauncherView::returnPressed( QIconViewItem *item )
{
    if ( item ) {
    AppLnk *appLnk = ((LauncherItem *)item)->appLnk();
    emit clicked( appLnk );
    }
}

void LauncherView::itemClicked( int btn, QIconViewItem *item )
{
    if ( item ) {
    AppLnk *appLnk = ((LauncherItem *)item)->appLnk();
    if ( btn == LeftButton ) {
        // Make sure it's the item we execute that gets highlighted
        icons->setCurrentItem( item );
        emit clicked( appLnk );
    }
        item->setSelected(FALSE);
    }
}

void LauncherView::itemPressed( int btn, QIconViewItem *item )
{
    if ( item ) {
    AppLnk *appLnk = ((LauncherItem *)item)->appLnk();
    if ( btn == RightButton )
        emit rightPressed( appLnk );
    else if ( btn == ShiftButton )
        emit rightPressed( appLnk );
    item->setSelected(FALSE);
    }
}

void LauncherView::removeAllItems()
{
    odebug << "LauncherView::removeAllItems()" << oendl;
    if (LauncherIconView::sm_EyeCache) LauncherIconView::sm_EyeCache->clear();
    icons->clear();
}

bool LauncherView::removeLink(const QString& linkfile)
{
    return icons->removeLink(linkfile);
}

void LauncherView::addItem(AppLnk* app, bool resort)
{
    icons->addItem(app,resort);
}

void LauncherView::changeItem(const AppLnk&old,AppLnk*nlink)
{
    icons->changeItem(old,nlink);
}

void LauncherView::setSortEnabled( bool v )
{
    icons->setSorting( v );
    if ( v )
    sort();
}

void LauncherView::setUpdatesEnabled( bool u )
{
    icons->setUpdatesEnabled( u );
}

void LauncherView::sort()
{
    icons->sort();
}

void LauncherView::paletteChange( const QPalette &p )
{
    icons->unsetPalette();
    QVBox::paletteChange( p );
    if ( bgType == Ruled )
    setBackgroundType( Ruled, QString::null );
    QColorGroup cg = icons->colorGroup();
    cg.setColor( QColorGroup::Text, textCol );
    icons->setPalette( QPalette(cg,cg,cg) );
}

void LauncherView::fontChanged(const QFont&)
{
    odebug << "LauncherView::fontChanged()" << oendl;
    icons->hideOrShowItems( FALSE );
}

void LauncherView::relayout(void)
{
    icons->hideOrShowItems(FALSE);
}

void LauncherView::flushBgCache()
{
    if ( !bgCache )
    return;
    // remove unreferenced backgrounds.
    QMap<QString,BgPixmap*>::Iterator it = bgCache->begin();
    while ( it != bgCache->end() ) {
    QMap<QString,BgPixmap*>::Iterator curr = it;
    ++it;
    if ( (*curr)->ref == 0 ) {
        delete (*curr);
        bgCache->remove( curr );
    }
    }
}

/*
 * Launcherthumbnail handling for image files
 */

/* special image handling - based on opie eye */
QDataStream &operator>>( QDataStream& s, PixmapInfo& inf ) {
    s >> inf.file >> inf.pixmap >> inf.width >> inf.height;
    return s;
}

QDataStream &operator<<( QDataStream& s, const PixmapInfo& inf) {
    return s << inf.file  << inf.width << inf.height;
}

LauncherThumbReceiver::LauncherThumbReceiver()
    :QObject(),requestTimer(this)
{

    connect(&requestTimer,SIGNAL(timeout()),SLOT(sendRequest()));
    QCopChannel * chan = new QCopChannel( "QPE/opie-eye",this );
    connect(chan, SIGNAL(received(const QCString&,const QByteArray&)),
            this, SLOT(recieve(const QCString&,const QByteArray&)) );

    {
        QCopEnvelope( "QPE/Application/opie-eye_slave", "refUp()" );
    }
}

LauncherThumbReceiver::~LauncherThumbReceiver()
{
    {
        QCopEnvelope( "QPE/Application/opie-eye_slave", "refDown()" );
    }
}

void LauncherThumbReceiver::recieve( const QCString&str, const QByteArray&at )
{
    PixmapInfos pixinfos;
    QDataStream stream( at, IO_ReadOnly );

    /* we are just interested in thumbmails */
    if ( str == "pixmapsHandled(PixmapList)" )
        stream >> pixinfos;

    for ( PixmapInfos::Iterator it = pixinfos.begin(); it != pixinfos.end(); ++it ) {
        emit sig_Thumbnail((*it).pixmap,(*it).file,(*it).width);
    }
}

void LauncherThumbReceiver::requestThumb(const QString&file,int width,int height)
{
    PixmapInfo rItem;
    rItem.file = file;
    rItem.width = width;
    rItem.height = height;
    m_inThumbNail.append(rItem);
    if (!requestTimer.isActive()) {
        requestTimer.start(100,true);
    }
}

void LauncherThumbReceiver::sendRequest()
{
    if (m_inThumbNail.count()>0) {
        QCopEnvelope env("QPE/opie-eye_slave", "pixmapInfos(PixmapInfos)" );
        env << m_inThumbNail;
        m_inThumbNail.clear();
    }
}