diff --git a/core.cpp b/core.cpp index dfa813b8d..ae19969d8 100644 --- a/core.cpp +++ b/core.cpp @@ -239,8 +239,10 @@ void Core::onFileSendRequestCallback(Tox*, int32_t friendnumber, uint8_t filenum { qDebug() << QString("Core: Received file request %1 with friend %2").arg(filenumber).arg(friendnumber); - fileRecvQueue.append(ToxFile(filenumber, friendnumber, QByteArray(), filesize, - CString::toString(filename,filename_length).toUtf8(), ToxFile::RECEIVING)); + ToxFile file{filenumber, friendnumber, + CString::toString(filename,filename_length).toUtf8(), "", ToxFile::RECEIVING}; + file.filesize = filesize; + fileRecvQueue.append(file); emit static_cast(core)->fileReceiveRequested(fileRecvQueue.last()); } void Core::onFileControlCallback(Tox*, int32_t friendnumber, uint8_t receive_send, uint8_t filenumber, @@ -330,7 +332,7 @@ void Core::onFileDataCallback(Tox*, int32_t friendnumber, uint8_t filenumber, co return; } - file->fileData.append((char*)data,length); + file->file->write((char*)data,length); file->bytesSent += length; //qDebug() << QString("Core::onFileDataCallback: received %1/%2 bytes").arg(file->fileData.size()).arg(file->filesize); emit static_cast(core)->fileTransferInfo(file->friendId, file->fileNum, @@ -392,10 +394,10 @@ void Core::sendGroupMessage(int groupId, const QString& message) tox_group_message_send(tox, groupId, cMessage.data(), cMessage.size()); } -void Core::sendFile(int32_t friendId, QString Filename, QByteArray data) +void Core::sendFile(int32_t friendId, QString Filename, QString FilePath, long long filesize) { QByteArray fileName = Filename.toUtf8(); - int fileNum = tox_new_file_sender(tox, friendId, data.size(), (uint8_t*)fileName.data(), fileName.size()); + int fileNum = tox_new_file_sender(tox, friendId, filesize, (uint8_t*)fileName.data(), fileName.size()); if (fileNum == -1) { qWarning() << "Core::sendFile: Can't create the Tox file sender"; @@ -403,7 +405,9 @@ void Core::sendFile(int32_t friendId, QString Filename, QByteArray data) } qDebug() << QString("Core::sendFile: Created file sender %1 with friend %2").arg(fileNum).arg(friendId); - fileSendQueue.append(ToxFile(fileNum, friendId, data, data.size(), fileName, ToxFile::SENDING)); + ToxFile file{fileNum, friendId, fileName, FilePath, ToxFile::SENDING}; + file.filesize = filesize; + fileSendQueue.append(file); emit fileSendStarted(fileSendQueue.last()); } @@ -910,7 +914,7 @@ void Core::removeFileFromQueue(bool sendQueue, int friendId, int fileId) void Core::sendAllFileData(Core *core, ToxFile* file) { - while (file->bytesSent < file->fileData.size()) + while (file->bytesSent < file->filesize) { if (file->status == ToxFile::PAUSED) { @@ -924,7 +928,7 @@ void Core::sendAllFileData(Core *core, ToxFile* file) } emit core->fileTransferInfo(file->friendId, file->fileNum, file->filesize, file->bytesSent, ToxFile::SENDING); qApp->processEvents(); - int chunkSize = tox_file_data_size(core->tox, file->friendId); + long long chunkSize = tox_file_data_size(core->tox, file->friendId); if (chunkSize == -1) { qWarning("Core::fileHeartbeat: Error getting preffered chunk size, aborting file send"); @@ -934,8 +938,8 @@ void Core::sendAllFileData(Core *core, ToxFile* file) removeFileFromQueue(true, file->friendId, file->fileNum); return; } - chunkSize = std::min(chunkSize, file->fileData.size()); - QByteArray toSend = file->fileData.mid(file->bytesSent, chunkSize); + chunkSize = std::min(chunkSize, file->filesize); + QByteArray toSend = file->file->read(chunkSize); if (tox_file_send_data(core->tox, file->friendId, file->fileNum, (uint8_t*)toSend.data(), toSend.size()) == -1) { //qWarning("Core::fileHeartbeat: Error sending data chunk"); diff --git a/core.h b/core.h index c4e7e2281..f8cf35335 100644 --- a/core.h +++ b/core.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -73,14 +74,18 @@ struct ToxFile }; ToxFile()=default; - ToxFile(int FileNum, int FriendId, QByteArray FileData, long long Filesize, QByteArray FileName, FileDirection Direction) - : fileNum(FileNum), friendId(FriendId), fileData{FileData}, fileName{FileName}, - bytesSent{0}, filesize(Filesize), status{STOPPED}, direction{Direction} {} + ToxFile(int FileNum, int FriendId, QByteArray FileName, QString FilePath, FileDirection Direction) + : fileNum(FileNum), friendId(FriendId), fileName{FileName}, filePath{FilePath}, file{new QFile(filePath)}, + bytesSent{0}, filesize{0}, status{STOPPED}, direction{Direction} {} + ~ToxFile(){file->close(); delete file;} + void setFilePath(QString path) {filePath=path; file->setFileName(path);} + bool open(bool write) {return write?file->open(QIODevice::WriteOnly):file->open(QIODevice::ReadOnly);} int fileNum; int friendId; - QByteArray fileData; QByteArray fileName; + QString filePath; + QFile* file; long long bytesSent; long long filesize; FileStatus status; @@ -147,7 +152,7 @@ public slots: void sendAction(int friendId, const QString& action); void sendTyping(int friendId, bool typing); - void sendFile(int32_t friendId, QString Filename, QByteArray data); + void sendFile(int32_t friendId, QString Filename, QString FilePath, long long filesize); void cancelFileSend(int friendId, int fileNum); void cancelFileRecv(int friendId, int fileNum); void rejectFileRecvRequest(int friendId, int fileNum); diff --git a/widget/filetransfertwidget.cpp b/widget/filetransfertwidget.cpp index 869a20755..6f4d04887 100644 --- a/widget/filetransfertwidget.cpp +++ b/widget/filetransfertwidget.cpp @@ -93,7 +93,7 @@ FileTransfertWidget::FileTransfertWidget(ToxFile File) connect(bottomright, SIGNAL(clicked()), this, SLOT(pauseResumeSend())); QPixmap preview; - if (preview.loadFromData(File.fileData)) + if (preview.loadFromData(File.file->readAll())) { preview = preview.scaledToHeight(40); pic->setPixmap(preview); @@ -234,14 +234,8 @@ void FileTransfertWidget::onFileTransferFinished(ToxFile File) if (File.direction == ToxFile::RECEIVING) { - QFile saveFile(savePath); - if (!saveFile.open(QIODevice::WriteOnly)) - return; - saveFile.write(File.fileData); - saveFile.close(); - QPixmap preview; - if (preview.loadFromData(File.fileData)) + if (preview.loadFromData(File.file->readAll())) { preview = preview.scaledToHeight(40); pic->setPixmap(preview);