~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/document/savejob.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
39
39
// Local
40
40
#include "documentloadedimpl.h"
41
41
 
42
 
namespace Gwenview {
43
 
 
 
42
namespace Gwenview
 
43
{
44
44
 
45
45
struct SaveJobPrivate {
46
 
        DocumentLoadedImpl* mImpl;
47
 
        KUrl mOldUrl;
48
 
        KUrl mNewUrl;
49
 
        QByteArray mFormat;
50
 
        QScopedPointer<KTemporaryFile> mTemporaryFile;
51
 
        QScopedPointer<KSaveFile> mSaveFile;
52
 
        QScopedPointer<QFutureWatcher<void> > mInternalSaveWatcher;
 
46
    DocumentLoadedImpl* mImpl;
 
47
    KUrl mOldUrl;
 
48
    KUrl mNewUrl;
 
49
    QByteArray mFormat;
 
50
    QScopedPointer<KTemporaryFile> mTemporaryFile;
 
51
    QScopedPointer<KSaveFile> mSaveFile;
 
52
    QScopedPointer<QFutureWatcher<void> > mInternalSaveWatcher;
53
53
 
54
 
        bool mKillReceived;
 
54
    bool mKillReceived;
55
55
};
56
56
 
57
 
 
58
57
SaveJob::SaveJob(DocumentLoadedImpl* impl, const KUrl& url, const QByteArray& format)
59
 
: d(new SaveJobPrivate) {
60
 
        d->mImpl = impl;
61
 
        d->mOldUrl = impl->document()->url();
62
 
        d->mNewUrl = url;
63
 
        d->mFormat = format;
64
 
        d->mKillReceived = false;
65
 
        setCapabilities(Killable);
66
 
}
67
 
 
68
 
 
69
 
SaveJob::~SaveJob() {
70
 
        delete d;
71
 
}
72
 
 
73
 
 
74
 
void SaveJob::saveInternal() {
75
 
        if (!d->mImpl->saveInternal(d->mSaveFile.data(), d->mFormat)) {
76
 
                d->mSaveFile->abort();
77
 
                setError(UserDefinedError + 2);
78
 
                setErrorText(d->mImpl->document()->errorString());
79
 
        }
80
 
}
81
 
 
82
 
 
83
 
void SaveJob::doStart() {
84
 
        if (d->mKillReceived) {
85
 
                return;
86
 
        }
87
 
        QString fileName;
88
 
 
89
 
        if (d->mNewUrl.isLocalFile()) {
90
 
                fileName = d->mNewUrl.toLocalFile();
91
 
        } else {
92
 
                d->mTemporaryFile.reset(new KTemporaryFile);
93
 
                d->mTemporaryFile->setAutoRemove(true);
94
 
                d->mTemporaryFile->open();
95
 
                fileName = d->mTemporaryFile->fileName();
96
 
        }
97
 
 
98
 
        d->mSaveFile.reset(new KSaveFile(fileName));
99
 
 
100
 
        if (!d->mSaveFile->open()) {
101
 
                KUrl dirUrl = d->mNewUrl;
102
 
                dirUrl.setFileName(QString());
103
 
                setError(UserDefinedError + 1);
104
 
                setErrorText(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl.pathOrUrl()));
105
 
                emitResult();
106
 
                return;
107
 
        }
108
 
 
109
 
        QFuture<void> future = QtConcurrent::run(this, &SaveJob::saveInternal);
110
 
        d->mInternalSaveWatcher.reset(new QFutureWatcher<void>(this));
111
 
        d->mInternalSaveWatcher->setFuture(future);
112
 
        connect(d->mInternalSaveWatcher.data(), SIGNAL(finished()), SLOT(finishSave()));
113
 
}
114
 
 
115
 
 
116
 
void SaveJob::finishSave() {
117
 
        d->mInternalSaveWatcher.reset(0);
118
 
        if (d->mKillReceived) {
119
 
                return;
120
 
        }
121
 
 
122
 
        if (error()) {
123
 
                emitResult();
124
 
                return;
125
 
        }
126
 
 
127
 
        if (!d->mSaveFile->finalize()) {
128
 
                setErrorText(i18nc("@info", "Could not overwrite file, check that you have the necessary rights to write in <filename>%1</filename>.", d->mNewUrl.pathOrUrl()));
129
 
                setError(UserDefinedError + 3);
130
 
                return;
131
 
        }
132
 
 
133
 
        if (d->mNewUrl.isLocalFile()) {
134
 
                emitResult();
135
 
        } else {
136
 
                KIO::Job* job = KIO::copy(KUrl::fromPath(d->mTemporaryFile->fileName()), d->mNewUrl);
137
 
                job->ui()->setWindow(KApplication::kApplication()->activeWindow());
138
 
                addSubjob(job);
139
 
        }
140
 
}
141
 
 
142
 
 
143
 
void SaveJob::slotResult(KJob* job) {
144
 
        DocumentJob::slotResult(job);
145
 
        if (!error()) {
146
 
                emitResult();
147
 
        }
148
 
}
149
 
 
150
 
 
151
 
KUrl SaveJob::oldUrl() const {
152
 
        return d->mOldUrl;
153
 
}
154
 
 
155
 
 
156
 
KUrl SaveJob::newUrl() const {
157
 
        return d->mNewUrl;
158
 
}
159
 
 
160
 
 
161
 
bool SaveJob::doKill() {
162
 
        d->mKillReceived = true;
163
 
        if (d->mInternalSaveWatcher) {
164
 
                d->mInternalSaveWatcher->waitForFinished();
165
 
        }
166
 
        return true;
167
 
}
168
 
 
 
58
: d(new SaveJobPrivate)
 
59
{
 
60
    d->mImpl = impl;
 
61
    d->mOldUrl = impl->document()->url();
 
62
    d->mNewUrl = url;
 
63
    d->mFormat = format;
 
64
    d->mKillReceived = false;
 
65
    setCapabilities(Killable);
 
66
}
 
67
 
 
68
SaveJob::~SaveJob()
 
69
{
 
70
    delete d;
 
71
}
 
72
 
 
73
void SaveJob::saveInternal()
 
74
{
 
75
    if (!d->mImpl->saveInternal(d->mSaveFile.data(), d->mFormat)) {
 
76
        d->mSaveFile->abort();
 
77
        setError(UserDefinedError + 2);
 
78
        setErrorText(d->mImpl->document()->errorString());
 
79
    }
 
80
}
 
81
 
 
82
void SaveJob::doStart()
 
83
{
 
84
    if (d->mKillReceived) {
 
85
        return;
 
86
    }
 
87
    QString fileName;
 
88
 
 
89
    if (d->mNewUrl.isLocalFile()) {
 
90
        fileName = d->mNewUrl.toLocalFile();
 
91
    } else {
 
92
        d->mTemporaryFile.reset(new KTemporaryFile);
 
93
        d->mTemporaryFile->setAutoRemove(true);
 
94
        d->mTemporaryFile->open();
 
95
        fileName = d->mTemporaryFile->fileName();
 
96
    }
 
97
 
 
98
    d->mSaveFile.reset(new KSaveFile(fileName));
 
99
 
 
100
    if (!d->mSaveFile->open()) {
 
101
        KUrl dirUrl = d->mNewUrl;
 
102
        dirUrl.setFileName(QString());
 
103
        setError(UserDefinedError + 1);
 
104
        setErrorText(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl.pathOrUrl()));
 
105
        emitResult();
 
106
        return;
 
107
    }
 
108
 
 
109
    QFuture<void> future = QtConcurrent::run(this, &SaveJob::saveInternal);
 
110
    d->mInternalSaveWatcher.reset(new QFutureWatcher<void>(this));
 
111
    d->mInternalSaveWatcher->setFuture(future);
 
112
    connect(d->mInternalSaveWatcher.data(), SIGNAL(finished()), SLOT(finishSave()));
 
113
}
 
