summaryrefslogtreecommitdiffstats
path: root/krename/threadedlister.cpp
blob: 322e2689508a4a973441e73a8b8c93d9c55e8415 (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
/***************************************************************************

                          threadedlister.cpp  -  description
                             -------------------
    begin                : Tue Feb 01 2005
    copyright            : (C) 2005 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/
 
#include "threadedlister.h"

#include "kmylistbox.h"
#include "krecursivelister.h"

#include <kapplication.h>
#include <tqmutex.h>

ThreadedLister::ThreadedLister( TQMutex* mutex, unsigned int* counter, KMyListBox* list  )
 : TQObject(), TQThread(), m_mutex( mutex ), m_counter( counter ), m_list( list )
{
    m_mutex->lock();
    ++(*m_counter);
    m_mutex->unlock();

    m_reclister = NULL;
    m_lister = NULL;
    m_internal = NULL;

    m_hidden = false;
    m_recursive = false;
    m_dirnames = false;
    m_dironly = false;
}


ThreadedLister::~ThreadedLister()
{
    if( m_reclister )
        delete m_reclister;
        
    if( m_lister )
        delete m_lister;
        
    if( m_internal )
        delete m_internal;
}

void ThreadedLister::run()
{
    m_internal = new TQMutex();
    m_internal->lock();
    
    if( m_recursive ) 
    {
        m_reclister = new KRecursiveLister();
        
        m_reclister->setShowingDotFiles( m_hidden );
        m_reclister->setNameFilter( m_filter );
        m_reclister->setDirOnlyMode( m_dironly );
                
        connect( m_reclister, TQT_SIGNAL( completed() ), this, TQT_SLOT( reclisterFinished() ) );
        
        m_reclister->openURL( m_dirname );
    } else {
        m_lister = new KDirLister();
    
        m_lister->setAutoUpdate( false );
        m_lister->setShowingDotFiles( m_hidden );
        m_lister->setNameFilter( m_filter );
        
        connect( m_lister, TQT_SIGNAL( completed() ), this, TQT_SLOT( listerFinished() ) );
        
        m_lister->openURL( m_dirname, false, false );    
    }
    
    // try to lock the mutex.
    // This will block run as long as *listerFinished() 
    // does not unlock the mutex.
    m_internal->lock();
    m_internal->unlock();
    
    KApplication::postEvent( m_list, new TQCustomEvent( (TQEvent::Type)ThreadedLister::TYPE(), (void*)this ) );
}


void ThreadedLister::reclisterFinished()
{
    KFileItemList l = m_reclister->items();
    FileList list = l;
    
    if( m_dirnames ) 
    {
        FileList dirs = m_reclister->dirs();
        KFileItem* item;
        for( item = dirs.first(); item; item = dirs.next() )
            list.append( item );
    }

    list.sort();

    m_mutex->lock();
    m_list->setUpdatesEnabled( false );

    if( m_dirnames ) 
    {
        TQString name = m_dirname.fileName();
        if( !m_hidden && name.right( 1 ) != TQString::tqfromLatin1(".") )        
            m_list->addDirName( m_dirname );
    }
    
    KFileItem* item;
    for( item = list.first(); item; item = list.next() )
        if( item->isFile() )
            m_list->addFile( item->url(), true );
        else if( item->isDir() )
            m_list->addDirName( item->url() );
            
    m_mutex->unlock();
    m_list->setUpdatesEnabled( true );
    
    m_internal->unlock();
}

void ThreadedLister::listerFinished()
{
    if( m_lister->isFinished() ) {
        FileList list = m_lister->items( KDirLister::FilteredItems );
        list.sort();

        m_mutex->lock();
        m_list->setUpdatesEnabled( false );
        
        if( m_dirnames ) 
        {
            TQString name = m_dirname.fileName();
            if( !m_hidden && name.right( 1 ) != TQString::tqfromLatin1(".") )        
                m_list->addDirName( m_dirname );
        }
        
        KFileItem* item;
        for( item = list.first(); item; item = list.next() )
            if( item->isFile() )
                m_list->addFile( item->url(), true );

        --(*m_counter);
        m_mutex->unlock();
        m_list->setUpdatesEnabled( true );
        
        m_internal->unlock();
    }
}

#include "threadedlister.moc"