File indexing completed on 2024-05-12 04:43:21

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 by Adam Pigg (adam@piggz.co.uk)
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2.1 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KoSimpleOdsDocument.h"
0021 
0022 #include <KoStore.h>
0023 #include <KoOdfWriteStore.h>
0024 #include <KoXmlWriter.h>
0025 #include "KoSimpleOdsSheet.h"
0026 
0027 KoSimpleOdsDocument::KoSimpleOdsDocument()
0028 {
0029     m_store = 0;
0030 }
0031 
0032 KoSimpleOdsDocument::~KoSimpleOdsDocument()
0033 {
0034     delete m_store;
0035     qDeleteAll(m_worksheets);
0036 }
0037 
0038 void KoSimpleOdsDocument::addSheet(KoSimpleOdsSheet* sheet)
0039 {
0040     if (!m_worksheets.contains(sheet)) {
0041         m_worksheets.append(sheet);
0042     }
0043 }
0044 
0045 QFile::FileError KoSimpleOdsDocument::saveDocument(const QString& path)
0046 {
0047     // create output store
0048     delete m_store;
0049     m_store = KoStore::createStore(path, KoStore::Write,
0050                                     "application/vnd.oasis.opendocument.spreadsheet", KoStore::Zip);
0051     if (!m_store) {
0052         kreportWarning() << "Couldn't open the requested file.";
0053         return QFile::OpenError;
0054     }
0055 
0056     KoOdfWriteStore oasisStore(m_store);
0057     //KoXmlWriter* manifestWriter = oasisStore.manifestWriter("application/vnd.oasis.opendocument.spreadsheet");
0058 
0059     if (!createContent(&oasisStore)) {
0060         delete m_store;
0061     m_store = 0;
0062         return QFile::WriteError;
0063     }
0064 
0065     delete m_store;
0066     m_store = 0;
0067     return QFile::NoError;
0068 
0069 }
0070 
0071 // Writes the spreadsheet content into the content.xml
0072 bool KoSimpleOdsDocument::createContent(KoOdfWriteStore* store)
0073 {
0074     KoXmlWriter* bodyWriter = store->bodyWriter();
0075     KoXmlWriter* contentWriter = store->contentWriter();
0076     KoXmlWriter* manifestWriter = store->manifestWriter("application/vnd.oasis.opendocument.spreadsheet");
0077 
0078     bool ok = bodyWriter && contentWriter && manifestWriter;
0079     if (!ok) {
0080         kreportWarning() << "Bad things happened";
0081     }
0082     if (ok) {
0083         // OpenDocument spec requires the manifest to include a list of the files in this package
0084         manifestWriter->addManifestEntry("content.xml",  "text/xml");
0085 
0086         //! @todo this is dummy and hardcoded, replace with real font names
0087         contentWriter->startElement("office:font-face-decls");
0088         contentWriter->startElement("style:font-face");
0089         contentWriter->addAttribute("style:name", "Arial");
0090         contentWriter->addAttribute("svg:font-family", "Arial");
0091         contentWriter->endElement(); // style:font-face
0092         contentWriter->startElement("style:font-face");
0093         contentWriter->addAttribute("style:name", "Times New Roman");
0094         contentWriter->addAttribute("svg:font-family", "&apos;Times New Roman&apos;");
0095         contentWriter->endElement(); // style:font-face
0096         contentWriter->endElement(); // office:font-face-decls
0097 
0098         // office:body
0099         bodyWriter->startElement("office:body");
0100         foreach(KoSimpleOdsSheet *sheet, m_worksheets) {
0101             bodyWriter->startElement("office:spreadsheet");
0102             sheet->saveSheet(bodyWriter);
0103             bodyWriter->endElement();
0104         }
0105         bodyWriter->endElement();  // office:body
0106     }
0107     if (!store->closeContentWriter()) { // always call
0108         ok = false;
0109     }
0110     if (!store->closeManifestWriter()) { // always call
0111         ok = false;
0112     }
0113     return ok;
0114 }