Commit 047b754f authored by nextime's avatar nextime

Handler for qextension: url reqeiring works.

parent 525064a2
/**************************************************************************** // Copyright (C) 2016 The Qt Company Ltd.
** // Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebChannel module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
"use strict"; "use strict";
...@@ -53,7 +17,7 @@ var QWebChannelMessageTypes = { ...@@ -53,7 +17,7 @@ var QWebChannelMessageTypes = {
response: 10, response: 10,
}; };
var QWebChannel = function(transport, initCallback) var QWebChannel = function(transport, initCallback, converters)
{ {
if (typeof transport !== "object" || typeof transport.send !== "function") { if (typeof transport !== "object" || typeof transport.send !== "function") {
console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." + console.error("The QWebChannel expects a transport object with a send function and onmessage callback property." +
...@@ -64,6 +28,43 @@ var QWebChannel = function(transport, initCallback) ...@@ -64,6 +28,43 @@ var QWebChannel = function(transport, initCallback)
var channel = this; var channel = this;
this.transport = transport; this.transport = transport;
var converterRegistry =
{
Date : function(response) {
if (typeof response === "string"
&& response.match(
/^-?\d+-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?([-+\u2212](\d{2}):(\d{2})|Z)?$/)) {
var date = new Date(response);
if (!isNaN(date))
return date;
}
return undefined; // Return undefined if current converter is not applicable
}
};
this.usedConverters = [];
this.addConverter = function(converter)
{
if (typeof converter === "string") {
if (converterRegistry.hasOwnProperty(converter))
this.usedConverters.push(converterRegistry[converter]);
else
console.error("Converter '" + converter + "' not found");
} else if (typeof converter === "function") {
this.usedConverters.push(converter);
} else {
console.error("Invalid converter object type " + typeof converter);
}
}
if (Array.isArray(converters)) {
for (const converter of converters)
this.addConverter(converter);
} else if (converters !== undefined) {
this.addConverter(converters);
}
this.send = function(data) this.send = function(data)
{ {
if (typeof(data) !== "string") { if (typeof(data) !== "string") {
...@@ -190,6 +191,12 @@ function QObject(name, data, webChannel) ...@@ -190,6 +191,12 @@ function QObject(name, data, webChannel)
this.unwrapQObject = function(response) this.unwrapQObject = function(response)
{ {
for (const converter of webChannel.usedConverters) {
var result = converter(response);
if (result !== undefined)
return result;
}
if (response instanceof Array) { if (response instanceof Array) {
// support list of objects // support list of objects
return response.map(qobj => object.unwrapQObject(qobj)) return response.map(qobj => object.unwrapQObject(qobj))
...@@ -445,4 +452,4 @@ if (typeof module === 'object') { ...@@ -445,4 +452,4 @@ if (typeof module === 'object') {
module.exports = { module.exports = {
QWebChannel: QWebChannel QWebChannel: QWebChannel
}; };
} }
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment