From c18d63553dc14730d7e08145b11e970f626e109c Mon Sep 17 00:00:00 2001 From: "Tux3 / Mlkj / !Lev.uXFMLA" Date: Thu, 26 Jun 2014 01:35:30 +0200 Subject: [PATCH] Improve file sending Improve the Widget, properly handle finished/cancelled tranfers --- chatform.cpp | 7 +++++- core.cpp | 14 +++++++----- filetransfertwidget.cpp | 50 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/chatform.cpp b/chatform.cpp index dae9d61ce..3b4e5f596 100644 --- a/chatform.cpp +++ b/chatform.cpp @@ -180,7 +180,10 @@ void ChatForm::onSliderRangeChanged() void ChatForm::startFileSend(ToxFile *file) { - QLabel *author = new QLabel(Widget::getInstance()->getUsername()), *date = new QLabel(); + if (file->friendId != f->friendId) + return; + QLabel *author = new QLabel(Widget::getInstance()->getUsername()); + QLabel *date = new QLabel(QTime::currentTime().toString("mm:ss")); QScrollBar* scroll = chatArea->verticalScrollBar(); lockSliderToBottom = scroll && scroll->value() == scroll->maximum(); author->setAlignment(Qt::AlignTop | Qt::AlignRight); @@ -207,4 +210,6 @@ void ChatForm::startFileSend(ToxFile *file) curRow++; connect(Widget::getInstance()->getCore(), &Core::fileTransferInfo, fileTrans, &FileTransfertWidget::onFileTransferInfo); + connect(Widget::getInstance()->getCore(), &Core::fileTransferCancelled, fileTrans, &FileTransfertWidget::onFileTransferCancelled); + connect(Widget::getInstance()->getCore(), &Core::fileTransferFinished, fileTrans, &FileTransfertWidget::onFileTransferFinished); } diff --git a/core.cpp b/core.cpp index 0ce49a740..ff2c16638 100644 --- a/core.cpp +++ b/core.cpp @@ -161,7 +161,9 @@ void Core::onFileControlCallback(Tox* tox, int32_t friendnumber, uint8_t receive if (chunkSize == -1) { qWarning("Core::onFileControlCallback: Error getting preffered chunk size, aborting file send"); - // TODO: Warn the Friend* that we're aborting (emit) + file->status = ToxFile::STOPPED; + emit static_cast(core)->fileTransferFinished(file); + tox_file_send_control(tox, file->friendId, 0, file->fileNum, TOX_FILECONTROL_KILL, nullptr, 0); return; } chunkSize = std::min(chunkSize, file->fileData.size()); @@ -169,7 +171,9 @@ void Core::onFileControlCallback(Tox* tox, int32_t friendnumber, uint8_t receive if (tox_file_send_data(tox, friendnumber, filenumber, (uint8_t*)toSend.data(), toSend.size()) == -1) { qWarning("Core::onFileControlCallback: Error sending first data chunk, aborting"); - // TODO: Warn the Friend* that we're aborting (emit) + file->status = ToxFile::STOPPED; + emit static_cast(core)->fileTransferFinished(file); + tox_file_send_control(tox, file->friendId, 0, file->fileNum, TOX_FILECONTROL_KILL, nullptr, 0); return; } else @@ -264,7 +268,6 @@ void Core::sendFile(int32_t friendId, QString Filename, QByteArray data) if (fileNum == -1) { qWarning() << "Core::sendFile: Can't create the Tox file sender"; - // TODO: Notify Widget (with the friendId), Widget will notify the chatForm return; } @@ -369,7 +372,8 @@ void Core::fileHeartbeat() { qWarning("Core::fileHeartbeat: Error getting preffered chunk size, aborting file send"); file.status = ToxFile::STOPPED; - // TODO: Warn the Friend* and the peer that we're aborting (emit and tox_control_...) + emit fileTransferFinished(&file); + tox_file_send_control(tox, file.friendId, 0, file.fileNum, TOX_FILECONTROL_KILL, nullptr, 0); return; } chunkSize = std::min(chunkSize, file.fileData.size()); @@ -387,7 +391,7 @@ void Core::fileHeartbeat() qDebug("Core::fileHeartbeat: Transfer finished"); tox_file_send_control(tox, file.friendId, 0, file.fileNum, TOX_FILECONTROL_FINISHED, nullptr, 0); file.status = ToxFile::STOPPED; // TODO: Remove it from the list and return; - // TODO: Notify the Friend* that we're done sending (emit) + emit fileTransferFinished(&file); } else qDebug() << QString("Core::fileHeartbeat: sent %1/%2 bytes").arg(file.bytesSent).arg(file.fileData.size()); diff --git a/filetransfertwidget.cpp b/filetransfertwidget.cpp index 0886806dc..85ff62d9d 100644 --- a/filetransfertwidget.cpp +++ b/filetransfertwidget.cpp @@ -11,12 +11,14 @@ FileTransfertWidget::FileTransfertWidget(ToxFile *File) QFont prettysmall; prettysmall.setPixelSize(10); QPalette greybg; - greybg.setColor(QPalette::Window, Qt::gray); + greybg.setColor(QPalette::Window, QColor(209,209,209)); + greybg.setColor(QPalette::Base, QColor(150,150,150)); setPalette(greybg); setAutoFillBackground(true); setFixedSize(250,50); setLayout(mainLayout); + mainLayout->setMargin(0); filename->setText(file->fileName); filename->setFont(prettysmall); @@ -27,6 +29,8 @@ FileTransfertWidget::FileTransfertWidget(ToxFile *File) eta->setText("00:00"); eta->setFont(prettysmall); progress->setValue(0); + progress->setMinimumHeight(11); + progress->setFont(prettysmall); topright->setIcon(QIcon("img/button icons/no_2x.png")); if (file->direction == ToxFile::SENDING) @@ -34,8 +38,18 @@ FileTransfertWidget::FileTransfertWidget(ToxFile *File) else bottomright->setIcon(QIcon("img/button icons/yes_2x.png")); - topright->setIconSize(QSize(25,25)); - bottomright->setIconSize(QSize(25,25)); + QPalette toxgreen; + toxgreen.setColor(QPalette::Button, QColor(107,194,96)); // Tox Green + topright->setIconSize(QSize(10,10)); + topright->setFixedSize(24,24); + topright->setFlat(true); + topright->setAutoFillBackground(true); + topright->setPalette(toxgreen); + bottomright->setIconSize(QSize(10,10)); + bottomright->setFixedSize(24,24); + bottomright->setFlat(true); + bottomright->setAutoFillBackground(true); + bottomright->setPalette(toxgreen); mainLayout->addWidget(pic); mainLayout->addLayout(infoLayout); @@ -44,12 +58,18 @@ FileTransfertWidget::FileTransfertWidget(ToxFile *File) infoLayout->addWidget(filename); infoLayout->addLayout(textLayout); infoLayout->addWidget(progress); + infoLayout->setMargin(5); textLayout->addWidget(size); textLayout->addWidget(speed); textLayout->addWidget(eta); textLayout->setMargin(0); textLayout->setSpacing(5); + + buttonLayout->addWidget(topright); + buttonLayout->addWidget(bottomright); + buttonLayout->setMargin(0); + buttonLayout->setSpacing(2); } QString FileTransfertWidget::getHumanReadableSize(int size) @@ -86,10 +106,30 @@ void FileTransfertWidget::onFileTransferInfo(ToxFile* File) void FileTransfertWidget::onFileTransferCancelled(ToxFile* File) { - + if (File != file) + return; + progress->hide(); + speed->hide(); + eta->hide(); + topright->hide(); + bottomright->hide(); + QPalette toxred; + toxred.setColor(QPalette::Window, QColor(200,78,78)); // Tox Red + setPalette(toxred); } void FileTransfertWidget::onFileTransferFinished(ToxFile* File) { - + if (File != file) + return; + progress->hide(); + speed->hide(); + eta->hide(); + topright->setIcon(QIcon("img/button icons/yes_2x.png")); + buttonLayout->addStretch(); + buttonLayout->setSpacing(0); + bottomright->hide(); + QPalette toxgreen; + toxgreen.setColor(QPalette::Window, QColor(107,194,96)); // Tox Green + setPalette(toxgreen); }