Merge branch 'master' of git://labs.trolltech.com/qtscriptgenerator
[qtscriptgenerator/amarok.git] / examples / WebKitPlugins.qs
blobcea1a21e1ac014abe77fce7d1a98030a7280a3c2
1 /****************************************************************************
2 **
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
4 **
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
6 **
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file.  Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function getArgumentValue(name, names, values, from)
26     var i = (from == undefined) ? 0 : from;
27     for ( ; i < names.length; ++i) {
28         if (names[i] == name)
29             return values[i];
30     }
31     return undefined;
35 // Plugin class
38 function MyWebPlugin(formUrl, scriptUrl, parent)
40     QWidget.call(this, parent);
42     this.initialized = false;
43     this.formReply = this.downloadFile(formUrl, this.formDownloaded);
44     if (scriptUrl == undefined)
45         this.script = "";
46     else
47         this.scriptReply = this.downloadFile(scriptUrl, this.scriptDownloaded);
50 MyWebPlugin.prototype = new QWidget();
52 MyWebPlugin.prototype.downloadFile = function(url, callback)
54     if (this.accessManager == undefined)
55         this.accessManager = new QNetworkAccessManager();
56     var reply = this.accessManager.get(new QNetworkRequest(url));
57     reply.finished.connect(this, callback);
58     return reply;
61 MyWebPlugin.prototype.formDownloaded = function()
63     var loader = new QUiLoader();
64     this.form = loader.load(this.formReply);
65     var layout = new QVBoxLayout(this);
66     layout.addWidget(this.form, 0, Qt.AlignCenter);
67     this.initialize();
70 MyWebPlugin.prototype.scriptDownloaded = function()
72     var stream = new QTextStream(this.scriptReply);
73     this.script = stream.readAll();
74     this.initialize();
77 MyWebPlugin.prototype.initialize = function()
79     if (this.initialized)
80         return;
81     if ((this.form == undefined) || (this.script == undefined))
82         return;
83     var ctor = eval(this.script);
84     if (typeof ctor != "function")
85         return;
86     this.instance = new ctor(this.form);
87     this.initialized = true;
92 // QWebPluginFactory subclass
95 function MyWebPluginFactory(parent)
97     QWebPluginFactory.call(this, parent);
100 MyWebPluginFactory.prototype = new QWebPluginFactory();
102 MyWebPluginFactory.prototype.create = function(mimeType, url, argumentNames, argumentValues)
104     if (mimeType != "application/x-qtform")
105         return null;
107     var formUrl = getArgumentValue("form", argumentNames, argumentValues);
108     var scriptUrl = getArgumentValue("script", argumentNames, argumentValues);
109     if (formUrl == undefined)
110         return null;
112     return new MyWebPlugin(new QUrl(formUrl), new QUrl(scriptUrl));
117 // Main
120 var view = new QWebView();
121 view.settings().setAttribute(QWebSettings.PluginsEnabled, true);
123 var factory = new MyWebPluginFactory();
124 view.page().setPluginFactory(factory);
126 view.load(new QUrl("WebKitPlugins.html"));
127 view.show();
129 QCoreApplication.exec();