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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
/*
* FileTransferController.cpp - implementation of FileTransferController class
*
* Copyright (c) 2018-2025 Tobias Junghans <tobydox@veyon.io>
*
* This file is part of Veyon - https://veyon.io
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program (see COPYING); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#include <QFileInfo>
#include "FileReadThread.h"
#include "FileTransferController.h"
#include "FileTransferPlugin.h"
FileTransferController::FileTransferController( FileTransferPlugin* plugin ) :
QObject( plugin ),
m_plugin( plugin ),
m_currentFileIndex( -1 ),
m_currentTransferId(),
m_files(),
m_flags( Transfer ),
m_interfaces(),
m_fileReadThread( nullptr ),
m_fileState( FileStateFinished ),
m_processTimer( this )
{
m_processTimer.setInterval( ProcessInterval );
connect( &m_processTimer, &QTimer::timeout, this, &FileTransferController::process );
}
FileTransferController::~FileTransferController()
{
if( m_fileReadThread )
{
delete m_fileReadThread;
}
}
void FileTransferController::setFiles( const QStringList& files )
{
m_files = files;
m_currentFileIndex = 0;
Q_EMIT filesChanged();
}
void FileTransferController::setInterfaces( const ComputerControlInterfaceList& interfaces )
{
m_interfaces = interfaces;
}
void FileTransferController::setFlags( Flags flags )
{
m_flags = flags;
}
void FileTransferController::start()
{
if( isRunning() == false && m_files.isEmpty() == false )
{
m_currentFileIndex = 0;
m_fileState = FileStateOpen;
m_processTimer.start();
Q_EMIT started();
}
}
void FileTransferController::stop()
{
if( isRunning() )
{
m_processTimer.stop();
if( m_fileReadThread )
{
delete m_fileReadThread;
m_fileReadThread = nullptr;
}
m_plugin->sendCancelMessage( m_currentTransferId, m_interfaces );
}
Q_EMIT finished();
}
const QStringList& FileTransferController::files() const
{
return m_files;
}
int FileTransferController::currentFileIndex() const
{
return m_currentFileIndex;
}
bool FileTransferController::isRunning() const
{
return m_processTimer.isActive();
}
void FileTransferController::process()
{
switch( m_fileState )
{
case FileStateOpen:
if( openFile() )
{
m_fileState = FileStateTransferring;
}
else
{
m_fileState = FileStateFinished;
}
break;
case FileStateTransferring:
if( transferFile() )
{
m_fileState = FileStateFinished;
}
break;
case FileStateFinished:
finishFile();
if( ++m_currentFileIndex >= m_files.count() )
{
if( m_flags.testFlag( OpenTransferFolder ) )
{
m_plugin->sendOpenTransferFolderMessage( m_interfaces );
}
m_plugin->sendStopWorkerMessage(m_interfaces);
m_processTimer.stop();
Q_EMIT finished();
}
else
{
m_fileState = FileStateOpen;
}
break;
}
updateProgress();
}
bool FileTransferController::openFile()
{
if( m_currentFileIndex >= m_files.count() )
{
return false;
}
m_fileReadThread = new FileReadThread( m_files[m_currentFileIndex], this );
if( m_fileReadThread->start() == false )
{
delete m_fileReadThread;
m_fileReadThread = nullptr;
Q_EMIT errorOccured( tr( "Could not open file \"%1\" for reading! Please check your permissions!" ).arg( m_currentFileIndex ) );
return false;
}
// start reading initial chunk in background
m_fileReadThread->readNextChunk( ChunkSize );
m_currentTransferId = QUuid::createUuid();
m_plugin->sendStartMessage( m_currentTransferId, QFileInfo( m_files[m_currentFileIndex] ).fileName(),
m_flags.testFlag( OverwriteExistingFiles ), m_interfaces );
return true;
}
bool FileTransferController::transferFile()
{
if( m_fileReadThread == nullptr )
{
// something went wrong so finish this file
return true;
}
// wait for all clients to catch up and current data chunk to be read completely
if( allQueuesEmpty() == false || m_fileReadThread->isChunkReady() == false )
{
return false;
}
m_plugin->sendDataMessage( m_currentTransferId, m_fileReadThread->currentChunk(), m_interfaces );
m_fileReadThread->readNextChunk( ChunkSize );
return m_fileReadThread->atEnd();
}
void FileTransferController::finishFile()
{
if( m_fileReadThread )
{
delete m_fileReadThread;
m_fileReadThread = nullptr;
m_plugin->sendFinishMessage( m_currentTransferId, QFileInfo( m_files[m_currentFileIndex] ).fileName(),
m_flags.testFlag( OpenFilesInApplication ), m_interfaces );
m_currentTransferId = QUuid();
}
}
void FileTransferController::updateProgress()
{
if( m_files.isEmpty() == false && m_fileReadThread )
{
Q_EMIT progressChanged( m_currentFileIndex * 100 / m_files.count() +
m_fileReadThread->progress() / m_files.count() );
}
else if( m_files.count() > 0 && m_currentFileIndex >= m_files.count() )
{
Q_EMIT progressChanged( 100 );
}
}
bool FileTransferController::allQueuesEmpty()
{
for( const auto& controlInterface : std::as_const(m_interfaces) )
{
if( controlInterface->isMessageQueueEmpty() == false )
{
return false;
}
}
return true;
}
|