QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
qgscopyfiletask.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgscopyfiletask.cpp
3
--------------------------------------
4
Date : March 2021
5
Copyright : (C) 2021 by Julien Cabieces
6
Email : julien dot cabieces at oslandia dot com
7
***************************************************************************
8
* *
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
13
* *
14
***************************************************************************/
15
16
#include "
qgscopyfiletask.h
"
17
18
#include <QDir>
19
#include <QFile>
20
#include <QFileInfo>
21
22
#include "moc_qgscopyfiletask.cpp"
23
24
QgsCopyFileTask::QgsCopyFileTask
(
const
QString &source,
const
QString &
destination
)
25
: mSource( source )
26
, mDestination(
destination
)
27
{}
28
29
bool
QgsCopyFileTask::run
()
30
{
31
QFile fileSource( mSource );
32
if
( !fileSource.exists() )
33
{
34
mErrorString = tr(
"Source file '%1' does not exist"
).arg( mSource );
35
return
false
;
36
}
37
38
if
( QFileInfo( mDestination ).isDir() )
39
{
40
mDestination = QDir( mDestination ).absoluteFilePath( QFileInfo( fileSource ).fileName() );
41
}
42
43
QFile fileDestination( mDestination );
44
if
( fileDestination.exists() )
45
{
46
mErrorString = tr(
"Destination file '%1' already exist"
).arg( mDestination );
47
return
false
;
48
}
49
50
const
QDir destinationDir = QFileInfo( mDestination ).absoluteDir();
51
if
( !destinationDir.exists() )
52
{
53
mErrorString = tr(
"Destination directory '%1' does not exist"
).arg( destinationDir.absolutePath() );
54
return
false
;
55
}
56
57
if
( !fileSource.open( QIODevice::ReadOnly ) )
58
{
59
mErrorString = tr(
"Could not open '%1' for reading"
).arg( mSource );
60
return
false
;
61
}
62
if
( !fileDestination.open( QIODevice::WriteOnly ) )
63
{
64
mErrorString = tr(
"Could not open '%1' for writing"
).arg( mDestination );
65
return
false
;
66
}
67
68
const
int
size = fileSource.size();
69
const
int
chunkSize = std::clamp( size / 100, 1024, 1024 * 1024 );
70
71
int
bytesRead = 0;
72
std::vector<char> data( chunkSize );
73
while
(
true
)
74
{
75
const
int
len = fileSource.read( data.data(), chunkSize );
76
if
( len == -1 )
77
{
78
mErrorString = tr(
"Fail reading from '%1'"
).arg( mSource );
79
return
false
;
80
}
81
82
// finish reading
83
if
( !len )
84
break
;
85
86
if
( fileDestination.write( data.data(), len ) != len )
87
{
88
mErrorString = tr(
"Fail writing to '%1'"
).arg( mDestination );
89
return
false
;
90
}
91
92
bytesRead += len;
93
setProgress
(
static_cast<
double
>
( bytesRead ) / size );
94
}
95
96
setProgress
( 100 );
97
98
fileSource.close();
99
fileDestination.close();
100
101
return
true
;
102
}
103
104
const
QString &
QgsCopyFileTask::errorString
()
const
105
{
106
return
mErrorString;
107
}
108
109
const
QString &
QgsCopyFileTask::destination
()
const
110
{
111
return
mDestination;
112
}
QgsCopyFileTask::destination
const QString & destination() const
It could be different from the original one.
Definition
qgscopyfiletask.cpp:109
QgsCopyFileTask::run
bool run() override
Performs the task's operation.
Definition
qgscopyfiletask.cpp:29
QgsCopyFileTask::errorString
const QString & errorString() const
Returns errorString if an error occurred, else returns an empty QString.
Definition
qgscopyfiletask.cpp:104
QgsCopyFileTask::QgsCopyFileTask
QgsCopyFileTask(const QString &source, const QString &destination)
Creates a task that copy source file to destination.
Definition
qgscopyfiletask.cpp:24
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition
qgstaskmanager.cpp:239
qgscopyfiletask.h
Generated on
for QGIS API Documentation by
1.17.0