summaryrefslogtreecommitdiffstats
path: root/krusader/DiskUsage/dulistview.cpp
blob: 1fbffe39107eeda844036bb491c5ea43c97c8622 (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
/***************************************************************************
                        dulistview.cpp  -  description
                             -------------------
    copyright            : (C) 2004 by Csaba Karai
    e-mail               : krusader@users.sourceforge.net
    web site             : http://krusader.sourceforge.net
 ---------------------------------------------------------------------------
  Description
 ***************************************************************************

  A

     db   dD d8888b. db    db .d8888.  .d8b.  d8888b. d88888b d8888b.
     88 ,8P' 88  `8D 88    88 88'  YP d8' `8b 88  `8D 88'     88  `8D
     88,8P   88oobY' 88    88 `8bo.   88ooo88 88   88 88ooooo 88oobY'
     88`8b   88`8b   88    88   `Y8b. 88~~~88 88   88 88~~~~~ 88`8b
     88 `88. 88 `88. 88b  d88 db   8D 88   88 88  .8D 88.     88 `88.
     YP   YD 88   YD ~Y8888P' `8888Y' YP   YP Y8888D' Y88888P 88   YD

                                                     S o u r c e    F i l e

 ***************************************************************************
 *                                                                         *
 *   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 "dulistview.h"
#include "../krusader.h"
#include "../kicons.h"
#include "../VFS/krpermhandler.h"
#include <tqfontmetrics.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kglobal.h>
#include <tqheader.h>
#include <time.h>

DUListView::DUListView( DiskUsage *usage, const char *name ) 
    : TQListView( usage, name ), diskUsage( usage )
{  
  setAllColumnsShowFocus(true);
  setVScrollBarMode(TQScrollView::Auto);
  setHScrollBarMode(TQScrollView::Auto);
  setShowSortIndicator(true);
  setRootIsDecorated( true );
  setTreeStepSize( 10 );

  int defaultSize = TQFontMetrics(font()).width("W");
  
  krConfig->setGroup( diskUsage->getConfigGroup() ); 
  int nameWidth  = krConfig->readNumEntry("D Name Width",  defaultSize * 20 );    
  addColumn( i18n("Name"), nameWidth );
  setColumnWidthMode(0,TQListView::Manual);
  int percentWidth  = krConfig->readNumEntry("D Percent Width",  defaultSize * 5 );    
  addColumn( i18n("Percent"), percentWidth );
  setColumnWidthMode(1,TQListView::Manual);
  int totalSizeWidth  = krConfig->readNumEntry("D Total Size Width",  defaultSize * 10 );    
  addColumn( i18n("Total size"), totalSizeWidth );
  setColumnWidthMode(1,TQListView::Manual);
  int ownSizeWidth  = krConfig->readNumEntry("D Own Size Width",  defaultSize * 10 );    
  addColumn( i18n("Own size"), ownSizeWidth );
  setColumnWidthMode(2,TQListView::Manual);
  int typeWidth  = krConfig->readNumEntry("D Type Width",  defaultSize * 10 );
  addColumn( i18n("Type"), typeWidth );
  setColumnWidthMode(3,TQListView::Manual);
  int dateWidth  = krConfig->readNumEntry("D Date Width",  defaultSize * 10 );
  addColumn( i18n("Date"), dateWidth );
  setColumnWidthMode(4,TQListView::Manual);
  int permissionsWidth  = krConfig->readNumEntry("D Permissions Width",  defaultSize * 6 );
  addColumn( i18n("Permissions"), permissionsWidth );
  setColumnWidthMode(5,TQListView::Manual);
  int ownerWidth  = krConfig->readNumEntry("D Owner Width",  defaultSize * 5 );    
  addColumn( i18n("Owner"), ownerWidth );
  setColumnWidthMode(6,TQListView::Manual);
  int groupWidth  = krConfig->readNumEntry("D Group Width",  defaultSize * 5 );    
  addColumn( i18n("Group"), groupWidth );
  setColumnWidthMode(7,TQListView::Manual);
  
  setColumnAlignment( 1, TQt::AlignRight );
  setColumnAlignment( 2, TQt::AlignRight );
  setColumnAlignment( 3, TQt::AlignRight );
  
  setSorting( 2 );

  connect( diskUsage, TQT_SIGNAL( enteringDirectory( Directory * ) ), this, TQT_SLOT( slotDirChanged( Directory * ) ) );
  connect( diskUsage, TQT_SIGNAL( clearing() ), this, TQT_SLOT( clear() ) );
  connect( diskUsage, TQT_SIGNAL( changed( File * ) ), this, TQT_SLOT( slotChanged( File * ) ) );
  connect( diskUsage, TQT_SIGNAL( deleted( File * ) ), this, TQT_SLOT( slotDeleted( File * ) ) );

  connect( this, TQT_SIGNAL(rightButtonPressed(TQListViewItem *, const TQPoint &, int)),
           this, TQT_SLOT( slotRightClicked(TQListViewItem *) ) );
  connect( this, TQT_SIGNAL( expanded ( TQListViewItem * ) ), 
           this, TQT_SLOT( slotExpanded( TQListViewItem * ) ) ); 
}

DUListView::~ DUListView()
{
  krConfig->setGroup( diskUsage->getConfigGroup() ); 
  krConfig->writeEntry("D Name Width",        columnWidth( 0 ) );
  krConfig->writeEntry("D Percent Width",     columnWidth( 1 ) );
  krConfig->writeEntry("D Total Size Width",  columnWidth( 2 ) );
  krConfig->writeEntry("D Own Size Width",    columnWidth( 3 ) );
  krConfig->writeEntry("D Type Width",        columnWidth( 4 ) );
  krConfig->writeEntry("D Date Width",        columnWidth( 5 ) );
  krConfig->writeEntry("D Permissions Width", columnWidth( 6 ) );
  krConfig->writeEntry("D Owner Width",       columnWidth( 7 ) );
  krConfig->writeEntry("D Group Width",       columnWidth( 8 ) );
}

void DUListView::addDirectory( Directory *dirEntry, TQListViewItem *tqparent )
{
  TQListViewItem * lastItem = 0;
    
  if( tqparent == 0 && ! ( dirEntry->tqparent() == 0 ) )
  {
    lastItem = new TQListViewItem( this, ".." );
    lastItem->setPixmap( 0, FL_LOADICON( "up" ) );
    lastItem->setSelectable( false );
  }
          
  for( Iterator<File> it = dirEntry->iterator(); it != dirEntry->end(); ++it )
  {
    File *item = *it;
    
    KMimeType::Ptr mimePtr = KMimeType::mimeType( item->mime() );
    TQString mime = mimePtr->comment();
       
    time_t tma = item->time();
    struct tm* t=localtime((time_t *)&tma);
    TQDateTime tmp(TQDate(t->tm_year+1900, t->tm_mon+1, t->tm_mday), TQTime(t->tm_hour, t->tm_min));
    TQString date = KGlobal::locale()->formatDateTime(tmp);    
    
    TQString totalSize = KRpermHandler::parseSize( item->size() ) + " ";
    TQString ownSize = KRpermHandler::parseSize( item->ownSize() ) + " ";
    TQString percent = item->percent();
    
    if( lastItem == 0 && tqparent == 0 )
      lastItem = new DUListViewItem( diskUsage, item, this, item->name(), percent, totalSize, ownSize, 
                                     mime, date, item->perm(), item->owner(), item->group() );
    else if ( lastItem == 0 )
      lastItem = new DUListViewItem( diskUsage, item, tqparent, item->name(), percent, totalSize, ownSize, 
                                     mime, date, item->perm(), item->owner(), item->group() );
    else if ( tqparent == 0 )
      lastItem = new DUListViewItem( diskUsage, item, this, lastItem, item->name(), percent, totalSize,
                                     ownSize, mime, date, item->perm(), item->owner(), item->group() );
    else
      lastItem = new DUListViewItem( diskUsage, item, tqparent, lastItem, item->name(), percent, totalSize, 
                                     ownSize, mime, date, item->perm(), item->owner(), item->group() );
   
    if( item->isExcluded() )
      lastItem->setVisible( false );
                                    
    lastItem->setPixmap( 0, diskUsage->getIcon( item->mime() ) );
    
    if( item->isDir() && !item->isSymLink() )
      lastItem->setExpandable( true );
  }
  
  TQListViewItem *first = firstChild();
  if( first )
    setCurrentItem( first );
}

void DUListView::slotDirChanged( Directory *dirEntry )
{
  clear();  
  addDirectory( dirEntry, 0 );
}

File * DUListView::getCurrentFile()
{
  TQListViewItem *item = currentItem();
  
  if( item == 0 || item->text( 0 ) == ".." )
    return 0;
  
  return ((DUListViewItem *)item)->getFile();
}

void DUListView::slotChanged( File * item )
{
  void * itemPtr = diskUsage->getProperty( item, "ListView-Ref" );
  if( itemPtr == 0 )
    return;
    
  DUListViewItem *duItem = (DUListViewItem *)itemPtr;
  duItem->setVisible( !item->isExcluded() );
  duItem->setText( 1, item->percent() );
  duItem->setText( 2, KRpermHandler::parseSize( item->size() ) + " " );
  duItem->setText( 3, KRpermHandler::parseSize( item->ownSize() ) + " " );
}

void DUListView::slotDeleted( File * item )
{
  void * itemPtr = diskUsage->getProperty( item, "ListView-Ref" );
  if( itemPtr == 0 )
    return;
    
  DUListViewItem *duItem = (DUListViewItem *)itemPtr;
  delete duItem;
}
  
void DUListView::slotRightClicked( TQListViewItem *item )
{
  File * file = 0;
  
  if ( item && item->text( 0 ) != ".." )
    file = ((DUListViewItem *)item)->getFile();

  diskUsage->rightClickMenu( file );
}

bool DUListView::doubleClicked( TQListViewItem * item )
{
  if( item )
  {
    if( item->text( 0 ) != ".." )
    {
      File *fileItem = ((DUListViewItem *)item)->getFile();
      if( fileItem->isDir() )
        diskUsage->changeDirectory( dynamic_cast<Directory *> ( fileItem ) );
      return true;
    }
    else
    {
      Directory *upDir = (Directory *)diskUsage->getCurrentDir()->tqparent();
    
      if( upDir )
        diskUsage->changeDirectory( upDir );
      return true;
    }
  }
  return false;
}

void DUListView::contentsMouseDoubleClickEvent ( TQMouseEvent * e )
{
  if ( e || e->button() == Qt::LeftButton )
  {
    TQPoint vp = contentsToViewport(e->pos());
    TQListViewItem * item = itemAt( vp );

    if( doubleClicked( item ) )
      return;
    
  }
  TQListView::contentsMouseDoubleClickEvent( e );
}

void DUListView::keyPressEvent( TQKeyEvent *e )
{
  switch ( e->key() )
  {
  case Key_Return :
  case Key_Enter :
    if( doubleClicked( currentItem() ) )
      return;
    break;
  case Key_Left :
  case Key_Right :
  case Key_Up :
  case Key_Down :
    if( e->state() == ShiftButton )
    {
      e->ignore();
      return;
    }
    break;
  case Key_Delete :
    e->ignore();
    return;
  }
  TQListView::keyPressEvent( e );
}

void DUListView::slotExpanded( TQListViewItem *item )
{
  if( item == 0 || item->text( 0 ) == ".." )
    return;
 
  if( item->childCount() == 0 )
  {
    File *fileItem = ((DUListViewItem *)item)->getFile();
    if( fileItem->isDir() )
      addDirectory( dynamic_cast<Directory *>( fileItem ), item );   
  }
}

#include "dulistview.moc"