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

Various improvement

Aka this commit is a mess, if you're bissecting this, good luck :)
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-07-01 19:54:26 +02:00
parent 941a237d4d
commit dc2d7f1085
3 changed files with 49 additions and 11 deletions

View File

@ -407,6 +407,10 @@ void Core::sendFile(int32_t friendId, QString Filename, QString FilePath, long l
ToxFile file{fileNum, friendId, fileName, FilePath, ToxFile::SENDING};
file.filesize = filesize;
if (!file.open(false))
{
qWarning() << QString("Core::sendFile: Can't open file, error: %1").arg(file.file->errorString());
}
fileSendQueue.append(file);
emit fileSendStarted(fileSendQueue.last());
@ -543,7 +547,7 @@ void Core::rejectFileRecvRequest(int friendId, int fileNum)
removeFileFromQueue(false, friendId, fileNum);
}
void Core::acceptFileRecvRequest(int friendId, int fileNum)
void Core::acceptFileRecvRequest(int friendId, int fileNum, QString path)
{
ToxFile* file{nullptr};
for (ToxFile& f : fileRecvQueue)
@ -559,6 +563,12 @@ void Core::acceptFileRecvRequest(int friendId, int fileNum)
qWarning("Core::acceptFileRecvRequest: No such file in queue");
return;
}
file->setFilePath(path);
if (!file->open(true))
{
qWarning() << "Core::acceptFileRecvRequest: Unable to open file";
return;
}
file->status = ToxFile::TRANSMITTING;
emit fileTransferAccepted(*file);
tox_file_send_control(tox, file->friendId, 1, file->fileNum, TOX_FILECONTROL_ACCEPT, nullptr, 0);
@ -889,6 +899,8 @@ void Core::removeFileFromQueue(bool sendQueue, int friendId, int fileId)
if (fileSendQueue[i].friendId == friendId && fileSendQueue[i].fileNum == fileId)
{
found = true;
fileSendQueue[i].file->close();
delete fileSendQueue[i].file;
fileSendQueue.removeAt(i);
continue;
}
@ -902,6 +914,8 @@ void Core::removeFileFromQueue(bool sendQueue, int friendId, int fileId)
if (fileRecvQueue[i].friendId == friendId && fileRecvQueue[i].fileNum == fileId)
{
found = true;
fileRecvQueue[i].file->close();
delete fileRecvQueue[i].file;
fileRecvQueue.removeAt(i);
continue;
}
@ -918,7 +932,7 @@ void Core::sendAllFileData(Core *core, ToxFile* file)
{
if (file->status == ToxFile::PAUSED)
{
QThread::sleep(0);
QThread::sleep(5);
continue;
}
else if (file->status == ToxFile::STOPPED)
@ -939,14 +953,33 @@ void Core::sendAllFileData(Core *core, ToxFile* file)
return;
}
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)
uint8_t* data = new uint8_t[chunkSize];
file->file->seek(file->bytesSent);
int readSize = file->file->read((char*)data, chunkSize);
if (readSize == -1)
{
//qWarning("Core::fileHeartbeat: Error sending data chunk");
QThread::sleep(0);
qWarning() << QString("Core::sendAllFileData: Error reading from file: %1").arg(file->file->errorString());
delete[] data;
QThread::msleep(5);
continue;
}
file->bytesSent += chunkSize;
else if (readSize == 0)
{
qWarning() << QString("Core::sendAllFileData: Nothing to read from file: %1").arg(file->file->errorString());
delete[] data;
QThread::msleep(5);
continue;
}
if (tox_file_send_data(core->tox, file->friendId, file->fileNum, data, readSize) == -1)
{
//qWarning("Core::fileHeartbeat: Error sending data chunk");
core->process();
delete[] data;
QThread::msleep(5);
continue;
}
delete[] data;
file->bytesSent += readSize;
//qDebug() << QString("Core::fileHeartbeat: sent %1/%2 bytes").arg(file->bytesSent).arg(file->fileData.size());
}
qDebug("Core::fileHeartbeat: Transfer finished");

6
core.h
View File

@ -77,9 +77,9 @@ struct ToxFile
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;}
~ToxFile(){}
void setFilePath(QString path) {filePath=path; file->setFileName(path);}
bool open(bool write) {return write?file->open(QIODevice::WriteOnly):file->open(QIODevice::ReadOnly);}
bool open(bool write) {return write?file->open(QIODevice::ReadWrite):file->open(QIODevice::ReadOnly);}
int fileNum;
int friendId;
@ -156,7 +156,7 @@ public slots:
void cancelFileSend(int friendId, int fileNum);
void cancelFileRecv(int friendId, int fileNum);
void rejectFileRecvRequest(int friendId, int fileNum);
void acceptFileRecvRequest(int friendId, int fileNum);
void acceptFileRecvRequest(int friendId, int fileNum, QString path);
void pauseResumeFileSend(int friendId, int fileNum);
void pauseResumeFileRecv(int friendId, int fileNum);

View File

@ -93,11 +93,13 @@ FileTransfertWidget::FileTransfertWidget(ToxFile File)
connect(bottomright, SIGNAL(clicked()), this, SLOT(pauseResumeSend()));
QPixmap preview;
File.file->seek(0);
if (preview.loadFromData(File.file->readAll()))
{
preview = preview.scaledToHeight(40);
pic->setPixmap(preview);
}
File.file->seek(0);
}
else
{
@ -167,7 +169,10 @@ void FileTransfertWidget::onFileTransferInfo(int FriendId, int FileNum, int File
return;
int diff = BytesSent - lastBytesSent;
if (diff < 0)
{
qWarning() << "FileTransfertWidget::onFileTransferInfo: Negative transfer speed !";
diff = 0;
}
int rawspeed = diff / timediff;
speed->setText(getHumanReadableSize(rawspeed)+"/s");
size->setText(getHumanReadableSize(Filesize));
@ -265,7 +270,7 @@ void FileTransfertWidget::acceptRecvRequest()
bottomright->setStyleSheet(pauseFileButtonStylesheet);
bottomright->disconnect();
connect(bottomright, SIGNAL(clicked()), this, SLOT(pauseResumeRecv()));
Widget::getInstance()->getCore()->acceptFileRecvRequest(friendId, fileNum);
Widget::getInstance()->getCore()->acceptFileRecvRequest(friendId, fileNum, path);
}
void FileTransfertWidget::pauseResumeRecv()