Skip to content

Commit

Permalink
refactor(filetransfer): Move file transfer progress into ToxFile
Browse files Browse the repository at this point in the history
* Refactor/test ToxFileProgress to ensure that when it's moved it
  behaves well
* Notice problems with speed averaging. We were average speeds without
  keeping track of the times they were over. Adding samples of different
  lengths would result in incorrect speeds. Refactor whole class to correct
* Move ToxFileProgress to be a member of ToxFile
* Remove duplicated members between ToxFile and ToxFileProgress
* Move sample addition into CoreFile
  • Loading branch information
sphaerophoria committed Dec 11, 2021
1 parent 3bc4aa5 commit c7efe32
Show file tree
Hide file tree
Showing 14 changed files with 552 additions and 164 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ set(${PROJECT_NAME}_SOURCES
src/chatlog/documentcache.h
src/chatlog/pixmapcache.cpp
src/chatlog/pixmapcache.h
src/chatlog/toxfileprogress.cpp
src/chatlog/toxfileprogress.h
src/core/toxfileprogress.cpp
src/core/toxfileprogress.h
src/chatlog/textformatter.cpp
src/chatlog/textformatter.h
src/core/coreav.cpp
Expand Down
1 change: 1 addition & 0 deletions cmake/Testing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ auto_test(core core "${${PROJECT_NAME}_RESOURCES}")
auto_test(core contactid "")
auto_test(core toxid "")
auto_test(core toxstring "")
auto_test(core fileprogress "")
auto_test(chatlog textformatter "")
auto_test(net bsu "${${PROJECT_NAME}_RESOURCES}") # needs nodes list
auto_test(chatlog chatlinestorage "")
Expand Down
32 changes: 10 additions & 22 deletions src/chatlog/content/filetransferwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ FileTransferWidget::FileTransferWidget(QWidget* parent, CoreFile& _coreFile, Tox
ui->previewButton->hide();
ui->filenameLabel->setText(file.fileName);
ui->progressBar->setValue(0);
ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize));
ui->fileSizeLabel->setText(getHumanReadableSize(file.progress.getFileSize()));
ui->etaLabel->setText("");

