summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bdirsizejob.cpp
blob: cddf0e7b02508cd31c056dac445af8dc80765e72 (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
/* 
 *
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
 * Copyright (C) 2006 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.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.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bdirsizejob.h"

#include <k3bthread.h>
#include <k3bthreadjob.h>
#include <k3bsimplejobhandler.h>
#include <k3bglobals.h>

#include <kdebug.h>
#include <kglobal.h>

#include <tqfileinfo.h>
#include <tqdir.h>


class K3bDirSizeJob::WorkThread : public K3bThread
{
public:
  WorkThread()
    : K3bThread(),
      followSymlinks(false),
      totalSize(0),
      totalFiles(0),
      totalDirs(0),
      totalSymlinks(0) {
  }

  void init() {
    m_canceled = false;

    totalSize = 0;
    totalFiles = 0;
    totalDirs = 0;
    totalSymlinks = 0;
  }

  void run() {
    emitStarted();

    TQStringList l;
    for( KURL::List::const_iterator it = urls.begin();
	 it != urls.end(); ++it ) {
      const KURL& url = *it;

      if( !url.isLocalFile() ) {
	kdDebug() << "(K3bDirSizeJob) no remote support." << endl;
	emitFinished( false );
	return;
      }

      l.append( url.path() );
    }

    emitFinished( countFiles( l, TQString() ) );
  }
  
  bool countDir( const TQString& dir ) {
    const TQString& dot = KGlobal::staticQString( "." );
    const TQString& dotdot = KGlobal::staticQString( ".." );
    TQStringList l = TQDir(dir).entryList( TQDir::All|TQDir::Hidden|TQDir::System );
    l.remove( dot );
    l.remove( dotdot );

    return countFiles( l, dir );
  }


  bool countFiles( const TQStringList& l, const TQString& dir ) {
    for( TQStringList::const_iterator it = l.begin();
	 it != l.end(); ++it ) {

      if( m_canceled )
	return false;

      k3b_struct_stat s;
      if( k3b_lstat( TQFile::encodeName( dir + *it ), &s ) )
	return false;	

      if( S_ISLNK( s.st_mode ) ) {
	++totalSymlinks;
	if( followSymlinks ) {
	  if( k3b_stat( TQFile::encodeName( dir + *it ), &s ) )
	    return false;	
	}
      }

      if( S_ISDIR( s.st_mode ) ) {
	++totalDirs;
	if( !countDir( dir + *it + '/' ) )
	  return false;
      }
      else if( !S_ISLNK( s.st_mode ) ) {
	++totalFiles;
	totalSize += (KIO::filesize_t)s.st_size;
      }
    }

    return true;
  }

  void cancel() {
    m_canceled = true;
    emitCanceled();
    wait();
  }

  KURL::List urls;
  bool followSymlinks;

  KIO::filesize_t totalSize;
  KIO::filesize_t totalFiles;
  KIO::filesize_t totalDirs;
  KIO::filesize_t totalSymlinks;

private:
  bool m_canceled;
};


K3bDirSizeJob::K3bDirSizeJob( TQObject* parent )
  : K3bThreadJob( new K3bSimpleJobHandler(), parent )
{
  d = new WorkThread;
  setThread( d );
}


K3bDirSizeJob::~K3bDirSizeJob()
{
  delete d;
  delete jobHandler();
}


KIO::filesize_t K3bDirSizeJob::totalSize() const
{
  return d->totalSize;
}


KIO::filesize_t K3bDirSizeJob::totalFiles() const
{
  return d->totalFiles;
}


KIO::filesize_t K3bDirSizeJob::totalDirs() const
{
  return d->totalDirs;
}


KIO::filesize_t K3bDirSizeJob::totalSymlinks() const
{
  return d->totalSymlinks;
}


void K3bDirSizeJob::setUrls( const KURL::List& urls )
{
  d->urls = urls;
}


void K3bDirSizeJob::setFollowSymlinks( bool b )
{
  d->followSymlinks = b;
}

#include "k3bdirsizejob.moc"