Warning, file /office/calligra/libs/flake/KoDrag.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) 2009 Thomas Zander <zander@kde.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 "KoDrag.h"
0022 #include "KoDragOdfSaveHelper.h"
0023 
0024 #include <QApplication>
0025 #include <QBuffer>
0026 #include <QByteArray>
0027 #include <QClipboard>
0028 #include <QMimeData>
0029 #include <QString>
0030 
0031 #include <FlakeDebug.h>
0032 
0033 #include <KoStore.h>
0034 #include <KoGenStyles.h>
0035 #include <KoOdfWriteStore.h>
0036 #include <KoXmlWriter.h>
0037 #include <KoDocumentBase.h>
0038 #include <KoEmbeddedDocumentSaver.h>
0039 #include "KoShapeSavingContext.h"
0040 
0041 class KoDragPrivate {
0042 public:
0043     KoDragPrivate() : mimeData(0) { }
0044     ~KoDragPrivate() { delete mimeData; }
0045     QMimeData *mimeData;
0046 };
0047 
0048 KoDrag::KoDrag()
0049     : d(new KoDragPrivate())
0050 {
0051 }
0052 
0053 KoDrag::~KoDrag()
0054 {
0055     delete d;
0056 }
0057 
0058 bool KoDrag::setOdf(const char *mimeType, KoDragOdfSaveHelper &helper)
0059 {
0060     struct Finally {
0061         Finally(KoStore *s) : store(s) { }
0062         ~Finally() {
0063             delete store;
0064         }
0065         KoStore *store;
0066     };
0067 
0068     QBuffer buffer;
0069     KoStore *store = KoStore::createStore(&buffer, KoStore::Write, mimeType);
0070     Finally finally(store); // delete store when we exit this scope
0071     Q_ASSERT(store);
0072     Q_ASSERT(!store->bad());
0073 
0074     KoOdfWriteStore odfStore(store);
0075     KoEmbeddedDocumentSaver embeddedSaver;
0076 
0077     KoXmlWriter *manifestWriter = odfStore.manifestWriter(mimeType);
0078     KoXmlWriter *contentWriter = odfStore.contentWriter();
0079 
0080     if (!contentWriter) {
0081         return false;
0082     }
0083 
0084     KoGenStyles mainStyles;
0085     KoXmlWriter *bodyWriter = odfStore.bodyWriter();
0086     KoShapeSavingContext *context = helper.context(bodyWriter, mainStyles, embeddedSaver);
0087 
0088     if (!helper.writeBody()) {
0089         return false;
0090     }
0091 
0092     mainStyles.saveOdfStyles(KoGenStyles::DocumentAutomaticStyles, contentWriter);
0093 
0094     odfStore.closeContentWriter();
0095 
0096     //add manifest line for content.xml
0097     manifestWriter->addManifestEntry("content.xml", "text/xml");
0098 
0099 
0100     if (!mainStyles.saveOdfStylesDotXml(store, manifestWriter)) {
0101         return false;
0102     }
0103 
0104     if (!context->saveDataCenter(store, manifestWriter)) {
0105         debugFlake << "save data centers failed";
0106         return false;
0107     }
0108 
0109     // Save embedded objects
0110     KoDocumentBase::SavingContext documentContext(odfStore, embeddedSaver);
0111     if (!embeddedSaver.saveEmbeddedDocuments(documentContext)) {
0112         debugFlake << "save embedded documents failed";
0113         return false;
0114     }
0115 
0116     // Write out manifest file
0117     if (!odfStore.closeManifestWriter()) {
0118         return false;
0119     }
0120 
0121     delete store; // make sure the buffer if fully flushed.
0122     finally.store = 0;
0123     setData(mimeType, buffer.buffer());
0124 
0125     return true;
0126 }
0127 
0128 void KoDrag::setData(const QString &mimeType, const QByteArray &data)
0129 {
0130     if (d->mimeData == 0) {
0131         d->mimeData = new QMimeData();
0132     }
0133     d->mimeData->setData(mimeType, data);
0134 }
0135 
0136 void KoDrag::addToClipboard()
0137 {
0138     if (d->mimeData) {
0139         QApplication::clipboard()->setMimeData(d->mimeData);
0140         d->mimeData = 0;
0141     }
0142 }
0143 
0144 QMimeData * KoDrag::mimeData()
0145 {
0146     QMimeData *mimeData = d->mimeData;
0147     d->mimeData = 0;
0148     return mimeData;
0149 }