Warning, file /office/calligra/libs/text/KoTextDrag.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-2008 Thorsten Zachmann <zachmann@kde.org>
0003  * Copyright (C) 2008 Pierre Stirnweiss \pierre.stirnweiss_calligra@gadz.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoTextDrag.h"
0022 
0023 #include <QBuffer>
0024 #include <QByteArray>
0025 #include <QMimeData>
0026 #include <QString>
0027 
0028 #include <KoStore.h>
0029 #include <KoGenStyles.h>
0030 #include <KoGenChanges.h>
0031 #include <KoOdfWriteStore.h>
0032 #include <KoXmlWriter.h>
0033 #include <KoDocumentBase.h>
0034 #include <KoEmbeddedDocumentSaver.h>
0035 #include "KoShapeSavingContext.h"
0036 #include "KoStyleManager.h"
0037 #include <opendocument/KoTextSharedSavingData.h>
0038 
0039 #include "KoTextOdfSaveHelper.h"
0040 
0041 #ifdef SHOULD_BUILD_RDF
0042 #include "KoTextRdfCore.h"
0043 #endif
0044 
0045 #include "TextDebug.h"
0046 
0047 KoTextDrag::KoTextDrag()
0048         : m_mimeData(0)
0049 {
0050 }
0051 
0052 KoTextDrag::~KoTextDrag()
0053 {
0054     if (m_mimeData == 0) {
0055         delete m_mimeData;
0056     }
0057 }
0058 
0059 bool KoTextDrag::setOdf(const char * mimeType, KoTextOdfSaveHelper &helper)
0060 {
0061     QBuffer buffer;
0062     QScopedPointer<KoStore> store(KoStore::createStore(&buffer, KoStore::Write, mimeType));
0063     Q_ASSERT(store);
0064     Q_ASSERT(!store->bad());
0065 
0066     KoOdfWriteStore odfStore(store.data());
0067     KoEmbeddedDocumentSaver embeddedSaver;
0068 
0069     KoXmlWriter* manifestWriter = odfStore.manifestWriter(mimeType);
0070     KoXmlWriter* contentWriter = odfStore.contentWriter();
0071 
0072     if (!contentWriter) {
0073         return false;
0074     }
0075 
0076     KoGenStyles mainStyles;
0077     KoXmlWriter *bodyWriter = odfStore.bodyWriter();
0078     KoShapeSavingContext * context = helper.context(bodyWriter, mainStyles, embeddedSaver);
0079     KoGenChanges changes;
0080 
0081     KoSharedSavingData *sharedData = context->sharedData(KOTEXT_SHARED_SAVING_ID);
0082     KoTextSharedSavingData *textSharedData = 0;
0083     if (sharedData) {
0084         textSharedData = dynamic_cast<KoTextSharedSavingData *>(sharedData);
0085     }
0086 
0087     if (!textSharedData) {
0088         textSharedData = new KoTextSharedSavingData();
0089         textSharedData->setGenChanges(changes);
0090         if (!sharedData) {
0091             context->addSharedData(KOTEXT_SHARED_SAVING_ID, textSharedData);
0092         } else {
0093             warnText << "A different type of sharedData was found under the" << KOTEXT_SHARED_SAVING_ID;
0094             Q_ASSERT(false);
0095         }
0096     }
0097 #ifdef SHOULD_BUILD_RDF
0098     debugText << "helper.model:" << helper.rdfModel();
0099     textSharedData->setRdfModel(helper.rdfModel());
0100 #endif
0101     if (!helper.writeBody()) {
0102         return false;
0103     }
0104     // Save the named styles that was referred to by the copied text
0105     if (KoStyleManager *styleManager = helper.styleManager()) {
0106         styleManager->saveReferredStylesToOdf(*context);
0107     }
0108 
0109     mainStyles.saveOdfStyles(KoGenStyles::DocumentAutomaticStyles, contentWriter);
0110     changes.saveOdfChanges(contentWriter, false);
0111 
0112     odfStore.closeContentWriter();
0113 
0114     //add manifest line for content.xml
0115     manifestWriter->addManifestEntry("content.xml", "text/xml");
0116 
0117     debugText << "testing to see if we should add rdf to odf file?";
0118 
0119 #ifdef SHOULD_BUILD_RDF
0120     debugText << "helper has model" << ( helper.rdfModel() != 0 );
0121     // RDF: Copy relevant RDF to output ODF
0122     if (QSharedPointer<Soprano::Model> m = helper.rdfModel()) {
0123         debugText << "rdf model size:" << m->statementCount();
0124         KoTextRdfCore::createAndSaveManifest(m, textSharedData->getRdfIdMapping(),
0125                                              store.data(), manifestWriter);
0126     }
0127 #endif
0128 
0129     if (!mainStyles.saveOdfStylesDotXml(store.data(), manifestWriter)) {
0130         return false;
0131     }
0132 
0133     if (!context->saveDataCenter(store.data(), manifestWriter)) {
0134         debugText << "save data centers failed";
0135         return false;
0136     }
0137 
0138     // Save embedded objects
0139     KoDocumentBase::SavingContext documentContext(odfStore, embeddedSaver);
0140     if (!embeddedSaver.saveEmbeddedDocuments(documentContext)) {
0141         debugText << "save embedded documents failed";
0142         return false;
0143     }
0144 
0145     // Write out manifest file
0146     if (!odfStore.closeManifestWriter()) {
0147         return false;
0148     }
0149 
0150     store.reset();
0151     setData(mimeType, buffer.buffer());
0152 
0153     return true;
0154 }
0155 
0156 void KoTextDrag::setData(const QString & mimeType, const QByteArray & data)
0157 {
0158     if (m_mimeData == 0) {
0159         m_mimeData = new QMimeData();
0160     }
0161     m_mimeData->setData(mimeType, data);
0162 }
0163 
0164 QMimeData * KoTextDrag::takeMimeData()
0165 {
0166     QMimeData * mimeData = m_mimeData;
0167     m_mimeData = 0;
0168     return mimeData;
0169 }