1 /* This file is part of the KDE project
2    Copyright (C) 2005 David Faure <faure@kde.org>
3    Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.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 #ifndef KOODFWRITESTORE_H
21 #define KOODFWRITESTORE_H
22 
23 class QIODevice;
24 class KoXmlWriter;
25 class KoStore;
26 
27 /**
28  * Helper class around KoStore for writing out ODF files.
29  * This class helps solving the problem that automatic styles must be before
30  * the body, but it's easier to iterate over the application's objects only
31  * once. So we open a KoXmlWriter into a memory buffer, write the body into it,
32  * collect automatic styles while doing that, write out automatic styles,
33  * and then copy the body XML from the buffer into the real KoXmlWriter.
34  *
35  * The typical use of this class is therefore:
36  *   - write body into bodyWriter() and collect auto styles
37  *   - write auto styles into contentWriter()
38  *   - call closeContentWriter()
39  *   - write other files into the store (styles.xml, settings.xml etc.)
40  *
41  *
42  * TODO: maybe we could encapsulate a bit more things, to e.g. handle
43  * adding manifest entries automatically.
44  *
45  * @author: David Faure <faure@kde.org>
46  */
47 #include "koodf_export.h"
48 
49 class KOODF_EXPORT KoOdfWriteStore
50 {
51 public:
52     /// @param store recontents the property of the caller
53     explicit KoOdfWriteStore(KoStore *store);
54 
55     ~KoOdfWriteStore();
56 
57     /**
58      * Return an XML writer for saving Oasis XML into the device @p dev,
59      * including the XML processing instruction,
60      * and the root element with all its namespaces.
61      * You can add more namespaces afterwards with addAttribute.
62      *
63      * @param dev the device into which the XML will be written.
64      * @param rootElementName the tag name of the root element.
65      *    This is either office:document, office:document-content,
66      *    office:document-styles, office:document-meta or office:document-settings
67      * @return the KoXmlWriter instance. It becomes owned by the caller, which
68      * must delete it at some point.
69      *
70      * Once done with writing the contents of the root element, you
71      * will need to call endElement(); endDocument(); before destroying the KoXmlWriter.
72      */
73     static KoXmlWriter *createOasisXmlWriter(QIODevice *dev, const char *rootElementName);
74 
75     KoStore *store() const;
76 
77     /**
78      * Open contents.xml for writing and return the KoXmlWriter
79      */
80     KoXmlWriter *contentWriter();
81 
82     /**
83      * Open another KoXmlWriter for writing out the contents
84      * into a temporary file, to collect automatic styles while doing that.
85      */
86     KoXmlWriter *bodyWriter();
87 
88     /**
89      * This will copy the body into the content writer,
90      * delete the bodyWriter and the contentWriter, and then
91      * close contents.xml.
92      */
93     bool closeContentWriter();
94 
95     // For other files in the store, use open/addManifestEntry/KoStoreDevice/createOasisXmlWriter/close
96 
97     /**
98      * Create and return a manifest writer. It will write to a memory buffer.
99      */
100     KoXmlWriter *manifestWriter(const char *mimeType);
101 
102     /**
103      * Return the manifest writer. It has to be created by manifestWriter( mimeType ) before you can use
104      * this function.
105      */
106     KoXmlWriter *manifestWriter();
107 
108     /**
109      * Close the manifest writer.
110      * @param writeMainfest If true then on closing we also write the contents to the manifest.xml else
111      *    we only close the writer and don't write the content to the manifest.xml
112      */
113     bool closeManifestWriter(bool writeMainfest = true);
114 
115 private:
116     struct Private;
117     Private * const d;
118 };
119 
120 #endif /* KOODFWRITESTORE_H */
121