Warning, file /office/calligra/libs/odf/KoOdfPaste.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
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 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 "KoOdfPaste.h"
0021 
0022 #include <QBuffer>
0023 #include <QByteArray>
0024 #include <QMimeData>
0025 #include <QString>
0026 
0027 #include <OdfDebug.h>
0028 
0029 #include <KoStore.h>
0030 #include <KoOdfReadStore.h>
0031 #include <KoXmlReader.h>
0032 #include <KoXmlNS.h>
0033 
0034 KoOdfPaste::KoOdfPaste()
0035 {
0036 }
0037 
0038 KoOdfPaste::~KoOdfPaste()
0039 {
0040 }
0041 
0042 bool KoOdfPaste::paste(KoOdf::DocumentType documentType, const QMimeData *data)
0043 {
0044     QByteArray arr = data->data(KoOdf::mimeType(documentType));
0045     return paste(documentType, arr);
0046 }
0047 
0048 bool KoOdfPaste::paste(KoOdf::DocumentType documentType, const QByteArray &bytes)
0049 {
0050     if (bytes.isEmpty())
0051         return false;
0052 
0053     QBuffer buffer;
0054     buffer.setData(bytes);
0055     KoStore *store = KoStore::createStore(&buffer, KoStore::Read);
0056     //FIXME: Use shared_ptr or smth like these to auto delete store on return
0057     // and delete all next "delete store;".
0058 
0059     KoOdfReadStore odfStore(store); // KoOdfReadStore does not delete the store on destruction
0060 
0061     QString errorMessage;
0062     if (! odfStore.loadAndParse(errorMessage)) {
0063         warnOdf << "loading and parsing failed:" << errorMessage;
0064         delete store;
0065         return false;
0066     }
0067 
0068     KoXmlElement content = odfStore.contentDoc().documentElement();
0069     KoXmlElement realBody(KoXml::namedItemNS(content, KoXmlNS::office, "body"));
0070 
0071     if (realBody.isNull()) {
0072         warnOdf << "No body tag found";
0073         delete store;
0074         return false;
0075     }
0076 
0077     KoXmlElement body = KoXml::namedItemNS(realBody, KoXmlNS::office, KoOdf::bodyContentElement(documentType, false));
0078 
0079     if (body.isNull()) {
0080         warnOdf << "No" << KoOdf::bodyContentElement(documentType, true) << "tag found";
0081         delete store;
0082         return false;
0083     }
0084 
0085     bool retval = process(body, odfStore);
0086     delete store;
0087     return retval;
0088 }