diff options
author | Warwick Allison <warwick.allison@nokia.com> | 2010-01-05 05:55:58 (GMT) |
---|---|---|
committer | Warwick Allison <warwick.allison@nokia.com> | 2010-01-05 05:55:58 (GMT) |
commit | 027a3a94955127712a76f15430a1f6cc0288722e (patch) | |
tree | 20f5ff73b9dca7210e69f78c6f6a0c0ac088b415 /tools/qmlviewer/qmlviewer.cpp | |
parent | 186cdd1c29f1c708455ec2f0771a6eb600d4a075 (diff) | |
download | Qt-027a3a94955127712a76f15430a1f6cc0288722e.zip Qt-027a3a94955127712a76f15430a1f6cc0288722e.tar.gz Qt-027a3a94955127712a76f15430a1f6cc0288722e.tar.bz2 |
Basic WGT/WGZ support.
Task-number: QT-524
Diffstat (limited to 'tools/qmlviewer/qmlviewer.cpp')
-rw-r--r-- | tools/qmlviewer/qmlviewer.cpp | 100 |
1 files changed, 98 insertions, 2 deletions
diff --git a/tools/qmlviewer/qmlviewer.cpp b/tools/qmlviewer/qmlviewer.cpp index 831c680..b838f40 100644 --- a/tools/qmlviewer/qmlviewer.cpp +++ b/tools/qmlviewer/qmlviewer.cpp @@ -51,7 +51,12 @@ #include <QAbstractAnimation> #include "deviceskin.h" +#include <private/qzipreader_p.h> + #include <QSettings> +#include <QXmlStreamReader> +#include <QBuffer> +#include <QNetworkReply> #include <QNetworkCookieJar> #include <QNetworkDiskCache> #include <QNetworkAccessManager> @@ -441,7 +446,7 @@ void QmlViewer::createMenu(QMenuBar *menu, QMenu *flatmenu) QAction *openAction = new QAction(tr("&Open..."), parent); openAction->setShortcut(QKeySequence("Ctrl+O")); - connect(openAction, SIGNAL(triggered()), this, SLOT(open())); + connect(openAction, SIGNAL(triggered()), this, SLOT(openFile())); fileMenu->addAction(openAction); QAction *reloadAction = new QAction(tr("&Reload"), parent); @@ -758,7 +763,98 @@ void QmlViewer::reload() openQml(currentFileOrUrl); } -void QmlViewer::open() +void QmlViewer::open(const QString& doc) +{ + if (doc.endsWith(".wgt",Qt::CaseInsensitive) + || doc.endsWith(".wgz",Qt::CaseInsensitive)) + openWgt(doc); + else + openQml(doc); +} + +void QmlViewer::openWgt(const QString& doc) +{ + // XXX This functionality could be migrated to QmlView once refined + + QUrl url(doc); + if (url.isRelative()) + url = QUrl::fromLocalFile(doc); + canvas->reset(); + QNetworkAccessManager * nam = canvas->engine()->networkAccessManager(); + wgtreply = nam->get(QNetworkRequest(url)); + connect(wgtreply,SIGNAL(finished()),this,SLOT(unpackWgt())); +} + +static void removeRecursive(const QString& dirname) +{ + QDir dir(dirname); + QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot)); + for (int i = 0; i < entries.count(); ++i) + if (entries[i].isDir()) + removeRecursive(entries[i].filePath()); + else + dir.remove(entries[i].fileName()); + QDir().rmdir(dirname); +} + +void QmlViewer::unpackWgt() +{ + QByteArray all = wgtreply->readAll(); + QBuffer buf(&all); + buf.open(QIODevice::ReadOnly); + QZipReader zip(&buf); + /* + for (int i=0; i<zip.count(); ++i) { + QZipReader::FileInfo info = zip.entryInfoAt(i); + qDebug() << "zip:" << info.filePath; + } + */ + wgtdir = QDir::tempPath()+QDir::separator()+QLatin1String("qmlviewer-wgt"); + removeRecursive(wgtdir); + QDir().mkpath(wgtdir); + zip.extractAll(wgtdir); + + QString rootfile; + + if (wgtreply->header(QNetworkRequest::ContentTypeHeader).toString() == "application/widget" || wgtreply->url().path().endsWith(".wgt",Qt::CaseInsensitive)) { + // W3C Draft http://www.w3.org/TR/2009/CR-widgets-20091201 + QFile configfile(wgtdir+QDir::separator()+"config.xml"); + if (configfile.open(QIODevice::ReadOnly)) { + QXmlStreamReader config(&configfile); + if (config.readNextStartElement() && config.name() == "widget") { + while (config.readNextStartElement()) { + if (config.name() == "content") { + rootfile = wgtdir + QDir::separator(); + rootfile += config.attributes().value(QLatin1String("src")); + } + // XXX process other config + + config.skipCurrentElement(); + } + } + } else { + qWarning("No config.xml found - non-standard WGT file"); + } + if (rootfile.isEmpty()) { + QString def = wgtdir+QDir::separator()+"index.qml"; + if (QFile::exists(def)) + rootfile = def; + } + } else { + // Just find index.qml, preferably at the root + for (int i=0; i<zip.count(); ++i) { + QZipReader::FileInfo info = zip.entryInfoAt(i); + if (info.filePath.compare(QLatin1String("index.qml"),Qt::CaseInsensitive)==0) + rootfile = wgtdir+QDir::separator()+info.filePath; + if (rootfile.isEmpty() && info.filePath.endsWith("/index.qml",Qt::CaseInsensitive)) + rootfile = wgtdir+QDir::separator()+info.filePath; + } + } + + openQml(rootfile); +} + +void QmlViewer::openFile() { QString cur = canvas->url().toLocalFile(); if (useQmlFileBrowser) { |