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

Check for writability for Qr saves

This commit is contained in:
Dubslow 2015-03-16 16:41:59 -05:00
parent ad09980ce7
commit e96211199d
No known key found for this signature in database
GPG Key ID: 3DB8E05315C220AA
5 changed files with 30 additions and 43 deletions

View File

@ -17,6 +17,7 @@
#include "filetransferwidget.h"
#include "ui_filetransferwidget.h"
#include "src/nexus.h"
#include "src/core.h"
#include "src/misc/style.h"
#include "src/widget/widget.h"
@ -113,7 +114,7 @@ void FileTransferWidget::autoAcceptTransfer(const QString &path)
//Do not automatically accept the file-transfer if the path is not writable.
//The user can still accept it manually.
if(isFilePathWritable(filepath))
if (Nexus::isFilePathWritable(filepath))
Core::getInstance()->acceptFileRecvRequest(fileInfo.friendId, fileInfo.fileNum, filepath);
else
qDebug() << "Warning: Cannot write to " << filepath;
@ -125,7 +126,7 @@ void FileTransferWidget::acceptTransfer(const QString &filepath)
return;
//test if writable
if(!isFilePathWritable(filepath))
if(!Nexus::isFilePathWritable(filepath))
{
QMessageBox::warning(0,
tr("Location not writable","Title of permissions popup"),
@ -164,14 +165,6 @@ void FileTransferWidget::setButtonColor(const QColor &c)
}
}
bool FileTransferWidget::isFilePathWritable(const QString &filepath) const
{
QFile tmp(filepath);
bool writable = tmp.open(QIODevice::WriteOnly);
tmp.remove();
return writable;
}
bool FileTransferWidget::drawButtonAreaNeeded() const
{
return (ui->bottomButton->isVisible() || ui->topButton->isVisible()) &&

View File

@ -57,7 +57,6 @@ protected:
void setBackgroundColor(const QColor& c, bool whiteFont);
void setButtonColor(const QColor& c);
bool isFilePathWritable(const QString& filepath) const;
bool drawButtonAreaNeeded() const;
virtual void paintEvent(QPaintEvent*);

View File

@ -169,3 +169,11 @@ QString Nexus::getSupportedImageFilter()
res += QString("*.%1 ").arg(QString(type));
return tr("Images (%1)", "filetype filter").arg(res.left(res.size()-1));
}
bool Nexus::isFilePathWritable(const QString& filepath)
{
QFile tmp(filepath);
bool writable = tmp.open(QIODevice::WriteOnly);
tmp.remove();
return writable;
}

View File

@ -23,6 +23,7 @@ public:
static AndroidGUI* getAndroidGUI(); ///< Will return 0 if not started
static Widget* getDesktopGUI(); ///< Will return 0 if not started
static QString getSupportedImageFilter();
static bool isFilePathWritable(const QString& filepath); // WARNING: Tests by brute force, i.e. removes the file in question
private:
explicit Nexus(QObject *parent = 0);

View File

@ -189,14 +189,14 @@ void ProfileForm::onAvatarClicked()
file.open(QIODevice::ReadOnly);
if (!file.isOpen())
{
QMessageBox::critical(this, tr("Error"), tr("Unable to open this file"));
GUI::showError(tr("Error"), tr("Unable to open this file"));
return;
}
QPixmap pic;
if (!pic.loadFromData(file.readAll()))
{
QMessageBox::critical(this, tr("Error"), tr("Unable to read this image"));
GUI::showError(tr("Error"), tr("Unable to read this image"));
return;
}
@ -217,7 +217,7 @@ void ProfileForm::onAvatarClicked()
if (bytes.size() >= TOX_AVATAR_MAX_DATA_LENGTH)
{
QMessageBox::critical(this, tr("Error"), tr("This image is too big"));
GUI::showError(tr("Error"), tr("This image is too big"));
return;
}
@ -229,7 +229,7 @@ void ProfileForm::onLoadClicked()
if (bodyUI->profiles->currentText() != Settings::getInstance().getCurrentProfile())
{
if (Core::getInstance()->anyActiveCalls())
QMessageBox::warning(this, tr("Call active", "popup title"),
GUI::showWarning(tr("Call active", "popup title"),
tr("You can't switch profiles while a call is active!", "popup text"));
else
emit Widget::getInstance()->changeProfile(bodyUI->profiles->currentText());
@ -273,20 +273,13 @@ void ProfileForm::onExportClicked()
tr("Tox save file (*.tox)", "save dialog filter"));
if (!path.isEmpty())
{
bool success;
if (QFile::exists(path))
if (!Nexus::isFilePathWritable(path))
{
// should we popup a warning?
success = QFile::remove(path);
if (!success)
{
QMessageBox::warning(this, tr("Failed to remove file"), tr("The file you chose to overwrite could not be removed first."));
return;
}
GUI::showWarning(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;
}
success = QFile::copy(QDir(Settings::getSettingsDirPath()).filePath(current), path);
if (!success)
QMessageBox::warning(this, tr("Failed to copy file"), tr("The file you chose could not be written to."));
if (!QFile::copy(QDir(Settings::getSettingsDirPath()).filePath(current), path))
GUI::showWarning(tr("Failed to copy file"), tr("The file you chose could not be written to."));
}
}
@ -294,7 +287,7 @@ void ProfileForm::onDeleteClicked()
{
if (Settings::getInstance().getCurrentProfile() == bodyUI->profiles->currentText())
{
QMessageBox::warning(this, tr("Profile currently loaded","current profile deletion warning title"), tr("This profile is currently in use. Please load a different profile before deleting this one.","current profile deletion warning text"));
GUI::showWarning(tr("Profile currently loaded","current profile deletion warning title"), tr("This profile is currently in use. Please load a different profile before deleting this one.","current profile deletion warning text"));
}
else
{
@ -329,9 +322,8 @@ void ProfileForm::onImportClicked()
if (info.suffix() != "tox")
{
QMessageBox::warning(this,
tr("Ignoring non-Tox file", "popup title"),
tr("Warning: you've chosen a file that is not a Tox save file; ignoring.", "popup text"));
GUI::showWarning(tr("Ignoring non-Tox file", "popup title"),
tr("Warning: you've chosen a file that is not a Tox save file; ignoring.", "popup text"));
return;
}
@ -385,22 +377,16 @@ void ProfileForm::on_saveQr_clicked()
{
QString current = bodyUI->profiles->currentText() + ".png";
QString path = QFileDialog::getSaveFileName(0, tr("Save", "save qr image"),
QDir::home().filePath(current),
QDir::home().filePath(current),
tr("Save QrCode (*.png)", "save dialog filter"));
if (!path.isEmpty())
{
bool success;
if (QFile::exists(path))
if (!Nexus::isFilePathWritable(path))
{
success = QFile::remove(path);
if (!success)
{
QMessageBox::warning(this, tr("Failed to remove file"), tr("The file you chose to overwrite could not be removed first."));
return;
}
GUI::showWarning(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;
}
success = qr->saveImage(path);
if (!success)
QMessageBox::warning(this, tr("Failed to copy file"), tr("The file you chose could not be written to."));
if (!qr->saveImage(path))
GUI::showWarning(tr("Failed to copy file"), tr("The file you chose could not be written to."));
}
}