summaryrefslogtreecommitdiffstats
path: root/libkonq/tdefileivi.cc
blob: 629d9a27da8ab754e4c18e03f7768eaf34951a19 (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
/* This file is part of the KDE project
   Copyright (C) 1999, 2000, 2001, 2002 David Faure <faure@kde.org>

   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 "tdefileivi.h"
#include "kivdirectoryoverlay.h"
#include "kivfreespaceoverlay.h"
#include "konq_iconviewwidget.h"
#include "konq_operations.h"
#include "konq_settings.h"

#include <tqpainter.h>

#include <kurldrag.h>
#include <kiconeffect.h>
#include <tdefileitem.h>
#include <kdebug.h>
#include <krun.h>
#include <kservice.h>

#undef Bool

/**
 * Private data for KFileIVI
 */
struct KFileIVI::Private
{
    TQIconSet icons; // Icon states (cached to prevent re-applying icon effects
		    // every time)
    TQPixmap  thumb; // Raw unprocessed thumbnail
    TQString m_animatedIcon; // Name of animation
    bool m_animated;        // Animation currently running ?
	KIVDirectoryOverlay* m_directoryOverlay;
	KIVFreeSpaceOverlay* m_freeSpaceOverlay;
	TQPixmap m_overlay;
	TQString m_overlayName;
	int m_progress;
};

KFileIVI::KFileIVI( KonqIconViewWidget *iconview, KFileItem* fileitem, int size )
    : TDEIconViewItem( iconview, fileitem->text() ),
    m_size( size ), m_state( TDEIcon::DefaultState ),
    m_bDisabled( false ), m_bThumbnail( false ), m_fileitem( fileitem )
{
    d = new KFileIVI::Private;

    updatePixmapSize();
    setPixmap( m_fileitem->pixmap( m_size, m_state ) );
    setDropEnabled( S_ISDIR( m_fileitem->mode() ) );

    // Cache entry for the icon effects
    d->icons.reset( *pixmap(), TQIconSet::Large );
    d->m_animated = false;

    // iconName() requires the mimetype to be known
    if ( fileitem->isMimeTypeKnown() )
    {
        TQString icon = fileitem->iconName();
        if ( !icon.isEmpty() )
            setMouseOverAnimation( icon );
        else
            setMouseOverAnimation( "unknown" );
    }
    d->m_progress = -1;
    d->m_directoryOverlay = 0;
    d->m_freeSpaceOverlay = 0;
}

KFileIVI::~KFileIVI()
{
    delete d->m_directoryOverlay;
    delete d->m_freeSpaceOverlay;
    delete d;
}

void KFileIVI::invalidateThumb( int state, bool redraw )
{
    TQIconSet::Mode mode;
    switch( state )
    {
	case TDEIcon::DisabledState:
	    mode = TQIconSet::Disabled;
	    break;
	case TDEIcon::ActiveState:
	    mode = TQIconSet::Active;
	    break;
	case TDEIcon::DefaultState:
	default:
	    mode = TQIconSet::Normal;
	    break;
    }
    d->icons = TQIconSet();
    d->icons.setPixmap( TDEGlobal::iconLoader()->iconEffect()->
			apply( d->thumb, TDEIcon::Desktop, state ),
			TQIconSet::Large, mode );
    m_state = state;

    TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
			      false, redraw );
}

void KFileIVI::setIcon( int size, int state, bool recalc, bool redraw )
{
    m_size = size;
    m_bThumbnail = false;
    if ( m_bDisabled )
      m_state = TDEIcon::DisabledState;
    else
      m_state = state;

    if ( d->m_overlayName.isNull() )
        d->m_overlay = TQPixmap();
    else {
        int halfSize;
        if (m_size == 0) {
            halfSize = IconSize(TDEIcon::Desktop) / 2;
        } else {
            halfSize = m_size / 2;
        }
        d->m_overlay = DesktopIcon(d->m_overlayName, halfSize);
    }

    setPixmapDirect(m_fileitem->pixmap( m_size, m_state ) , recalc, redraw );
}

void KFileIVI::setOverlay( const TQString& iconName )
{
    d->m_overlayName = iconName;

    refreshIcon(true);
}

void KFileIVI::setOverlayProgressBar( const int progress )
{
    d->m_progress = progress;

    refreshIcon(true);
}

KIVDirectoryOverlay* KFileIVI::setShowDirectoryOverlay( bool show )
{
    if ( !m_fileitem->isDir() || m_fileitem->iconName() != "folder" )
        return 0;

    if (show) {
        if (!d->m_directoryOverlay)
            d->m_directoryOverlay = new KIVDirectoryOverlay(this);
        return d->m_directoryOverlay;
    } else {
        delete d->m_directoryOverlay;
        d->m_directoryOverlay = 0;
        setOverlay(TQString());
        return 0;
    }
}

