File indexing completed on 2025-11-02 05:20:20

0001 /*
0002     SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org>
0003     SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "documenthandler.h"
0009 #include "note.h"
0010 #include "notemanager.h"
0011 
0012 #include <QFile>
0013 #include <QQmlEngine>
0014 #include <QQmlExtensionPlugin>
0015 
0016 class NotesHelper : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     explicit NotesHelper(QObject *parent = nullptr)
0022         : QObject(parent)
0023     {
0024     }
0025 
0026     ~NotesHelper() override = default;
0027 
0028     Q_INVOKABLE QString fileContents(const QString &path) const
0029     {
0030         const QUrl &url = QUrl::fromUserInput(path);
0031         if (!url.isValid()) {
0032             return QString();
0033         }
0034 
0035         QFile file(url.toLocalFile());
0036         if (!file.open(QIODevice::ReadOnly)) {
0037             return QString();
0038         }
0039 
0040         return QString::fromUtf8(file.readAll());
0041     }
0042 };
0043 
0044 class NotesPlugin : public QQmlExtensionPlugin
0045 {
0046     Q_OBJECT
0047     Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
0048 public:
0049     void registerTypes(const char *uri) override
0050 
0051     {
0052         Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.plasma.private.notes"));
0053         qmlRegisterType<DocumentHandler>(uri, 0, 1, "DocumentHandler");
0054         qmlRegisterType<NoteManager>(uri, 0, 1, "NoteManager");
0055         qmlRegisterUncreatableType<Note>(uri, 0, 1, "Note", QStringLiteral("Create through NoteManager"));
0056         qmlRegisterSingletonType<NotesHelper>(uri, 0, 1, "NotesHelper", [](QQmlEngine *, QJSEngine *) {
0057             return new NotesHelper();
0058         });
0059     }
0060 };
0061 
0062 #include "notesplugin.moc"