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 "qmluserscripts.h"
0019 #include "mainapplication.h"
0020 #include <QWebEngineProfile>
0021 #include <QWebEngineScriptCollection>
0022 #include <QQmlEngine>
0023 
0024 QmlUserScripts::QmlUserScripts(QObject *parent)
0025     : QObject(parent)
0026 {
0027 }
0028 
0029 int QmlUserScripts::count() const
0030 {
0031     return mApp->webProfile()->scripts()->count();
0032 }
0033 
0034 int QmlUserScripts::size() const
0035 {
0036     return mApp->webProfile()->scripts()->count();
0037 }
0038 
0039 bool QmlUserScripts::empty() const
0040 {
0041     return mApp->webProfile()->scripts()->isEmpty();
0042 }
0043 
0044 QList<QObject*> QmlUserScripts::toQObjectList(const QList<QWebEngineScript> &list) const
0045 {
0046     QList<QObject*> userScriptList;
0047     userScriptList.reserve(list.size());
0048     for (const QWebEngineScript &script : list) {
0049         auto *userScript = new QmlUserScript();
0050         userScript->setWebEngineScript(script);
0051         userScriptList.append(userScript);
0052     }
0053     return userScriptList;
0054 }
0055 
0056 bool QmlUserScripts::contains(QObject *object) const
0057 {
0058     auto *userScript = qobject_cast<QmlUserScript*>(object);
0059     if (!userScript) {
0060         return false;
0061     }
0062     QWebEngineScript webEngineScript = userScript->webEngineScript();
0063     return mApp->webProfile()->scripts()->contains(webEngineScript);
0064 }
0065 
0066 QObject *QmlUserScripts::findScript(const QString &name) const
0067 {
0068     auto *qmlUserScript = new QmlUserScript();
0069 
0070     auto scripts = mApp->webProfile()->scripts()->find(name);
0071     if (!scripts.empty()) {
0072         qmlUserScript->setWebEngineScript(scripts.first());
0073     }
0074 
0075     return qmlUserScript;
0076 }
0077 
0078 QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
0079 {
0080     QList<QWebEngineScript> list = mApp->webProfile()->scripts()->find(name);
0081     return toQObjectList(list);
0082 }
0083 
0084 void QmlUserScripts::remove(QObject *object) const
0085 {
0086     auto *userScript = qobject_cast<QmlUserScript*>(object);
0087     if (!userScript) {
0088         return;
0089     }
0090     QWebEngineScript webEngineScript = userScript->webEngineScript();
0091     mApp->webProfile()->scripts()->remove(webEngineScript);
0092 }
0093 
0094 QList<QObject*> QmlUserScripts::toList() const
0095 {
0096     QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList();
0097     return toQObjectList(list);
0098 }
0099 
0100 void QmlUserScripts::insert(QObject *object)
0101 {
0102     auto *userScript = qobject_cast<QmlUserScript*>(object);
0103     if (!userScript) {
0104         return;
0105     }
0106     QWebEngineScript webEngineScript = userScript->webEngineScript();
0107     mApp->webProfile()->scripts()->insert(webEngineScript);
0108 }