-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirsizecounter.cpp
95 lines (73 loc) · 2 KB
/
dirsizecounter.cpp
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
#include <QDir>
#include <QDirIterator>
#include <QThread>
#include <QTimer>
#include "dirsizecounter.h"
#include <QDebug>
DirSizeCounter::DirSizeCounter(QObject *parent) :
QObject(parent)
{
DirSizeCounterWorker *worker = new DirSizeCounterWorker;
worker->moveToThread(&thread_);
connect(&thread_, &QThread::finished,
worker, &QObject::deleteLater);
connect(this, SIGNAL(operate(const QString &)),
worker, SLOT(countSize(const QString &)));
connect(worker, SIGNAL(resultPartlyReady(qint64)),
this, SLOT(emitSizePartlyCounted(qint64)));
connect(worker, SIGNAL(resultReady(qint64)),
this, SLOT(emitSizeCounted(qint64)));
thread_.start();
}
DirSizeCounter::~DirSizeCounter()
{
thread_.quit();
thread_.wait();
}
void DirSizeCounter::countSize(const QString &path)
{
emit operate(path);
}
void DirSizeCounter::emitSizePartlyCounted(qint64 res)
{
emit sizePartlyCounted(res);
}
void DirSizeCounter::emitSizeCounted(qint64 res)
{
emit sizeCounted(res);
}
DirSizeCounterWorker::DirSizeCounterWorker(QObject *parent) :
QObject(parent)
{
// Nothing here
}
qint64 DirSizeCounterWorker::countSize(const QString &path)
{
size_ = 0;
size_ = recursiveCounter(path);
emit resultReady(size_);
return size_;
}
qint64 DirSizeCounterWorker::recursiveCounter(const QString &path)
{
QDir dir(path);
dir.setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Dirs);
QDirIterator iter(dir);
qint64 size = 0;
while(iter.hasNext()) {
QString nPath = iter.next();
QFileInfo nInfo(nPath);
if(nInfo.isDir()) {
size += recursiveCounter(nPath);
}
else {
qint64 tSize = nInfo.size();
size += tSize;
// Don't wanna think the non recursive equivalent, so size_
// is just a duplicate size tracker outside recursion.
size_ += tSize;
}
}
emit resultPartlyReady(size_);
return size;
}