114
 
 
115
void SaveJob::finishSave()
 
116
{
 
117
    d->mInternalSaveWatcher.reset(0);
 
118
    if (d->mKillReceived) {
 
119
        return;
 
120
    }
 
121
 
 
122
    if (error()) {
 
123
        emitResult();
 
124
        return;
 
125
    }
 
126
 
 
127
    if (!d->mSaveFile->finalize()) {
 
128
        setErrorText(i18nc("@info", "Could not overwrite file, check that you have the necessary rights to write in <filename>%1</filename>.", d->mNewUrl.pathOrUrl()));
 
129
        setError(UserDefinedError + 3);
 
130
        return;
 
131
    }
 
132
 
 
133
    if (d->mNewUrl.isLocalFile()) {
 
134
        emitResult();
 
135
    } else {
 
136
        KIO::Job* job = KIO::copy(KUrl::fromPath(d->mTemporaryFile->fileName()), d->mNewUrl);
 
137
        job->ui()->setWindow(KApplication::kApplication()->activeWindow());
 
138
        addSubjob(job);
 
139
    }
 
140
}
 
141
 
 
142
void SaveJob::slotResult(KJob* job)
 
143
{
 
144
    DocumentJob::slotResult(job);
 
145
    if (!error()) {
 
146
        emitResult();
 
147
    }
 
148
}
 
149
 
 
150
KUrl SaveJob::oldUrl() const
 
151
{
 
152
    return d->mOldUrl;
 
153
}
 
154
 
 
155
KUrl SaveJob::newUrl() const
 
156
{
 
157
    return d->mNewUrl;
 
158
}
 
159
 
 
160
bool SaveJob::doKill()
 
161
{
 
162
    d->mKillReceived = true;
 
163
    if (d->mInternalSaveWatcher) {
 
164
        d->mInternalSaveWatcher->waitForFinished();
 
165
    }
 
166
    return true;
 
167
}
169
168
 
170
169
} // namespace