bool KFileIVI::showDirectoryOverlay(  )
{
    return (bool)d->m_directoryOverlay;
}

KIVFreeSpaceOverlay* KFileIVI::setShowFreeSpaceOverlay( bool show )
{
    if ( !m_fileitem->mimetype().startsWith("media/") ) {
        return 0;
    }

    if (show) {
        if (!d->m_freeSpaceOverlay)
            d->m_freeSpaceOverlay = new KIVFreeSpaceOverlay(this);
        return d->m_freeSpaceOverlay;
    } else {
        delete d->m_freeSpaceOverlay;
        d->m_freeSpaceOverlay = 0;
        setOverlayProgressBar(-1);
        return 0;
    }
}

bool KFileIVI::showFreeSpaceOverlay(  )
{
    return (bool)d->m_freeSpaceOverlay;
}

void KFileIVI::setPixmapDirect( const TQPixmap& pixmap, bool recalc, bool redraw )
{
    TQIconSet::Mode mode;
    switch( m_state )
    {
	case TDEIcon::DisabledState:
	    mode = TQIconSet::Disabled;
	    break;
	case TDEIcon::ActiveState:
	    mode = TQIconSet::Active;
	    break;
	case TDEIcon::DefaultState:
	default:
	    mode = TQIconSet::Normal;
	    break;
    }

    // We cannot just reset() the iconset here, because setIcon can be
    // called with any state and not just normal state. So we just
    // create a dummy empty iconset as base object.
    d->icons = TQIconSet();
    d->icons.setPixmap( pixmap, TQIconSet::Large, mode );

    updatePixmapSize();
    TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ),
			      recalc, redraw );
}

void KFileIVI::setDisabled( bool disabled )
{
    if ( m_bDisabled != disabled )
    {
        m_bDisabled = disabled;
        bool active = ( m_state == TDEIcon::ActiveState );
        setEffect( m_bDisabled ? TDEIcon::DisabledState : 
                   ( active ? TDEIcon::ActiveState : TDEIcon::DefaultState ) );
    }
}

void KFileIVI::setThumbnailPixmap( const TQPixmap & pixmap )
{
    m_bThumbnail = true;
    d->thumb = pixmap;
    // TQIconSet::reset() doesn't seem to clear the other generated pixmaps,
    // so we just create a blank TQIconSet here
    d->icons = TQIconSet();
    d->icons.setPixmap( TDEGlobal::iconLoader()->iconEffect()->
		    apply( pixmap, TDEIcon::Desktop, TDEIcon::DefaultState ),
		    TQIconSet::Large, TQIconSet::Normal );

    m_state = TDEIcon::DefaultState;

    // Recalc when setting this pixmap!
    updatePixmapSize();
    TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large,
			      TQIconSet::Normal ), true );
}

void KFileIVI::setActive( bool active )
{
    if ( active )
        setEffect( TDEIcon::ActiveState );
    else
        setEffect( m_bDisabled ? TDEIcon::DisabledState : TDEIcon::DefaultState );
}

void KFileIVI::setEffect( int state )
{
    TQIconSet::Mode mode;
    switch( state )
    {
	case TDEIcon::DisabledState:
	    mode = TQIconSet::Disabled;
	    break;
	case TDEIcon::ActiveState:
	    mode = TQIconSet::Active;
	    break;
	case TDEIcon::DefaultState:
	default:
	    mode = TQIconSet::Normal;
	    break;
    }
    // Do not update if the fingerprint is identical (prevents flicker)!

    TDEIconEffect *effect = TDEGlobal::iconLoader()->iconEffect();

    bool haveEffect = effect->hasEffect( TDEIcon::Desktop, m_state ) !=
                      effect->hasEffect( TDEIcon::Desktop, state );

                //kdDebug(1203) << "desktop;defaultstate=" <<
                //      effect->fingerprint(TDEIcon::Desktop, TDEIcon::DefaultState) <<
                //      endl;
                //kdDebug(1203) << "desktop;activestate=" <<
                //      effect->fingerprint(TDEIcon::Desktop, TDEIcon::ActiveState) <<
                //      endl;

    if( haveEffect &&
        effect->fingerprint( TDEIcon::Desktop, m_state ) !=
	effect->fingerprint( TDEIcon::Desktop, state ) )
    {
	// Effects on are not applied until they are first accessed to
	// save memory. Do this now when needed
	if( m_bThumbnail )
	{
	    if( d->icons.isGenerated( TQIconSet::Large, mode ) )
		d->icons.setPixmap( effect->apply( d->thumb, TDEIcon::Desktop, state ),
				    TQIconSet::Large, mode );
	}
	else
	{
	    if( d->icons.isGenerated( TQIconSet::Large, mode ) )
		d->icons.setPixmap( m_fileitem->pixmap( m_size, state ),
				    TQIconSet::Large, mode );
	}
	TQIconViewItem::setPixmap( d->icons.pixmap( TQIconSet::Large, mode ) );
    }
    m_state = state;
}

