1 /*
2     SPDX-FileCopyrightText: 2009 Alan Alpert <alan.alpert@nokia.com>
3     SPDX-FileCopyrightText: 2010 Ménard Alexis <menard@kde.org>
4     SPDX-FileCopyrightText: 2010 Marco Martin <mart@kde.org>
5     SPDX-FileCopyrightText: 2013 Sebastian Kügler <sebas@kde.org>
6 
7     SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "corebindingsplugin.h"
11 
12 #include <QQmlContext>
13 
14 #include <KLocalizedContext>
15 
16 #include <kdeclarative/kdeclarative.h>
17 
18 #include <plasma/framesvg.h>
19 #include <plasma/svg.h>
20 
21 #include "colorscope.h"
22 #include "datamodel.h"
23 #include "datasource.h"
24 #include "dialog.h"
25 #include "framesvgitem.h"
26 #include "iconitem.h"
27 #include "quicktheme.h"
28 #include "serviceoperationstatus.h"
29 #include "svgitem.h"
30 #include "theme.h"
31 
32 #include "tooltip.h"
33 #include "units.h"
34 #include "windowthumbnail.h"
35 #include <plasma/servicejob.h>
36 
37 // #include "dataenginebindings_p.h"
38 
39 #include <QDebug>
40 #include <QWindow>
41 
initializeEngine(QQmlEngine * engine,const char * uri)42 void CoreBindingsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
43 {
44     QQmlExtensionPlugin::initializeEngine(engine, uri);
45 
46     QQmlContext *context = engine->rootContext();
47 
48     Plasma::QuickTheme *theme = new Plasma::QuickTheme(engine);
49     context->setContextProperty(QStringLiteral("theme"), theme);
50 
51     context->setContextProperty(QStringLiteral("units"), &Units::instance());
52 
53     if (!context->contextObject()) {
54         KLocalizedContext *localizedContextObject = new KLocalizedContext(engine);
55         context->setContextObject(localizedContextObject);
56 
57         KDeclarative::KDeclarative::setupEngine(engine);
58     }
59 }
60 
registerTypes(const char * uri)61 void CoreBindingsPlugin::registerTypes(const char *uri)
62 {
63     Q_ASSERT(uri == QByteArray("org.kde.plasma.core"));
64 
65     qmlRegisterUncreatableType<Plasma::Types>(uri, 2, 0, "Types", {});
66     qmlRegisterSingletonType<Units>(uri, 2, 0, "Units", [](QQmlEngine *engine, QJSEngine *) -> QObject * {
67         engine->setObjectOwnership(&Units::instance(), QQmlEngine::CppOwnership);
68         return &Units::instance();
69     });
70 
71     qmlRegisterType<Plasma::Svg>(uri, 2, 0, "Svg");
72     qmlRegisterType<Plasma::FrameSvg>(uri, 2, 0, "FrameSvg");
73     qmlRegisterType<Plasma::SvgItem>(uri, 2, 0, "SvgItem");
74     qmlRegisterType<Plasma::FrameSvgItem>(uri, 2, 0, "FrameSvgItem");
75 
76     // qmlRegisterType<ThemeProxy>(uri, 2, 0, "Theme");
77     qmlRegisterSingletonType<Plasma::QuickTheme>(uri, 2, 0, "Theme", [](QQmlEngine *engine, QJSEngine *) -> QObject * {
78         return new Plasma::QuickTheme(engine);
79     });
80     qmlRegisterType<ColorScope>(uri, 2, 0, "ColorScope");
81 
82     qmlRegisterType<Plasma::DataSource>(uri, 2, 0, "DataSource");
83     qmlRegisterType<Plasma::DataModel>(uri, 2, 0, "DataModel");
84     qmlRegisterType<Plasma::SortFilterModel, 0>(uri, 2, 0, "SortFilterModel");
85     qmlRegisterType<Plasma::SortFilterModel, 1>(uri, 2, 1, "SortFilterModel");
86 
87     qmlRegisterType<PlasmaQuick::Dialog>(uri, 2, 0, "Dialog");
88     // HACK make properties like "opacity" work that are in REVISION 1 of QWindow
89     qmlRegisterRevision<QWindow, 1>(uri, 2, 0);
90     qmlRegisterRevision<QQuickItem, 1>(uri, 2, 0);
91     qmlRegisterType<ToolTip>(uri, 2, 0, "ToolTipArea");
92 
93     // KF6: check if it makes sense to call qmlRegisterInterface for any of these
94     // as they seem currently not used as properties and are only used from JavaScript engine
95     // due to being return types of Q_INVOKABLE methods,
96     // so registering the pointers to the qobject meta-object system would be enough:
97     // Plasma::Service, Plasma::ServiceJob
98     // So this here would become just
99     // qRegisterMetaType<Plasma::Service *>();
100     // qRegisterMetaType<Plasma::ServiceJob *>();
101     // For that also change all usages with those methods to use the fully namespaced type name
102     // in the method signature.
103     QT_WARNING_PUSH
104     QT_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
105     QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
106     // Do not to port these two for KF5 to
107     // qmlRegisterInterface<Plasma::T>(uri, 1);
108     // as this will incompatibly register with the fully namespaced name "Plasma::T",
109     // not just the now explicitly passed alias name "T"
110     qmlRegisterInterface<Plasma::Service>("Service");
111     qmlRegisterInterface<Plasma::ServiceJob>("ServiceJob");
112     QT_WARNING_POP
113 
114     qmlRegisterType<ServiceOperationStatus>(uri, 2, 0, "ServiceOperationStatus");
115     qmlRegisterAnonymousType<QAbstractItemModel>(uri, 1);
116 
117     qmlRegisterAnonymousType<QQmlPropertyMap>(uri, 1);
118     qmlRegisterType<IconItem>(uri, 2, 0, "IconItem");
119 
120     qmlRegisterType<Plasma::WindowThumbnail>(uri, 2, 0, "WindowThumbnail");
121 }
122