diff --git a/javascriptzip/nativeQtClient/mainwindow.cpp b/javascriptzip/nativeQtClient/mainwindow.cpp index d9a91cf3..f802d704 100644 --- a/javascriptzip/nativeQtClient/mainwindow.cpp +++ b/javascriptzip/nativeQtClient/mainwindow.cpp @@ -15,6 +15,11 @@ MainWindow::MainWindow(QWidget *parent) : setWindowTitle(tr("MDI")); setUnifiedTitleAndToolBarOnMac(true); + + OdfView *child = createOdfView(); + if (child->loadFile("../kofficetests/odf/DanskTest01.odt")) { + child->showMaximized(); + } } MainWindow::~MainWindow() @@ -46,7 +51,7 @@ void MainWindow::open() OdfView *child = createOdfView(); if (child->loadFile(fileName)) { statusBar()->showMessage(tr("File loaded"), 2000); - child->show(); + child->showMaximized(); } else { child->close(); } diff --git a/javascriptzip/nativeQtClient/nativeQtClient.pro b/javascriptzip/nativeQtClient/nativeQtClient.pro index 629943d2..958e2bde 100644 --- a/javascriptzip/nativeQtClient/nativeQtClient.pro +++ b/javascriptzip/nativeQtClient/nativeQtClient.pro @@ -9,10 +9,14 @@ QT += network \ xmlpatterns \ testlib TARGET = nativeQtClient +INCLUDEPATH+=/usr/include/minizip +LIBS+=-lquazip TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp \ - odfview.cpp + odfview.cpp \ + odfcontainer.cpp HEADERS += mainwindow.h \ - odfview.h + odfview.h \ + odfcontainer.h FORMS += mainwindow.ui diff --git a/javascriptzip/nativeQtClient/odfcontainer.cpp b/javascriptzip/nativeQtClient/odfcontainer.cpp new file mode 100644 index 00000000..be56609c --- /dev/null +++ b/javascriptzip/nativeQtClient/odfcontainer.cpp @@ -0,0 +1,35 @@ +#include "odfcontainer.h" +#include +#include +#include + +namespace { + QString readzipfile(QuaZip* quazip, const char* name) { + quazip->setCurrentFile(name, QuaZip::csSensitive); + QuaZipFile file(quazip); + file.open(QIODevice::ReadOnly); + QByteArray data = file.readAll(); + return QString::fromUtf8(data); // assume utf8 for now + } +} + +OdfContainer::OdfContainer(const QUrl& u, QObject* parent) :QObject(parent), url(u) +{ + quazip = new QuaZip(u.toLocalFile()); + quazip->open(QuaZip::mdUnzip); + qDebug() << quazip->getEntriesCount(); +} + +OdfContainer::~OdfContainer() { + delete quazip; +} + +QString +OdfContainer::getContentXml() { + return readzipfile(quazip, "content.xml"); +} + +QString +OdfContainer::getStylesXml() { + return readzipfile(quazip, "styles.xml"); +} diff --git a/javascriptzip/nativeQtClient/odfcontainer.h b/javascriptzip/nativeQtClient/odfcontainer.h new file mode 100644 index 00000000..3e74ab67 --- /dev/null +++ b/javascriptzip/nativeQtClient/odfcontainer.h @@ -0,0 +1,28 @@ +#ifndef ODFCONTAINER_H +#define ODFCONTAINER_H + +#include +#include + +class QuaZip; + +class OdfContainer : public QObject +{ +Q_OBJECT +public: + OdfContainer(const QUrl& url, QObject* parent); + ~OdfContainer(); + Q_PROPERTY(QString name READ getName SCRIPTABLE true); + +public slots: + QString getContentXml(); + QString getStylesXml(); + +private: + const QUrl url; + QuaZip* quazip; + + QString getName() const { return "hello"; } +}; + +#endif // ODFCONTAINER_H diff --git a/javascriptzip/nativeQtClient/odfview.cpp b/javascriptzip/nativeQtClient/odfview.cpp index 9bc2a63b..5c353b25 100644 --- a/javascriptzip/nativeQtClient/odfview.cpp +++ b/javascriptzip/nativeQtClient/odfview.cpp @@ -1,15 +1,87 @@ #include "odfview.h" #include +#include +#include "odfcontainer.h" +#include +#include +#include +#include +#include +#include + +class OdfView::OdfNetworkAccessManager : public QNetworkAccessManager { +private: + class DummyNetworkReply : public QNetworkReply { + public: + DummyNetworkReply(QObject* parent) :QNetworkReply(parent) {} + private: + void abort() {} + qint64 readData(char*, qint64) { return -1; } + }; + QDir dir; + QDir odfdir; + QStringList allowedFiles; +public: + OdfNetworkAccessManager(const QDir& localdir) :dir(localdir) { + allowedFiles << "qtodf.html" << "style2css.js" << "defaultodfstyle.css"; + } + QNetworkReply* createRequest(Operation op, const QNetworkRequest& req, + QIODevice* data = 0) { + if (op != GetOperation) return 0; + QFileInfo fileinfo = req.url().toLocalFile(); + if (allowedFiles.contains(fileinfo.fileName()) && fileinfo.dir() == dir) { + return QNetworkAccessManager::createRequest(op, req, data); + } + qDebug() << "not loading " << req.url(); + return new DummyNetworkReply(this); + } +}; + +namespace { + class OdfPage : public QWebPage { + public: + OdfPage(QObject* parent) :QWebPage(parent) {} + void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString & sourceID) { + qDebug() << message; + } + }; +} OdfView::OdfView() { + odfcontainer = 0; + setPage(new OdfPage(this)); + connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool))); + + // use our own networkaccessmanager that gives limited access to the local + // file system + networkaccessmanager = new OdfNetworkAccessManager(QDir("..")); + page()->setNetworkAccessManager(networkaccessmanager); + + // for now, we simply point to the file, we want to read + setUrl(QUrl("../qtodf.html")); + loaded = false; +} + +OdfView::~OdfView() { } bool OdfView::loadFile(const QString &fileName) { - QByteArray content = ""+fileName.toUtf8()+""; - setContent(content); + odfcontainer = new OdfContainer(fileName, this); + if (loaded) { + slotLoadFinished(true); + } return true; } +void +OdfView::slotLoadFinished(bool ok) { + if (!ok) return; + QWebFrame *frame = page()->mainFrame(); + frame->addToJavaScriptWindowObject("odf", odfcontainer); + QVariant output = frame->evaluateJavaScript("refreshOdf();"); + qDebug() << output; +} + #include "moc_odfview.cpp" diff --git a/javascriptzip/nativeQtClient/odfview.h b/javascriptzip/nativeQtClient/odfview.h index 43b97f2a..3efb9e9a 100644 --- a/javascriptzip/nativeQtClient/odfview.h +++ b/javascriptzip/nativeQtClient/odfview.h @@ -1,18 +1,28 @@ #ifndef ODFVIEW_H #define ODFVIEW_H -#include +#include + +class OdfContainer; class OdfView : public QWebView { Q_OBJECT public: OdfView(); + ~OdfView(); bool loadFile(const QString &fileName); QString currentFile() { return curFile; } +private slots: + void slotLoadFinished(bool ok); + private: + bool loaded; QString curFile; + class OdfNetworkAccessManager; + OdfNetworkAccessManager* networkaccessmanager; + OdfContainer* odfcontainer; }; #endif // ODFVIEW_H diff --git a/javascriptzip/qtodf.html b/javascriptzip/qtodf.html new file mode 100644 index 00000000..eb144009 --- /dev/null +++ b/javascriptzip/qtodf.html @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + +