From 7ce196b5552b2acfa2d54e46809dd4248b63a156 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 25 Dec 2017 21:26:18 +0100 Subject: Load files from qml --- framework/src/CMakeLists.txt | 1 + framework/src/file.cpp | 46 +++++++++++++++++++++++++++++++++++++++ framework/src/file.h | 38 ++++++++++++++++++++++++++++++++ framework/src/frameworkplugin.cpp | 2 ++ 4 files changed, 87 insertions(+) create mode 100644 framework/src/file.cpp create mode 100644 framework/src/file.h diff --git a/framework/src/CMakeLists.txt b/framework/src/CMakeLists.txt index fab7bf20..5ad5e910 100644 --- a/framework/src/CMakeLists.txt +++ b/framework/src/CMakeLists.txt @@ -53,6 +53,7 @@ add_library(kubeframework SHARED domainobjectcontroller.cpp extensionmodel.cpp viewhighlighter.cpp + file.cpp ) generate_export_header(kubeframework BASE_NAME Kube EXPORT_FILE_NAME kube_export.h) set_target_properties(kubeframework PROPERTIES diff --git a/framework/src/file.cpp b/framework/src/file.cpp new file mode 100644 index 00000000..b2a19c23 --- /dev/null +++ b/framework/src/file.cpp @@ -0,0 +1,46 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "file.h" +#include +#include +#include +#include + +using namespace Kube; + +QString File::data() +{ + auto s = read(mPath); + if (s.isEmpty()) { + return mDefaultContent; + } + return s; +} + +QString File::read(const QString &path) +{ + QFile file(QDir::homePath() + "/" + path); + if (file.open(QIODevice::ReadOnly)) { + return file.readAll(); + } + qWarning() << "Failed to open the file " << file.fileName() << file.errorString(); + return {}; +} + diff --git a/framework/src/file.h b/framework/src/file.h new file mode 100644 index 00000000..6d894250 --- /dev/null +++ b/framework/src/file.h @@ -0,0 +1,38 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library 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 Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include + +namespace Kube { + +class File : public QObject { + Q_OBJECT + Q_PROPERTY(QString path MEMBER mPath) + Q_PROPERTY(QString defaultContent MEMBER mDefaultContent) + Q_PROPERTY(QString data READ data CONSTANT) +public: + QString data(); + Q_INVOKABLE QString read(const QString &path); +private: + QString mPath; + QString mDefaultContent; +}; + +} diff --git a/framework/src/frameworkplugin.cpp b/framework/src/frameworkplugin.cpp index bfedc752..b5635733 100644 --- a/framework/src/frameworkplugin.cpp +++ b/framework/src/frameworkplugin.cpp @@ -45,6 +45,7 @@ #include "domainobjectcontroller.h" #include "extensionmodel.h" #include "viewhighlighter.h" +#include "file.h" #include #include @@ -181,6 +182,7 @@ void FrameworkPlugin::registerTypes (const char *uri) qmlRegisterType(uri, 1, 0, "AccountFactory"); qmlRegisterType(uri, 1, 0, "AccountsModel"); qmlRegisterType(uri, 1, 0, "ExtensionModel"); + qmlRegisterType(uri, 1, 0, "File"); qmlRegisterType(uri, 1, 0, "Settings"); -- cgit v1.2.3