1 /* This file is part of the KDE project
2 * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
3 * Copyright (C) 2011 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "KoTextOdfSaveHelper.h"
22
23 #include <KoXmlWriter.h>
24 #include <KoOdf.h>
25 #include "KoTextWriter.h"
26 #include <KoShapeSavingContext.h>
27 #include <KoTextDocument.h>
28
29 #include <QTextDocument>
30
31 struct Q_DECL_HIDDEN KoTextOdfSaveHelper::Private {
PrivateKoTextOdfSaveHelper::Private32 Private(const QTextDocument *document, int from, int to)
33 : context(0)
34 , document(document)
35 , from(from)
36 , to(to)
37 #ifdef SHOULD_BUILD_RDF
38 , rdfModel(0)
39 #endif
40 {
41 }
42
43 KoShapeSavingContext *context;
44 const QTextDocument *document;
45
46 int from;
47 int to;
48
49 #ifdef SHOULD_BUILD_RDF
50 QSharedPointer<Soprano::Model> rdfModel; //< This is so cut/paste can serialize the relevant RDF to the clipboard
51 #endif
52 };
53
54
KoTextOdfSaveHelper(const QTextDocument * document,int from,int to)55 KoTextOdfSaveHelper::KoTextOdfSaveHelper(const QTextDocument *document, int from, int to)
56 : d(new Private(document, from, to))
57 {
58 }
59
~KoTextOdfSaveHelper()60 KoTextOdfSaveHelper::~KoTextOdfSaveHelper()
61 {
62 delete d;
63 }
64
writeBody()65 bool KoTextOdfSaveHelper::writeBody()
66 {
67 if (d->to < d->from) {
68 qSwap(d->to, d->from);
69 }
70 Q_ASSERT(d->context);
71 KoXmlWriter & bodyWriter = d->context->xmlWriter();
72 bodyWriter.startElement("office:body");
73 bodyWriter.startElement(KoOdf::bodyContentElement(KoOdf::Text, true));
74
75 KoTextWriter writer(*d->context, 0);
76 writer.write(d->document, d->from, d->to);
77
78 bodyWriter.endElement(); // office:element
79 bodyWriter.endElement(); // office:body
80 return true;
81 }
82
context(KoXmlWriter * bodyWriter,KoGenStyles & mainStyles,KoEmbeddedDocumentSaver & embeddedSaver)83 KoShapeSavingContext * KoTextOdfSaveHelper::context(KoXmlWriter * bodyWriter,
84 KoGenStyles & mainStyles,
85 KoEmbeddedDocumentSaver & embeddedSaver)
86 {
87 d->context = new KoShapeSavingContext(*bodyWriter, mainStyles, embeddedSaver);
88 return d->context;
89 }
90
91 #ifdef SHOULD_BUILD_RDF
setRdfModel(QSharedPointer<Soprano::Model> m)92 void KoTextOdfSaveHelper::setRdfModel(QSharedPointer<Soprano::Model> m)
93 {
94 d->rdfModel = m;
95 }
96
rdfModel() const97 QSharedPointer<Soprano::Model> KoTextOdfSaveHelper::rdfModel() const
98 {
99 return d->rdfModel;
100 }
101 #endif
102
styleManager() const103 KoStyleManager *KoTextOdfSaveHelper::styleManager() const
104 {
105 return KoTextDocument(d->document).styleManager();
106 }
107