2010-06-17 18:30:32 +02:00
|
|
|
#include "odfview.h"
|
2010-06-25 07:05:29 -07:00
|
|
|
|
2010-06-18 23:04:31 +02:00
|
|
|
#include "odfcontainer.h"
|
2010-06-22 14:43:03 +02:00
|
|
|
#include "odf.h"
|
2010-06-25 07:05:29 -07:00
|
|
|
#include "zipnetworkreply.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>
|
|
|
|
|
#include <QtCore/QDebug>
|
|
|
|
|
|
|
|
|
|
class OdfView::OdfNetworkAccessManager : public QNetworkAccessManager {
|
|
|
|
|
private:
|
|
|
|
|
QDir dir;
|
|
|
|
|
QDir odfdir;
|
|
|
|
|
QStringList allowedFiles;
|
2010-06-25 10:08:51 -07:00
|
|
|
OdfContainer *currentFile;
|
2010-06-18 23:04:31 +02:00
|
|
|
public:
|
|
|
|
|
OdfNetworkAccessManager(const QDir& localdir) :dir(localdir) {
|
2010-06-22 14:43:03 +02:00
|
|
|
allowedFiles << "odf.html" << "style2css.js" << "defaultodfstyle.css"
|
|
|
|
|
<< "qtodf.js";
|
2010-06-18 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
QNetworkReply* createRequest(Operation op, const QNetworkRequest& req,
|
|
|
|
|
QIODevice* data = 0) {
|
2010-06-25 07:05:29 -07:00
|
|
|
if (req.url().scheme() == "odfkit") {
|
|
|
|
|
//data is inside the current zip file
|
|
|
|
|
qDebug() << "zip! " << req.url();
|
2010-06-25 10:08:51 -07:00
|
|
|
return new ZipNetworkReply(this, currentFile, req, op);
|
2010-06-25 07:05:29 -07:00
|
|
|
}
|
|
|
|
|
|
2010-06-19 10:33:55 +02:00
|
|
|
QNetworkRequest r(req);
|
2010-06-24 12:01:32 +02:00
|
|
|
QString path;
|
|
|
|
|
if (dir.absolutePath().startsWith(":")) {
|
|
|
|
|
path = req.url().toString().mid(3);
|
|
|
|
|
} else {
|
|
|
|
|
path = req.url().toLocalFile();
|
|
|
|
|
}
|
|
|
|
|
QFileInfo fileinfo = path;
|
2010-06-19 10:33:55 +02:00
|
|
|
if (op != GetOperation
|
|
|
|
|
|| !allowedFiles.contains(fileinfo.fileName())
|
|
|
|
|
|| fileinfo.dir() != dir) {
|
|
|
|
|
// changing the url seems to be the only easy way to deny
|
|
|
|
|
// requests
|
2010-06-25 07:05:29 -07:00
|
|
|
qDebug() << "deny " << req.url();
|
|
|
|
|
r.setUrl(QUrl("error:not-allowed"));
|
2010-06-18 23:04:31 +02:00
|
|
|
}
|
2010-06-19 10:33:55 +02:00
|
|
|
return QNetworkAccessManager::createRequest(op, r, data);
|
2010-06-18 23:04:31 +02:00
|
|
|
}
|
2010-06-25 10:08:51 -07:00
|
|
|
void setCurrentFile(OdfContainer *file) {currentFile = file;}
|
2010-06-18 23:04:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
class OdfPage : public QWebPage {
|
|
|
|
|
public:
|
|
|
|
|
OdfPage(QObject* parent) :QWebPage(parent) {}
|
|
|
|
|
void javaScriptConsoleMessage(const QString& message, int lineNumber, const QString & sourceID) {
|
|
|
|
|
qDebug() << message;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2010-06-17 18:30:32 +02:00
|
|
|
|
2010-06-22 14:43:03 +02:00
|
|
|
OdfView::OdfView(QWidget* parent) :QWebView(parent)
|
2010-06-17 18:30:32 +02:00
|
|
|
{
|
2010-06-18 23:04:31 +02:00
|
|
|
setPage(new OdfPage(this));
|
2010-06-24 12:01:32 +02:00
|
|
|
odf = new Odf(page()->mainFrame(), this);
|
2010-06-18 23:04:31 +02:00
|
|
|
connect(page(), SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
|
2010-06-19 10:33:55 +02:00
|
|
|
page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
|
|
|
|
|
|
2010-06-22 14:43:03 +02:00
|
|
|
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
|
2010-06-25 23:16:09 +02:00
|
|
|
QString prefix = "../webodf";
|
|
|
|
|
QString htmlfile = prefix + "/odf.html";
|
2010-06-25 22:15:32 +02:00
|
|
|
if (!QFileInfo(htmlfile).exists()) {
|
2010-06-25 23:16:09 +02:00
|
|
|
prefix = ":/";
|
|
|
|
|
htmlfile = "qrc:/odf.html";
|
2010-06-25 22:15:32 +02:00
|
|
|
}
|
2010-06-24 12:01:32 +02:00
|
|
|
networkaccessmanager = new OdfNetworkAccessManager(QDir(prefix));
|
2010-06-18 23:04:31 +02:00
|
|
|
page()->setNetworkAccessManager(networkaccessmanager);
|
2010-06-25 22:15:32 +02:00
|
|
|
setUrl(QUrl(htmlfile));
|
2010-06-18 23:04:31 +02:00
|
|
|
loaded = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OdfView::~OdfView() {
|
2010-06-17 18:30:32 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-22 14:43:03 +02:00
|
|
|
void
|
|
|
|
|
OdfView::slotInitWindowObjects()
|
|
|
|
|
{
|
|
|
|
|
QWebFrame *frame = page()->mainFrame();
|
|
|
|
|
frame->addToJavaScriptWindowObject("qtodf", odf);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-17 18:30:32 +02:00
|
|
|
bool
|
|
|
|
|
OdfView::loadFile(const QString &fileName) {
|
2010-06-22 14:43:03 +02:00
|
|
|
curFile = fileName;
|
|
|
|
|
identifier = QString::number(qrand());
|
|
|
|
|
odf->addFile(identifier, fileName);
|
2010-06-25 10:08:51 -07:00
|
|
|
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;
|
2010-06-22 14:43:03 +02:00
|
|
|
loaded = true;
|
2010-06-18 23:04:31 +02:00
|
|
|
QWebFrame *frame = page()->mainFrame();
|
2010-06-22 14:43:03 +02:00
|
|
|
QString js = "window.odfcontainer = new window.odf.OdfContainer('"
|
|
|
|
|
+ identifier + "'); refreshOdf();";
|
|
|
|
|
QVariant out = frame->evaluateJavaScript(js);
|
|
|
|
|
qDebug() << out;
|
2010-06-18 23:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
2010-06-17 18:30:32 +02:00
|
|
|
#include "moc_odfview.cpp"
|