backgroundColorAnimation = new QVariantAnimation(this);
Expand Down Expand Up @@ -321,19 +321,12 @@ void FileTransferWidget::updateFileProgress(ToxFile const& file)
{
switch (file.status) {
case ToxFile::INITIALIZING:
break;
case ToxFile::PAUSED:
fileProgress.resetSpeed();
break;
case ToxFile::TRANSMITTING: {
if (!fileProgress.needsUpdate()) {
break;
}

fileProgress.addSample(file);
auto speed = fileProgress.getSpeed();
auto progress = fileProgress.getProgress();
auto remainingTime = fileProgress.getTimeLeftSeconds();
auto speed = file.progress.getSpeed();
auto progress = file.progress.getProgress();
auto remainingTime = file.progress.getTimeLeftSeconds();

ui->progressBar->setValue(static_cast<int>(progress * 100.0));

Expand Down Expand Up @@ -525,26 +518,21 @@ void FileTransferWidget::updateWidget(ToxFile const& file)

fileInfo = file;

// If we repainted on every packet our gui would be *very* slow
bool bTransmitNeedsUpdate = fileProgress.needsUpdate();
bool shouldUpdateFileProgress = file.status != ToxFile::TRANSMITTING || lastTransmissionUpdate ==
QTime() || lastTransmissionUpdate.msecsTo(file.progress.lastSampleTime()) > 1000;

updatePreview(file);
updateFileProgress(file);
if (shouldUpdateFileProgress)
updateFileProgress(file);
updateWidgetText(file);
updateWidgetColor(file);
setupButtons(file);
updateSignals(file);

lastStatus = file.status;

// trigger repaint
switch (file.status) {
case ToxFile::TRANSMITTING:
if (!bTransmitNeedsUpdate) {
break;
}
// fallthrough
default:
if (shouldUpdateFileProgress) {
lastTransmissionUpdate = QTime::currentTime();
update();
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/chatlog/content/filetransferwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <QWidget>

#include "src/chatlog/chatlinecontent.h"
#include "src/chatlog/toxfileprogress.h"
#include "src/core/toxfile.h"

class CoreFile;
Expand Down Expand Up @@ -81,7 +80,6 @@ private slots:
private:
CoreFile& coreFile;
Ui::FileTransferWidget* ui;
ToxFileProgress fileProgress;
ToxFile fileInfo;
QVariantAnimation* backgroundColorAnimation = nullptr;
QVariantAnimation* buttonColorAnimation = nullptr;
Expand All @@ -90,6 +88,7 @@ private slots:
QColor buttonBackgroundColor;

bool active;
QTime lastTransmissionUpdate;
ToxFile::FileStatus lastStatus = ToxFile::INITIALIZING;

};
93 changes: 0 additions & 93 deletions src/chatlog/toxfileprogress.cpp

This file was deleted.

19 changes: 8 additions & 11 deletions src/core/corefile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ void CoreFile::sendAvatarFile(uint32_t friendId, const QByteArray& data)
return;
}

ToxFile file{fileNum, friendId, "", "", ToxFile::SENDING};
file.filesize = filesize;
ToxFile file{fileNum, friendId, "", "", filesize, ToxFile::SENDING};
file.fileKind = TOX_FILE_KIND_AVATAR;
file.avatarData = data;
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
Expand All @@ -158,8 +157,7 @@ void CoreFile::sendFile(uint32_t friendId, QString filename, QString filePath,
}
qDebug() << QString("sendFile: Created file sender %1 with friend %2").arg(fileNum).arg(friendId);

ToxFile file{fileNum, friendId, fileName.getQString(), filePath, ToxFile::SENDING};
file.filesize = filesize;
ToxFile file{fileNum, friendId, fileName.getQString(), filePath, static_cast<uint64_t>(filesize), ToxFile::SENDING};
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
tox_file_get_file_id(tox, friendId, fileNum, reinterpret_cast<uint8_t*>(file.resumeFileId.data()),
nullptr);
Expand Down Expand Up @@ -191,6 +189,7 @@ void CoreFile::pauseResumeFile(uint32_t friendId, uint32_t fileId)

if (file->pauseStatus.paused()) {
file->status = ToxFile::PAUSED;
file->progress.resetSpeed();
emit fileTransferPaused(*file);
} else {
file->status = ToxFile::TRANSMITTING;
Expand Down Expand Up @@ -360,8 +359,7 @@ void CoreFile::onFileReceiveCallback(Tox* tox, uint32_t friendId, uint32_t fileI
qDebug() << QString("Received file request %1:%2 kind %3").arg(friendId).arg(fileId).arg(kind);
}

ToxFile file{fileId, friendId, filename.getBytes(), "", ToxFile::RECEIVING};
file.filesize = filesize;
ToxFile file{fileId, friendId, filename.getBytes(), "", filesize, ToxFile::RECEIVING};
file.fileKind = kind;
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
tox_file_get_file_id(tox, friendId, fileId, reinterpret_cast<uint8_t*>(file.resumeFileId.data()),
Expand Down Expand Up @@ -390,8 +388,7 @@ void CoreFile::handleAvatarOffer(uint32_t friendId, uint32_t fileId, bool accept
.arg(fileId);
tox_file_control(tox, friendId, fileId, TOX_FILE_CONTROL_RESUME, nullptr);

ToxFile file{fileId, friendId, "<avatar>", "", ToxFile::RECEIVING};
file.filesize = 0;
ToxFile file{fileId, friendId, "<avatar>", "", 0, ToxFile::RECEIVING};
file.fileKind = TOX_FILE_KIND_AVATAR;
file.resumeFileId.resize(TOX_FILE_ID_LENGTH);
tox_file_get_file_id(tox, friendId, fileId, reinterpret_cast<uint8_t*>(file.resumeFileId.data()),
Expand Down Expand Up @@ -475,7 +472,7 @@ void CoreFile::onFileDataCallback(Tox* tox, uint32_t friendId, uint32_t fileId,
coreFile->removeFile(friendId, fileId);
return;
}
file->bytesSent += length;
file->progress.addSample(file->progress.getBytesSent() + length);
file->hashGenerator->addData(reinterpret_cast<const char*>(data.get()), length);
}

Expand All @@ -500,7 +497,7 @@ void CoreFile::onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fil
return;
}

if (file->bytesSent != position) {
if (file->progress.getBytesSent() != position) {
qWarning("onFileRecvChunkCallback: Received a chunk out-of-order, aborting transfer");
if (file->fileKind != TOX_FILE_KIND_AVATAR) {
file->status = ToxFile::CANCELED;
Expand Down Expand Up @@ -533,7 +530,7 @@ void CoreFile::onFileRecvChunkCallback(Tox* tox, uint32_t friendId, uint32_t fil
} else {
file->file->write(reinterpret_cast<const char*>(data), length);
}
file->bytesSent += length;
file->progress.addSample(file->progress.getBytesSent() + length);
file->hashGenerator->addData(reinterpret_cast<const char*>(data), length);

if (file->fileKind != TOX_FILE_KIND_AVATAR) {
Expand Down
14 changes: 11 additions & 3 deletions src/core/toxfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,29 @@
* @brief Data file (default) or avatar
*/

ToxFile::ToxFile()
: fileKind(0)
, fileNum(0)
, friendId(0)
, status(INITIALIZING)
, direction(SENDING)
, progress(0)
{}

/**
* @brief ToxFile constructor
*/
ToxFile::ToxFile(uint32_t fileNum, uint32_t friendId, QString filename, QString filePath,
FileDirection Direction)
uint64_t filesize, FileDirection Direction)
: fileKind{TOX_FILE_KIND_DATA}
, fileNum(fileNum)
, friendId(friendId)
, fileName{filename}
, filePath{filePath}
, file{new QFile(filePath)}
, bytesSent{0}
, filesize{0}
, status{INITIALIZING}
, direction{Direction}
, progress(filesize)
{}

bool ToxFile::operator==(const ToxFile& other) const
Expand Down
8 changes: 4 additions & 4 deletions src/core/toxfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include "src/core/toxfilepause.h"
#include "src/core/toxfileprogress.h"

#include <QString>
#include <memory>
Expand Down Expand Up @@ -50,9 +51,9 @@ struct ToxFile
RECEIVING = 1,
};

ToxFile() = default;
ToxFile();
ToxFile(uint32_t FileNum, uint32_t FriendId, QString FileName, QString filePath,
FileDirection Direction);
uint64_t filesize, FileDirection Direction);

bool operator==(const ToxFile& other) const;
bool operator!=(const ToxFile& other) const;
Expand All @@ -66,12 +67,11 @@ struct ToxFile
QString fileName;
QString filePath;
std::shared_ptr<QFile> file;
quint64 bytesSent;
quint64 filesize;
FileStatus status;
FileDirection direction;
QByteArray avatarData;
QByteArray resumeFileId;
std::shared_ptr<QCryptographicHash> hashGenerator = std::make_shared<QCryptographicHash>(QCryptographicHash::Sha256);
ToxFilePause pauseStatus;
ToxFileProgress progress;
};
Loading

0 comments on commit c7efe32

Please sign in to comment.