Native client can now open odf files.

This commit is contained in:
Jos van den Oever
2010-06-18 23:04:31 +02:00
parent 7b78c1c69d
commit 75c08bd0ce
7 changed files with 221 additions and 6 deletions
+6 -1
View File
@@ -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();
}
@@ -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
@@ -0,0 +1,35 @@
#include "odfcontainer.h"
#include <quazip/quazip.h>
#include <quazip/quazipfile.h>
#include <QtCore/QDebug>
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");
}
@@ -0,0 +1,28 @@
#ifndef ODFCONTAINER_H
#define ODFCONTAINER_H
#include <QtCore/QObject>
#include <QtCore/QUrl>
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
+74 -2
View File
@@ -1,15 +1,87 @@
#include "odfview.h"
#include <QtCore/QByteArray>
#include <QtWebKit/QWebFrame>
#include "odfcontainer.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QDebug>
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 = "<b>"+fileName.toUtf8()+"</b>";
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"
+11 -1
View File
@@ -1,18 +1,28 @@
#ifndef ODFVIEW_H
#define ODFVIEW_H
#include <QWebView>
#include <QtWebKit/QWebView>
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
+61
View File
@@ -0,0 +1,61 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" media="screen">
body, html, div {
padding: 0px;
margin: 0px;
border: 0px;
background: #888888;
}
</style>
<link rel="stylesheet" type="text/css" href="defaultodfstyle.css"></link>
<style id='stylesxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='autostylesxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='contentxmlcss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace fo url(urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0);
@namespace office url(urn:oasis:names:tc:opendocument:xmlns:office:1.0);
@namespace style url(urn:oasis:names:tc:opendocument:xmlns:style:1.0);
@namespace table url(urn:oasis:names:tc:opendocument:xmlns:table:1.0);
@namespace text url(urn:oasis:names:tc:opendocument:xmlns:text:1.0);
</style>
<style id='positioncss' type="text/css">
/* define namespaces here, because in WebKit they cannot be added dynamically */
@namespace draw url(urn:oasis:names:tc:opendocument:xmlns:drawing:1.0);
@namespace svg url(urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0);
</style>
<script src="style2css.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function refreshOdf() {
var parser = new DOMParser();
var contentxml = window.odf.getContentXml();
var doc = parser.parseFromString(contentxml, "text/xml");
var root = doc.documentElement;
root = document.importNode(root, true);
document.body.appendChild(root);
}
</script>
<title></title>
</head>
<body>
</body>
</html>