-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerthread.cpp
More file actions
135 lines (126 loc) · 3.85 KB
/
workerthread.cpp
File metadata and controls
135 lines (126 loc) · 3.85 KB
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
#include "workerthread.h"
#include <QDebug>
Worker::Worker(
QObject *parent)
: QObject{parent}
{}
Controller::Controller()
{
pWorker = new Worker;
if ( pWorker == NULL ) //sendMessage, problem
{
emit sendControllerStringBasic( tr( "Error! Worker thread pointer is NULL." ) );
return;
}
//qDebug() << "Controller constructor";
pWorker->moveToThread( &workerThread );
pWorker->setbRunning( false );
connect( &workerThread, &QThread::finished, pWorker, &QObject::deleteLater );
connect( this, &Controller::sendWorkerStartSignal, pWorker, &Worker::doCopyFiles );
connect( pWorker, &Worker::resultReady, this, &Controller::handleResults );
connect( pWorker, &Worker::sendWorkerStringBasic, this, &Controller::receiveControllerStringBasic );
connect( this, &Controller::sendWorkerInterruptSignal, &Controller::InterruptWorker );
workerThread.start();
}
Controller::~Controller()
{
if ( pWorker == NULL )
return;
workerThread.quit();
workerThread.wait();
}
bool Controller::bIsWorkerRunning()
{
if ( pWorker == NULL )
return false;
if ( pWorker->bIsRunning() )
return true;
return false;
}
void Controller::handleResults( const QString s )
{
if ( pWorker == NULL ) // not possible?
return;
if ( !pWorker->bIsRunning() ) // interrupted
return;
pWorker->setbRunning(false);
if ( s.length() )
emit sendControllerStringBasic( s );
emit sendWorkerFinishedSignal();
//qDebug() << "Controller::Result handler called.";
}
void Controller::receiveControllerStringBasic( QString s )
{
emit sendControllerStringBasic( s );
}
void Controller::InterruptWorker()
{
if ( pWorker == NULL )
return;
pWorker->setbRunning( false );
emit sendControllerStringBasic( tr("Copying interrupted, aborting..." ) );
}
void Worker::doCopyFiles( QString sDLPath, QString sGamePath )
{
QStringList slFilters;
QFileInfoList fiList;
qint32 i, j;
QString sFileNameSource, sFileNameTarget;
QFile * pFileSource, * pFileTarget;
this->setbRunning( true );
QDir dirSource( sDLPath );
QDir dirSourceData( sDLPath + "/Data" );
slFilters << "*"; // "*.*" works but don't find all entries!
fiList = dirSource.entryInfoList( slFilters, QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot );
if ( fiList.isEmpty() )
{
//qDebug() << "fiList is empty.";
emit sendWorkerStringBasic( tr( "ERROR: No downloaded files found in %1.\n").arg( dirSource.absolutePath() ) );
return;
}
fiList.append( dirSourceData.entryInfoList( slFilters, QDir::Files | QDir::NoDotAndDotDot ) );
for ( i = fiList.size() - 1; i >= 0; i-- )
{
if ( fiList.at( i ).filePath().contains( ".DepotDownloader", Qt::CaseInsensitive ) )
fiList.removeAt( i );
if ( fiList.at( i ).filePath() == sDLPath + "/Data" )
fiList.removeAt( i );
}
emit sendWorkerStringBasic( tr( "Copying files..." ) );
for ( i = 0; i < fiList.size(); i++ )
{
if ( !this->bIsRunning() ) // interrupted
{
emit sendWorkerStringBasic( tr("Copying aborted." ) );
break;
}
sFileNameSource = fiList.at( i ).filePath();
pFileSource = new QFile( sFileNameSource );
if ( pFileSource == NULL )
continue;
sFileNameTarget = sGamePath;
if ( !sFileNameTarget.endsWith( '/' ) )
sFileNameTarget.append( '/' );
j = sFileNameSource.lastIndexOf( "/Data", Qt::CaseInsensitive );
if ( j > -1 )
sFileNameTarget += "Data/";
sFileNameTarget += fiList.at( i ).fileName(); //pFileSource->fileName();
pFileTarget = new QFile( sFileNameTarget );
if ( pFileTarget == NULL )
{
if ( pFileSource != NULL )
delete pFileSource;
continue;
}
if ( pFileTarget->exists() )
pFileTarget->remove();
pFileSource->copy( pFileTarget->fileName() );
//qDebug() << "copied to: " << pFileTarget->fileName();
emit sendWorkerStringBasic( sFileNameTarget ); //sFileNameSource + "->" +
if ( pFileSource != NULL )
delete pFileSource;
if ( pFileTarget != NULL )
delete pFileTarget;
}
emit resultReady( tr( "Copying files finished." ) );
}