summaryrefslogtreecommitdiffstats
path: root/tdeio/tdefile/tdefileview.cpp
blob: 01bc5e6b0ace8ad0fa09e327f69f8eeaa446a417 (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
/* This file is part of the KDE libraries
    Copyright (C) 1998 Stephan Kulow <[email protected]>
                  1998 Daniel Grana <[email protected]>
                  2001 Carsten Pfeiffer <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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 <assert.h>
#include <stdlib.h>

#include <kaction.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include "config-tdefile.h"
#include "tdefileview.h"

#ifdef Unsorted // the "I hate X.h" modus
#undef Unsorted
#endif

TQDir::SortSpec KFileView::defaultSortSpec = static_cast<TQDir::SortSpec>(TQDir::Name | TQDir::IgnoreCase | TQDir::DirsFirst);

class KFileView::KFileViewPrivate
{
public:
    KFileViewPrivate()
    {
        actions = 0;
        dropOptions = 0;
    }

    ~KFileViewPrivate()
    {
        if( actions ) {
            actions->clear(); // so that the removed() signal is emitted!
            delete actions;
        }
    }

    TQGuardedPtr<TDEActionCollection> actions;
    int dropOptions;
};


KFileView::KFileView()
{
    d = new KFileViewPrivate();
    m_sorting  = KFileView::defaultSortSpec;

    sig = new KFileViewSignaler();
    sig->setName("view-signaller");

    m_selectedList = 0L;
    filesNumber = 0;
    dirsNumber = 0;

    view_mode = All;
    selection_mode = KFile::Single;
    m_viewName = i18n("Unknown View");

    myOnlyDoubleClickSelectsFiles = false;
    m_itemList.setAutoDelete( false ); // just references
}

KFileView::~KFileView()
{
    delete d;
    delete sig;
    delete m_selectedList;
}

void KFileView::setParentView(KFileView *parent)
{
    if ( parent ) { // pass all signals right to our parent
        TQObject::connect(sig, TQT_SIGNAL( activatedMenu(const KFileItem *,
                                                        const TQPoint& ) ),
                parent->sig, TQT_SIGNAL( activatedMenu(const KFileItem *,
                                                        const TQPoint& )));
        TQObject::connect(sig, TQT_SIGNAL( dirActivated(const KFileItem *)),
                parent->sig, TQT_SIGNAL( dirActivated(const KFileItem*)));
        TQObject::connect(sig, TQT_SIGNAL( fileSelected(const KFileItem *)),
                parent->sig, TQT_SIGNAL( fileSelected(const KFileItem*)));
        TQObject::connect(sig, TQT_SIGNAL( fileHighlighted(const KFileItem *) ),
                            parent->sig,TQT_SIGNAL(fileHighlighted(const KFileItem*)));
        TQObject::connect(sig, TQT_SIGNAL( sortingChanged( TQDir::SortSpec ) ),
                            parent->sig, TQT_SIGNAL(sortingChanged( TQDir::SortSpec)));
        TQObject::connect(sig, TQT_SIGNAL( dropped(const KFileItem *, TQDropEvent*, const KURL::List&) ),
                            parent->sig, TQT_SIGNAL(dropped(const KFileItem *, TQDropEvent*, const KURL::List&)));
    }
}

bool KFileView::updateNumbers(const KFileItem *i)
{
    if (!( viewMode() & Files ) && i->isFile())
        return false;

    if (!( viewMode() & Directories ) && i->isDir())
        return false;

    if (i->isDir())
        dirsNumber++;
    else
        filesNumber++;

    return true;
}

void qt_qstring_stats();

// filter out files if we're in directory mode and count files/directories
// and insert into the view
void KFileView::addItemList(const KFileItemList& list)
{
    KFileItem *tmp;

    for (KFileItemListIterator it(list); (tmp = it.current()); ++it)
    {
        if (!updateNumbers(tmp))
            continue;

        insertItem( tmp );
    }

#ifdef Q2HELPER
    qt_qstring_stats();
#endif
}

void KFileView::insertItem( KFileItem * )
{
}

void KFileView::setSorting(TQDir::SortSpec new_sort)
{
    m_sorting = new_sort;
}

void KFileView::clear()
{
    m_itemList.clear();
    filesNumber = 0;
    dirsNumber = 0;
    clearView();
}

void KFileView::sortReversed()
{
    int spec = sorting();

    setSorting( static_cast<TQDir::SortSpec>( spec ^ TQDir::Reversed ) );
}

#if 0
int KFileView::compareItems(const KFileItem *fi1, const KFileItem *fi2) const
{
    static const TQString &dirup = TDEGlobal::staticQString("..");
    bool bigger = true;
    bool keepFirst = false;
    bool dirsFirst = ((m_sorting & TQDir::DirsFirst) == TQDir::DirsFirst);

    if (fi1 == fi2)
	return 0;

    // .. is always bigger, independent of the sort criteria
    if ( fi1->name() == dirup ) {
	bigger = false;
	keepFirst = dirsFirst;
    }
    else if ( fi2->name() == dirup ) {
	bigger = true;
	keepFirst = dirsFirst;
    }

    else {
	if ( fi1->isDir() != fi2->isDir() && dirsFirst ) {
	    bigger = fi2->isDir();
	    keepFirst = true;
	}
	else {

	    TQDir::SortSpec sort = static_cast<TQDir::SortSpec>(m_sorting & TQDir::SortByMask);

	    //if (fi1->isDir() || fi2->isDir())
            // sort = static_cast<TQDir::SortSpec>(KFileView::defaultSortSpec & TQDir::SortByMask);

            switch (sort) {
                case TQDir::Name:
                default:
sort_by_name:
                    if ( (m_sorting & TQDir::IgnoreCase) == TQDir::IgnoreCase )
                        bigger = (fi1->name( true ) > fi2->name( true ));
                    else
                        bigger = (fi1->name() > fi2->name());
                    break;
                case TQDir::Time:
                {
                    time_t t1 = fi1->time( TDEIO::UDS_MODIFICATION_TIME );
                    time_t t2 = fi2->time( TDEIO::UDS_MODIFICATION_TIME );
                    if ( t1 != t2 ) {
                        bigger = (t1 > t2);
                        break;
                    }

                    // Sort by name if both items have the same timestamp.
                    // Don't honor the reverse flag tho.
                    else {
                        keepFirst = true;
                        goto sort_by_name;
                    }
                }
                case TQDir::Size:
                {
                    TDEIO::filesize_t s1 = fi1->size();
                    TDEIO::filesize_t s2 = fi2->size();
                    if ( s1 != s2 ) {
                        bigger = (s1 > s2);
                        break;
                    }

                    // Sort by name if both items have the same size.
                    // Don't honor the reverse flag tho.
                    else {
                        keepFirst = true;
                        goto sort_by_name;
                    }
                }
                case TQDir::Unsorted:
                    bigger = true;  // nothing
                    break;
	    }
	}
    }

    if (reversed && !keepFirst ) // don't reverse dirs to the end!
      bigger = !bigger;

    return (bigger ? 1 : -1);
}
#endif

void  KFileView::updateView(bool f)
{
    widget()->repaint(f);
}

void KFileView::updateView(const KFileItem *)
{
}

void KFileView::setCurrentItem(const TQString &filename )
{
    if (!filename.isNull()) {
        KFileItem *item;
        for ( (item = firstFileItem()); item; item = nextItem( item ) ) {
            if (item->name() == filename) {
                setCurrentItem( item );
                return;
            }
        }
    }

    kdDebug(tdefile_area) << "setCurrentItem: no match found: " << filename << endl;
}

const KFileItemList * KFileView::items() const
{
    KFileItem *item = 0L;

    // only ever use m_itemList in this method!
    m_itemList.clear();
    for ( (item = firstFileItem()); item; item = nextItem( item ) )
        m_itemList.append( item );

    return &m_itemList;
}


const KFileItemList * KFileView::selectedItems() const
{
    if ( !m_selectedList )
	m_selectedList = new KFileItemList;

    m_selectedList->clear();

    KFileItem *item;
    for ( (item = firstFileItem()); item; item = nextItem( item ) ) {
        if ( isSelected( item ) )
            m_selectedList->append( item );
    }

    return m_selectedList;
}

void KFileView::selectAll()
{
    if (selection_mode == KFile::NoSelection || selection_mode== KFile::Single)
        return;

    KFileItem *item = 0L;
    for ( (item = firstFileItem()); item; item = nextItem( item ) )
        setSelected( item, true );
}


void KFileView::invertSelection()
{
    KFileItem *item = 0L;
    for ( (item = firstFileItem()); item; item = nextItem( item ) )
        setSelected( item, !isSelected( item ) );
}


void KFileView::setSelectionMode( KFile::SelectionMode sm )
{
    selection_mode = sm;
}

KFile::SelectionMode KFileView::selectionMode() const
{
    return selection_mode;
}

void KFileView::setViewMode( ViewMode vm )
{
    view_mode = vm;
}

void KFileView::removeItem( const KFileItem *item )
{
    if ( !item )
	return;

    if ( item->isDir() )
        dirsNumber--;
    else
        filesNumber--;

    if ( m_selectedList )
        m_selectedList->removeRef( item );
}

void KFileView::listingCompleted()
{
    // empty default impl.
}

TDEActionCollection * KFileView::actionCollection() const
{
    if ( !d->actions )
        d->actions = new TDEActionCollection( widget(), "KFileView::d->actions" );
    return d->actions;
}

void KFileView::readConfig( TDEConfig *, const TQString&  )
{
}

void KFileView::writeConfig( TDEConfig *, const TQString& )
{
}

TQString KFileView::sortingKey( const TQString& value, bool isDir, int sortSpec )
{
    bool reverse   = sortSpec & TQDir::Reversed;
    bool dirsFirst = sortSpec & TQDir::DirsFirst;
    char start = (isDir && dirsFirst) ? (reverse ? '2' : '0') : '1';
    TQString result = (sortSpec & TQDir::IgnoreCase) ? value.lower() : value;
    return result.prepend( start );
}

TQString KFileView::sortingKey( TDEIO::filesize_t value, bool isDir, int sortSpec)
{
    bool reverse = sortSpec & TQDir::Reversed;
    bool dirsFirst = sortSpec & TQDir::DirsFirst;
    char start = (isDir && dirsFirst) ? (reverse ? '2' : '0') : '1';
    return TDEIO::number( value ).rightJustify( 24, '0' ).prepend( start );
}

void KFileView::setDropOptions(int options)
{
    virtual_hook(VIRTUAL_SET_DROP_OPTIONS, &options); // Virtual call
}

void KFileView::setDropOptions_impl(int options)
{
    d->dropOptions = options;
}

int KFileView::dropOptions()
{
    return d->dropOptions;
}

int KFileView::autoOpenDelay()
{
    return (TQApplication::startDragTime() * 3) / 2;
}

void KFileView::virtual_hook( int id, void* data)
{ 
    switch(id) {
      case VIRTUAL_SET_DROP_OPTIONS: 
         setDropOptions_impl(*(int *)data);
         break;
      default:
         /*BASE::virtual_hook( id, data );*/
         break;
    }
}

#include "tdefileview.moc"