KReport

KoSimpleOdsDocument.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2010 by Adam Pigg (adam@piggz.co.uk)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "KoSimpleOdsDocument.h"
21
22#include <KoStore.h>
23#include <KoOdfWriteStore.h>
24#include <KoXmlWriter.h>
25#include "KoSimpleOdsSheet.h"
26
27KoSimpleOdsDocument::KoSimpleOdsDocument()
28{
29 m_store = 0;
30}
31
32KoSimpleOdsDocument::~KoSimpleOdsDocument()
33{
34 delete m_store;
35 qDeleteAll(m_worksheets);
36}
37
38void KoSimpleOdsDocument::addSheet(KoSimpleOdsSheet* sheet)
39{
40 if (!m_worksheets.contains(sheet)) {
41 m_worksheets.append(sheet);
42 }
43}
44
45QFile::FileError KoSimpleOdsDocument::saveDocument(const QString& path)
46{
47 // create output store
48 delete m_store;
49 m_store = KoStore::createStore(path, KoStore::Write,
50 "application/vnd.oasis.opendocument.spreadsheet", KoStore::Zip);
51 if (!m_store) {
52 kreportWarning() << "Couldn't open the requested file.";
53 return QFile::OpenError;
54 }
55
56 KoOdfWriteStore oasisStore(m_store);
57 //KoXmlWriter* manifestWriter = oasisStore.manifestWriter("application/vnd.oasis.opendocument.spreadsheet");
58
59 if (!createContent(&oasisStore)) {
60 delete m_store;
61 m_store = 0;
62 return QFile::WriteError;
63 }
64
65 delete m_store;
66 m_store = 0;
67 return QFile::NoError;
68
69}
70
71// Writes the spreadsheet content into the content.xml
72bool KoSimpleOdsDocument::createContent(KoOdfWriteStore* store)
73{
74 KoXmlWriter* bodyWriter = store->bodyWriter();
75 KoXmlWriter* contentWriter = store->contentWriter();
76 KoXmlWriter* manifestWriter = store->manifestWriter("application/vnd.oasis.opendocument.spreadsheet");
77
78 bool ok = bodyWriter && contentWriter && manifestWriter;
79 if (!ok) {
80 kreportWarning() << "Bad things happened";
81 }
82 if (ok) {
83 // OpenDocument spec requires the manifest to include a list of the files in this package
84 manifestWriter->addManifestEntry("content.xml", "text/xml");
85
86 //! @todo this is dummy and hardcoded, replace with real font names
87 contentWriter->startElement("office:font-face-decls");
88 contentWriter->startElement("style:font-face");
89 contentWriter->addAttribute("style:name", "Arial");
90 contentWriter->addAttribute("svg:font-family", "Arial");
91 contentWriter->endElement(); // style:font-face
92 contentWriter->startElement("style:font-face");
93 contentWriter->addAttribute("style:name", "Times New Roman");
94 contentWriter->addAttribute("svg:font-family", "&apos;Times New Roman&apos;");
95 contentWriter->endElement(); // style:font-face
96 contentWriter->endElement(); // office:font-face-decls
97
98 // office:body
99 bodyWriter->startElement("office:body");
100 foreach(KoSimpleOdsSheet *sheet, m_worksheets) {
101 bodyWriter->startElement("office:spreadsheet");
102 sheet->saveSheet(bodyWriter);
103 bodyWriter->endElement();
104 }
105 bodyWriter->endElement(); // office:body
106 }
107 if (!store->closeContentWriter()) { // always call
108 ok = false;
109 }
110 if (!store->closeManifestWriter()) { // always call
111 ok = false;
112 }
113 return ok;
114}
void append(QList< T > &&value)
bool contains(const AT &value) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.