QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgssimplecopyexternalstorage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssimplecopyexternalstorage.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 
17 
18 #include "qgscopyfiletask.h"
19 #include "qgsapplication.h"
20 
21 #include <QFileInfo>
22 
24 
25 QgsSimpleCopyExternalStorageStoredContent::QgsSimpleCopyExternalStorageStoredContent( const QString &filePath, const QString &url, const QString &authcfg )
26 {
27  Q_UNUSED( authcfg );
28 
29  mCopyTask = new QgsCopyFileTask( filePath, url );
30 
31  connect( mCopyTask, &QgsTask::taskCompleted, this, [ = ]
32  {
33  mUrl = mCopyTask->destination();
35  emit stored();
36  } );
37 
38  connect( mCopyTask, &QgsTask::taskTerminated, this, [ = ]
39  {
40  reportError( mCopyTask->errorString() );
41  } );
42 
43  connect( mCopyTask, &QgsTask::progressChanged, this, [ = ]( double progress )
44  {
45  emit progressChanged( progress );
46  } );
47 }
48 
49 void QgsSimpleCopyExternalStorageStoredContent::store()
50 {
52  QgsApplication::taskManager()->addTask( mCopyTask );
53 }
54 
55 void QgsSimpleCopyExternalStorageStoredContent::cancel()
56 {
57  if ( !mCopyTask )
58  return;
59 
60  disconnect( mCopyTask, &QgsTask::taskTerminated, this, nullptr );
61  connect( mCopyTask, &QgsTask::taskTerminated, this, [ = ]
62  {
64  emit canceled();
65  } );
66 
67  mCopyTask->cancel();
68 }
69 
70 QString QgsSimpleCopyExternalStorageStoredContent::url() const
71 {
72  return mUrl;
73 }
74 
75 QgsSimpleCopyExternalStorageFetchedContent::QgsSimpleCopyExternalStorageFetchedContent( const QString &filePath )
76  : mFilePath( filePath )
77 {
78 }
79 
80 void QgsSimpleCopyExternalStorageFetchedContent::fetch()
81 {
82  // no fetching process, we read directly from its location
83  if ( !QFileInfo::exists( mFilePath ) )
84  {
85  reportError( tr( "File '%1' does not exist" ).arg( mFilePath ) );
86  }
87  else
88  {
90  mResultFilePath = mFilePath;
91  emit fetched();
92  }
93 }
94 
95 QString QgsSimpleCopyExternalStorageFetchedContent::filePath() const
96 {
97  return mResultFilePath;
98 }
99 
100 QString QgsSimpleCopyExternalStorage::type() const
101 {
102  return QStringLiteral( "SimpleCopy" );
103 };
104 
105 QString QgsSimpleCopyExternalStorage::displayName() const
106 {
107  return QObject::tr( "Simple copy" );
108 };
109 
110 QgsExternalStorageStoredContent *QgsSimpleCopyExternalStorage::doStore( const QString &filePath, const QString &url, const QString &authcfg ) const
111 {
112  return new QgsSimpleCopyExternalStorageStoredContent( filePath, url, authcfg );
113 };
114 
115 QgsExternalStorageFetchedContent *QgsSimpleCopyExternalStorage::doFetch( const QString &url, const QString &authConfig ) const
116 {
117  Q_UNUSED( authConfig );
118 
119  return new QgsSimpleCopyExternalStorageFetchedContent( url );
120 }
121 
@ Canceled
Content fetching/storing has been canceled.
@ Running
Content fetching/storing is in progress.
@ Finished
Content fetching/storing is finished and successful.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
Task to copy a file on disk.
Class for QgsExternalStorage fetched content.
Class for QgsExternalStorage stored content.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
void taskCompleted()
Will be emitted by task to indicate its successful completion.
void progressChanged(double progress)
Will be emitted by task when its progress changes.
void taskTerminated()
Will be emitted by task if it has terminated for any reason other then completion (e....