mirror of
https://bitbucket.org/smil3y/kde-extraapps.git
synced 2025-02-24 02:42:52 +00:00
gwenview: save files to temporary files and then move them
fixes saving in some cases Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
2088b65d44
commit
42e3ab6a4d
3 changed files with 23 additions and 38 deletions
|
@ -79,9 +79,9 @@ Document::LoadingState DocumentLoadedImpl::loadingState() const
|
||||||
return Document::Loaded;
|
return Document::Loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DocumentLoadedImpl::saveInternal(QIODevice* device, const QByteArray& format)
|
bool DocumentLoadedImpl::saveInternal(const QString& filePath, const QByteArray& format)
|
||||||
{
|
{
|
||||||
QImageWriter writer(device, format);
|
QImageWriter writer(filePath, format);
|
||||||
bool ok = writer.write(document()->image());
|
bool ok = writer.write(document()->image());
|
||||||
if (ok) {
|
if (ok) {
|
||||||
setDocumentFormat(format);
|
setDocumentFormat(format);
|
||||||
|
|
|
@ -58,7 +58,7 @@ public:
|
||||||
//
|
//
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool saveInternal(QIODevice* device, const QByteArray& format);
|
virtual bool saveInternal(const QString& filePath, const QByteArray& format);
|
||||||
|
|
||||||
// AbstractDocumentEditor
|
// AbstractDocumentEditor
|
||||||
virtual void setImage(const QImage&);
|
virtual void setImage(const QImage&);
|
||||||
|
|
|
@ -48,8 +48,7 @@ struct SaveJobPrivate
|
||||||
KUrl mOldUrl;
|
KUrl mOldUrl;
|
||||||
KUrl mNewUrl;
|
KUrl mNewUrl;
|
||||||
QByteArray mFormat;
|
QByteArray mFormat;
|
||||||
QScopedPointer<KTemporaryFile> mTemporaryFile;
|
QString mTemporaryFile;
|
||||||
QScopedPointer<KSaveFile> mSaveFile;
|
|
||||||
QScopedPointer<QFutureWatcher<void> > mInternalSaveWatcher;
|
QScopedPointer<QFutureWatcher<void> > mInternalSaveWatcher;
|
||||||
|
|
||||||
bool mKillReceived;
|
bool mKillReceived;
|
||||||
|
@ -73,8 +72,8 @@ SaveJob::~SaveJob()
|
||||||
|
|
||||||
void SaveJob::saveInternal()
|
void SaveJob::saveInternal()
|
||||||
{
|
{
|
||||||
if (!d->mImpl->saveInternal(d->mSaveFile.data(), d->mFormat)) {
|
if (!d->mImpl->saveInternal(d->mTemporaryFile, d->mFormat)) {
|
||||||
d->mSaveFile->abort();
|
QFile::remove(d->mTemporaryFile);
|
||||||
setError(UserDefinedError + 2);
|
setError(UserDefinedError + 2);
|
||||||
setErrorText(d->mImpl->document()->errorString());
|
setErrorText(d->mImpl->document()->errorString());
|
||||||
}
|
}
|
||||||
|
@ -85,26 +84,21 @@ void SaveJob::doStart()
|
||||||
if (d->mKillReceived) {
|
if (d->mKillReceived) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QString fileName;
|
|
||||||
|
|
||||||
if (d->mNewUrl.isLocalFile()) {
|
{
|
||||||
fileName = d->mNewUrl.toLocalFile();
|
QScopedPointer<KTemporaryFile> temporaryFile(new KTemporaryFile());
|
||||||
} else {
|
temporaryFile->setAutoRemove(true);
|
||||||
d->mTemporaryFile.reset(new KTemporaryFile);
|
temporaryFile->setSuffix(QString::fromLatin1(".%1").arg(d->mFormat.constData()));
|
||||||
d->mTemporaryFile->setAutoRemove(true);
|
|
||||||
d->mTemporaryFile->open();
|
|
||||||
fileName = d->mTemporaryFile->fileName();
|
|
||||||
}
|
|
||||||
|
|
||||||
d->mSaveFile.reset(new KSaveFile(fileName));
|
if (!temporaryFile->open()) {
|
||||||
|
KUrl dirUrl = d->mNewUrl;
|
||||||
if (!d->mSaveFile->open()) {
|
dirUrl.setFileName(QString());
|
||||||
KUrl dirUrl = d->mNewUrl;
|
setError(UserDefinedError + 1);
|
||||||
dirUrl.setFileName(QString());
|
setErrorText(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl.pathOrUrl()));
|
||||||
setError(UserDefinedError + 1);
|
emitResult();
|
||||||
setErrorText(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl.pathOrUrl()));
|
return;
|
||||||
emitResult();
|
}
|
||||||
return;
|
d->mTemporaryFile = temporaryFile->fileName();
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<void> future = QtConcurrent::run(this, &SaveJob::saveInternal);
|
QFuture<void> future = QtConcurrent::run(this, &SaveJob::saveInternal);
|
||||||
|
@ -125,19 +119,10 @@ void SaveJob::finishSave()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!d->mSaveFile->finalize()) {
|
// whether to overwite has already been asked for
|
||||||
setErrorText(i18nc("@info", "Could not overwrite file, check that you have the necessary rights to write in <filename>%1</filename>.", d->mNewUrl.pathOrUrl()));
|
KIO::Job* job = KIO::move(KUrl::fromPath(d->mTemporaryFile), d->mNewUrl, KIO::Overwrite);
|
||||||
setError(UserDefinedError + 3);
|
job->ui()->setWindow(KApplication::kApplication()->activeWindow());
|
||||||
return;
|
addSubjob(job);
|
||||||
}
|
|
||||||
|
|
||||||
if (d->mNewUrl.isLocalFile()) {
|
|
||||||
emitResult();
|
|
||||||
} else {
|
|
||||||
KIO::Job* job = KIO::copy(KUrl::fromPath(d->mTemporaryFile->fileName()), d->mNewUrl);
|
|
||||||
job->ui()->setWindow(KApplication::kApplication()->activeWindow());
|
|
||||||
addSubjob(job);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SaveJob::slotResult(KJob* job)
|
void SaveJob::slotResult(KJob* job)
|
||||||
|
|
Loading…
Add table
Reference in a new issue