diff --git a/src/chatlog/chatlinecontentproxy.cpp b/src/chatlog/chatlinecontentproxy.cpp index 0325763fc..129f6fe5b 100644 --- a/src/chatlog/chatlinecontentproxy.cpp +++ b/src/chatlog/chatlinecontentproxy.cpp @@ -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(width*widthPercent), widthMin)); diff --git a/src/chatlog/chatlinecontentproxy.h b/src/chatlog/chatlinecontentproxy.h index da435fd91..b73fb126a 100644 --- a/src/chatlog/chatlinecontentproxy.h +++ b/src/chatlog/chatlinecontentproxy.h @@ -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; diff --git a/src/chatlog/content/filetransferwidget.cpp b/src/chatlog/content/filetransferwidget.cpp index 70200d7da..553d68c23 100644 --- a/src/chatlog/content/filetransferwidget.cpp +++ b/src/chatlog/content/filetransferwidget.cpp @@ -22,8 +22,7 @@ #include #include -#include -#include +#include #include #include #include @@ -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) diff --git a/src/chatlog/content/filetransferwidget.h b/src/chatlog/content/filetransferwidget.h index 90a3e2eee..34c0d7581 100644 --- a/src/chatlog/content/filetransferwidget.h +++ b/src/chatlog/content/filetransferwidget.h @@ -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(); diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 3d3349edb..99b481170 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -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(msg->getContent(1)); - if(tfWidget) - tfWidget->acceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxID())); + ChatLineContentProxy* proxy = dynamic_cast(msg->getContent(1)); + if(proxy) + { + FileTransferWidget* tfWidget = dynamic_cast(proxy->getWidget()); + + if(tfWidget) + tfWidget->autoAcceptTransfer(Settings::getInstance().getAutoAcceptDir(f->getToxID())); + } } }