File indexing completed on 2024-05-12 16:59:34

0001 /*
0002  *   SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
0003  *   SPDX-FileCopyrightText: 2016 David Edmundson <davidedmundson@kde.org>
0004  *
0005  *   SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "groupedappletscontainer.h"
0009 #include "../debug.h"
0010 
0011 #include <QDebug>
0012 
0013 #include <KActionCollection>
0014 #include <Plasma/Corona>
0015 #include <QAction>
0016 
0017 GroupedAppletsContainer::GroupedAppletsContainer(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0018     : Plasma::Applet(parent, data, args)
0019 {
0020 }
0021 
0022 GroupedAppletsContainer::~GroupedAppletsContainer()
0023 {
0024     if (destroyed()) {
0025         m_innerContainment->destroy();
0026     }
0027 }
0028 
0029 void GroupedAppletsContainer::init()
0030 {
0031     Applet::init();
0032 
0033     // in the first creation we immediately create the systray: so it's accessible during desktop scripting
0034     uint id = config().readEntry("ContainmentId", 0);
0035 
0036     if (id == 0) {
0037         ensureSystrayExists();
0038     }
0039 }
0040 
0041 void GroupedAppletsContainer::ensureSystrayExists()
0042 {
0043     if (m_innerContainment) {
0044         return;
0045     }
0046 
0047     Plasma::Containment *cont = containment();
0048     if (!cont) {
0049         return;
0050     }
0051 
0052     Plasma::Corona *c = cont->corona();
0053     if (!c) {
0054         return;
0055     }
0056 
0057     uint id = config().readEntry("ContainmentId", 0);
0058     if (id > 0) {
0059         const auto containments = c->containments();
0060         for (Plasma::Containment *candidate : containments) {
0061             if (candidate->id() == id) {
0062                 m_innerContainment = candidate;
0063                 break;
0064             }
0065         }
0066         qCDebug(GROUPING_DEBUG) << "Containment id" << id << "that used to be a grouped containment that was deleted";
0067         // id = 0;
0068     }
0069 
0070     if (!m_innerContainment) {
0071         m_innerContainment = c->createContainment(QStringLiteral("org.kde.plasma.private.grouping"), //
0072                                                   QVariantList() << QStringLiteral("org.kde.plasma:force-create"));
0073         config().writeEntry("ContainmentId", m_innerContainment->id());
0074     }
0075 
0076     if (!m_innerContainment) {
0077         return;
0078     }
0079 
0080     m_innerContainment->setParent(this);
0081     connect(containment(), &Plasma::Containment::screenChanged, m_innerContainment.data(), &Plasma::Containment::reactToScreenChange);
0082     m_innerContainment->setFormFactor(formFactor());
0083     m_innerContainment->setLocation(location());
0084 
0085     m_internalContainmentItem = m_innerContainment->property("_plasma_graphicObject").value<QQuickItem *>();
0086     Q_EMIT internalContainmentItemChanged();
0087 
0088     actions()->addAction(QStringLiteral("configure"), m_innerContainment->actions()->action(QStringLiteral("configure")));
0089     connect(m_innerContainment.data(), &Plasma::Containment::configureRequested, this, [this](Plasma::Applet *applet) {
0090         Q_EMIT containment()->configureRequested(applet);
0091     });
0092 
0093     if (m_internalContainmentItem) {
0094         // don't let internal systray manage context menus
0095         m_internalContainmentItem->setAcceptedMouseButtons(Qt::NoButton);
0096     }
0097 
0098     // replace internal remove action with ours
0099     m_innerContainment->actions()->addAction(QStringLiteral("remove"), actions()->action(QStringLiteral("remove")));
0100 }
0101 
0102 void GroupedAppletsContainer::constraintsEvent(Plasma::Types::Constraints constraints)
0103 {
0104     if (constraints & Plasma::Types::LocationConstraint) {
0105         if (m_innerContainment) {
0106             m_innerContainment->setLocation(location());
0107         }
0108     }
0109     if (constraints & Plasma::Types::FormFactorConstraint) {
0110         if (m_innerContainment) {
0111             m_innerContainment->setFormFactor(formFactor());
0112         }
0113     }
0114 
0115     if (constraints & Plasma::Types::UiReadyConstraint) {
0116         ensureSystrayExists();
0117     }
0118 }
0119 
0120 QQuickItem *GroupedAppletsContainer::internalContainmentItem()
0121 {
0122     return m_internalContainmentItem;
0123 }
0124 
0125 K_PLUGIN_CLASS(GroupedAppletsContainer)
0126 
0127 #include "groupedappletscontainer.moc"