polish
[kdegraphics.git] / gwenview / lib / document / documentloadedimpl.cpp
blobeccf69ab0c1acb46690417f89a53897461b0e872
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2007 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 // Self
22 #include "documentloadedimpl.h"
24 // STL
25 #include <memory>
27 // Qt
28 #include <QByteArray>
29 #include <QImage>
30 #include <QImageWriter>
31 #include <QMatrix>
33 // KDE
34 #include <kdebug.h>
35 #include <kio/netaccess.h>
36 #include <klocale.h>
37 #include <ksavefile.h>
38 #include <ktemporaryfile.h>
39 #include <kurl.h>
41 // Local
42 #include "imageutils.h"
44 namespace Gwenview {
47 struct DocumentLoadedImplPrivate {
48 QByteArray mRawData;
52 DocumentLoadedImpl::DocumentLoadedImpl(Document* document, const QByteArray& rawData)
53 : AbstractDocumentImpl(document)
54 , d(new DocumentLoadedImplPrivate) {
55 if (document->keepRawData()) {
56 d->mRawData = rawData;
61 DocumentLoadedImpl::~DocumentLoadedImpl() {
62 delete d;
66 void DocumentLoadedImpl::init() {
70 bool DocumentLoadedImpl::isEditable() const {
71 return true;
75 bool DocumentLoadedImpl::isMetaInfoLoaded() const {
76 return true;
80 Document::LoadingState DocumentLoadedImpl::loadingState() const {
81 return Document::Loaded;
85 bool DocumentLoadedImpl::saveInternal(QIODevice* device, const QByteArray& format) {
86 QImageWriter writer(device, format);
87 bool ok = writer.write(document()->image());
88 if (ok) {
89 setDocumentFormat(format);
90 } else {
91 setDocumentErrorString(writer.errorString());
93 return ok;
97 bool DocumentLoadedImpl::save(const KUrl& url, const QByteArray& format) {
98 QString fileName;
100 // This tmp is used to save to remote urls.
101 // It's an auto_ptr, this way it's not instantiated for local urls, but
102 // for remote urls we are sure it will remove its file when we leave the
103 // function.
104 std::auto_ptr<KTemporaryFile> tmp;
106 if (url.isLocalFile()) {
107 fileName = url.path();
108 } else {
109 tmp.reset(new KTemporaryFile);
110 tmp->setAutoRemove(true);
111 tmp->open();
112 fileName = tmp->fileName();
115 KSaveFile file(fileName);
117 if (!file.open()) {
118 KUrl dirUrl = url;
119 dirUrl.setFileName(QString());
120 setDocumentErrorString(i18nc("@info", "Could not open file for writing, check that you have the necessary rights in <filename>%1</filename>.", dirUrl.pathOrUrl()));
121 return false;
124 if (!saveInternal(&file, format)) {
125 file.abort();
126 return false;
129 if (!file.finalize()) {
130 setDocumentErrorString(i18nc("@info", "Could not overwrite file, check that you have the necessary rights to write in <filename>%1</filename>.", url.pathOrUrl()));
131 return false;
134 if (!url.isLocalFile()) {
135 if (!KIO::NetAccess::upload(fileName, url, 0)) {
136 setDocumentErrorString(i18nc("@info", "Could not upload file."));
137 return false;
141 return true;
145 AbstractDocumentEditor* DocumentLoadedImpl::editor() {
146 return this;
150 void DocumentLoadedImpl::setImage(const QImage& image) {
151 setDocumentImage(image);
152 imageRectUpdated(image.rect());
156 void DocumentLoadedImpl::applyTransformation(Orientation orientation) {
157 QImage image = document()->image();
158 QMatrix matrix = ImageUtils::transformMatrix(orientation);
159 image = image.transformed(matrix);
160 setDocumentImage(image);
161 imageRectUpdated(image.rect());
165 QByteArray DocumentLoadedImpl::rawData() const {
166 return d->mRawData;
170 } // namespace