QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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#include "moc_qgscopyfiletask.cpp"
18
19#include <QFile>
20#include <QFileInfo>
21#include <QDir>
22
23QgsCopyFileTask::QgsCopyFileTask( const QString &source, const QString &destination )
24 : mSource( source ),
25 mDestination( destination )
26{
27}
28
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 fileSource.open( QIODevice::ReadOnly );
58 fileDestination.open( QIODevice::WriteOnly );
59
60 const int size = fileSource.size();
61 const int chunkSize = std::clamp( size / 100, 1024, 1024 * 1024 );
62
63 int bytesRead = 0;
64 std::vector<char> data( chunkSize );
65 while ( true )
66 {
67 const int len = fileSource.read( data.data(), chunkSize );
68 if ( len == -1 )
69 {
70 mErrorString = tr( "Fail reading from '%1'" ).arg( mSource );
71 return false;
72 }
73
74 // finish reading
75 if ( !len )
76 break;
77
78 if ( fileDestination.write( data.data(), len ) != len )
79 {
80 mErrorString = tr( "Fail writing to '%1'" ).arg( mDestination );
81 return false;
82 }
83
84 bytesRead += len;
85 setProgress( static_cast<double>( bytesRead ) / size );
86 }
87
88 setProgress( 100 );
89
90 fileSource.close();
91 fileDestination.close();
92
93 return true;
94}
95
96const QString &QgsCopyFileTask::errorString() const
97{
98 return mErrorString;
99}
100
101const QString &QgsCopyFileTask::destination() const
102{
103 return mDestination;
104}
const QString & destination() const
It could be different from the original one.
bool run() override
Performs the task's operation.
const QString & errorString() const
Returns errorString if an error occurred, else returns null QString.
QgsCopyFileTask(const QString &source, const QString &destination)
Creates a task that copy source file to destination.
void setProgress(double progress)
Sets the task's current progress.