1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/* This file is part of the KDE project
* Copyright (C) 2007-2008 Thorsten Zachmann <zachmann@kde.org>
* Copyright (C) 2009 Thomas Zander <zander@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoDrag.h"
#include "KoDragOdfSaveHelper.h"
#include <QApplication>
#include <QBuffer>
#include <QByteArray>
#include <QClipboard>
#include <QMimeData>
#include <QString>
#include <kdebug.h>
#include <KoStore.h>
#include <KoGenStyles.h>
#include <KoOdfWriteStore.h>
#include <KoXmlWriter.h>
#include <KoOdfDocument.h>
#include <KoEmbeddedDocumentSaver.h>
#include "KoShapeSavingContext.h"
class KoDragPrivate {
public:
KoDragPrivate() : mimeData(0) { }
~KoDragPrivate() { delete mimeData; }
QMimeData *mimeData;
};
KoDrag::KoDrag()
: d(new KoDragPrivate())
{
}
KoDrag::~KoDrag()
{
delete d;
}
bool KoDrag::setOdf(const char *mimeType, KoDragOdfSaveHelper &helper)
{
struct Finally {
Finally(KoStore *s) : store(s) { }
~Finally() {
delete store;
}
KoStore *store;
};
QBuffer buffer;
KoStore *store = KoStore::createStore(&buffer, KoStore::Write, mimeType);
Finally finally(store); // delete store when we exit this scope
Q_ASSERT(store);
Q_ASSERT(!store->bad());
KoOdfWriteStore odfStore(store);
KoEmbeddedDocumentSaver embeddedSaver;
KoXmlWriter *manifestWriter = odfStore.manifestWriter(mimeType);
KoXmlWriter *contentWriter = odfStore.contentWriter();
if (!contentWriter) {
return false;
}
KoGenStyles mainStyles;
KoXmlWriter *bodyWriter = odfStore.bodyWriter();
KoShapeSavingContext *context = helper.context(bodyWriter, mainStyles, embeddedSaver);
if (!helper.writeBody()) {
return false;
}
mainStyles.saveOdfStyles(KoGenStyles::DocumentAutomaticStyles, contentWriter);
odfStore.closeContentWriter();
//add manifest line for content.xml
manifestWriter->addManifestEntry("content.xml", "text/xml");
if (!mainStyles.saveOdfStylesDotXml(store, manifestWriter)) {
return false;
}
if (!context->saveDataCenter(store, manifestWriter)) {
kDebug(30006) << "save data centers failed";
return false;
}
// Save embedded objects
KoOdfDocument::SavingContext documentContext(odfStore, embeddedSaver);
if (!embeddedSaver.saveEmbeddedDocuments(documentContext)) {
kDebug(30006) << "save embedded documents failed";
return false;
}
// Write out manifest file
if (!odfStore.closeManifestWriter()) {
return false;
}
delete store; // make sure the buffer if fully flushed.
finally.store = 0;
setData(mimeType, buffer.buffer());
return true;
}
void KoDrag::setData(const QString &mimeType, const QByteArray &data)
{
if (d->mimeData == 0) {
d->mimeData = new QMimeData();
}
d->mimeData->setData(mimeType, data);
}
void KoDrag::addToClipboard()
{
if (d->mimeData) {
QApplication::clipboard()->setMimeData(d->mimeData);
d->mimeData = 0;
}
}
QMimeData * KoDrag::mimeData()
{
QMimeData *mimeData = d->mimeData;
d->mimeData = 0;
return mimeData;
}
|