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

FileTransferWidget: fixed autoaccept

This commit is contained in:
krepa098 2015-01-11 11:57:33 +01:00
parent 3bb52e5538
commit f574a43581
5 changed files with 61 additions and 12 deletions

View File

@ -47,6 +47,11 @@ qreal ChatLineContentProxy::getAscent() const
return proxy->widget()->layout()->contentsMargins().top();
}
QWidget *ChatLineContentProxy::getWidget() const
{
return proxy->widget();
}
void ChatLineContentProxy::setWidth(qreal width)
{
proxy->widget()->setFixedWidth(qMax(static_cast<int>(width*widthPercent), widthMin));

View File

@ -31,6 +31,8 @@ public:
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
virtual qreal getAscent() const;
QWidget* getWidget() const;
private:
QGraphicsProxyWidget* proxy;
int widthMin;

View File

@ -22,8 +22,7 @@
#include <QMouseEvent>
#include <QFileDialog>
#include <QDir>
#include <QFileInfo>
#include <QFile>
#include <QMessageBox>
#include <QDesktopServices>
#include <QDebug>
@ -69,19 +68,54 @@ FileTransferWidget::~FileTransferWidget()
delete ui;
}
void FileTransferWidget::acceptTransfer(const QString &path)
void FileTransferWidget::autoAcceptTransfer(const QString &path)
{
if(!QFileInfo(QDir(path).path()).isWritable())
QString filepath;
int number = 0;
QString suffix = QFileInfo(fileInfo.fileName).completeSuffix();
QString base = QFileInfo(fileInfo.fileName).completeBaseName();
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());
if(!isFilePathWritable(filepath))
{
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"));
return;
}
if(!path.isEmpty())
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, path);
//everything ok!
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
}
void FileTransferWidget::acceptTransfer(const QString &filepath)
{
//test if writable
if(isFilePathWritable(filepath))
{
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"));
return;
}
//everything ok!
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
}
bool FileTransferWidget::isFilePathWritable(const QString &filepath)
{
QFile tmp(filepath);
bool writable = tmp.open(QIODevice::WriteOnly);
tmp.remove();
return writable;
}
void FileTransferWidget::onFileTransferInfo(ToxFile file)

View File

@ -35,8 +35,7 @@ class FileTransferWidget : public QWidget
public:
explicit FileTransferWidget(QWidget *parent, ToxFile file);
virtual ~FileTransferWidget();
void acceptTransfer(const QString& path);
void autoAcceptTransfer(const QString& path);
protected slots:
void onFileTransferInfo(ToxFile file);
@ -51,6 +50,9 @@ protected:
void setupButtons();
void handleButton(QPushButton* btn);
void showPreview(const QString& filename);
void acceptTransfer(const QString& filepath);
bool isFilePathWritable(const QString& filepath);
private slots:
void on_topButton_clicked();

View File

@ -40,6 +40,7 @@
#include "src/misc/cstring.h"
#include "src/chatlog/chatmessage.h"
#include "src/chatlog/content/filetransferwidget.h"
#include "src/chatlog/chatlinecontentproxy.h"
#include "src/chatlog/content/text.h"
#include "src/chatlog/chatlog.h"
@ -222,9 +223,14 @@ void ChatForm::onFileRecvRequest(ToxFile file)
if (!Settings::getInstance().getAutoAcceptDir(f->getToxID()).isEmpty()
|| Settings::getInstance().getAutoSaveEnabled())
{
FileTransferWidget* tfWidget = dynamic_cast<FileTransferWidget*>(msg->getContent(1));
if(tfWidget)
tfWidget->acceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxID()));
ChatLineContentProxy* proxy = dynamic_cast<ChatLineContentProxy*>(msg->getContent(1));
if(proxy)
{
FileTransferWidget* tfWidget = dynamic_cast<FileTransferWidget*>(proxy->getWidget());
if(tfWidget)
tfWidget->autoAcceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxID()));
}
}
}