File indexing completed on 2024-05-12 17:08:48

0001 /*
0002     SPDX-FileCopyrightText: 2018 Kai Uwe Broulik <kde@privat.broulik.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "notificationapplet.h"
0008 
0009 #include <QGuiApplication>
0010 #include <QJSEngine>
0011 #include <QJSValue>
0012 #include <QQuickItem>
0013 #include <QQuickWindow>
0014 #include <QScreen>
0015 
0016 #include <KX11Extras>
0017 
0018 #include <Plasma/Containment>
0019 #include <PlasmaQuick/Dialog>
0020 
0021 #include "draghelper.h"
0022 #include "fileinfo.h"
0023 #include "filemenu.h"
0024 #include "globalshortcuts.h"
0025 #include "jobaggregator.h"
0026 #include "texteditclickhandler.h"
0027 #include "thumbnailer.h"
0028 
0029 class InputDisabler
0030 {
0031     Q_GADGET
0032 
0033 public:
0034     Q_INVOKABLE void makeTransparentForInput(QQuickItem *item)
0035     {
0036         if (item) {
0037             item->setAcceptedMouseButtons(Qt::NoButton);
0038             item->setAcceptHoverEvents(false);
0039             item->setAcceptTouchEvents(false);
0040             item->unsetCursor();
0041         }
0042     }
0043 };
0044 
0045 NotificationApplet::NotificationApplet(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0046     : Plasma::Applet(parent, data, args)
0047 {
0048     static bool s_typesRegistered = false;
0049     if (!s_typesRegistered) {
0050         const char uri[] = "org.kde.plasma.private.notifications";
0051         qmlRegisterSingletonType<DragHelper>(uri, 2, 0, "DragHelper", [](QQmlEngine *, QJSEngine *) -> QObject * {
0052             return new DragHelper();
0053         });
0054         qmlRegisterType<FileInfo>(uri, 2, 0, "FileInfo");
0055         qmlRegisterType<FileMenu>(uri, 2, 0, "FileMenu");
0056         qmlRegisterType<GlobalShortcuts>(uri, 2, 0, "GlobalShortcuts");
0057         qmlRegisterType<JobAggregator>(uri, 2, 0, "JobAggregator");
0058         qmlRegisterType<TextEditClickHandler>(uri, 2, 0, "TextEditClickHandler");
0059         qmlRegisterType<Thumbnailer>(uri, 2, 0, "Thumbnailer");
0060         qmlRegisterSingletonType(uri, 2, 0, "InputDisabler", [](QQmlEngine *, QJSEngine *jsEngine) -> QJSValue {
0061             return jsEngine->toScriptValue(InputDisabler());
0062         });
0063         qmlProtectModule(uri, 2);
0064         s_typesRegistered = true;
0065     }
0066 
0067     connect(qApp, &QGuiApplication::focusWindowChanged, this, &NotificationApplet::focussedPlasmaDialogChanged);
0068 }
0069 
0070 NotificationApplet::~NotificationApplet() = default;
0071 
0072 void NotificationApplet::init()
0073 {
0074 }
0075 
0076 void NotificationApplet::configChanged()
0077 {
0078 }
0079 
0080 QWindow *NotificationApplet::focussedPlasmaDialog() const
0081 {
0082     auto *focusWindow = qApp->focusWindow();
0083     if (qobject_cast<PlasmaQuick::Dialog *>(focusWindow)) {
0084         return focusWindow;
0085     }
0086 
0087     if (focusWindow) {
0088         return qobject_cast<PlasmaQuick::Dialog *>(focusWindow->transientParent());
0089     }
0090 
0091     return nullptr;
0092 }
0093 
0094 QQuickItem *NotificationApplet::systemTrayRepresentation() const
0095 {
0096     auto *c = containment();
0097     if (!c) {
0098         return nullptr;
0099     }
0100 
0101     if (strcmp(c->metaObject()->className(), "SystemTray") != 0) {
0102         return nullptr;
0103     }
0104 
0105     return c->property("_plasma_graphicObject").value<QQuickItem *>();
0106 }
0107 
0108 bool NotificationApplet::isPrimaryScreen(const QRect &rect) const
0109 {
0110     QScreen *screen = QGuiApplication::primaryScreen();
0111     if (!screen) {
0112         return false;
0113     }
0114 
0115     // HACK
0116     return rect == screen->geometry();
0117 }
0118 
0119 void NotificationApplet::forceActivateWindow(QWindow *window)
0120 {
0121     if (window && window->winId()) {
0122         KX11Extras::forceActiveWindow(window->winId());
0123     }
0124 }
0125 
0126 K_PLUGIN_CLASS(NotificationApplet)
0127 
0128 #include "notificationapplet.moc"