summaryrefslogtreecommitdiffstats
path: root/tdeioslave/trash/discspaceutil.cpp
blob: e231b901c583af0bafed5f8f6080516453545df8 (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
/*
   This file is part of the TDE project

   Copyright (C) 2008 Tobias Koenig <tokoe@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 <tqapplication.h>
#include <tqdir.h>
#include <tqeventloop.h>
#include <tqfileinfo.h>

#include <kdiskfreesp.h>
#include <kdebug.h>

#include "discspaceutil.h"

DiscSpaceUtil::DiscSpaceUtil( const TQString &directory, TQObject *parent )
  : TQObject( parent ),
    mDirectory( directory ),
    mFullSize( 0 )
{
  calculateFullSize();
}

unsigned long DiscSpaceUtil::sizeOfPath( const TQString &path )
{
  TQFileInfo info( path );
  if ( !info.exists() ) {
    return 0;
  }

  if ( info.isFile() ) {
    return info.size();
  } else if ( info.isDir() ) {
    TQDir dir( path );
    const TQFileInfoList *infos = dir.entryInfoList( TQDir::Files | TQDir::Dirs | TQDir::NoSymLinks );
    TQFileInfoListIterator it( *infos );

    unsigned long sum = 0;
    TQFileInfo *info = 0;
    while ( (info = it.current()) != 0 ) {
      if ( info->fileName() != "." && info->fileName() != ".." )
      sum += sizeOfPath( info->absFilePath() );
      ++it;
    }

    return sum;
  } else {
    return 0;
  }
}

double DiscSpaceUtil::usage( unsigned long additional ) const
{
  if ( mFullSize == 0 )
    return 0;

  unsigned long sum = sizeOfPath( mDirectory );
  sum += additional;

  sum = sum/1024; // convert to kB

  return (((double)sum*100)/(double)mFullSize);
}

unsigned long DiscSpaceUtil::size() const
{
  return mFullSize;
}

TQString DiscSpaceUtil::mountPoint() const
{
  return mMountPoint;
}

void DiscSpaceUtil::foundMountPoint( const TQString &mountPoint, unsigned long kbSize, unsigned long, unsigned long )
{
  mFullSize = kbSize;
  mMountPoint = mountPoint;
}

void DiscSpaceUtil::done()
{
  tqApp->eventLoop()->exitLoop();
}

void DiscSpaceUtil::calculateFullSize()
{
  KDiskFreeSp *sp = KDiskFreeSp::findUsageInfo( mDirectory );
  connect( sp, TQ_SIGNAL( foundMountPoint( const TQString&, unsigned long, unsigned long, unsigned long ) ),
           this, TQ_SLOT( foundMountPoint( const TQString&, unsigned long, unsigned long, unsigned long ) ) );
  connect( sp, TQ_SIGNAL( done() ), this, TQ_SLOT( done() ) );

  tqApp->eventLoop()->enterLoop();
}

#include "discspaceutil.moc"