summaryrefslogtreecommitdiffstats
path: root/tdeio/tdefile/tdefiletreebranch.cpp
blob: 70e7d7c4096b5be6fb9681cadeaa4d555f19a944 (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
/* This file is part of the KDEproject
   Copyright (C) 2000 David Faure <faure@kde.org>
   2000 Carsten Pfeiffer <pfeiffer@kde.org>
   2002 Klaas Freitag <freitag@suse.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqfile.h>

#include <tdefileitem.h>
#include <kdebug.h>
#include <kde_file.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "tdefiletreeviewitem.h"
#include "tdefiletreebranch.h"


/* --- KFileTreeViewToplevelItem --- */
KFileTreeBranch::KFileTreeBranch( KFileTreeView *parent, const KURL& url,
                                  const TQString& name,
				  const TQPixmap& pix, bool showHidden,
				  KFileTreeViewItem *branchRoot )

    : KDirLister( false ),
      m_root( branchRoot ),
      m_startURL( url ),
      m_name ( name ),
      m_rootIcon( pix ),
      m_openRootIcon( pix ),
      m_recurseChildren(true),
      m_showExtensions(true)
{
    kdDebug( 250) << "Creating branch for url " << url.prettyURL() << endl;

    /* if non exists, create one */
    if( ! branchRoot )
    {
        m_root =  new KFileTreeViewItem( parent,
                                         new KFileItem( url, "inode/directory",
                                                        S_IFDIR  ),
                                         this );
    }

    m_root->setExpandable( true );
    m_root->setPixmap( 0, pix );
    m_root->setText( 0, name );

    setShowingDotFiles( showHidden );

    connect( this, TQ_SIGNAL( refreshItems(const KFileItemList&)),
             this, TQ_SLOT  ( slotRefreshItems( const KFileItemList& )));

    connect( this, TQ_SIGNAL( newItems(const KFileItemList&)),
             this, TQ_SLOT  ( addItems( const KFileItemList& )));

    connect( this, TQ_SIGNAL( completed(const KURL& )),
             this,   TQ_SLOT(slCompleted(const KURL&)));

    connect( this, TQ_SIGNAL( started( const KURL& )),
             this,   TQ_SLOT( slotListerStarted( const KURL& )));

    connect( this, TQ_SIGNAL( deleteItem( KFileItem* )),
             this,   TQ_SLOT( slotDeleteItem( KFileItem* )));

    connect( this, TQ_SIGNAL( canceled(const KURL&) ),
             this,   TQ_SLOT( slotCanceled(const KURL&) ));

    connect( this, TQ_SIGNAL( clear()),
             this, TQ_SLOT( slotDirlisterClear()));

    connect( this, TQ_SIGNAL( clear(const KURL&)),
             this, TQ_SLOT( slotDirlisterClearURL(const KURL&)));

    connect( this, TQ_SIGNAL( redirection( const KURL& , const KURL& ) ),
             this, TQ_SLOT( slotRedirect( const KURL&, const KURL& )));

    m_openChildrenURLs.append( url );
}

void KFileTreeBranch::setOpenPixmap( const TQPixmap& pix )
{
    m_openRootIcon = pix;

    if( root()->isOpen())
    {
        root()->setPixmap( 0, pix );
    }
}

void KFileTreeBranch::slotListerStarted( const KURL &url )
{
    /* set the parent correct if it is zero. */
    kdDebug( 250) << "Starting to list " << url.prettyURL() << endl;
}


KFileTreeViewItem *KFileTreeBranch::parentKFTVItem( KFileItem *item )
{
    KFileTreeViewItem *parent = 0;

    if( ! item ) return 0;

    /* If it is a directory, check, if it exists in the dict. If not, go one up
     * and check again.
     */
    KURL url = item->url();
    // kdDebug(250) << "Item's url is " << url.prettyURL() << endl;
    KURL dirUrl( url );
    dirUrl.setFileName( TQString::null );
    // kdDebug(250) << "Directory url is " << dirUrl.prettyURL() << endl;

    parent  = findTVIByURL( dirUrl );
    // kdDebug(250) << "Returning as parent item <" << parent <<  ">" << endl;
    return( parent );
}


void KFileTreeBranch::slotRefreshItems( const KFileItemList& list )
{
    KFileItemListIterator it( list );
    kdDebug(250) << "Refreshing " << list.count() << " items !" << endl;
    KFileItem *currItem;
    KFileTreeViewItem *item = 0;

    while ( (currItem = it.current()) != 0 )
    {
        item = findTVIByURL(currItem->url());
        if (item) {
            item->setPixmap(0, item->fileItem()->pixmap( TDEIcon::SizeSmall ));
            item->setText( 0, item->fileItem()->text());
        }
        ++it;
    }
}

void KFileTreeBranch::addItems( const KFileItemList& list )
{
    KFileItemListIterator it( list );
    kdDebug(250) << "Adding " << list.count() << " items !" << endl;
    KFileItem *currItem;
    KFileTreeViewItemList treeViewItList;
    KFileTreeViewItem *parentItem = 0;

    while ( (currItem = it.current()) != 0 )
    {
        parentItem = parentKFTVItem( currItem );


        /* Only create a new KFileTreeViewItem if it does not yet exist */
        KFileTreeViewItem *newKFTVI =
            static_cast<KFileTreeViewItem *>(currItem->extraData( this ));

        if( ! newKFTVI )
        {
            newKFTVI = createTreeViewItem( parentItem, currItem );
            if (!newKFTVI)
            {
                // TODO: Don't fail if parentItem == 0
                ++it;
                continue;
            }
            currItem->setExtraData( this, newKFTVI );

            /* Cut off the file extension in case it is not a directory */
            if( !m_showExtensions && !currItem->isDir() )	/* Need to cut the extension */
            {
                TQString name = currItem->text();
                int mPoint = name.findRev( '.' );
                if( mPoint > 0 )
                    name = name.left( mPoint );
                newKFTVI->setText( 0, name );
            }
        }

        /* Now try to find out if there are children for dirs in the treeview */
        /* This stats a directory on the local file system and checks the */
        /* hardlink entry in the stat-buf. This works only for local directories. */
        if( dirOnlyMode() && !m_recurseChildren && currItem->isLocalFile( ) && currItem->isDir() )
        {
            KURL url = currItem->url();
            TQString filename = url.directory( false, true ) + url.fileName();
            /* do the stat trick of Carsten. The problem is, that the hardlink
             *  count only contains directory links. Thus, this method only seem
             * to work in dir-only mode */
            kdDebug(250) << "Doing stat on " << filename << endl;
            KDE_struct_stat statBuf;
            if( KDE_stat( TQFile::encodeName( filename ), &statBuf ) == 0 )
            {
                int hardLinks = statBuf.st_nlink;  /* Count of dirs */
                kdDebug(250) << "stat succeeded, hardlinks: " << hardLinks << endl;
                // If the link count is > 2, the directory likely has subdirs. If it's < 2
                // it's something weird like a mounted SMB share. In that case we don't know
                // if there are subdirs, thus show it as expandable.

                if( hardLinks != 2 )
                {
                    newKFTVI->setExpandable(true);
                }
                else
                {
                    newKFTVI->setExpandable(false);
                }
                if( hardLinks >= 2 ) // "Normal" directory with subdirs
                {
                    kdDebug(250) << "Emitting for " << url.prettyURL() << endl;
                    emit( directoryChildCount( newKFTVI, hardLinks-2)); // parentItem, hardLinks-1 ));
                }
            }
            else
            {
                kdDebug(250) << "stat of " << filename << " failed !" << endl;
            }
        }
        ++it;

        treeViewItList.append( newKFTVI );
    }

    emit newTreeViewItems( this, treeViewItList );
}

KFileTreeViewItem* KFileTreeBranch::createTreeViewItem( KFileTreeViewItem *parent,
							KFileItem *fileItem )
{
    KFileTreeViewItem  *tvi = 0;
    if( parent && fileItem )
    {
        tvi = new KFileTreeViewItem( parent,
                                     fileItem,
                                     this );
    }
    else
    {
        kdDebug(250) << "createTreeViewItem: Have no parent" << endl;
    }
    return( tvi );
}

void KFileTreeBranch::setChildRecurse( bool t )
{
    m_recurseChildren = t;
    if( t == false )
        m_openChildrenURLs.clear();
}


void KFileTreeBranch::setShowExtensions( bool visible )
{
    m_showExtensions = visible;
}

bool KFileTreeBranch::showExtensions( ) const
{
    return( m_showExtensions );
}

/*
 * The signal that tells that a directory was deleted may arrive before the signal
 * for its children arrive. Thus, we must walk through the children of a dir and
 * remove them before removing the dir itself.
 */
void KFileTreeBranch::slotDeleteItem( KFileItem *it )
{
    if( !it ) return;
    kdDebug(250) << "Slot Delete Item hitted for " << it->url().prettyURL() << endl;

    KFileTreeViewItem *kfti = static_cast<KFileTreeViewItem*>(it->extraData(this));

    if( kfti )
    {
        kdDebug( 250 ) << "Child count: " << kfti->childCount() << endl;
        if( kfti->childCount() > 0 )
        {
            KFileTreeViewItem *child = static_cast<KFileTreeViewItem*>(kfti->firstChild());

            while( child )
            {
                kdDebug(250) << "Calling child to be deleted !" << endl;
                KFileTreeViewItem *nextChild = static_cast<KFileTreeViewItem*>(child->nextSibling());
                slotDeleteItem( child->fileItem());
                child = nextChild;
            }
        }

        kdDebug(250) << "Found corresponding KFileTreeViewItem" << endl;
        if( m_lastFoundURL.equals(it->url(), true ))
        {
          m_lastFoundURL = KURL();
          m_lastFoundItem = 0L;
        }
        delete( kfti );
    }
    else
    {
        kdDebug(250) << "Error: tdefiletreeviewitem: "<< kfti << endl;
    }
}


void KFileTreeBranch::slotCanceled( const KURL& url )
{
    // ### anything else to do?
    // remove the url from the childrento-recurse-list
    m_openChildrenURLs.remove( url);

    // stop animations etc.
    KFileTreeViewItem *item = findTVIByURL(url);
    if (!item) return; // Uh oh...
    emit populateFinished(item);
}

void KFileTreeBranch::slotDirlisterClear()
{
    kdDebug(250)<< "*** Clear all !" << endl;
    /* this slots needs to clear all listed items, but NOT the root item */
    if( m_root )
        deleteChildrenOf( m_root );
}

void KFileTreeBranch::slotDirlisterClearURL( const KURL& url )
{
    kdDebug(250)<< "*** Clear for URL !" << url.prettyURL() << endl;
    KFileItem *item = findByURL( url );
    if( item )
    {
        KFileTreeViewItem *ftvi =
            static_cast<KFileTreeViewItem *>(item->extraData( this ));
        deleteChildrenOf( ftvi );
    }
}

void KFileTreeBranch::deleteChildrenOf( TQListViewItem *parent )
{
    // for some strange reason, slotDirlisterClearURL() sometimes calls us
    // with a 0L parent.
    if ( !parent )
        return;

    while ( parent->firstChild() )
        delete parent->firstChild();
}

void KFileTreeBranch::slotRedirect( const KURL& oldUrl, const KURL&newUrl )
{
    if( oldUrl.equals( m_startURL, true ))
    {
        m_startURL = newUrl;
    }
}

KFileTreeViewItem* KFileTreeBranch::findTVIByURL( const KURL& url )
{
    KFileTreeViewItem *resultItem = 0;

    if( m_startURL.equals(url, true) )
    {
        kdDebug(250) << "findByURL: Returning root as a parent !" << endl;
        resultItem = m_root;
    }
    else if( m_lastFoundURL.equals( url, true ))
    {
        kdDebug(250) << "findByURL: Returning from lastFoundURL!" << endl;
        resultItem = m_lastFoundItem;
    }
    else
    {
        kdDebug(250) << "findByURL: searching by dirlister: " << url.url() << endl;

        KFileItem *it = findByURL( url );

        if( it )
        {
            resultItem = static_cast<KFileTreeViewItem*>(it->extraData(this));
            m_lastFoundItem = resultItem;
            m_lastFoundURL = url;
        }
    }

    return( resultItem );
}


void KFileTreeBranch::slCompleted( const KURL& url )
{
    kdDebug(250) << "SlotCompleted hit for " << url.prettyURL() << endl;
    KFileTreeViewItem *currParent = findTVIByURL( url );
    if( ! currParent ) return;

    kdDebug(250) << "current parent " << currParent << " is already listed: "
                 << currParent->alreadyListed() << endl;

    emit( populateFinished(currParent));
    emit( directoryChildCount(currParent, currParent->childCount()));

    /* This is a walk through the children of the last populated directory.
     * Here we start the dirlister on every child of the dir and wait for its
     * finish. When it has finished, we go to the next child.
     * This must be done for non local file systems in dirOnly- and Full-Mode
     * and for local file systems only in full mode, because the stat trick
     * (see addItem-Method) does only work for dirs, not for files in the directory.
     */
    /* Set bit that the parent dir was listed completely */
    currParent->setListed(true);

    kdDebug(250) << "recurseChildren: " << m_recurseChildren << endl;
    kdDebug(250) << "isLocalFile: " << m_startURL.isLocalFile() << endl;
    kdDebug(250) << "dirOnlyMode: " << dirOnlyMode() << endl;


    if( m_recurseChildren && (!m_startURL.isLocalFile() || ! dirOnlyMode()) )
    {
        bool wantRecurseUrl = false;
        /* look if the url is in the list for url to recurse */
        for ( KURL::List::Iterator it = m_openChildrenURLs.begin();
              it != m_openChildrenURLs.end(); ++it )
        {
            /* it is only interesting that the url _is_in_ the list. */
            if( (*it).equals( url, true ) )
                wantRecurseUrl = true;
        }

        KFileTreeViewItem    *nextChild = 0;
        kdDebug(250) << "Recursing " << url.prettyURL() << "? " << wantRecurseUrl << endl;

        if( wantRecurseUrl && currParent )
        {

            /* now walk again through the tree and populate the children to get +-signs */
            /* This is the starting point. The visible folder has finished,
               processing the children has not yet started */
            nextChild = static_cast<KFileTreeViewItem*>
                        (static_cast<TQListViewItem*>(currParent)->firstChild());

            if( ! nextChild )
            {
                /* This happens if there is no child at all */
                kdDebug( 250 ) << "No children to recuse" << endl;
            }

            /* Since we have listed the children to recurse, we can remove the entry
             * in the list of the URLs to see the children.
             */
            m_openChildrenURLs.remove(url);
        }

        if( nextChild ) /* This implies that idx > -1 */
        {
            /* Next child is defined. We start a dirlister job on every child item
             * which is a directory to find out how much children are in the child
             * of the last opened dir
             */

            /* Skip non directory entries */
            while( nextChild )
            {
                if( nextChild->isDir() && ! nextChild->alreadyListed())
                {
                    KFileItem *kfi = nextChild->fileItem();
                    if( kfi && kfi->isReadable())
                    {
                        KURL recurseUrl = kfi->url();
                        kdDebug(250) << "Starting to recurse NOW " << recurseUrl.prettyURL() << endl;
                        openURL( recurseUrl, true );
                    }
                }
                nextChild = static_cast<KFileTreeViewItem*>(static_cast<TQListViewItem*>(nextChild->nextSibling()));
                // kdDebug(250) << "Next child " << m_nextChild << endl;
            }
        }
    }
    else
    {
        kdDebug(250) << "skipping to recurse in complete-slot" << endl;
    }
}

/* This slot is called when a treeviewitem is expanded in the gui */
bool KFileTreeBranch::populate( const KURL& url,  KFileTreeViewItem *currItem )
{
    bool ret = false;
    if( ! currItem )
        return ret;

    kdDebug(250) << "Populating <" << url.prettyURL() << ">" << endl;

    /* Add this url to the list of urls to recurse for children */
    if( m_recurseChildren )
    {
        m_openChildrenURLs.append( url );
        kdDebug(250) << "Appending to list " << url.prettyURL() << endl;
    }

    if( ! currItem->alreadyListed() )
    {
        /* start the lister */
        ret = openURL( url, true );
    }
    else
    {
        kdDebug(250) << "Children already existing in treeview!" << endl;
        slCompleted( url );
        ret = true;
    }
    return ret;
}

void KFileTreeBranch::virtual_hook( int id, void* data )
{ KDirLister::virtual_hook( id, data ); }

#include "tdefiletreebranch.moc"