1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

Preparation for new file transfer

This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-07-01 13:33:59 +02:00
parent fa9529ecae
commit ae1ce8ec88
3 changed files with 26 additions and 23 deletions

View File

@ -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*>(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*>(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");

15
core.h
View File

@ -27,6 +27,7 @@
#include <QObject>
#include <QTimer>
#include <QString>
#include <QFile>
#include <QList>
#include <QByteArray>
#include <QFuture>
@ -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);

View File

@ -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);