Falkon Develop
Cross-platform Qt-based web browser
qmluserscripts.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmluserscripts.h"
19#include "mainapplication.h"
20#include <QWebEngineProfile>
21#include <QWebEngineScriptCollection>
22#include <QQmlEngine>
23
25 : QObject(parent)
26{
27}
28
29int QmlUserScripts::count() const
30{
31 return mApp->webProfile()->scripts()->count();
32}
33
34int QmlUserScripts::size() const
35{
36 return mApp->webProfile()->scripts()->count();
37}
38
39bool QmlUserScripts::empty() const
40{
41 return mApp->webProfile()->scripts()->isEmpty();
42}
43
44QList<QObject*> QmlUserScripts::toQObjectList(const QList<QWebEngineScript> &list) const
45{
46 QList<QObject*> userScriptList;
47 userScriptList.reserve(list.size());
48 for (const QWebEngineScript &script : list) {
49 auto *userScript = new QmlUserScript();
50 userScript->setWebEngineScript(script);
51 userScriptList.append(userScript);
52 }
53 return userScriptList;
54}
55
56bool QmlUserScripts::contains(QObject *object) const
57{
58 auto *userScript = qobject_cast<QmlUserScript*>(object);
59 if (!userScript) {
60 return false;
61 }
62 QWebEngineScript webEngineScript = userScript->webEngineScript();
63 return mApp->webProfile()->scripts()->contains(webEngineScript);
64}
65
66QObject *QmlUserScripts::findScript(const QString &name) const
67{
68 auto *qmlUserScript = new QmlUserScript();
69
70 auto scripts = mApp->webProfile()->scripts()->find(name);
71 if (!scripts.empty()) {
72 qmlUserScript->setWebEngineScript(scripts.first());
73 }
74
75 return qmlUserScript;
76}
77
78QList<QObject*> QmlUserScripts::findScripts(const QString &name) const
79{
80 QList<QWebEngineScript> list = mApp->webProfile()->scripts()->find(name);
81 return toQObjectList(list);
82}
83
84void QmlUserScripts::remove(QObject *object) const
85{
86 auto *userScript = qobject_cast<QmlUserScript*>(object);
87 if (!userScript) {
88 return;
89 }
90 QWebEngineScript webEngineScript = userScript->webEngineScript();
91 mApp->webProfile()->scripts()->remove(webEngineScript);
92}
93
94QList<QObject*> QmlUserScripts::toList() const
95{
96 QList<QWebEngineScript> list = mApp->webProfile()->scripts()->toList();
97 return toQObjectList(list);
98}
99
100void QmlUserScripts::insert(QObject *object)
101{
102 auto *userScript = qobject_cast<QmlUserScript*>(object);
103 if (!userScript) {
104 return;
105 }
106 QWebEngineScript webEngineScript = userScript->webEngineScript();
107 mApp->webProfile()->scripts()->insert(webEngineScript);
108}
The class exposing QWebEngineScript to QML.
Definition: qmluserscript.h:30
int count
Number of elements in the collection.
int size
Size of the collection.
Q_INVOKABLE bool contains(QObject *object) const
Checks if the script is in collection.
Q_INVOKABLE QList< QObject * > toList() const
Gets all the scripts of the collection.
Q_INVOKABLE QObject * findScript(const QString &name) const
Finds a script in collection by name.
bool empty
Checks if the collection is empty.
QmlUserScripts(QObject *parent=nullptr)
Q_INVOKABLE void remove(QObject *object) const
Removes a script from collection.
void insert(QObject *object)
Q_INVOKABLE QList< QObject * > findScripts(const QString &name) const
Finds all scripts in collection by a given name.
#define mApp