QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include <QDir>
19#include <QFile>
20#include <QFileInfo>
21
22#include "moc_qgscopyfiletask.cpp"
23
24QgsCopyFileTask::QgsCopyFileTask( const QString &source, const QString &destination )
25 : mSource( source ),
26 mDestination( destination )
27{
28}
29
31{
32 QFile fileSource( mSource );
33 if ( !fileSource.exists() )
34 {
35 mErrorString = tr( "Source file '%1' does not exist" ).arg( mSource );
36 return false;
37 }
38
39 if ( QFileInfo( mDestination ).isDir() )
40 {
41 mDestination = QDir( mDestination ).absoluteFilePath( QFileInfo( fileSource ).fileName() );
42 }
43
44 QFile fileDestination( mDestination );
45 if ( fileDestination.exists() )
46 {
47 mErrorString = tr( "Destination file '%1' already exist" ).arg( mDestination );
48 return false;
49 }
50
51 const QDir destinationDir = QFileInfo( mDestination ).absoluteDir();
52 if ( !destinationDir.exists() )
53 {
54 mErrorString = tr( "Destination directory '%1' does not exist" ).arg( destinationDir.absolutePath() );
55 return false;
56 }
57
58 if ( !fileSource.open( QIODevice::ReadOnly ) )
59 {
60 mErrorString = tr( "Could not open '%1' for reading" ).arg( mSource );
61 return false;
62 }
63 if ( !fileDestination.open( QIODevice::WriteOnly ) )
64 {
65 mErrorString = tr( "Could not open '%1' for writing" ).arg( mDestination );
66 return false;
67 }
68
69 const int size = fileSource.size();
70 const int chunkSize = std::clamp( size / 100, 1024, 1024 * 1024 );
71
72 int bytesRead = 0;
73 std::vector<char> data( chunkSize );
74 while ( true )
75 {
76 const int len = fileSource.read( data.data(), chunkSize );
77 if ( len == -1 )
78 {
79 mErrorString = tr( "Fail reading from '%1'" ).arg( mSource );
80 return false;
81 }
82
83 // finish reading
84 if ( !len )
85 break;
86
87 if ( fileDestination.write( data.data(), len ) != len )
88 {
89 mErrorString = tr( "Fail writing to '%1'" ).arg( mDestination );
90 return false;
91 }
92
93 bytesRead += len;
94 setProgress( static_cast<double>( bytesRead ) / size );
95 }
96
97 setProgress( 100 );
98
99 fileSource.close();
100 fileDestination.close();
101
102 return true;
103}
104
105const QString &QgsCopyFileTask::errorString() const
106{
107 return mErrorString;
108}
109
110const QString &QgsCopyFileTask::destination() const
111{
112 return mDestination;
113}
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 an empty 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.