void KFileIVI::refreshIcon( bool redraw )
{
    if (!isThumbnail()) {
        setIcon( m_size, m_state, true, redraw );
    }
}

void KFileIVI::invalidateThumbnail()
{
    d->thumb = TQPixmap();
}

bool KFileIVI::isThumbnailInvalid() const
{
    return d->thumb.isNull();
}

bool KFileIVI::acceptDrop( const TQMimeSource *mime ) const
{
    if ( mime->provides( "text/uri-list" ) ) // We're dragging URLs
    {
        if ( m_fileitem->acceptsDrops() ) // Directory, executables, ...
            return true;

        // Use cache
        KURL::List uris = ( static_cast<KonqIconViewWidget*>(iconView()) )->dragURLs();

        // Check if we want to drop something on itself
        // (Nothing will happen, but it's a convenient way to move icons)
        KURL::List::Iterator it = uris.begin();
        for ( ; it != uris.end() ; it++ )
        {
            if ( m_fileitem->url().equals( *it, true /*ignore trailing slashes*/ ) )
                return true;
        }
    }
    return TQIconViewItem::acceptDrop( mime );
}

void KFileIVI::setKey( const TQString &key )
{
    TQString theKey = key;

    TQVariant sortDirProp = iconView()->property( "sortDirectoriesFirst" );

    bool isdir = ( S_ISDIR( m_fileitem->mode() ) && ( !sortDirProp.isValid() || ( sortDirProp.type() == TQVariant::Bool && sortDirProp.toBool() ) ) );

    // The order is: .dir (0), dir (1), .file (2), file (3)
    int sortChar = isdir ? 1 : 3;
    if ( m_fileitem->text()[0] == '.' )
        --sortChar;

    if ( !iconView()->sortDirection() ) // reverse sorting
        sortChar = 3 - sortChar;

    theKey.prepend( TQChar( sortChar + '0' ) );

    TQIconViewItem::setKey( theKey );
}

void KFileIVI::dropped( TQDropEvent *e, const TQValueList<TQIconDragItem> & )
{
    KonqOperations::doDrop( item(), item()->url(), e, iconView() );
}

void KFileIVI::returnPressed()
{
    if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
        KURL url = m_fileitem->url();
        if (url.protocol() == "media") {
            // The user reasonably expects to be placed within the media:/ tree
            // when opening a media device from the desktop
            KService::Ptr service = KService::serviceByDesktopName("konqueror");
            if (service) {
                // HACK
                // There doesn't seem to be a way to prevent KRun from resolving the URL to its
                // local path, so simpy launch Konqueror with the correct arguments instead...
                KRun::runCommand("konqueror " + url.url(), "konqueror", service->icon());
            }
            else {
                (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
            }
        }
        else {
            // When clicking on a link to e.g. $HOME from the desktop, we want to open $HOME
            // Symlink resolution must only happen on the desktop though (#63014)
            if ( m_fileitem->isLink() && url.isLocalFile() ) {
                url = KURL( url, m_fileitem->linkDest() );
            }

            (void) new KRun( url, m_fileitem->mode(), m_fileitem->isLocalFile() );
        }
    }
    else {
        m_fileitem->run();
    }
}


void KFileIVI::paintItem( TQPainter *p, const TQColorGroup &c )
{
    TQColorGroup cg = updateColors(c);
    paintFontUpdate( p );

    //*** TEMPORARY CODE - MUST BE MADE CONFIGURABLE FIRST - Martijn
    // SET UNDERLINE ON HOVER ONLY
    /*if ( ( ( KonqIconViewWidget* ) iconView() )->m_pActiveItem == this )
    {
        TQFont f( p->font() );
        f.setUnderline( TRUE );
        p->setFont( f );
    }*/

    TDEIconViewItem::paintItem( p, cg );
    paintOverlay(p);
    paintOverlayProgressBar(p);

}

void KFileIVI::paintOverlay( TQPainter *p ) const
{
    if ( !d->m_overlay.isNull() ) {
        TQRect rect = pixmapRect(true);
        p->drawPixmap(x() + rect.x() , y() + pixmapRect().height() - d->m_overlay.height(), d->m_overlay);
    }
}

