Check if device is opened before trying to create image handler.
Change-Id: I60f1f6890fdd73e489da4aab9928370163f55f58 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com> Reviewed-by: aavit <eirik.aavitsland@digia.com>
This commit is contained in:
parent
37f99502f9
commit
4d8a12904a
@ -245,6 +245,8 @@ class QImageWriterPrivate
|
||||
public:
|
||||
QImageWriterPrivate(QImageWriter *qq);
|
||||
|
||||
bool canWriteHelper();
|
||||
|
||||
// device
|
||||
QByteArray format;
|
||||
QIODevice *device;
|
||||
@ -282,6 +284,31 @@ QImageWriterPrivate::QImageWriterPrivate(QImageWriter *qq)
|
||||
q = qq;
|
||||
}
|
||||
|
||||
bool QImageWriterPrivate::canWriteHelper()
|
||||
{
|
||||
if (!device) {
|
||||
imageWriterError = QImageWriter::DeviceError;
|
||||
errorString = QT_TRANSLATE_NOOP(QImageWriter,
|
||||
QLatin1String("Device is not set"));
|
||||
return false;
|
||||
}
|
||||
if (!device->isOpen())
|
||||
device->open(QIODevice::WriteOnly);
|
||||
if (!device->isWritable()) {
|
||||
imageWriterError = QImageWriter::DeviceError;
|
||||
errorString = QT_TRANSLATE_NOOP(QImageWriter,
|
||||
QLatin1String("Device not writable"));
|
||||
return false;
|
||||
}
|
||||
if (!handler && (handler = createWriteHandlerHelper(device, format)) == 0) {
|
||||
imageWriterError = QImageWriter::UnsupportedFormatError;
|
||||
errorString = QT_TRANSLATE_NOOP(QImageWriter,
|
||||
QLatin1String("Unsupported image format"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
Constructs an empty QImageWriter object. Before writing, you must
|
||||
call setFormat() to set an image format, then setDevice() or
|
||||
@ -561,21 +588,15 @@ void QImageWriter::setText(const QString &key, const QString &text)
|
||||
*/
|
||||
bool QImageWriter::canWrite() const
|
||||
{
|
||||
if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
|
||||
d->imageWriterError = QImageWriter::UnsupportedFormatError;
|
||||
d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
|
||||
QLatin1String("Unsupported image format"));
|
||||
return false;
|
||||
if (QFile *file = qobject_cast<QFile *>(d->device)) {
|
||||
const bool remove = !file->isOpen() && !file->exists();
|
||||
const bool result = d->canWriteHelper();
|
||||
if (!result && remove)
|
||||
file->remove();
|
||||
return result;
|
||||
}
|
||||
if (d->device && !d->device->isOpen())
|
||||
d->device->open(QIODevice::WriteOnly);
|
||||
if (!d->device || !d->device->isWritable()) {
|
||||
d->imageWriterError = QImageWriter::DeviceError;
|
||||
d->errorString = QT_TRANSLATE_NOOP(QImageWriter,
|
||||
QLatin1String("Device not writable"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
return d->canWriteHelper();
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -50,6 +50,7 @@
|
||||
#include <QImageWriter>
|
||||
#include <QPainter>
|
||||
#include <QSet>
|
||||
#include <QTemporaryDir>
|
||||
|
||||
#ifdef Q_OS_UNIX // for geteuid()
|
||||
# include <sys/types.h>
|
||||
@ -84,6 +85,7 @@ private slots:
|
||||
void supportedMimeTypes();
|
||||
|
||||
void writeToInvalidDevice();
|
||||
void testCanWrite();
|
||||
|
||||
void supportsOption_data();
|
||||
void supportsOption();
|
||||
@ -402,6 +404,28 @@ void tst_QImageWriter::writeToInvalidDevice()
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QImageWriter::testCanWrite()
|
||||
{
|
||||
{
|
||||
// device is not set
|
||||
QImageWriter writer;
|
||||
QVERIFY(!writer.canWrite());
|
||||
QCOMPARE(writer.error(), QImageWriter::DeviceError);
|
||||
}
|
||||
|
||||
{
|
||||
// check if canWrite won't leave an empty file
|
||||
QTemporaryDir dir;
|
||||
QVERIFY(dir.isValid());
|
||||
QString fileName(dir.path() + QLatin1String("/001.garble"));
|
||||
QVERIFY(!QFileInfo(fileName).exists());
|
||||
QImageWriter writer(fileName);
|
||||
QVERIFY(!writer.canWrite());
|
||||
QCOMPARE(writer.error(), QImageWriter::UnsupportedFormatError);
|
||||
QVERIFY(!QFileInfo(fileName).exists());
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QImageWriter::supportsOption_data()
|
||||
{
|
||||
QTest::addColumn<QString>("fileName");
|
||||
|
Loading…
Reference in New Issue
Block a user