2014-11-16 19:58:43 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-11-16 19:58:43 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-16 19:58:43 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-16 19:58:43 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-16 19:58:43 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-16 19:58:43 +08:00
|
|
|
*/
|
|
|
|
|
2014-11-12 21:11:25 +08:00
|
|
|
#include "filetransferwidget.h"
|
|
|
|
#include "ui_filetransferwidget.h"
|
|
|
|
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/core/core.h"
|
2017-02-26 19:52:45 +08:00
|
|
|
#include "src/nexus.h"
|
|
|
|
#include "src/persistence/settings.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/widget/gui.h"
|
|
|
|
#include "src/widget/style.h"
|
|
|
|
#include "src/widget/widget.h"
|
2014-11-17 03:01:37 +08:00
|
|
|
|
2015-03-09 04:01:34 +08:00
|
|
|
#include <QBuffer>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QDebug>
|
2014-12-10 17:35:56 +08:00
|
|
|
#include <QDesktopServices>
|
2015-03-01 00:14:58 +08:00
|
|
|
#include <QDesktopWidget>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QMouseEvent>
|
2015-01-26 18:09:21 +08:00
|
|
|
#include <QPainter>
|
2015-02-07 21:02:40 +08:00
|
|
|
#include <QVariantAnimation>
|
2014-11-12 21:11:25 +08:00
|
|
|
|
2015-02-18 14:02:09 +08:00
|
|
|
#include <math.h>
|
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
// The leftButton is used to accept, pause, or resume a file transfer, as well as to open a
|
|
|
|
// received file.
|
|
|
|
// The rightButton is used to cancel a file transfer, or to open the directory a file was
|
|
|
|
// downloaded to.
|
|
|
|
|
2017-01-06 19:02:54 +08:00
|
|
|
FileTransferWidget::FileTransferWidget(QWidget* parent, ToxFile file)
|
2014-11-17 03:01:37 +08:00
|
|
|
: QWidget(parent)
|
|
|
|
, ui(new Ui::FileTransferWidget)
|
|
|
|
, fileInfo(file)
|
2014-11-17 23:05:14 +08:00
|
|
|
, lastTick(QTime::currentTime())
|
2015-02-13 23:46:59 +08:00
|
|
|
, backgroundColor(Style::getColor(Style::LightGrey))
|
|
|
|
, buttonColor(Style::getColor(Style::Yellow))
|
2017-02-28 20:29:51 +08:00
|
|
|
, buttonBackgroundColor(Style::getColor(Style::White))
|
2016-07-30 21:14:18 +08:00
|
|
|
, active(true)
|
2014-11-12 21:11:25 +08:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2014-11-18 03:08:55 +08:00
|
|
|
// hide the QWidget background (background-color: transparent doesn't seem to work)
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground, true);
|
|
|
|
|
2016-02-14 23:20:11 +08:00
|
|
|
ui->previewButton->hide();
|
2014-11-17 03:01:37 +08:00
|
|
|
ui->filenameLabel->setText(file.fileName);
|
|
|
|
ui->progressBar->setValue(0);
|
2014-11-17 23:05:14 +08:00
|
|
|
ui->fileSizeLabel->setText(getHumanReadableSize(file.filesize));
|
2015-01-18 01:17:40 +08:00
|
|
|
ui->etaLabel->setText("");
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-02-13 23:46:59 +08:00
|
|
|
backgroundColorAnimation = new QVariantAnimation(this);
|
|
|
|
backgroundColorAnimation->setDuration(500);
|
|
|
|
backgroundColorAnimation->setEasingCurve(QEasingCurve::OutCubic);
|
2017-02-26 19:52:45 +08:00
|
|
|
connect(backgroundColorAnimation, &QVariantAnimation::valueChanged, this,
|
|
|
|
[this](const QVariant& val) {
|
|
|
|
backgroundColor = val.value<QColor>();
|
|
|
|
update();
|
|
|
|
});
|
2015-02-05 00:21:56 +08:00
|
|
|
|
2015-02-14 19:14:09 +08:00
|
|
|
buttonColorAnimation = new QVariantAnimation(this);
|
|
|
|
buttonColorAnimation->setDuration(500);
|
|
|
|
buttonColorAnimation->setEasingCurve(QEasingCurve::OutCubic);
|
|
|
|
connect(buttonColorAnimation, &QVariantAnimation::valueChanged, this, [this](const QVariant& val) {
|
|
|
|
buttonColor = val.value<QColor>();
|
|
|
|
update();
|
|
|
|
});
|
|
|
|
|
|
|
|
setBackgroundColor(Style::getColor(Style::LightGrey), false);
|
2014-11-17 03:01:37 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
connect(Core::getInstance(), &Core::fileTransferInfo, this,
|
|
|
|
&FileTransferWidget::onFileTransferInfo);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferAccepted, this,
|
|
|
|
&FileTransferWidget::onFileTransferAccepted);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferCancelled, this,
|
|
|
|
&FileTransferWidget::onFileTransferCancelled);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferPaused, this,
|
|
|
|
&FileTransferWidget::onFileTransferPaused);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferFinished, this,
|
|
|
|
&FileTransferWidget::onFileTransferFinished);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferRemotePausedUnpaused, this,
|
|
|
|
&FileTransferWidget::fileTransferRemotePausedUnpaused);
|
|
|
|
connect(Core::getInstance(), &Core::fileTransferBrokenUnbroken, this,
|
|
|
|
&FileTransferWidget::fileTransferBrokenUnbroken);
|
2017-02-28 20:29:51 +08:00
|
|
|
connect(ui->leftButton, &QPushButton::clicked, this,
|
|
|
|
&FileTransferWidget::onLeftButtonClicked);
|
|
|
|
connect(ui->rightButton, &QPushButton::clicked, this,
|
|
|
|
&FileTransferWidget::onRightButtonClicked);
|
2017-02-26 19:52:45 +08:00
|
|
|
connect(ui->previewButton, &QPushButton::clicked, this,
|
|
|
|
&FileTransferWidget::onPreviewButtonClicked);
|
2014-11-17 23:05:14 +08:00
|
|
|
|
|
|
|
setupButtons();
|
2014-11-17 03:01:37 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
// preview
|
|
|
|
if (fileInfo.direction == ToxFile::SENDING) {
|
2014-11-18 03:08:55 +08:00
|
|
|
showPreview(fileInfo.filePath);
|
2015-02-16 08:56:11 +08:00
|
|
|
ui->progressLabel->setText(tr("Waiting to send...", "file transfer widget"));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2015-02-16 08:56:11 +08:00
|
|
|
ui->progressLabel->setText(tr("Accept to receive this file", "file transfer widget"));
|
2015-04-24 19:01:50 +08:00
|
|
|
}
|
2014-11-18 03:08:55 +08:00
|
|
|
|
2016-01-21 12:19:30 +08:00
|
|
|
setFixedHeight(64);
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileTransferWidget::~FileTransferWidget()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::autoAcceptTransfer(const QString& path)
|
2014-12-14 04:11:03 +08:00
|
|
|
{
|
2015-01-11 18:57:33 +08:00
|
|
|
QString filepath;
|
|
|
|
int number = 0;
|
|
|
|
|
|
|
|
QString suffix = QFileInfo(fileInfo.fileName).completeSuffix();
|
2015-01-12 23:03:47 +08:00
|
|
|
QString base = QFileInfo(fileInfo.fileName).baseName();
|
2015-01-11 18:57:33 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
do {
|
|
|
|
filepath = QString("%1/%2%3.%4")
|
|
|
|
.arg(path, base,
|
|
|
|
number > 0 ? QString(" (%1)").arg(QString::number(number)) : QString(),
|
|
|
|
suffix);
|
2016-10-30 06:48:03 +08:00
|
|
|
++number;
|
2017-02-26 19:52:45 +08:00
|
|
|
} while (QFileInfo(filepath).exists());
|
2015-01-11 18:57:33 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
// Do not automatically accept the file-transfer if the path is not writable.
|
|
|
|
// The user can still accept it manually.
|
2015-04-26 23:39:39 +08:00
|
|
|
if (Nexus::tryRemoveFile(filepath))
|
2015-01-12 23:03:47 +08:00
|
|
|
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
|
|
|
|
else
|
2015-05-11 20:54:03 +08:00
|
|
|
qWarning() << "Cannot write to " << filepath;
|
2015-01-11 18:57:33 +08:00
|
|
|
}
|
2014-12-14 04:11:03 +08:00
|
|
|
|
2016-07-30 21:14:18 +08:00
|
|
|
bool FileTransferWidget::isActive() const
|
|
|
|
{
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::acceptTransfer(const QString& filepath)
|
2015-01-11 18:57:33 +08:00
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (filepath.isEmpty())
|
2015-02-05 16:59:47 +08:00
|
|
|
return;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
// test if writable
|
|
|
|
if (!Nexus::tryRemoveFile(filepath)) {
|
2016-01-04 19:02:37 +08:00
|
|
|
GUI::showWarning(tr("Location not writable", "Title of permissions popup"),
|
2017-02-26 19:52:45 +08:00
|
|
|
tr("You do not have permission to write that location. Choose another, or "
|
|
|
|
"cancel the save dialog.",
|
|
|
|
"text of permissions popup"));
|
2014-12-14 04:11:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
// everything ok!
|
2015-01-11 18:57:33 +08:00
|
|
|
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::setBackgroundColor(const QColor& c, bool whiteFont)
|
2015-01-26 18:09:21 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (c != backgroundColor) {
|
2015-02-13 23:46:59 +08:00
|
|
|
backgroundColorAnimation->setStartValue(backgroundColor);
|
|
|
|
backgroundColorAnimation->setEndValue(c);
|
|
|
|
backgroundColorAnimation->start();
|
2015-02-07 21:38:12 +08:00
|
|
|
}
|
2015-02-05 00:21:56 +08:00
|
|
|
|
2015-01-26 18:09:21 +08:00
|
|
|
setProperty("fontColor", whiteFont ? "white" : "black");
|
|
|
|
|
|
|
|
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/filetransferWidget.css"));
|
|
|
|
Style::repolish(this);
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::setButtonColor(const QColor& c)
|
2015-02-14 19:14:09 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (c != buttonColor) {
|
2015-02-14 19:14:09 +08:00
|
|
|
buttonColorAnimation->setStartValue(buttonColor);
|
|
|
|
buttonColorAnimation->setEndValue(c);
|
|
|
|
buttonColorAnimation->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileTransferWidget::drawButtonAreaNeeded() const
|
|
|
|
{
|
2017-02-28 20:29:51 +08:00
|
|
|
return (ui->rightButton->isVisible() || ui->leftButton->isVisible())
|
|
|
|
&& !(ui->leftButton->isVisible() && ui->leftButton->objectName() == "ok");
|
2015-02-14 19:14:09 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::paintEvent(QPaintEvent*)
|
2015-01-26 18:09:21 +08:00
|
|
|
{
|
|
|
|
// required by Hi-DPI support as border-image doesn't work.
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
|
|
|
painter.setPen(Qt::NoPen);
|
|
|
|
|
2015-02-13 23:46:59 +08:00
|
|
|
qreal ratio = static_cast<qreal>(geometry().height()) / static_cast<qreal>(geometry().width());
|
2015-02-14 19:14:09 +08:00
|
|
|
const int r = 24;
|
2017-02-28 20:29:51 +08:00
|
|
|
const int buttonFieldWidth = 32;
|
2015-02-14 19:21:03 +08:00
|
|
|
const int lineWidth = 1;
|
2015-02-13 23:46:59 +08:00
|
|
|
|
2017-03-05 18:56:20 +08:00
|
|
|
// Draw the widget background:
|
|
|
|
painter.setClipRect(QRect(0, 0, width(), height()));
|
|
|
|
painter.setBrush(QBrush(backgroundColor));
|
|
|
|
painter.drawRoundRect(geometry(), r * ratio, r);
|
2015-02-13 23:46:59 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (drawButtonAreaNeeded()) {
|
2017-02-28 20:29:51 +08:00
|
|
|
// Draw the button background:
|
|
|
|
QPainterPath buttonBackground;
|
|
|
|
buttonBackground.addRoundRect(width()-2*buttonFieldWidth-lineWidth*2, 0, buttonFieldWidth,
|
|
|
|
buttonFieldWidth+lineWidth, 50, 50);
|
|
|
|
buttonBackground.addRect(width()-2*buttonFieldWidth-lineWidth*2, 0, buttonFieldWidth*2,
|
|
|
|
buttonFieldWidth/2);
|
|
|
|
buttonBackground.addRect(width()-1.5*buttonFieldWidth-lineWidth*2, 0, buttonFieldWidth*2,
|
|
|
|
buttonFieldWidth+1);
|
|
|
|
buttonBackground.setFillRule(Qt::WindingFill);
|
|
|
|
painter.setBrush(QBrush(buttonBackgroundColor));
|
|
|
|
painter.drawPath(buttonBackground);
|
|
|
|
|
|
|
|
// Draw the left button:
|
|
|
|
QPainterPath leftButton;
|
|
|
|
leftButton.addRoundRect(QRect(width()-2*buttonFieldWidth-lineWidth, 0, buttonFieldWidth,
|
|
|
|
buttonFieldWidth), 50, 50);
|
|
|
|
leftButton.addRect(QRect(width()-2*buttonFieldWidth-lineWidth, 0, buttonFieldWidth/2,
|
|
|
|
buttonFieldWidth/2));
|
|
|
|
leftButton.addRect(QRect(width()-1.5*buttonFieldWidth-lineWidth, 0, buttonFieldWidth/2,
|
|
|
|
buttonFieldWidth));
|
|
|
|
leftButton.setFillRule(Qt::WindingFill);
|
2015-02-14 19:14:09 +08:00
|
|
|
painter.setBrush(QBrush(buttonColor));
|
2017-02-28 20:29:51 +08:00
|
|
|
painter.drawPath(leftButton);
|
2015-02-14 19:14:09 +08:00
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
// Draw the right button:
|
2015-02-14 19:14:09 +08:00
|
|
|
painter.setBrush(QBrush(buttonColor));
|
2017-02-28 20:29:51 +08:00
|
|
|
painter.setClipRect(QRect(width()-buttonFieldWidth, 0, buttonFieldWidth,
|
|
|
|
buttonFieldWidth));
|
2015-02-14 19:14:09 +08:00
|
|
|
painter.drawRoundRect(geometry(), r * ratio, r);
|
|
|
|
}
|
2015-01-26 18:09:21 +08:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:05:14 +08:00
|
|
|
void FileTransferWidget::onFileTransferInfo(ToxFile file)
|
2014-11-12 21:11:25 +08:00
|
|
|
{
|
2015-01-18 01:17:40 +08:00
|
|
|
QTime now = QTime::currentTime();
|
2017-02-26 19:52:45 +08:00
|
|
|
qint64 dt = lastTick.msecsTo(now); // ms
|
2015-01-18 01:17:40 +08:00
|
|
|
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo != file || dt < 1000)
|
2014-11-17 03:01:37 +08:00
|
|
|
return;
|
2014-11-12 21:11:25 +08:00
|
|
|
|
2014-11-17 23:05:14 +08:00
|
|
|
fileInfo = file;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (fileInfo.status == ToxFile::TRANSMITTING) {
|
2015-01-04 19:04:30 +08:00
|
|
|
// update progress
|
|
|
|
qreal progress = static_cast<qreal>(file.bytesSent) / static_cast<qreal>(file.filesize);
|
|
|
|
ui->progressBar->setValue(static_cast<int>(progress * 100.0));
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-01-18 01:17:40 +08:00
|
|
|
// ETA, speed
|
|
|
|
qreal deltaSecs = dt / 1000.0;
|
|
|
|
|
2015-04-24 21:31:30 +08:00
|
|
|
// (can't use ::abs or ::max on unsigned types substraction, they'd just overflow)
|
2017-02-26 19:52:45 +08:00
|
|
|
quint64 deltaBytes = file.bytesSent > lastBytesSent ? file.bytesSent - lastBytesSent
|
|
|
|
: lastBytesSent - file.bytesSent;
|
2015-01-18 01:17:40 +08:00
|
|
|
qreal bytesPerSec = static_cast<int>(static_cast<qreal>(deltaBytes) / deltaSecs);
|
|
|
|
|
|
|
|
// calculate mean
|
2015-01-20 18:03:06 +08:00
|
|
|
meanIndex = meanIndex % TRANSFER_ROLLING_AVG_COUNT;
|
|
|
|
meanData[meanIndex++] = bytesPerSec;
|
2015-01-04 19:04:30 +08:00
|
|
|
|
2015-01-18 01:17:40 +08:00
|
|
|
qreal meanBytesPerSec = 0.0;
|
2015-03-21 02:38:10 +08:00
|
|
|
for (size_t i = 0; i < TRANSFER_ROLLING_AVG_COUNT; ++i)
|
2015-01-18 01:17:40 +08:00
|
|
|
meanBytesPerSec += meanData[i];
|
|
|
|
|
2015-01-20 18:03:06 +08:00
|
|
|
meanBytesPerSec /= static_cast<qreal>(TRANSFER_ROLLING_AVG_COUNT);
|
2015-01-18 01:17:40 +08:00
|
|
|
|
|
|
|
// update UI
|
2017-02-26 19:52:45 +08:00
|
|
|
if (meanBytesPerSec > 0) {
|
2015-01-18 01:17:40 +08:00
|
|
|
// ETA
|
2017-02-26 19:52:45 +08:00
|
|
|
QTime toGo = QTime(0, 0).addSecs((file.filesize - file.bytesSent) / meanBytesPerSec);
|
2015-02-14 19:14:09 +08:00
|
|
|
QString format = toGo.hour() > 0 ? "hh:mm:ss" : "mm:ss";
|
|
|
|
ui->etaLabel->setText(toGo.toString(format));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2015-01-18 01:17:40 +08:00
|
|
|
ui->etaLabel->setText("");
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->progressLabel->setText(getHumanReadableSize(meanBytesPerSec) + "/s");
|
|
|
|
|
|
|
|
lastBytesSent = file.bytesSent;
|
2015-01-04 19:04:30 +08:00
|
|
|
}
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-01-18 01:17:40 +08:00
|
|
|
lastTick = now;
|
|
|
|
|
|
|
|
// trigger repaint
|
2015-01-04 19:04:30 +08:00
|
|
|
update();
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferWidget::onFileTransferAccepted(ToxFile file)
|
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo != file)
|
2014-11-17 23:05:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo = file;
|
|
|
|
|
2015-02-14 19:14:09 +08:00
|
|
|
setBackgroundColor(Style::getColor(Style::LightGrey), false);
|
2014-11-17 23:05:14 +08:00
|
|
|
|
|
|
|
setupButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferWidget::onFileTransferCancelled(ToxFile file)
|
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo != file)
|
2014-11-17 23:05:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo = file;
|
2016-07-30 21:14:18 +08:00
|
|
|
active = false;
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-02-14 19:14:09 +08:00
|
|
|
setBackgroundColor(Style::getColor(Style::Red), true);
|
2014-11-17 23:05:14 +08:00
|
|
|
|
|
|
|
setupButtons();
|
|
|
|
hideWidgets();
|
|
|
|
|
2014-11-18 03:08:55 +08:00
|
|
|
disconnect(Core::getInstance(), 0, this, 0);
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferWidget::onFileTransferPaused(ToxFile file)
|
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo != file)
|
2014-11-17 23:05:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo = file;
|
|
|
|
|
2015-01-18 01:17:40 +08:00
|
|
|
ui->etaLabel->setText("");
|
2015-08-31 20:59:39 +08:00
|
|
|
ui->progressLabel->setText(tr("Paused", "file transfer widget"));
|
2015-01-04 19:04:30 +08:00
|
|
|
|
2015-01-18 01:17:40 +08:00
|
|
|
// reset mean
|
|
|
|
meanIndex = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
for (size_t i = 0; i < TRANSFER_ROLLING_AVG_COUNT; ++i)
|
2015-01-18 01:17:40 +08:00
|
|
|
meanData[i] = 0.0;
|
|
|
|
|
2015-02-14 19:14:09 +08:00
|
|
|
setBackgroundColor(Style::getColor(Style::LightGrey), false);
|
2014-11-17 23:05:14 +08:00
|
|
|
|
|
|
|
setupButtons();
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:31:30 +08:00
|
|
|
void FileTransferWidget::onFileTransferResumed(ToxFile file)
|
|
|
|
{
|
2015-05-08 04:00:40 +08:00
|
|
|
if (fileInfo != file)
|
2015-04-24 21:31:30 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo = file;
|
|
|
|
|
|
|
|
ui->etaLabel->setText("");
|
|
|
|
ui->progressLabel->setText(tr("Resuming...", "file transfer widget"));
|
|
|
|
|
|
|
|
// reset mean
|
|
|
|
meanIndex = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
for (size_t i = 0; i < TRANSFER_ROLLING_AVG_COUNT; ++i)
|
2015-04-24 21:31:30 +08:00
|
|
|
meanData[i] = 0.0;
|
|
|
|
|
|
|
|
setBackgroundColor(Style::getColor(Style::LightGrey), false);
|
|
|
|
|
|
|
|
setupButtons();
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:05:14 +08:00
|
|
|
void FileTransferWidget::onFileTransferFinished(ToxFile file)
|
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo != file)
|
2014-11-17 23:05:14 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
fileInfo = file;
|
2016-07-30 21:14:18 +08:00
|
|
|
active = false;
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-02-14 19:14:09 +08:00
|
|
|
setBackgroundColor(Style::getColor(Style::Green), true);
|
2014-11-17 23:05:14 +08:00
|
|
|
|
|
|
|
setupButtons();
|
|
|
|
hideWidgets();
|
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->setIcon(QIcon(":/ui/fileTransferInstance/yes.svg"));
|
|
|
|
ui->leftButton->setObjectName("ok");
|
|
|
|
ui->leftButton->setToolTip(tr("Open file"));
|
|
|
|
ui->leftButton->show();
|
2014-12-10 17:35:56 +08:00
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->rightButton->setIcon(QIcon(":/ui/fileTransferInstance/dir.svg"));
|
|
|
|
ui->rightButton->setObjectName("dir");
|
|
|
|
ui->rightButton->setToolTip(tr("Open file directory"));
|
|
|
|
ui->rightButton->show();
|
2015-02-18 03:07:25 +08:00
|
|
|
|
2014-11-18 03:08:55 +08:00
|
|
|
// preview
|
2015-03-21 02:38:10 +08:00
|
|
|
if (fileInfo.direction == ToxFile::RECEIVING)
|
2014-11-18 03:08:55 +08:00
|
|
|
showPreview(fileInfo.filePath);
|
|
|
|
|
|
|
|
disconnect(Core::getInstance(), 0, this, 0);
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
2015-04-24 21:31:30 +08:00
|
|
|
void FileTransferWidget::fileTransferRemotePausedUnpaused(ToxFile file, bool paused)
|
|
|
|
{
|
|
|
|
if (paused)
|
|
|
|
onFileTransferPaused(file);
|
|
|
|
else
|
|
|
|
onFileTransferResumed(file);
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:18:46 +08:00
|
|
|
void FileTransferWidget::fileTransferBrokenUnbroken(ToxFile file, bool broken)
|
|
|
|
{
|
2016-08-15 17:00:06 +08:00
|
|
|
// TODO: Handle broken transfer differently once we have resuming code
|
2015-04-26 01:18:46 +08:00
|
|
|
if (broken)
|
|
|
|
onFileTransferCancelled(file);
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:05:14 +08:00
|
|
|
QString FileTransferWidget::getHumanReadableSize(qint64 size)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
static const char* suffix[] = {"B", "kiB", "MiB", "GiB", "TiB"};
|
2014-11-17 23:05:14 +08:00
|
|
|
int exp = 0;
|
|
|
|
|
|
|
|
if (size > 0)
|
2017-02-26 19:52:45 +08:00
|
|
|
exp = std::min((int)(log(size) / log(1024)), (int)(sizeof(suffix) / sizeof(suffix[0]) - 1));
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-08-31 03:36:39 +08:00
|
|
|
return QString().setNum(size / pow(1024, exp), 'f', exp > 1 ? 2 : 0).append(suffix[exp]);
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferWidget::hideWidgets()
|
|
|
|
{
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->hide();
|
|
|
|
ui->rightButton->hide();
|
2014-11-17 23:05:14 +08:00
|
|
|
ui->progressBar->hide();
|
|
|
|
ui->progressLabel->hide();
|
|
|
|
ui->etaLabel->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTransferWidget::setupButtons()
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
switch (fileInfo.status) {
|
2014-11-17 23:05:14 +08:00
|
|
|
case ToxFile::TRANSMITTING:
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->setIcon(QIcon(":/ui/fileTransferInstance/pause.svg"));
|
|
|
|
ui->leftButton->setObjectName("pause");
|
|
|
|
ui->leftButton->setToolTip(tr("Pause transfer"));
|
2015-02-14 19:14:09 +08:00
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->rightButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
|
|
|
|
ui->rightButton->setObjectName("cancel");
|
|
|
|
ui->rightButton->setToolTip(tr("Cancel transfer"));
|
2014-11-17 23:05:14 +08:00
|
|
|
|
2015-08-31 20:59:39 +08:00
|
|
|
setButtonColor(Style::getColor(Style::Green));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ToxFile::PAUSED:
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->setIcon(QIcon(":/ui/fileTransferInstance/arrow_white.svg"));
|
|
|
|
ui->leftButton->setObjectName("resume");
|
|
|
|
ui->leftButton->setToolTip(tr("Resume transfer"));
|
2015-02-14 19:14:09 +08:00
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->rightButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
|
|
|
|
ui->rightButton->setObjectName("cancel");
|
|
|
|
ui->rightButton->setToolTip(tr("Cancel transfer"));
|
2015-02-14 19:14:09 +08:00
|
|
|
|
2015-08-31 20:59:39 +08:00
|
|
|
setButtonColor(Style::getColor(Style::LightGrey));
|
2014-11-17 23:05:14 +08:00
|
|
|
break;
|
2015-08-31 20:59:39 +08:00
|
|
|
|
2014-11-17 23:05:14 +08:00
|
|
|
case ToxFile::STOPPED:
|
2015-08-31 03:36:39 +08:00
|
|
|
case ToxFile::BROKEN:
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->rightButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
|
|
|
|
ui->rightButton->setObjectName("cancel");
|
|
|
|
ui->rightButton->setToolTip(tr("Cancel transfer"));
|
2014-11-18 03:08:55 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (fileInfo.direction == ToxFile::SENDING) {
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->setIcon(QIcon(":/ui/fileTransferInstance/pause.svg"));
|
|
|
|
ui->leftButton->setObjectName("pause");
|
|
|
|
ui->leftButton->setToolTip(tr("Pause transfer"));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2017-02-28 20:29:51 +08:00
|
|
|
ui->leftButton->setIcon(QIcon(":/ui/fileTransferInstance/yes.svg"));
|
|
|
|
ui->leftButton->setObjectName("accept");
|
|
|
|
ui->leftButton->setToolTip(tr("Accept transfer"));
|
2014-11-18 03:08:55 +08:00
|
|
|
}
|
2014-11-17 23:05:14 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::handleButton(QPushButton* btn)
|
2014-11-17 23:05:14 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (fileInfo.direction == ToxFile::SENDING) {
|
2015-03-21 02:38:10 +08:00
|
|
|
if (btn->objectName() == "cancel")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->cancelFileSend(fileInfo.friendId, fileInfo.fileNum);
|
2015-03-21 02:38:10 +08:00
|
|
|
else if (btn->objectName() == "pause")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
|
2015-03-21 02:38:10 +08:00
|
|
|
else if (btn->objectName() == "resume")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
|
2017-02-26 19:52:45 +08:00
|
|
|
} else // receiving or paused
|
2014-11-17 23:05:14 +08:00
|
|
|
{
|
2015-03-21 02:38:10 +08:00
|
|
|
if (btn->objectName() == "cancel")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->cancelFileRecv(fileInfo.friendId, fileInfo.fileNum);
|
2015-03-21 02:38:10 +08:00
|
|
|
else if (btn->objectName() == "pause")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
|
2015-03-21 02:38:10 +08:00
|
|
|
else if (btn->objectName() == "resume")
|
2014-11-17 23:05:14 +08:00
|
|
|
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
|
2017-02-26 19:52:45 +08:00
|
|
|
else if (btn->objectName() == "accept") {
|
|
|
|
QString path =
|
|
|
|
QFileDialog::getSaveFileName(parentWidget(),
|
|
|
|
tr("Save a file", "Title of the file saving dialog"),
|
|
|
|
Settings::getInstance().getGlobalAutoAcceptDir() + "/"
|
|
|
|
+ fileInfo.fileName,
|
|
|
|
0, 0, QFileDialog::DontUseNativeDialog);
|
2014-12-14 04:11:03 +08:00
|
|
|
acceptTransfer(path);
|
2014-11-18 03:08:55 +08:00
|
|
|
}
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
2014-12-10 17:35:56 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (btn->objectName() == "ok" || btn->objectName() == "previewButton") {
|
2015-02-22 20:21:42 +08:00
|
|
|
Widget::confirmExecutableOpen(QFileInfo(fileInfo.filePath));
|
2017-02-26 19:52:45 +08:00
|
|
|
} else if (btn->objectName() == "dir") {
|
2015-02-20 03:14:19 +08:00
|
|
|
QString dirPath = QFileInfo(fileInfo.filePath).dir().path();
|
2015-03-01 17:43:43 +08:00
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(dirPath));
|
2015-02-18 03:07:25 +08:00
|
|
|
}
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
void FileTransferWidget::showPreview(const QString& filename)
|
2014-11-18 03:08:55 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
static const QStringList previewExtensions = {"png", "jpeg", "jpg", "gif", "svg",
|
|
|
|
"PNG", "JPEG", "JPG", "GIF", "SVG"};
|
2014-12-08 03:52:01 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (previewExtensions.contains(QFileInfo(filename).suffix())) {
|
2016-04-15 02:19:18 +08:00
|
|
|
// Subtract to make border visible
|
|
|
|
const int size = qMax(ui->previewButton->width(), ui->previewButton->height()) - 4;
|
2015-05-10 18:46:22 +08:00
|
|
|
|
2016-04-15 02:19:18 +08:00
|
|
|
const QImage image = QImage(filename);
|
|
|
|
const QPixmap iconPixmap = scaleCropIntoSquare(QPixmap::fromImage(image), size);
|
|
|
|
|
|
|
|
ui->previewButton->setIcon(QIcon(iconPixmap));
|
|
|
|
ui->previewButton->setIconSize(iconPixmap.size());
|
2016-02-14 23:20:11 +08:00
|
|
|
ui->previewButton->show();
|
2015-03-09 04:01:34 +08:00
|
|
|
// Show mouseover preview, but make sure it's not larger than 50% of the screen width/height
|
2016-04-15 02:19:18 +08:00
|
|
|
const QRect desktopSize = QApplication::desktop()->screenGeometry();
|
2017-02-26 19:52:45 +08:00
|
|
|
const QImage previewImage = image.scaled(0.5 * desktopSize.width(), 0.5 * desktopSize.height(),
|
|
|
|
Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2015-03-09 04:01:34 +08:00
|
|
|
QByteArray imageData;
|
|
|
|
QBuffer buffer(&imageData);
|
2015-05-10 08:30:47 +08:00
|
|
|
buffer.open(QIODevice::WriteOnly);
|
2016-04-15 02:19:18 +08:00
|
|
|
previewImage.save(&buffer, "PNG");
|
2015-05-10 08:30:47 +08:00
|
|
|
buffer.close();
|
2017-02-26 19:52:45 +08:00
|
|
|
ui->previewButton->setToolTip("<img src=data:image/png;base64," + imageData.toBase64()
|
|
|
|
+ "/>");
|
2014-12-08 03:52:01 +08:00
|
|
|
}
|
2014-11-18 03:08:55 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
void FileTransferWidget::onLeftButtonClicked()
|
2014-11-17 23:05:14 +08:00
|
|
|
{
|
2017-02-28 20:29:51 +08:00
|
|
|
handleButton(ui->leftButton);
|
2014-11-17 23:05:14 +08:00
|
|
|
}
|
|
|
|
|
2017-02-28 20:29:51 +08:00
|
|
|
void FileTransferWidget::onRightButtonClicked()
|
2014-11-17 23:05:14 +08:00
|
|
|
{
|
2017-02-28 20:29:51 +08:00
|
|
|
handleButton(ui->rightButton);
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
2016-02-14 23:20:11 +08:00
|
|
|
|
|
|
|
void FileTransferWidget::onPreviewButtonClicked()
|
|
|
|
{
|
|
|
|
handleButton(ui->previewButton);
|
|
|
|
}
|
2016-04-15 02:19:18 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
QPixmap FileTransferWidget::scaleCropIntoSquare(const QPixmap& source, const int targetSize)
|
2016-04-15 02:19:18 +08:00
|
|
|
{
|
|
|
|
QPixmap result;
|
|
|
|
|
|
|
|
// Make sure smaller-than-icon images (at least one dimension is smaller) will not be upscaled
|
2017-02-26 19:52:45 +08:00
|
|
|
if (source.width() < targetSize || source.height() < targetSize) {
|
2016-04-15 02:19:18 +08:00
|
|
|
result = source;
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
|
|
|
result = source.scaled(targetSize, targetSize, Qt::KeepAspectRatioByExpanding,
|
2016-04-15 02:19:18 +08:00
|
|
|
Qt::SmoothTransformation);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, image has to be cropped (if needed) so it will not overflow rectangle
|
|
|
|
// Only one dimension will be bigger after Qt::KeepAspectRatioByExpanding
|
|
|
|
if (result.width() > targetSize)
|
|
|
|
return result.copy((result.width() - targetSize) / 2, 0, targetSize, targetSize);
|
|
|
|
else if (result.height() > targetSize)
|
|
|
|
return result.copy(0, (result.height() - targetSize) / 2, targetSize, targetSize);
|
|
|
|
|
|
|
|
// Picture was rectangle in the first place, no cropping
|
|
|
|
return result;
|
|
|
|
}
|