void KFileIVI::paintOverlayProgressBar( TQPainter *p ) const
{
    if (d->m_progress != -1) {
//         // Pie chart
//         TQRect rect = pixmapRect(true);
//         rect.setX(x() + rect.x());
//         rect.setY(y() + rect.y() + ((pixmapRect().height()*3)/4));
//         rect.setWidth(pixmapRect().width()/4);
//         rect.setHeight(pixmapRect().height()/4);
//
//         p->save();
//
//         p->setPen(TQPen::NoPen);
//         p->setBrush(TQt::red);
//         p->drawEllipse(rect);
//         p->setBrush(TQt::green);
//         p->drawPie(rect, 1440, (((100-d->m_progress)*5760)/100));

//         // Horizontal progress bar
//         TQRect rect = pixmapRect(true);
//         int verticalOffset = 0;
//         int usedBarWidth = ((d->m_progress*pixmapRect().width())/100);
//         int endPosition = x() + rect.x() + usedBarWidth;
// 
//         p->save();
// 
//         p->setPen(TQPen::NoPen);
//         p->setBrush(TQt::red);
//         p->drawRect(TQRect(x() + rect.x(), y() + rect.y() + (pixmapRect().height() - verticalOffset), usedBarWidth, 1));
//         p->setBrush(TQt::green);
//         p->drawRect(TQRect(endPosition, y() + rect.y() + (pixmapRect().height() - verticalOffset), pixmapRect().width() - usedBarWidth, 1));

        // Vertical progress bar
        TQRect rect = pixmapRect(true);
        int horizontalOffset = 0;
        int usedBarHeight = (((100-d->m_progress)*pixmapRect().height())/100);
        int endPosition = y() + rect.y() + usedBarHeight;

        p->save();

        p->setPen(TQPen::NoPen);
        p->setBrush(TQt::green);
        p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), y() + rect.y(), 1, usedBarHeight));
        p->setBrush(TQt::red);
        p->drawRect(TQRect(x() + rect.x() + (pixmapRect().width() - horizontalOffset), endPosition, 1, pixmapRect().height() - usedBarHeight));

        p->restore();
    }
}

void KFileIVI::paintFontUpdate( TQPainter *p ) const
{
    if ( m_fileitem->isLink() )
    {
        TQFont f( p->font() );
        f.setItalic( TRUE );
        p->setFont( f );
    }
}

TQColorGroup KFileIVI::updateColors( const TQColorGroup &c ) const
{
    TQColorGroup cg( c );
    cg.setColor( TQColorGroup::Text, static_cast<KonqIconViewWidget*>(iconView())->itemColor() );
    return cg;
}

bool KFileIVI::move( int x, int y )
{
    if ( static_cast<KonqIconViewWidget*>(iconView())->isDesktop() ) {
	if ( x < 5 )
	    x = 5;
	if ( x > iconView()->viewport()->width() - ( width() + 5 ) )
	    x = iconView()->viewport()->width() - ( width() + 5 );
	if ( y < 5 )
	    y = 5;
	if ( y > iconView()->viewport()->height() - ( height() + 5 ) )
	    y = iconView()->viewport()->height() - ( height() + 5 );
    }
    return TQIconViewItem::move( x, y );
}

bool KFileIVI::hasAnimation() const
{
    return !d->m_animatedIcon.isEmpty() && !m_bThumbnail;
}

void KFileIVI::setMouseOverAnimation( const TQString& movieFileName )
{
    if ( !movieFileName.isEmpty() )
    {
        //kdDebug(1203) << "TDEIconViewItem::setMouseOverAnimation " << movieFileName << endl;
        d->m_animatedIcon = movieFileName;
    }
}

TQString KFileIVI::mouseOverAnimation() const
{
    return d->m_animatedIcon;
}

bool KFileIVI::isAnimated() const
{
    return d->m_animated;
}

void KFileIVI::setAnimated( bool a )
{
    d->m_animated = a;
}

int KFileIVI::compare( TQIconViewItem *i ) const
{
    KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>(iconView());
    if ( view->caseInsensitiveSort() )
        return key().localeAwareCompare( i->key() );
    else
        return view->m_pSettings->caseSensitiveCompare( key(), i->key() );
}

void KFileIVI::updatePixmapSize()
{
    int size = m_size ? m_size :
        TDEGlobal::iconLoader()->currentSize( TDEIcon::Desktop );

    KonqIconViewWidget* view = static_cast<KonqIconViewWidget*>( iconView() );

    if ( view && view->canPreview( item() ) ) {
        int previewSize = view->previewIconSize( size );
        setPixmapSize( TQSize( previewSize, previewSize ) );
    }
    else {
        TQSize pixSize = TQSize( size, size );
        if ( pixSize != pixmapSize() )
            setPixmapSize( pixSize );
    }
}

/* vim: set noet sw=4 ts=8 softtabstop=4: */