| 1 |
/* This file is part of the KDE project |
| 2 |
* SPDX-FileCopyrightText: 2004 David Faure <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
// clazy:excludeall=qstring-arg |
| 7 |
#include <KoStore.h> |
| 8 |
|
| 9 |
#include <QStringList> |
| 10 |
#include <QBuffer> |
| 11 |
#include <QMimeData> |
| 12 |
#include <QClipboard> |
| 13 |
#include <QTextBrowser> |
| 14 |
#include <QDragEnterEvent> |
| 15 |
#include <QDragMoveEvent> |
| 16 |
#include <QKeyEvent> |
| 17 |
#include <QDropEvent> |
| 18 |
#include <QApplication> |
| 19 |
#include <QCommandLineParser> |
| 20 |
|
| 21 |
class StoreDropTest : public QTextBrowser |
| 22 |
{ |
| 23 |
Q_OBJECT |
| 24 |
public: |
| 25 |
StoreDropTest(QWidget* parent); |
| 26 |
protected: |
| 27 |
virtual void contentsDragEnterEvent(QDragEnterEvent * e); |
| 28 |
virtual void contentsDragMoveEvent(QDragMoveEvent * e); |
| 29 |
virtual void contentsDropEvent(QDropEvent * e); |
| 30 |
void keyPressEvent(QKeyEvent * e) override; |
| 31 |
virtual void paste(); |
| 32 |
private: |
| 33 |
bool processMimeData(const QMimeData* mimeData); |
| 34 |
void showZipContents(QByteArray data, const QString &mimeType, bool oasis); |
| 35 |
QString loadTextFile(KoStore* store, const QString& fileName); |
| 36 |
}; |
| 37 |
|
| 38 |
int main(int argc, char** argv) |
| 39 |
{ |
| 40 |
QApplication app(argc, argv); |
| 41 |
StoreDropTest* window = new StoreDropTest(nullptr); |
| 42 |
window->resize(500, 500); |
| 43 |
window->show(); |
| 44 |
|
| 45 |
QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit())); |
| 46 |
return app.exec(); |
| 47 |
} |
| 48 |
|
| 49 |
StoreDropTest::StoreDropTest(QWidget* parent) |
| 50 |
: QTextBrowser(parent) |
| 51 |
{ |
| 52 |
setText(QStringLiteral("KoStore drop/paste test\nDrop or paste a selection from a Calligra application into this widget to see the ZIP contents")); |
| 53 |
setAcceptDrops(true); |
| 54 |
} |
| 55 |
|
| 56 |
void StoreDropTest::contentsDragEnterEvent(QDragEnterEvent * ev) |
| 57 |
{ |
| 58 |
ev->acceptProposedAction(); |
| 59 |
} |
| 60 |
|
| 61 |
void StoreDropTest::contentsDragMoveEvent(QDragMoveEvent * ev) |
| 62 |
{ |
| 63 |
ev->acceptProposedAction(); |
| 64 |
} |
| 65 |
|
| 66 |
void StoreDropTest::keyPressEvent(QKeyEvent * e) |
| 67 |
{ |
| 68 |
if (((e->modifiers() & Qt::ShiftModifier) && e->key() == Qt::Key_Insert) || |
| 69 |
((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_V)) |
| 70 |
paste(); |
| 71 |
//else |
| 72 |
// QTextBrowser::keyPressEvent(e); |
| 73 |
} |
| 74 |
|
| 75 |
void StoreDropTest::paste() |
| 76 |
{ |
| 77 |
qDebug("paste"); |
| 78 |
const QMimeData* m = QApplication::clipboard()->mimeData(); |
| 79 |
if (!m) |
| 80 |
return; |
| 81 |
|
| 82 |
const QString acceptMimeType(QStringLiteral("application/vnd.oasis.opendocument.")); |
| 83 |
const QStringList formats = m->formats(); |
| 84 |
for (const QString &fmt : formats) { |
| 85 |
bool oasis = fmt.startsWith(acceptMimeType); |
| 86 |
if (oasis || fmt == QStringLiteral("application/x-kpresenter")) { |
| 87 |
QByteArray data = m->data(fmt); |
| 88 |
showZipContents(data, fmt, oasis); |
| 89 |
return; |
| 90 |
} |
| 91 |
} |
| 92 |
setText(QStringLiteral("No acceptable format found. All I got was:\n") + formats.join(QLatin1Char('\n'))); |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
void StoreDropTest::contentsDropEvent(QDropEvent *ev) |
| 97 |
{ |
| 98 |
if (processMimeData(ev->mimeData())) |
| 99 |
ev->acceptProposedAction(); |
| 100 |
else |
| 101 |
ev->ignore(); |
| 102 |
} |
| 103 |
|
| 104 |
bool StoreDropTest::processMimeData(const QMimeData* mimeData) |
| 105 |
{ |
| 106 |
const QString acceptMimeType(QStringLiteral("application/vnd.oasis.opendocument.")); |
| 107 |
const QStringList formats = mimeData->formats(); |
| 108 |
for (const QString &format : formats) { |
| 109 |
bool oasis = format.startsWith(acceptMimeType); |
| 110 |
if (oasis || format == QStringLiteral("application/x-kpresenter")) { |
| 111 |
const QByteArray data = mimeData->data(format); |
| 112 |
showZipContents(data, format, oasis); |
| 113 |
return true; |
| 114 |
} |
| 115 |
} |
| 116 |
setText(QStringLiteral("No acceptable format found. All I got was:\n") + mimeData->formats().join(QLatin1Char('\n'))); |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
void StoreDropTest::showZipContents(QByteArray data, const QString &mimeType, bool oasis) |
| 121 |
{ |
| 122 |
if (data.isEmpty()) { |
| 123 |
setText(QStringLiteral("No data!")); |
| 124 |
return; |
| 125 |
} |
| 126 |
QBuffer buffer(&data); |
| 127 |
KoStore * store = KoStore::createStore(&buffer, KoStore::Read); |
| 128 |
if (store->bad()) { |
| 129 |
setText(QStringLiteral("Invalid ZIP!")); |
| 130 |
delete store; |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
QString txt = QStringLiteral("Valid ZIP file found for format ") + mimeType + QLatin1Char('\n'); |
| 136 |
|
| 137 |
if (oasis) { |
| 138 |
txt += loadTextFile(store, QStringLiteral("content.xml")); |
| 139 |
txt += loadTextFile(store, QStringLiteral("styles.xml")); |
| 140 |
txt += loadTextFile(store, QStringLiteral("settings.xml")); |
| 141 |
txt += loadTextFile(store, QStringLiteral("META-INF/manifest.xml")); |
| 142 |
} else { |
| 143 |
txt += loadTextFile(store, QStringLiteral("maindoc.xml")); |
| 144 |
} |
| 145 |
setText(txt); |
| 146 |
delete store; |
| 147 |
} |
| 148 |
|
| 149 |
QString StoreDropTest::loadTextFile(KoStore* store, const QString& fileName) |
| 150 |
{ |
| 151 |
if (!store->open(fileName)) |
| 152 |
return QStringLiteral("%1 not found\n").arg(fileName); |
| 153 |
|
| 154 |
QByteArray data = store->device()->readAll(); |
| 155 |
store->close(); |
| 156 |
QString txt = QStringLiteral("Found %1: \n").arg(fileName); |
| 157 |
txt += QString::fromUtf8(data.data(), data.size()); |
| 158 |
txt += QLatin1Char('\n'); |
| 159 |
return txt; |
| 160 |
} |
| 161 |
#include "storedroptest.moc" |