mirror of
https://invent.kde.org/network/falkon.git
synced 2024-12-20 02:36:34 +01:00
Created FileUtils API
This commit is contained in:
parent
855b30b575
commit
68201ffe59
@ -188,6 +188,7 @@ set(SRCS ${SRCS}
|
||||
plugins/qml/api/userscript/qmlexternaljsobject.cpp
|
||||
plugins/qml/api/extensionscheme/qmlextensionscheme.cpp
|
||||
plugins/qml/api/extensionscheme/qmlwebengineurlrequestjob.cpp
|
||||
plugins/qml/api/fileutils/qmlfileutils.cpp
|
||||
popupwindow/popuplocationbar.cpp
|
||||
popupwindow/popupstatusbarmessage.cpp
|
||||
popupwindow/popupwebview.cpp
|
||||
|
62
src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp
Normal file
62
src/lib/plugins/qml/api/fileutils/qmlfileutils.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#include "qmlfileutils.h"
|
||||
#include "datapaths.h"
|
||||
#include "qztools.h"
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
|
||||
QmlFileUtils::QmlFileUtils(QString filePath, QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
// Get the plugin root directory - of the form
|
||||
// some-path-in-plugin-paths/qml/plugin-directory
|
||||
const QStringList dirs = DataPaths::allPaths(DataPaths::Plugins);
|
||||
for (QString path : dirs) {
|
||||
path.append(QSL("/qml/"));
|
||||
if (filePath.contains(path)) {
|
||||
m_path = path;
|
||||
QString pluginDirName = filePath.remove(path).split(QSL("/"))[0];
|
||||
m_path.append(pluginDirName);
|
||||
m_path.append(QSL("/"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QString QmlFileUtils::resolve(const QString &filePath)
|
||||
{
|
||||
QUrl resolvedUrl = QUrl::fromLocalFile(m_path).resolved(QUrl::fromEncoded(filePath.toUtf8()));
|
||||
QString resolvedPath = resolvedUrl.toLocalFile();
|
||||
if (resolvedPath.contains(m_path)) {
|
||||
return resolvedPath;
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QString QmlFileUtils::readAllFileContents(const QString &fileName)
|
||||
{
|
||||
QString path = resolve(fileName);
|
||||
return QzTools::readAllFileContents(path);
|
||||
}
|
||||
|
||||
bool QmlFileUtils::exists(const QString &filePath)
|
||||
{
|
||||
QString path = resolve(filePath);
|
||||
return QFile(path).exists();
|
||||
}
|
32
src/lib/plugins/qml/api/fileutils/qmlfileutils.h
Normal file
32
src/lib/plugins/qml/api/fileutils/qmlfileutils.h
Normal file
@ -0,0 +1,32 @@
|
||||
/* ============================================================
|
||||
* Falkon - Qt web browser
|
||||
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* ============================================================ */
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class QmlFileUtils : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QmlFileUtils(QString filePath, QObject *parent = nullptr);
|
||||
Q_INVOKABLE QString resolve(const QString &filePath);
|
||||
Q_INVOKABLE QString readAllFileContents(const QString &fileName);
|
||||
Q_INVOKABLE bool exists(const QString &filePath);
|
||||
private:
|
||||
QString m_path;
|
||||
};
|
@ -21,8 +21,7 @@
|
||||
QmlPluginLoader::QmlPluginLoader(const QString &path)
|
||||
{
|
||||
m_path = path;
|
||||
m_engine = new QQmlEngine();
|
||||
m_component = new QQmlComponent(m_engine, m_path);
|
||||
initEngineAndComponent();
|
||||
}
|
||||
|
||||
void QmlPluginLoader::createComponent()
|
||||
@ -37,8 +36,7 @@ void QmlPluginLoader::createComponent()
|
||||
connect(m_interface, &QmlPluginInterface::qmlPluginUnloaded, this, [this]{
|
||||
delete m_component;
|
||||
delete m_engine;
|
||||
m_engine = new QQmlEngine();
|
||||
m_component = new QQmlComponent(m_engine, m_path);
|
||||
initEngineAndComponent();
|
||||
});
|
||||
}
|
||||
|
||||
@ -57,3 +55,10 @@ void QmlPluginLoader::setName(const QString &name)
|
||||
m_interface->setName(name);
|
||||
m_engine->rootContext()->setContextProperty("__name__", name);
|
||||
}
|
||||
|
||||
void QmlPluginLoader::initEngineAndComponent()
|
||||
{
|
||||
m_engine = new QQmlEngine();
|
||||
m_component = new QQmlComponent(m_engine, m_path);
|
||||
m_engine->rootContext()->setContextProperty("__path__", m_path);
|
||||
}
|
||||
|
@ -36,4 +36,6 @@ private:
|
||||
QQmlEngine *m_engine;
|
||||
QQmlComponent *m_component;
|
||||
QmlPluginInterface *m_interface;
|
||||
|
||||
void initEngineAndComponent();
|
||||
};
|
||||
|
@ -47,6 +47,7 @@
|
||||
#include "api/userscript/qmlexternaljsobject.h"
|
||||
#include "api/extensionscheme/qmlextensionscheme.h"
|
||||
#include "api/extensionscheme/qmlwebengineurlrequestjob.h"
|
||||
#include "api/fileutils/qmlfileutils.h"
|
||||
|
||||
#ifdef LibIntl_FOUND
|
||||
#include "qml/api/i18n/qmli18n.h"
|
||||
@ -205,8 +206,19 @@ void QmlPlugins::registerQmlTypes()
|
||||
return object;
|
||||
});
|
||||
|
||||
// SchemeHandler
|
||||
// ExtensionScheme
|
||||
qmlRegisterType<QmlExtensionScheme>("org.kde.falkon", 1, 0, "ExtensionScheme");
|
||||
|
||||
qmlRegisterUncreatableType<QmlWebEngineUrlRequestJob>("org.kde.falkon", 1, 0, "WebEngineUrlRequestJob", "Unable to register type: WebEngineUrlRequestJob");
|
||||
|
||||
// FileUtils
|
||||
qmlRegisterSingletonType<QmlFileUtils>("org.kde.falkon", 1, 0, "FileUtils", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
|
||||
Q_UNUSED(engine)
|
||||
Q_UNUSED(scriptEngine)
|
||||
|
||||
QString filePath = engine->rootContext()->contextProperty("__path__").toString();
|
||||
|
||||
auto *object = new QmlFileUtils(filePath);
|
||||
return object;
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user