Files
WebODF/programs/nativeQtClient/odfview.cpp
T

82 lines
2.6 KiB
C++
Raw Normal View History

2010-06-17 18:30:32 +02:00
#include "odfview.h"
2011-07-26 13:22:33 +02:00
#include "../qtjsruntime/nativeio.h"
#include "../qtjsruntime/nam.h"
#include "odfpage.h"
#include <QtCore/QByteArray>
#include <QtWebKit/QWebFrame>
2010-06-18 23:04:31 +02:00
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QFileInfo>
#include <QtCore/QDir>
OdfView::OdfView(QWidget* parent) :QWebView(parent)
2010-06-17 18:30:32 +02:00
{
2011-06-07 22:24:51 +02:00
QString prefix = "../../webodf/"; // set this to the right value when debugging
QString htmlfile = QDir(prefix).absoluteFilePath("embedodf.html");
2011-07-26 13:22:33 +02:00
if (QFileInfo(htmlfile).exists()) {
prefix = "qrc:/";
2011-06-07 22:24:51 +02:00
htmlfile = "qrc:/embedodf.html";
}
2010-06-18 23:04:31 +02:00
setPage(new OdfPage(this));
2011-07-26 13:22:33 +02:00
nativeio = new NativeIO(this);
2010-06-18 23:04:31 +02:00
connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
this, SLOT(slotInitWindowObjects()));
2010-06-18 23:04:31 +02:00
// use our own networkaccessmanager that gives limited access to the local
// file system
2011-07-26 13:22:33 +02:00
networkaccessmanager = new NAM(this);
2010-06-18 23:04:31 +02:00
page()->setNetworkAccessManager(networkaccessmanager);
setUrl(QUrl(htmlfile));
2010-06-18 23:04:31 +02:00
loaded = false;
}
OdfView::~OdfView() {
2010-06-17 18:30:32 +02:00
}
void
OdfView::slotInitWindowObjects()
{
QWebFrame *frame = page()->mainFrame();
2011-07-26 13:22:33 +02:00
frame->addToJavaScriptWindowObject("nativeio", nativeio);
}
2010-06-17 18:30:32 +02:00
bool
OdfView::loadFile(const QString &fileName) {
curFile = fileName;
2011-07-26 13:22:33 +02:00
// odf->addFile(identifier, fileName);
// networkaccessmanager->setCurrentFile(odf->getOpenContainer(identifier));
2010-06-18 23:04:31 +02:00
if (loaded) {
slotLoadFinished(true);
}
2010-06-17 18:30:32 +02:00
return true;
}
2010-06-18 23:04:31 +02:00
void
OdfView::slotLoadFinished(bool ok) {
if (!ok) return;
loaded = true;
2010-06-18 23:04:31 +02:00
QWebFrame *frame = page()->mainFrame();
QString js =
2011-07-26 13:22:33 +02:00
"runtime.readFileSync = function (path, encoding) {"
" return nativeio.readFileSync(path, encoding);"
"};"
"runtime.read = function (path, offset, length, callback) {"
" var data = nativeio.read(path, offset, length);"
" data = runtime.byteArrayFromString(data, 'binary');"
" callback(nativeio.error()||null, data);"
"};"
"runtime.getFileSize = function (path, callback) {"
" callback(nativeio.getFileSize(path));"
"};"
"runtime.loadClass('odf.OdfCanvas');"
"window.canvas = new odf.OdfCanvas(document.getElementById('odf'));"
"window.canvas.load('" + curFile + "');";
2011-06-07 22:24:51 +02:00
frame->evaluateJavaScript(js);
2010-06-18 23:04:31 +02:00
}