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

393 lines
12 KiB
C++
Raw Normal View History

2014-11-16 19:58:43 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
2014-11-12 21:11:25 +08:00
#include "filetransferwidget.h"
#include "ui_filetransferwidget.h"
2014-11-17 03:01:37 +08:00
#include "src/core.h"
2014-11-17 23:05:14 +08:00
#include "src/misc/style.h"
2014-11-17 03:01:37 +08:00
2014-11-12 21:11:25 +08:00
#include <QMouseEvent>
2014-11-18 03:08:55 +08:00
#include <QFileDialog>
2015-01-11 18:57:33 +08:00
#include <QFile>
2014-11-18 03:08:55 +08:00
#include <QMessageBox>
#include <QDesktopServices>
#include <QPainter>
2015-02-05 00:21:56 +08:00
#include <QPropertyAnimation>
2014-11-12 21:11:25 +08:00
#include <QDebug>
2014-11-17 03:01:37 +08:00
FileTransferWidget::FileTransferWidget(QWidget *parent, ToxFile file)
: QWidget(parent)
, ui(new Ui::FileTransferWidget)
, fileInfo(file)
2014-11-17 23:05:14 +08:00
, lastTick(QTime::currentTime())
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);
ui->previewLabel->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));
2014-12-08 03:52:01 +08:00
ui->progressLabel->setText("0kiB/s");
2015-01-18 01:17:40 +08:00
ui->etaLabel->setText("");
2014-11-17 23:05:14 +08:00
2015-02-05 00:21:56 +08:00
colorAnimation = new QPropertyAnimation(this, "color");
colorAnimation->setDuration(500);
colorAnimation->setEasingCurve(QEasingCurve::OutCubic);
connect(colorAnimation, &QPropertyAnimation::valueChanged, this, [this] { update(); });
setProperty("color", Style::getColor(Style::LightGrey));
setColor(Style::getColor(Style::LightGrey), false);
2014-11-17 03:01:37 +08:00
connect(Core::getInstance(), &Core::fileTransferInfo, this, &FileTransferWidget::onFileTransferInfo);
2014-11-17 23:05:14 +08:00
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);
setupButtons();
2014-11-17 03:01:37 +08:00
2014-11-18 03:08:55 +08:00
//preview
if(fileInfo.direction == ToxFile::SENDING)
showPreview(fileInfo.filePath);
2015-01-27 17:49:18 +08:00
setFixedHeight(60);
2014-11-12 21:11:25 +08:00
}
FileTransferWidget::~FileTransferWidget()
{
delete ui;
}
2015-01-11 18:57:33 +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();
QString base = QFileInfo(fileInfo.fileName).baseName();
2015-01-11 18:57:33 +08:00
do
{
filepath = QString("%1/%2%3.%4").arg(path, base, number > 0 ? QString(" (%1)").arg(QString::number(number)) : QString(), suffix);
number++;
}
while(QFileInfo(filepath).exists());
//Do not automatically accept the file-transfer if the path is not writable.
//The user can still accept it manually.
if(isFilePathWritable(filepath))
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
else
qDebug() << "Warning: Cannot write to " << filepath;
2015-01-11 18:57:33 +08:00
}
2014-12-14 04:11:03 +08:00
2015-01-11 18:57:33 +08:00
void FileTransferWidget::acceptTransfer(const QString &filepath)
{
//test if writable
if(!isFilePathWritable(filepath))
2015-01-11 18:57:33 +08:00
{
QMessageBox::warning(0,
tr("Location not writable","Title of permissions popup"),
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;
}
2015-01-11 18:57:33 +08:00
//everything ok!
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
}
void FileTransferWidget::setColor(const QColor &c, bool whiteFont)
{
2015-02-05 00:21:56 +08:00
colorAnimation->setStartValue(property("color").value<QColor>());
colorAnimation->setEndValue(c);
colorAnimation->start();
setProperty("fontColor", whiteFont ? "white" : "black");
setStyleSheet(Style::getStylesheet(":/ui/fileTransferInstance/filetransferWidget.css"));
Style::repolish(this);
update();
}
2015-01-11 18:57:33 +08:00
bool FileTransferWidget::isFilePathWritable(const QString &filepath)
{
QFile tmp(filepath);
bool writable = tmp.open(QIODevice::WriteOnly);
tmp.remove();
return writable;
2014-12-14 04:11:03 +08:00
}
void FileTransferWidget::paintEvent(QPaintEvent *)
{
// required by Hi-DPI support as border-image doesn't work.
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
2015-02-05 00:21:56 +08:00
painter.setBrush(QBrush(property("color").value<QColor>()));
painter.setPen(Qt::NoPen);
qreal s = static_cast<qreal>(geometry().height()) / static_cast<qreal>(geometry().width());
2015-01-27 17:49:18 +08:00
int r = 20;
painter.drawRoundRect(geometry(), r * s, r);
}
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();
qint64 dt = lastTick.msecsTo(now); //ms
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;
if(fileInfo.status == ToxFile::TRANSMITTING)
2014-11-17 23:05:14 +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;
qint64 deltaBytes = qMax(file.bytesSent - lastBytesSent, qint64(0));
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-18 01:17:40 +08:00
qreal meanBytesPerSec = 0.0;
2015-01-19 03:57:51 +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
if(meanBytesPerSec > 0)
2014-11-17 23:05:14 +08:00
{
2015-01-18 01:17:40 +08:00
// ETA
QTime toGo = QTime(0,0).addSecs((file.filesize - file.bytesSent) / meanBytesPerSec);
ui->etaLabel->setText(toGo.toString("hh:mm:ss"));
2014-11-17 23:05:14 +08:00
}
2015-01-18 01:17:40 +08:00
else
{
ui->etaLabel->setText("");
}
ui->progressLabel->setText(getHumanReadableSize(meanBytesPerSec) + "/s");
lastBytesSent = file.bytesSent;
}
2014-11-17 23:05:14 +08:00
2015-01-18 01:17:40 +08:00
lastTick = now;
// trigger repaint
update();
2014-11-17 23:05:14 +08:00
}
void FileTransferWidget::onFileTransferAccepted(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setColor(Style::getColor(Style::Yellow), false);
2014-11-17 23:05:14 +08:00
setupButtons();
}
void FileTransferWidget::onFileTransferCancelled(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setColor(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)
{
if(fileInfo != file)
return;
fileInfo = file;
2015-01-18 01:17:40 +08:00
ui->etaLabel->setText("");
ui->progressLabel->setText(getHumanReadableSize(0) + "/s");
2015-01-18 01:17:40 +08:00
// reset mean
meanIndex = 0;
2015-01-19 03:57:51 +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;
setColor(Style::getColor(Style::LightGrey), false);
2014-11-17 23:05:14 +08:00
setupButtons();
}
void FileTransferWidget::onFileTransferFinished(ToxFile file)
{
if(fileInfo != file)
return;
fileInfo = file;
setColor(Style::getColor(Style::Green), true);
2014-11-17 23:05:14 +08:00
setupButtons();
hideWidgets();
static const QStringList openExtensions = { "png", "jpeg", "jpg", "gif", "zip", "rar" };
if(openExtensions.contains(QFileInfo(file.fileName).suffix()))
{
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/browse.svg"));
ui->bottomButton->setObjectName("browse");
ui->bottomButton->show();
}
2014-11-18 03:08:55 +08:00
// preview
if(fileInfo.direction == ToxFile::RECEIVING)
showPreview(fileInfo.filePath);
disconnect(Core::getInstance(), 0, this, 0);
2014-11-17 23:05:14 +08:00
}
QString FileTransferWidget::getHumanReadableSize(qint64 size)
{
static const char* suffix[] = {"B","kiB","MiB","GiB","TiB"};
int exp = 0;
if (size > 0)
exp = std::min( (int) (log(size) / log(1024)), (int) (sizeof(suffix) / sizeof(suffix[0]) - 1));
2015-01-20 18:08:40 +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()
{
ui->topButton->hide();
ui->bottomButton->hide();
2014-11-17 23:05:14 +08:00
ui->progressBar->hide();
ui->progressLabel->hide();
ui->etaLabel->hide();
}
void FileTransferWidget::setupButtons()
{
switch(fileInfo.status)
{
case ToxFile::TRANSMITTING:
ui->topButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
2014-11-17 23:05:14 +08:00
ui->topButton->setObjectName("cancel");
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/pause.svg"));
2014-11-17 23:05:14 +08:00
ui->bottomButton->setObjectName("pause");
break;
case ToxFile::PAUSED:
ui->topButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
2014-11-17 23:05:14 +08:00
ui->topButton->setObjectName("cancel");
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/arrow_white.svg"));
2014-11-17 23:05:14 +08:00
ui->bottomButton->setObjectName("resume");
break;
case ToxFile::STOPPED:
case ToxFile::BROKEN: //TODO: ?
ui->topButton->setIcon(QIcon(":/ui/fileTransferInstance/no.svg"));
2014-11-18 03:08:55 +08:00
ui->topButton->setObjectName("cancel");
if(fileInfo.direction == ToxFile::SENDING)
{
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/pause.svg"));
2014-11-18 03:08:55 +08:00
ui->bottomButton->setObjectName("pause");
}
else
{
ui->bottomButton->setIcon(QIcon(":/ui/fileTransferInstance/yes.svg"));
2014-11-18 03:08:55 +08:00
ui->bottomButton->setObjectName("accept");
}
2014-11-17 23:05:14 +08:00
break;
}
}
void FileTransferWidget::handleButton(QPushButton *btn)
{
if(fileInfo.direction == ToxFile::SENDING)
{
if(btn->objectName() == "cancel")
Core::getInstance()->cancelFileSend(fileInfo.friendId, fileInfo.fileNum);
else if(btn->objectName() == "pause")
2014-11-17 23:05:14 +08:00
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
else if(btn->objectName() == "resume")
2014-11-17 23:05:14 +08:00
Core::getInstance()->pauseResumeFileSend(fileInfo.friendId, fileInfo.fileNum);
}
else
{
if(btn->objectName() == "cancel")
Core::getInstance()->cancelFileRecv(fileInfo.friendId, fileInfo.fileNum);
else if(btn->objectName() == "pause")
2014-11-17 23:05:14 +08:00
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
else if(btn->objectName() == "resume")
2014-11-17 23:05:14 +08:00
Core::getInstance()->pauseResumeFileRecv(fileInfo.friendId, fileInfo.fileNum);
else if(btn->objectName() == "accept")
2014-11-18 03:08:55 +08:00
{
QString path = QFileDialog::getSaveFileName(0, tr("Save a file","Title of the file saving dialog"), QDir::home().filePath(fileInfo.fileName));
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
}
if(btn->objectName() == "browse")
{
QDesktopServices::openUrl("file://" + fileInfo.filePath);
}
2014-11-17 23:05:14 +08:00
}
2014-11-18 03:08:55 +08:00
void FileTransferWidget::showPreview(const QString &filename)
{
2014-12-08 03:52:01 +08:00
static const QStringList previewExtensions = { "png", "jpeg", "jpg", "gif" };
if(previewExtensions.contains(QFileInfo(filename).suffix()))
{
//QPixmap pmap = QPixmap(filename).scaled(QSize(ui->previewLabel->maximumWidth(), maximumHeight()), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap pmap = QPixmap(filename).scaledToWidth(ui->previewLabel->maximumWidth(), Qt::SmoothTransformation);
ui->previewLabel->setPixmap(pmap);
ui->previewLabel->show();
}
2014-11-18 03:08:55 +08:00
}
2014-11-17 23:05:14 +08:00
void FileTransferWidget::on_topButton_clicked()
{
handleButton(ui->topButton);
}
void FileTransferWidget::on_bottomButton_clicked()
{
handleButton(ui->bottomButton);
2014-11-12 21:11:25 +08:00
}