summaryrefslogtreecommitdiff
path: root/core/launcher/documentlist.cpp
blob: 35a5d5693e2d1ef0bbbb8bf5df0b8a098954b787 (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
/*
                             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 "documentlist.h"
#include "serverinterface.h"
#include "mediadlg.h"

/* OPIE */
#include <opie2/oglobal.h>
#include <opie2/odebug.h>
#include <opie2/oresource.h>
#include <qtopia/config.h>
#include <qtopia/mimetype.h>
#include <qtopia/private/categories.h>
#include <qtopia/qpeapplication.h>
#include <qtopia/applnk.h>
#include <qtopia/storage.h>
#ifdef Q_WS_QWS
#include <qtopia/qcopenvelope_qws.h>
#endif
using namespace Opie::Core;

/* QT */
#include <qtimer.h>
#include <qfileinfo.h>
#include <qtextstream.h>
#include <qfile.h>
#include <qdir.h>
#include <qpainter.h>
#include <qimage.h>
#include <qcopchannel_qws.h>
#include <qlistview.h>
#include <qlist.h>
#include <qpixmap.h>


AppLnkSet *DocumentList::appLnkSet = 0;

static const int MAX_SEARCH_DEPTH = 10;


class DocumentListPrivate : public QObject {
    Q_OBJECT
public:
    DocumentListPrivate( ServerInterface *gui );
    ~DocumentListPrivate();

    void initialize();

    const QString nextFile();
    const DocLnk *iterate();
    bool store( DocLnk* dl );
    void estimatedPercentScanned();
    void appendDocpath(FileSystem*);


    DocLnkSet dls;
    QDict<void> reference;
    QDictIterator<void> *dit;
    enum { Find, RemoveKnownFiles, MakeUnknownFiles, Done } state;

    QStringList docPaths;
    unsigned int docPathsSearched;

    int searchDepth;
    QDir *listDirs[MAX_SEARCH_DEPTH];
    const QFileInfoList *lists[MAX_SEARCH_DEPTH];
    unsigned int listPositions[MAX_SEARCH_DEPTH];

    StorageInfo *storage;

    int tid;

    ServerInterface *serverGui;

    bool needToSendAllDocLinks;
    bool sendAppLnks;
    bool sendDocLnks;
    bool scanDocs;
};


/*
 * scandocs will be read from Config
 */
DocumentList::DocumentList( ServerInterface *serverGui, bool /*scanDocs*/,
                QObject *parent, const char *name )
 : QObject( parent, name )
{
    appLnkSet = new AppLnkSet( MimeType::appsFolderName() );
    d = new DocumentListPrivate( serverGui );
    d->needToSendAllDocLinks = false;

    Config cfg( "Launcher" );
    cfg.setGroup( "DocTab" );
    d->scanDocs = cfg.readBoolEntry( "Enable", true );
    odebug << "DocumentList::DocumentList() : scanDocs = " << d->scanDocs << "" << oendl;

    QTimer::singleShot( 0, this, SLOT( startInitialScan() ) );
}

void DocumentList::startInitialScan()
{
    reloadAppLnks();
    reloadDocLnks();
}

DocumentList::~DocumentList()
{
    delete appLnkSet;
    delete d;
}


void DocumentList::add( const DocLnk& doc )
{
    if ( d->serverGui && QFile::exists( doc.file() ) )
    d->serverGui->documentAdded( doc );
}


void DocumentList::start()
{
    resume();
}


void DocumentList::pause()
{
    //odebug << "pause " << d->tid << "" << oendl;
    killTimer( d->tid );
    d->tid = 0;
}


void DocumentList::resume()
{
    if ( d->tid == 0 ) {
    d->tid = startTimer( 20 );
    //odebug << "resumed " << d->tid << "" << oendl;
    }
}

/*
void DocumentList::resend()
{
    // Re-emits all the added items to the list (firstly letting everyone know to
    // clear what they have as it is being sent again)
    pause();
    emit allRemoved();
    QTimer::singleShot( 5, this, SLOT( resendWorker() ) );
}


void DocumentList::resendWorker()
{
    const QList<DocLnk> &list = d->dls.children();
    for ( QListIterator<DocLnk> it( list ); it.current(); ++it )
    add( *(*it) );
    resume();
}
*/

void DocumentList::rescan()
{
  owarn << "XXXXXXXXXXX rescan" << oendl;
    pause();
    d->initialize();
    resume();
}


void DocumentList::timerEvent( QTimerEvent *te )
{
    if ( te->timerId() == d->tid ) {
    // Do 3 at a time
        if ( d->serverGui )
            d->serverGui->aboutToAddBegin();
    for (int i = 0; i < 3; i++ ) {
        const DocLnk *lnk = d->iterate();
        if ( lnk ) {
        add( *lnk );
        } else {
        // stop when done
        pause();
        if ( d->serverGui )
            d->serverGui->documentScanningProgress( 100 );
        if ( d->needToSendAllDocLinks )
            sendAllDocLinks();
        break;
        }
        }
        if ( d->serverGui )
            d->serverGui->aboutToAddEnd();
    }
}


void DocumentList::reloadAppLnks()
{
    if ( d->sendAppLnks && d->serverGui ) {
    d->serverGui->applicationScanningProgress( 0 );
    d->serverGui->allApplicationsRemoved();
    }

    delete appLnkSet;
    appLnkSet = new AppLnkSet( MimeType::appsFolderName() );

    if ( d->sendAppLnks && d->serverGui ) {
    static QStringList prevTypeList;
    QStringList types = appLnkSet->types();
    for ( QStringList::Iterator ittypes=types.begin(); ittypes!=types.end(); ++ittypes) {
        if ( !(*ittypes).isEmpty() ) {
        if ( !prevTypeList.contains(*ittypes) ) {
            QString name = appLnkSet->typeName(*ittypes);
            QPixmap pm = appLnkSet->typePixmap(*ittypes);
            QPixmap bgPm = appLnkSet->typeBigPixmap(*ittypes);

            if (pm.isNull())
            {
                pm = OResource::loadImage( "UnknownDocument", OResource::SmallIcon );
                bgPm = OResource::loadImage( "UnknownDocument", OResource::BigIcon );
            }

            //FIXME our current launcher expects docs tab to be last
            d->serverGui->typeAdded( *ittypes, name.isNull() ? (*ittypes) : name, pm, bgPm );
        }
        prevTypeList.remove(*ittypes);
        }
    }
    for ( QStringList::Iterator ittypes=prevTypeList.begin(); ittypes!=prevTypeList.end(); ++ittypes) {
        d->serverGui->typeRemoved(*ittypes);
    }
    prevTypeList = types;
    }

    QListIterator<AppLnk> itapp( appLnkSet->children() );
    AppLnk* l;
    while ( (l=itapp.current()) ) {
    ++itapp;
    if ( d->sendAppLnks && d->serverGui )
        d->serverGui->applicationAdded( l->type(), *l );
    }

    if ( d->sendAppLnks && d->serverGui )
    d->serverGui->applicationScanningProgress( 100 );
}

void DocumentList::reloadDocLnks()
{
    if ( !d->scanDocs )
    return;

    if ( d->sendDocLnks && d->serverGui ) {
    d->serverGui->documentScanningProgress( 0 );
    d->serverGui->allDocumentsRemoved();
    }

    rescan();
}

void DocumentList::reforceDocuments()
{
    Config cfg( "Launcher" );
    cfg.setGroup( "DocTab" );
    d->scanDocs = cfg.readBoolEntry( "Enable", true );
    reloadDocLnks();
}

void DocumentList::linkChanged( QString arg )
{
    odebug << "linkchanged( " << arg << " )" << oendl;

    if ( arg.isNull() || OGlobal::isAppLnkFileName( arg ) ) {
    reloadAppLnks();
    } else {

    const QList<DocLnk> &list = d->dls.children();
    QListIterator<DocLnk> it( list );
    while ( it.current() ) {
        DocLnk *doc = it.current();
        ++it;
        if ( ( doc->linkFileKnown() && doc->linkFile() == arg )
        || ( doc->fileKnown() && doc->file() == arg ) ) {
        //odebug << "found old link" << oendl;
        DocLnk* dl = new DocLnk( arg );
        // add new one if it exists and matches the mimetype
        if ( d->store( dl ) ) {
            // Existing link has been changed, send old link ref and a ref
            // to the new link
            //odebug << "change case" << oendl;
            if ( d->serverGui )
            d->serverGui->documentChanged( *doc, *dl );

        } else {
            // Link has been removed or doesn't match the mimetypes any more
            // so we aren't interested in it, so take it away from the list
            //odebug << "removal case" << oendl;
            if ( d->serverGui )
            d->serverGui->documentRemoved( *doc );

        }
        d->dls.remove( doc ); // remove old link from docLnkSet
        delete doc;
        return;
        }
    }
    // Didn't find existing link, must be new
    DocLnk* dl = new DocLnk( arg );
    if ( d->store( dl ) ) {
        // Add if it's a link we are interested in
        //odebug << "add case" << oendl;
        add( *dl );
    }

    }
}

void DocumentList::restoreDone()
{
    reloadAppLnks();
    reloadDocLnks();
}

void DocumentList::DiffAppLnks()
{
    static AppLnkSet *appLnkSet2;

    appLnkSet2 = new AppLnkSet( MimeType::appsFolderName() );

    if ( d->sendAppLnks && d->serverGui ) {
    static QStringList prevTypeList = appLnkSet->types();
    QStringList types = appLnkSet2->types();
    for ( QStringList::Iterator ittypes=types.begin(); ittypes!=types.end(); ++ittypes) {
        if ( !(*ittypes).isEmpty() ) {
        if ( !prevTypeList.contains(*ittypes) ) {
            QString name = appLnkSet2->typeName(*ittypes);
            QPixmap pm = appLnkSet2->typePixmap(*ittypes);
            QPixmap bgPm = appLnkSet2->typeBigPixmap(*ittypes);

            if (pm.isNull())
            {
                pm = OResource::loadImage( "UnknownDocument", OResource::SmallIcon );
                bgPm = OResource::loadImage( "UnknownDocument", OResource::BigIcon );
            }

            // ### our current launcher expects docs tab to be last
            d->serverGui->typeAdded( *ittypes, name.isNull() ? (*ittypes) : name, pm, bgPm );
        }
        prevTypeList.remove(*ittypes);
        }
    }
    for ( QStringList::Iterator ittypes=prevTypeList.begin(); ittypes!=prevTypeList.end(); ++ittypes) {
        d->serverGui->typeRemoved(*ittypes);
    }
    prevTypeList = types;
    }


    QListIterator<AppLnk> it1( appLnkSet->children() );
    QListIterator<AppLnk> it2( appLnkSet2->children() );

    AppLnk *i;
    AppLnk *j;
    bool found;

    while ( (j=it2.current()) ) {
    it1 = appLnkSet->children();
    found = false;
    while ( (i=it1.current()) ){
        if (j->name().ascii() && i->name().ascii() && strcmp(i->name().ascii(),j->name().ascii()) == 0)
        found = true;
        ++it1;
    }
    if (!found) {
        odebug << "Item " << j->name().ascii() << " needs to be added" << oendl;
        d->serverGui->applicationAdded( j->type(), *j );
    }
        ++it2;
    }

    it1 = appLnkSet->children();
     while ( (i=it1.current()) ) {
    it2 = appLnkSet2->children();
    found = false;
    while ( (j=it2.current()) ){
        if (j->name().ascii() && i->name().ascii() && strcmp(i->name().ascii(),j->name().ascii()) == 0)
        found = true;
        ++it2;
    }
    if (!found) {
        odebug << "Item " << i->name().ascii() << " needs to be removed" << oendl;
        d->serverGui->applicationRemoved( i->type(), *i );
    }

        ++it1;
    }

    delete appLnkSet;
    appLnkSet = appLnkSet2;

}
void DocumentList::storageChanged()
{
    QTime t;
    // ### can implement better

    t.start();
    DiffAppLnks();
//    reloadAppLnks();
    odebug << "Reload App links took " << t.elapsed() << " ms" << oendl;
    reloadDocLnks();
//    odebug << "Reload links took " << t.elapsed() << " ms " << oendl;
    odebug << "Reload All links took " << t.elapsed() << " ms" << oendl;
// ### Optimization opportunity
    // Could be a bit more intelligent and somehow work out which
    // mtab entry has changed and then only scan that and add and remove
    // links appropriately.
//    rescan();
}

void DocumentList::sendAllDocLinks()
{
    if ( d->tid != 0 ) {
    // We are in the middle of scanning, set a flag so
    // we do this when we finish our scanning
    d->needToSendAllDocLinks = true;
    return;
    }

    QString contents;
    Categories cats;
    for ( QListIterator<DocLnk> it( d->dls.children() ); it.current(); ++it ) {
    DocLnk *doc = it.current();
    QFileInfo fi( doc->file() );
    if ( !fi.exists() )
        continue;

    bool fake = !doc->linkFileKnown();
    if ( !fake ) {
        QFile f( doc->linkFile() );
        if ( f.open( IO_ReadOnly ) ) {
        QTextStream ts( &f );
        ts.setEncoding( QTextStream::UnicodeUTF8 );
        QString docLnk = ts.read();
        // Strip out the (stale) LinkFile entry
        int start = docLnk.find( "\nLinkFile = " ) + 1;
        if ( start > 0 ) {
            int end = docLnk.find( "\n", start + 1 ) + 1;
            contents += docLnk.left(start);
            contents += docLnk.mid(end);
        } else {
            contents += docLnk;
        }
        contents += "LinkFile = " + doc->linkFile() + "\n";

        f.close();
        } else
        fake = TRUE;
    }
    if (fake) {
        contents += "[Desktop Entry]\n"; // No tr
        contents += "Categories = " + // No tr
        cats.labels("Document View",doc->categories()).join(";") + "\n"; // No tr
        contents += "Name = "+doc->name()+"\n"; // No tr
        contents += "Type = "+doc->type()+"\n"; // No tr
    }
    contents += "File = "+doc->file()+"\n"; // No tr // (resolves path)
    contents += QString("Size = %1\n").arg( fi.size() ); // No tr
    }

    //odebug << "sending length " << contents.length() << "" << oendl;
#ifndef QT_NO_COP
    QCopEnvelope e( "QPE/Desktop", "docLinks(QString)" );
    e << contents;
#endif
    //odebug << "================ \n\n" << contents << "\n\n===============" << oendl;

    d->needToSendAllDocLinks = false;
}
























DocumentListPrivate::DocumentListPrivate( ServerInterface *gui )
{
    storage = new StorageInfo( this );
    serverGui = gui;
    if ( serverGui ) {
    sendAppLnks = serverGui->requiresApplications();
    sendDocLnks = serverGui->requiresDocuments();
    } else {
    sendAppLnks = false;
    sendDocLnks = false;
    }
    for ( int i = 0; i < MAX_SEARCH_DEPTH; i++ ) {
    listDirs[i] = 0;
    lists[i] = 0;
    listPositions[i] = 0;
    }
    initialize();
    tid = 0;
}

void DocumentListPrivate::appendDocpath(FileSystem*fs)
{
    Config c( "qpe" );
    c.setGroup( "Startup" );
    QDir defPath(fs->path()+"/Documents");
    QFileInfo f(fs->path()+"/.opiestorage.cf");
    if ( c.readNumEntry( "FirstUse", 42 ) == 0 && !f.exists()) {
        Mediadlg dlg(fs);
        if (QDialog::Accepted != QPEApplication::execDialog( &dlg )) {
            return;
        }
    }
    Config conf(f.filePath(), Config::File );
    conf.setGroup("main");
    if (!conf.readBoolEntry("check",false)) {
        return;
    }
    conf.setGroup("subdirs");
    bool read_all = conf.readBoolEntry("wholemedia",true);
    if (read_all) {
        docPaths+=fs->path();
        return;
    }
    QStringList subDirs = conf.readListEntry("subdirs",':');
    if (subDirs.isEmpty()) {
        if (defPath.exists()) {
            docPaths+=defPath.path();
        }
        return;
    }
    for (unsigned c = 0; c < subDirs.count();++c) {
        QDir docDir(QString(fs->path()+"/"+subDirs[c]));
        if (docDir.exists()) {
            docPaths+=docDir.path();
        }
    }
}

void DocumentListPrivate::initialize()
{
    // Reset
    dls.clear();
    docPaths.clear();
    reference.clear();

    QDir docDir( QPEApplication::documentDir() );
    if ( docDir.exists() )
    docPaths += QPEApplication::documentDir();
    int i = 1;
    const QList<FileSystem> &fs = storage->fileSystems();
    QListIterator<FileSystem> it( fs );
    for ( ; it.current(); ++it ) {
        if ( (*it)->isRemovable() ) {
            appendDocpath((*it));
            ++i;
        }
    }

    for ( int i = 0; i < MAX_SEARCH_DEPTH; ++i ) {
    if ( listDirs[i] ) {
        delete listDirs[i];
        listDirs[i] = 0;
    }
    lists[i] = 0;
    listPositions[i] = 0;
    }

    docPathsSearched = 0;
    searchDepth = -1;
    state = Find;
    dit = 0;
}


DocumentListPrivate::~DocumentListPrivate()
{
    for ( int i = 0; i < MAX_SEARCH_DEPTH; i++ )
    if ( listDirs[i] )
        delete listDirs[i];
    delete dit;
}


void DocumentListPrivate::estimatedPercentScanned()
{
    double overallProgress = 0.0;
    double levelWeight = 75.0;

    int topCount = docPaths.count();
    if ( topCount > 1 ) {
    levelWeight = levelWeight / topCount;
    overallProgress += (docPathsSearched - 1) * levelWeight;
    }

    for ( int d = 0; d <= searchDepth; d++ ) {
    if ( listDirs[d] ) {
        int items = lists[d]->count();
        if ( items > 1 ) {
        levelWeight = levelWeight / items;
        // Take in to account "." and ".."
        overallProgress += (listPositions[d] - 3) * levelWeight;
        }
    } else {
        break;
    }
    }

    // odebug << "overallProgress: " << overallProgress << "" << oendl;

    if ( serverGui )
    serverGui->documentScanningProgress( (int)overallProgress );
}


const QString DocumentListPrivate::nextFile()
{
    while ( TRUE ) {
    while ( searchDepth < 0 ) {
        // go to next base path
        if ( docPathsSearched >= docPaths.count() ) {
        // end of base paths
        return QString::null;
        } else {
        QDir dir( docPaths[docPathsSearched] );
        // odebug << "now using base path: " << docPaths[docPathsSearched] << "" << oendl;
        docPathsSearched++;
        if ( !dir.exists( ".Qtopia-ignore" ) ) {
            listDirs[0] = new QDir( dir );
            lists[0] = listDirs[0]->entryInfoList();
            listPositions[0] = 0;
            searchDepth = 0;
        }
        }
    }

    const QFileInfoList *fil = lists[searchDepth];
    if (!fil) {
        return QString::null;
    }
    QFileInfoList *fl = (QFileInfoList *)fil;
    unsigned int pos = listPositions[searchDepth];

    if ( pos >= fl->count() ) {
        // go up a depth
        delete listDirs[searchDepth];
        listDirs[searchDepth] = 0;
        lists[searchDepth] = 0;
        listPositions[searchDepth] = 0;
        searchDepth--;
    } else {
        const QFileInfo *fi = fl->at(pos);
        listPositions[searchDepth]++;
        QString bn = fi->fileName();
        if ( bn[0] != '.' ) {
        if ( fi->isDir()  ) {
        if ( bn != "CVS" && bn != "Qtopia" && bn != "QtPalmtop"
           && bn != "proc" && bn != "dev" && bn != "bin" && bn != "usr"
           && bn != "etc" && bn != "lib" && bn != "sbin" && bn != "tmp" && bn != "var") {
            // go down a depth
            QDir dir( fi->filePath() );
            // odebug << "now going in to path: " << bn << "" << oendl;
            if ( !dir.exists( ".Qtopia-ignore" ) ) {
                if ( searchDepth < MAX_SEARCH_DEPTH - 1) {
                searchDepth++;
                listDirs[searchDepth] = new QDir( dir );
                lists[searchDepth] = listDirs[searchDepth]->entryInfoList();
                listPositions[searchDepth] = 0;
                }
            }
            }
        } else {
            estimatedPercentScanned();
            return fl->at(pos)->filePath();
        }
        }
    }
    }

    return QString::null;
}


bool DocumentListPrivate::store( DocLnk* dl )
{
    // if ( dl->fileKnown() && !dl->file().isEmpty() ) {
    if ( dl && dl->fileKnown() ) {
    dls.add( dl ); // store
    return TRUE;
    }

    // don't store - delete
    delete dl;
    return FALSE;
}


#define MAGIC_NUMBER    ((void*)2)

const DocLnk *DocumentListPrivate::iterate()
{
    if ( state == Find ) {
    //odebug << "state Find" << oendl;
    QString file = nextFile();
    while ( !file.isNull() ) {
        if ( file.right(8) == ".desktop" ) { // No tr
        DocLnk* dl = new DocLnk( file );
        if ( store(dl) )
            return dl;
        } else {
        reference.insert( file, MAGIC_NUMBER );
        }
        file = nextFile();
    }
    state = RemoveKnownFiles;

    if ( serverGui )
        serverGui->documentScanningProgress( 75 );
    }

    static int iterationI;
    static int iterationCount;

    if ( state == RemoveKnownFiles ) {
    //odebug << "state RemoveKnownFiles" << oendl;
    const QList<DocLnk> &list = dls.children();
    for ( QListIterator<DocLnk> it( list ); it.current(); ++it ) {
        reference.remove( (*it)->file() );
        // ### does this need to be deleted?
    }
    dit = new QDictIterator<void>(reference);
    state = MakeUnknownFiles;

    iterationI = 0;
    iterationCount = dit->count();
    }

    if ( state == MakeUnknownFiles ) {
    //odebug << "state MakeUnknownFiles" << oendl;
    for (void* c; (c=dit->current()); ++(*dit) ) {
        if ( c == MAGIC_NUMBER ) {
        DocLnk* dl = new DocLnk;
        QFileInfo fi( dit->currentKey() );
        dl->setFile( fi.filePath() );
        dl->setName( fi.baseName() );
        if ( store(dl) ) {
            ++*dit;
            iterationI++;
            if ( serverGui )
            serverGui->documentScanningProgress( 75 + (25*iterationI)/iterationCount );
            return dl;
        }
        }
        iterationI++;
    }

    delete dit;
    dit = 0;
    state = Done;
    }

    //odebug << "state Done" << oendl;
    return NULL;
}


#include "documentlist.moc"