summaryrefslogtreecommitdiffstats
path: root/konqueror/listview/konq_listviewitems.cpp
blob: 61abceed3f0ac845b7d288a616c5a6f888ad88cb (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
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>

   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.

   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
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

//-Not needed right now: define DEBUG_SORTFUNCS

#include "konq_listview.h"
#include <konq_settings.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <assert.h>
#include <stdio.h>
#include <tqpainter.h>
#include <tqheader.h>
#include <kiconloader.h>

static TQString retrieveExtraEntry( KFileItem* fileitem, int numExtra )
{
    /// ######## SLOOOOW
    TDEIO::UDSEntry::ConstIterator it = fileitem->entry().begin();
    const TDEIO::UDSEntry::ConstIterator end = fileitem->entry().end();
    int n = 0;
    for( ; it != end; ++it )
    {
        if ((*it).m_uds == TDEIO::UDS_EXTRA)
        {
            ++n;
            if ( n == numExtra )
            {
                return (*it).m_str;
            }
        }
    }
    return TQString::null;
}


/**************************************************************
 *
 * KonqListViewItem
 *
 **************************************************************/
KonqListViewItem::KonqListViewItem( KonqBaseListViewWidget *_listViewWidget,
                                    KonqListViewItem * _parent, KFileItem* _fileitem )
   : KonqBaseListViewItem( _listViewWidget, _parent, _fileitem ),
     m_pixmaps( listView()->columns() )
{
   updateContents();
}

KonqListViewItem::KonqListViewItem( KonqBaseListViewWidget *_listViewWidget, KFileItem* _fileitem )
   : KonqBaseListViewItem( _listViewWidget, _fileitem ),
     m_pixmaps( listView()->columns() )
{
   updateContents();
}

KonqListViewItem::~KonqListViewItem()
{
   for ( TQValueVector<TQPixmap*>::iterator
            it = m_pixmaps.begin(), itEnd = m_pixmaps.end();
         it != itEnd; ++it )
      delete *it;
}

void KonqListViewItem::updateContents()
{
   // Set the pixmap
   setDisabled( m_bDisabled );

   // Set the text of each column
   setText( 0, m_fileitem->text() );

   //----------------------------------------
   //--- Begin: listview sorting enhancements
   //----------------------------------------

   //--- Retrieve user-set extended sorting options:

   bool Group_Directories_First =
     m_pListViewWidget->m_pBrowserView->m_pProps->isDirsFirst() ;
   bool Group_Hidden_First =
     m_pListViewWidget->m_pBrowserView->m_pProps->isHiddenFirst() ;
   bool Dictionary_Order_Sort =
     m_pListViewWidget->m_pBrowserView->m_pProps->isDictionaryOrder() ;

   // The default TDE order is: .dir (0), dir (1), .file (2), file (3)

   //--- Implement sort option "Group Directories First"

   if ( Group_Directories_First ) 
     sortChar = S_ISDIR( m_fileitem->mode() ) ? 1 : 3;
   else
     sortChar = 3; // Effectively disable grouping directories first

   //--- Implement sort option "Group Hidden First"

   if ( Group_Hidden_First && m_fileitem->text()[0] == '.' )
      --sortChar; // Effectively disable grouping hidden first

   //--- Implement option "Dictionary Order Sort"

   sortString = "" ;
   if ( Dictionary_Order_Sort && text(0).length() > 1 ) {
     /* 
      * Objective is to ignore non-alphnumeric leading characters
      * but append them as trailing characters to ensure that there
      * is a predictable collation of what might be otherwise end up
      * being identical strings. Example: a, " a" b, +a, ~a will end
      * up sorting to a, +a, " a", ~a, b
     */
     TQChar  Our1stChar = text(0)[0] ;
     if ( ! Our1stChar.isLetterOrNumber() ) {
       sortString = text(0).mid(1) ;
       sortString.append(Our1stChar) ;
       #ifdef DEBUG_SORTFUNCS
       kdWarning() << "Stripping non-alphanumeric leading character '" <<
        Our1stChar << "' yields sort key '" << sortString << "'" << endl ;
       #endif
     }
     else {
       sortString = text(0).left(-1) ;
       sortString.append( TQChar(0) ) ;
     }
   }
   #ifdef DEBUG_SORTFUNCS
   kdWarning() << "Sortkey ='" << sortString << "'" << endl ;
   #endif

   //----------------------------------------
   //--- End: listview sorting enhancements 
   //----------------------------------------

   //now we have the first column, so let's do the rest

   int numExtra = 1;
   for ( unsigned int i=0; i<m_pListViewWidget->NumberOfAtoms; i++ )
   {
      ColumnInfo *tmpColumn=&m_pListViewWidget->columnConfigInfo()[i];
      if (tmpColumn->displayThisOne)
      {
         switch (tmpColumn->udsId)
         {
         case TDEIO::UDS_USER:
            setText(tmpColumn->displayInColumn,m_fileitem->user());
            break;
         case TDEIO::UDS_GROUP:
            setText(tmpColumn->displayInColumn,m_fileitem->group());
            break;
         case TDEIO::UDS_FILE_TYPE:
            if (m_fileitem->isMimeTypeKnown()) {
               setText(tmpColumn->displayInColumn,m_fileitem->mimeComment());
            }
            break;
         case TDEIO::UDS_MIME_TYPE:
            if (m_fileitem->isMimeTypeKnown()) {
               setText(tmpColumn->displayInColumn,m_fileitem->mimetype());
            }
            break;
         case TDEIO::UDS_URL:
            setText(tmpColumn->displayInColumn,m_fileitem->url().prettyURL());
            break;
         case TDEIO::UDS_LINK_DEST:
            setText(tmpColumn->displayInColumn,m_fileitem->linkDest());
            break;
         case TDEIO::UDS_SIZE:
            if ( m_pListViewWidget->m_pSettings->fileSizeInBytes() )
                setText(tmpColumn->displayInColumn,TDEGlobal::locale()->formatNumber( m_fileitem->size(),0)+" ");
            else
                setText(tmpColumn->displayInColumn,TDEIO::convertSize(m_fileitem->size())+" ");
            break;
         case TDEIO::UDS_ACCESS:
            setText(tmpColumn->displayInColumn,m_fileitem->permissionsString());
            break;
         case TDEIO::UDS_MODIFICATION_TIME:
         case TDEIO::UDS_ACCESS_TIME:
         case TDEIO::UDS_CREATION_TIME:
            {
               TQDateTime dt;
               time_t _time = m_fileitem->time( tmpColumn->udsId );
               if ( _time != 0 )
               {
                   dt.setTime_t( _time );
                   setText(tmpColumn->displayInColumn, TDEGlobal::locale()->formatDateTime(dt));
               }
               else
               {
                   setText(tmpColumn->displayInColumn, TQString::null);
               }
            }
            break;
         case TDEIO::UDS_EXTRA:
         {
             const TQString entryStr = retrieveExtraEntry( m_fileitem, numExtra );
             if ( tmpColumn->type == TQVariant::DateTime )
             {
                 TQDateTime dt = TQT_TQDATETIME_OBJECT(TQDateTime::fromString( entryStr, Qt::ISODate ));
                 setText(tmpColumn->displayInColumn,
                         TDEGlobal::locale()->formatDateTime(dt));
             }
             else // if ( tmpColumn->type == TQVariant::String )
                 setText(tmpColumn->displayInColumn, entryStr);
             ++numExtra;
             break;
         }
         default:
            break;
         };
      };
   };
}

void KonqListViewItem::setDisabled( bool disabled )
{
    KonqBaseListViewItem::setDisabled( disabled );
    int iconSize = m_pListViewWidget->iconSize();
    iconSize = iconSize ? iconSize : TDEGlobal::iconLoader()->currentSize( TDEIcon::Small ); // Default = small
    setPixmap( 0, m_fileitem->pixmap( iconSize, state() ) );
}

void KonqListViewItem::setActive( bool active )
{
    if ( m_bActive == active )
        return;

    //#### Optimize away repaint if possible, like the iconview does?
    KonqBaseListViewItem::setActive( active );
    int iconSize = m_pListViewWidget->iconSize();
    iconSize = iconSize ? iconSize : TDEGlobal::iconLoader()->currentSize( TDEIcon::Small ); // Default = small
    setPixmap( 0, m_fileitem->pixmap( iconSize, state() ) );
}

void KonqListViewItem::setPixmap( int column, const TQPixmap& pm )
{
   if ( column < 0 )
      return;

   const TQPixmap *current = pixmap( column );

   if ( ( pm.isNull() && !current ) ||
        ( current && pm.serialNumber() == current->serialNumber() ) )
      return;

   int oldWidth = current ? current->width() : 0;
   int oldHeight = current ? current->height() : 0;

   if ( (int)m_pixmaps.size() <= column )
      m_pixmaps.resize( column+1 );

   delete current;
   m_pixmaps[column] = pm.isNull() ? 0 : new TQPixmap( pm );

   int newWidth = pm.isNull() ? 0 : pm.width();
   int newHeight = pm.isNull() ? 0 : pm.height();

   // If the height or width have changed then we're going to have to repaint
   // this whole thing.  Fortunately since most of the calls are coming from
   // setActive() this is the uncommon case.

   if ( oldWidth != newWidth || oldHeight != newHeight )
   {
      setup();
      widthChanged( column );
      invalidateHeight();
      return;
   }

   // If we're just replacing the icon with another one its size -- i.e. a
   // "highlighted" icon, don't bother repainting the whole widget.

   TQListView *lv = m_pListViewWidget;

   int decorationWidth = lv->treeStepSize() * ( depth() + ( lv->rootIsDecorated() ? 1 : 0 ) );
   int x = lv->header()->sectionPos( column ) + decorationWidth + lv->itemMargin();
   int y = lv->itemPos( this );
   int w = newWidth;
   int h = height();
   lv->repaintContents( x, y, w, h );
}

const TQPixmap* KonqListViewItem::pixmap( int column ) const
{
   bool ok;
   if ((int)m_pixmaps.count() <= column)
      return 0;

   TQPixmap *pm = m_pixmaps.at( column, &ok );
   if( !ok )
      return 0;
   return pm;
}

int KonqBaseListViewItem::compare( TQListViewItem* item, int col, bool ascending ) const
{
   KonqListViewItem* k = static_cast<KonqListViewItem*>( item );
   if ( sortChar != k->sortChar )
      // Dirs are always first, even when sorting in descending order
      return !ascending ? k->sortChar - sortChar : sortChar - k->sortChar;

   int numExtra = 0;
   for ( unsigned int i=0; i<m_pListViewWidget->NumberOfAtoms; i++ )
   {
      ColumnInfo *cInfo = &m_pListViewWidget->columnConfigInfo()[i];
      if ( cInfo->udsId == TDEIO::UDS_EXTRA )
          ++numExtra;
      if ( col == cInfo->displayInColumn )
      {
         switch ( cInfo->udsId )
         {
            case TDEIO::UDS_MODIFICATION_TIME:
            case TDEIO::UDS_ACCESS_TIME:
            case TDEIO::UDS_CREATION_TIME:
            {
                time_t t1 = m_fileitem->time( cInfo->udsId );
                time_t t2 = k->m_fileitem->time( cInfo->udsId );
                return ( t1 > t2 ) ? 1 : ( t1 < t2 ) ? -1 : 0;
            }
            case TDEIO::UDS_SIZE:
            {
                TDEIO::filesize_t s1 = m_fileitem->size();
                TDEIO::filesize_t s2 = k->m_fileitem->size();
                return ( s1 > s2 ) ? 1 : ( s1 < s2 ) ? -1 : 0;
            }
            case TDEIO::UDS_EXTRA:
            {
                if ( cInfo->type & TQVariant::DateTime ) {
                    const TQString entryStr1 = retrieveExtraEntry( m_fileitem, numExtra );
                    TQDateTime dt1 = TQT_TQDATETIME_OBJECT(TQDateTime::fromString( entryStr1, Qt::ISODate ));
                    const TQString entryStr2 = retrieveExtraEntry( k->m_fileitem, numExtra );
                    TQDateTime dt2 = TQT_TQDATETIME_OBJECT(TQDateTime::fromString( entryStr2, Qt::ISODate ));
                    return ( dt1 > dt2 ) ? 1 : ( dt1 < dt2 ) ? -1 : 0;
                }
            }
            default:
                break;
         }
         break;
      }
   }

   //----------------------------------------
   //--- Begin: listview sorting enhancements 
   //----------------------------------------

   if ( col == 0 && ! sortString.isEmpty() ) {
     /* We are here because sort column is FileName
        and "Dictionary Order Sort" is in effect
     */
     #ifdef DEBUG_SORTFUNCS
     kdWarning() << "Comparing '" << sortString 
      << "' with '" << k->sortString  <<  "'" << endl ;
     #endif

     /* FIXME: The combination of settings "Dictionary Order Sort" = ON
        and "Case Insensitive Sort" = OFF produces a seemingly odd and
        potentially undesirable outcome. Consider ignoring the latter
        setting and always doing the case-insensitive sort.
     */
     if ( m_pListViewWidget->caseInsensitiveSort() )
       return sortString.lower().localeAwareCompare( k->sortString.lower() );
     else {
       return m_pListViewWidget->m_pSettings->caseSensitiveCompare( sortString, k->sortString );
     }
   }
   /* Original code:
   */
   if ( m_pListViewWidget->caseInsensitiveSort() )
       return text( col ).lower().localeAwareCompare( k->text( col ).lower() );
   else {
       return m_pListViewWidget->m_pSettings->caseSensitiveCompare( text( col ), k->text( col ) );
   }

   //----------------------------------------
   //--- End: listview sorting enhancements 
   //----------------------------------------
}

void KonqListViewItem::paintCell( TQPainter *_painter, const TQColorGroup & _cg, int _column, int _width, int _alignment )
{
    TQColorGroup cg( _cg );

    if ( _column == 0 )
    {
        _painter->setFont( m_pListViewWidget->itemFont() );
    }

    cg.setColor( TQColorGroup::Text, m_pListViewWidget->itemColor() );

    TDEListView *lv = static_cast< TDEListView* >( listView() );
    const TQPixmap *pm = TQT_TQPIXMAP_CONST(lv->viewport()->paletteBackgroundPixmap());
    if ( _column == 0 && isSelected() && !lv->allColumnsShowFocus() )
    {
        int newWidth = width( lv->fontMetrics(), lv, _column );
        if ( newWidth > _width )
            newWidth = _width;
        if ( pm && !pm->isNull() )
        {
            cg.setBrush( TQColorGroup::Base, TQBrush( backgroundColor(_column), *pm ) );
            TQPoint o = _painter->brushOrigin();
            _painter->setBrushOrigin( o.x() - lv->contentsX(), o.y() - lv->contentsY() );
            const TQColorGroup::ColorRole crole =
                TQPalette::backgroundRoleFromMode( lv->viewport()->backgroundMode() );
            _painter->fillRect( newWidth, 0, _width - newWidth, height(), cg.brush( crole ) );
            _painter->setBrushOrigin( o );
        }
        else
        {
            _painter->fillRect( newWidth, 0, _width - newWidth, height(), backgroundColor(_column) );
        }

        _width = newWidth;
    }

    TDEListViewItem::paintCell( _painter, cg, _column, _width, _alignment );
}

void KonqListViewItem::paintFocus( TQPainter * _painter, const TQColorGroup & cg, const TQRect & _r )
{
    TQRect r( _r );
    TQListView *lv = static_cast< TQListView * >( listView() );
    r.setWidth( width( lv->fontMetrics(), lv, 0 ) );
    if ( r.right() > lv->header()->sectionRect( 0 ).right() )
        r.setRight( lv->header()->sectionRect( 0 ).right() );
    TQListViewItem::paintFocus( _painter, cg, r );
}

const char* KonqBaseListViewItem::makeAccessString( const mode_t mode)
{
   static char buffer[ 12 ];

   char uxbit,gxbit,oxbit;

   if ( (mode & (S_IXUSR|S_ISUID)) == (S_IXUSR|S_ISUID) )
      uxbit = 's';
   else if ( (mode & (S_IXUSR|S_ISUID)) == S_ISUID )
      uxbit = 'S';
   else if ( (mode & (S_IXUSR|S_ISUID)) == S_IXUSR )
      uxbit = 'x';
   else
      uxbit = '-';

   if ( (mode & (S_IXGRP|S_ISGID)) == (S_IXGRP|S_ISGID) )
      gxbit = 's';
   else if ( (mode & (S_IXGRP|S_ISGID)) == S_ISGID )
      gxbit = 'S';
   else if ( (mode & (S_IXGRP|S_ISGID)) == S_IXGRP )
      gxbit = 'x';
   else
      gxbit = '-';

   if ( (mode & (S_IXOTH|S_ISVTX)) == (S_IXOTH|S_ISVTX) )
      oxbit = 't';
   else if ( (mode & (S_IXOTH|S_ISVTX)) == S_ISVTX )
      oxbit = 'T';
   else if ( (mode & (S_IXOTH|S_ISVTX)) == S_IXOTH )
      oxbit = 'x';
   else
      oxbit = '-';

   buffer[0] = ((( mode & S_IRUSR ) == S_IRUSR ) ? 'r' : '-' );
   buffer[1] = ((( mode & S_IWUSR ) == S_IWUSR ) ? 'w' : '-' );
   buffer[2] = uxbit;
   buffer[3] = ((( mode & S_IRGRP ) == S_IRGRP ) ? 'r' : '-' );
   buffer[4] = ((( mode & S_IWGRP ) == S_IWGRP ) ? 'w' : '-' );
   buffer[5] = gxbit;
   buffer[6] = ((( mode & S_IROTH ) == S_IROTH ) ? 'r' : '-' );
   buffer[7] = ((( mode & S_IWOTH ) == S_IWOTH ) ? 'w' : '-' );
   buffer[8] = oxbit;
   buffer[9] = 0;

   return buffer;
}

KonqBaseListViewItem::KonqBaseListViewItem(KonqBaseListViewWidget *_listViewWidget, KFileItem* _fileitem)
:TDEListViewItem(_listViewWidget)
,sortChar(0)
,sortString() // listview sorting enhancement
,m_bDisabled(false)
,m_bActive(false)
,m_fileitem(_fileitem)
,m_fileitemURL(_fileitem->url())
,m_pListViewWidget(_listViewWidget)
{}

KonqBaseListViewItem::KonqBaseListViewItem(KonqBaseListViewWidget *_listViewWidget, KonqBaseListViewItem *_parent, KFileItem* _fileitem)
:TDEListViewItem(_parent)
,sortChar(0)
,sortString() // listview sorting enhancement
,m_bDisabled(false)
,m_bActive(false)
,m_fileitem(_fileitem)
,m_fileitemURL(_fileitem->url())
,m_pListViewWidget(_listViewWidget)
{}

KonqBaseListViewItem::~KonqBaseListViewItem()
{
   if (m_pListViewWidget->m_activeItem == this)
      m_pListViewWidget->m_activeItem = 0;
   if (m_pListViewWidget->m_dragOverItem == this)
      m_pListViewWidget->m_dragOverItem = 0;

   if (m_pListViewWidget->m_selected)
      m_pListViewWidget->m_selected->removeRef(this);

   // FIXME: Newbie questiion: do I need to somehow "free" sortString?
}

TQRect KonqBaseListViewItem::rect() const
{
    TQRect r = m_pListViewWidget->itemRect(this);
    return TQRect( m_pListViewWidget->viewportToContents( r.topLeft() ), TQSize( r.width(), r.height() ) );
}

void KonqBaseListViewItem::mimetypeFound()
{
    // Update icon
    setDisabled( m_bDisabled );
    uint done = 0;
    KonqBaseListViewWidget * lv = m_pListViewWidget;
    for (unsigned int i=0; i<m_pListViewWidget->NumberOfAtoms && done < 2; i++)
    {
        ColumnInfo *tmpColumn=&lv->columnConfigInfo()[i];
        if (lv->columnConfigInfo()[i].udsId==TDEIO::UDS_FILE_TYPE && tmpColumn->displayThisOne)
        {
            setText(tmpColumn->displayInColumn, m_fileitem->mimeComment());
            done++;
        }
        if (lv->columnConfigInfo()[i].udsId==TDEIO::UDS_MIME_TYPE && tmpColumn->displayThisOne)
        {
            setText(tmpColumn->displayInColumn, m_fileitem->mimetype());
            done++;
        }
    }
}