refactor:move qml handling from globalshortcuts
--all the qml handling code from globalshortcuts is now moved to Latte::View::ContaimentInterface. This way the code is cleaner and can be expanded easier.pull/16/head
parent
d72745910a
commit
55cb8efb17
@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* This file is part of Latte-Dock
|
||||
*
|
||||
* Latte-Dock is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* Latte-Dock 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "containmentinterface.h"
|
||||
|
||||
// local
|
||||
#include "view.h"
|
||||
#include "../lattecorona.h"
|
||||
#include "../settings/universalsettings.h"
|
||||
|
||||
// Qt
|
||||
#include <QDebug>
|
||||
|
||||
// Plasma
|
||||
#include <Plasma/Applet>
|
||||
#include <Plasma/Containment>
|
||||
|
||||
// KDE
|
||||
#include <KLocalizedString>
|
||||
#include <KPluginMetaData>
|
||||
|
||||
namespace Latte {
|
||||
namespace ViewPart {
|
||||
|
||||
ContainmentInterface::ContainmentInterface(Latte::View *parent)
|
||||
: QObject(parent),
|
||||
m_view(parent)
|
||||
{
|
||||
m_corona = qobject_cast<Latte::Corona *>(m_view->corona());
|
||||
}
|
||||
|
||||
ContainmentInterface::~ContainmentInterface()
|
||||
{
|
||||
}
|
||||
|
||||
void ContainmentInterface::identifyMainItem()
|
||||
{
|
||||
if (m_mainItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (QQuickItem *graphicItem = m_view->containment()->property("_plasma_graphicObject").value<QQuickItem *>()) {
|
||||
const auto &childItems = graphicItem->childItems();
|
||||
|
||||
for (QQuickItem *item : childItems) {
|
||||
if (item->objectName() == "containmentViewLayout" ) {
|
||||
m_mainItem = item;
|
||||
identifyMethods();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ContainmentInterface::identifyMethods()
|
||||
{
|
||||
int aeIndex = m_mainItem->metaObject()->indexOfMethod("activateEntryAtIndex(QVariant)");
|
||||
int niIndex = m_mainItem->metaObject()->indexOfMethod("newInstanceForEntryAtIndex(QVariant)");
|
||||
int sbIndex = m_mainItem->metaObject()->indexOfMethod("setShowAppletShortcutBadges(QVariant,QVariant,QVariant,QVariant)");
|
||||
|
||||
m_activateEntryMethod = m_mainItem->metaObject()->method(aeIndex);
|
||||
m_newInstanceMethod = m_mainItem->metaObject()->method(niIndex);
|
||||
m_showShortcutsMethod = m_mainItem->metaObject()->method(sbIndex);
|
||||
}
|
||||
|
||||
bool ContainmentInterface::containsApplicationLauncher() const
|
||||
{
|
||||
return applicationLauncherId() == -1 ? false : true;
|
||||
}
|
||||
|
||||
bool ContainmentInterface::isCapableToShowShortcutBadges() const
|
||||
{
|
||||
if (!m_view->latteTasksArePresent() && m_view->tasksPresent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_showShortcutsMethod.isValid();
|
||||
}
|
||||
|
||||
int ContainmentInterface::applicationLauncherId() const
|
||||
{
|
||||
const auto applets = m_view->containment()->applets();
|
||||
|
||||
for (auto applet : applets) {
|
||||
const auto provides = applet->kPackage().metadata().value(QStringLiteral("X-Plasma-Provides"));
|
||||
|
||||
if (provides.contains(QLatin1String("org.kde.plasma.launchermenu"))) {
|
||||
return applet->id();
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool ContainmentInterface::updateBadgeForLatteTask(const QString identifier, const QString value)
|
||||
{
|
||||
if (!m_view->latteTasksArePresent()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto &applets = m_view->containment()->applets();
|
||||
|
||||
for (auto *applet : applets) {
|
||||
KPluginMetaData meta = applet->kPackage().metadata();
|
||||
|
||||
if (meta.pluginId() == "org.kde.latte.plasmoid") {
|
||||
|
||||
if (QQuickItem *appletInterface = applet->property("_plasma_graphicObject").value<QQuickItem *>()) {
|
||||
const auto &childItems = appletInterface->childItems();
|
||||
|
||||
if (childItems.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (QQuickItem *item : childItems) {
|
||||
if (auto *metaObject = item->metaObject()) {
|
||||
// not using QMetaObject::invokeMethod to avoid warnings when calling
|
||||
// this on applets that don't have it or other child items since this
|
||||
// is pretty much trial and error.
|
||||
// Also, "var" arguments are treated as QVariant in QMetaObject
|
||||
|
||||
int methodIndex = metaObject->indexOfMethod("updateBadge(QVariant,QVariant)");
|
||||
|
||||
if (methodIndex == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QMetaMethod method = metaObject->method(methodIndex);
|
||||
|
||||
if (method.invoke(item, Q_ARG(QVariant, identifier), Q_ARG(QVariant, value))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ContainmentInterface::activatePlasmaTask(const int index)
|
||||
{
|
||||
bool containsPlasmaTaskManager{m_view->tasksPresent() && !m_view->latteTasksArePresent()};
|
||||
|
||||
if (!containsPlasmaTaskManager) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto &applets = m_view->containment()->applets();
|
||||
|
||||
for (auto *applet : applets) {
|
||||
const auto &provides = KPluginMetaData::readStringList(applet->pluginMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
|
||||
|
||||
if (provides.contains(QLatin1String("org.kde.plasma.multitasking"))) {
|
||||
if (QQuickItem *appletInterface = applet->property("_plasma_graphicObject").value<QQuickItem *>()) {
|
||||
const auto &childItems = appletInterface->childItems();
|
||||
|
||||
if (childItems.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
KPluginMetaData meta = applet->kPackage().metadata();
|
||||
|
||||
for (QQuickItem *item : childItems) {
|
||||
if (auto *metaObject = item->metaObject()) {
|
||||
int methodIndex{metaObject->indexOfMethod("activateTaskAtIndex(QVariant)")};
|
||||
|
||||
if (methodIndex == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QMetaMethod method = metaObject->method(methodIndex);
|
||||
|
||||
if (method.invoke(item, Q_ARG(QVariant, index - 1))) {
|
||||
showShortcutBadges(false, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ContainmentInterface::newInstanceForPlasmaTask(const int index)
|
||||
{
|
||||
bool containsPlasmaTaskManager{m_view->tasksPresent() && !m_view->latteTasksArePresent()};
|
||||
|
||||
if (!containsPlasmaTaskManager) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto &applets = m_view->containment()->applets();
|
||||
|
||||
for (auto *applet : applets) {
|
||||
const auto &provides = KPluginMetaData::readStringList(applet->pluginMetaData().rawData(), QStringLiteral("X-Plasma-Provides"));
|
||||
|
||||
if (provides.contains(QLatin1String("org.kde.plasma.multitasking"))) {
|
||||
if (QQuickItem *appletInterface = applet->property("_plasma_graphicObject").value<QQuickItem *>()) {
|
||||
const auto &childItems = appletInterface->childItems();
|
||||
|
||||
if (childItems.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
KPluginMetaData meta = applet->kPackage().metadata();
|
||||
|
||||
for (QQuickItem *item : childItems) {
|
||||
if (auto *metaObject = item->metaObject()) {
|
||||
int methodIndex{metaObject->indexOfMethod("ewInstanceForTaskAtIndex(QVariant)")};
|
||||
|
||||
if (methodIndex == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QMetaMethod method = metaObject->method(methodIndex);
|
||||
|
||||
if (method.invoke(item, Q_ARG(QVariant, index - 1))) {
|
||||
showShortcutBadges(false, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ContainmentInterface::activateEntry(const int index)
|
||||
{
|
||||
identifyMainItem();
|
||||
|
||||
if (!m_activateEntryMethod.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_activateEntryMethod.invoke(m_mainItem, Q_ARG(QVariant, index));
|
||||
}
|
||||
|
||||
bool ContainmentInterface::newInstanceForEntry(const int index)
|
||||
{
|
||||
identifyMainItem();
|
||||
|
||||
if (!m_newInstanceMethod.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_newInstanceMethod.invoke(m_mainItem, Q_ARG(QVariant, index));
|
||||
}
|
||||
|
||||
bool ContainmentInterface::hideShortcutBadges()
|
||||
{
|
||||
identifyMainItem();
|
||||
|
||||
if (!m_showShortcutsMethod.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_showShortcutsMethod.invoke(m_mainItem, Q_ARG(QVariant, false), Q_ARG(QVariant, false), Q_ARG(QVariant, false), Q_ARG(QVariant, -1));
|
||||
}
|
||||
|
||||
bool ContainmentInterface::showOnlyMeta()
|
||||
{
|
||||
if (!m_corona->universalSettings()->kwin_metaForwardedToLatte()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return showShortcutBadges(false, true);
|
||||
}
|
||||
|
||||
bool ContainmentInterface::showShortcutBadges(const bool showLatteShortcuts, const bool showMeta)
|
||||
{
|
||||
identifyMainItem();
|
||||
|
||||
if (!m_showShortcutsMethod.isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int appLauncherId = m_corona->universalSettings()->kwin_metaForwardedToLatte() && showMeta ? applicationLauncherId() : -1;
|
||||
|
||||
return m_showShortcutsMethod.invoke(m_mainItem, Q_ARG(QVariant, showLatteShortcuts), Q_ARG(QVariant, true), Q_ARG(QVariant, showMeta), Q_ARG(QVariant, appLauncherId));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2019 Michail Vourlakos <mvourlakos@gmail.com>
|
||||
*
|
||||
* This file is part of Latte-Dock
|
||||
*
|
||||
* Latte-Dock is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* Latte-Dock 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef VIEWCONTAINMENTINTERFACE_H
|
||||
#define VIEWCONTAINMENTINTERFACE_H
|
||||
|
||||
// Qt
|
||||
#include <QMetaMethod>
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
#include <QQuickItem>
|
||||
|
||||
namespace Latte {
|
||||
class Corona;
|
||||
class View;
|
||||
}
|
||||
|
||||
namespace Latte {
|
||||
namespace ViewPart {
|
||||
|
||||
class ContainmentInterface: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ContainmentInterface(Latte::View *parent);
|
||||
virtual ~ContainmentInterface();
|
||||
|
||||
bool containsApplicationLauncher() const;
|
||||
bool isCapableToShowShortcutBadges() const;
|
||||
|
||||
bool activateEntry(const int index);
|
||||
bool newInstanceForEntry(const int index);
|
||||
|
||||
bool activatePlasmaTask(const int index);
|
||||
bool newInstanceForPlasmaTask(const int index);
|
||||
|
||||
bool hideShortcutBadges();
|
||||
bool showOnlyMeta();
|
||||
bool showShortcutBadges(const bool showLatteShortcuts, const bool showMeta);
|
||||
|
||||
//! this is updated from external apps e.g. a thunderbird plugin
|
||||
bool updateBadgeForLatteTask(const QString identifier, const QString value);
|
||||
|
||||
int applicationLauncherId() const;
|
||||
|
||||
private slots:
|
||||
void identifyMainItem();
|
||||
void identifyMethods();
|
||||
|
||||
private:
|
||||
QMetaMethod m_activateEntryMethod;
|
||||
QMetaMethod m_newInstanceMethod;
|
||||
QMetaMethod m_showShortcutsMethod;
|
||||
|
||||
QPointer<Latte::Corona> m_corona;
|
||||
QPointer<Latte::View> m_view;
|
||||
QPointer<QQuickItem> m_mainItem;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue