File indexing completed on 2024-12-22 04:41:15

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmlexternaljsobject.h"
0019 #include "javascript/externaljsobject.h"
0020 #include <QQmlEngine>
0021 
0022 QmlExternalJsObject::QmlExternalJsObject(QObject *parent)
0023     : QObject(parent)
0024 {
0025 }
0026 
0027 QmlExternalJsObject::~QmlExternalJsObject()
0028 {
0029     for (QObject *object : std::as_const(m_objects)) {
0030         ExternalJsObject::unregisterExtraObject(object);
0031     }
0032 }
0033 
0034 void QmlExternalJsObject::registerExtraObject(const QVariantMap &map)
0035 {
0036     if (!map.contains(QSL("id")) || !map.contains(QSL("object"))) {
0037         qWarning() << "Unable to call" << __FUNCTION__ << ": insufficient parameters";
0038         return;
0039     }
0040 
0041     const QString id = map.value(QSL("id")).toString();
0042     auto *object = qvariant_cast<QObject*>(map.value(QSL("object")));
0043     if (!object) {
0044         qWarning() << "Unable to cast to QObject";
0045         return;
0046     }
0047     ExternalJsObject::registerExtraObject(id, object);
0048     m_objects.append(object);
0049 }
0050 
0051 void QmlExternalJsObject::unregisterExtraObject(QObject *object)
0052 {
0053     ExternalJsObject::unregisterExtraObject(object);
0054 }