1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
SPDX-FileCopyrightText: 2014 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "containmentinterface.h"
#include <Plasma/Applet>
#include <Plasma/Containment>
#include <Plasma/Corona>
#include <PlasmaQuick/AppletQuickItem>
#include <KActionCollection>
// FIXME HACK TODO: Unfortunately we have no choice but to hard-code a list of
// applets we know to expose the correct interface right now -- this is slated
// for replacement with some form of generic service.
QStringList ContainmentInterface::m_knownTaskManagers{
QLatin1String("org.kde.plasma.taskmanager"),
QLatin1String("org.kde.plasma.icontasks"),
QLatin1String("org.kde.plasma.expandingiconstaskmanager"),
};
ContainmentInterface::ContainmentInterface(QObject *parent)
: QObject(parent)
{
}
ContainmentInterface::~ContainmentInterface()
{
}
bool ContainmentInterface::mayAddLauncher(QObject *appletInterface, ContainmentInterface::Target target, const KService::Ptr &service)
{
if (!appletInterface) {
return false;
}
Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
Plasma::Containment *containment = applet->containment();
if (!containment) {
return false;
}
Plasma::Corona *corona = containment->corona();
if (!corona) {
return false;
}
switch (target) {
case Desktop: {
containment = corona->containmentForScreen(containment->screen(), QString(), QString());
if (containment) {
return (containment->immutability() == Plasma::Types::Mutable);
}
break;
}
case Panel: {
if (containment->pluginMetaData().pluginId() == QLatin1String("org.kde.panel")) {
return (containment->immutability() == Plasma::Types::Mutable);
}
break;
}
case TaskManager: {
if (service && containment->pluginMetaData().pluginId() == QLatin1String("org.kde.panel")) {
auto *taskManager = findTaskManagerApplet(containment);
if (!taskManager) {
return false;
}
auto *taskManagerQuickItem = PlasmaQuick::AppletQuickItem::itemForApplet(taskManager);
if (!taskManagerQuickItem) {
return false;
}
return taskManagerQuickItem->property("supportsLaunchers").toBool();
}
break;
}
}
return false;
}
bool ContainmentInterface::hasLauncher(QObject *appletInterface, ContainmentInterface::Target target, const KService::Ptr &service)
{
// Only the task manager supports toggle-able launchers
if (target != TaskManager) {
return false;
}
if (!appletInterface) {
return false;
}
Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
Plasma::Containment *containment = applet->containment();
if (!containment) {
return false;
}
if (service && containment->pluginMetaData().pluginId() == QLatin1String("org.kde.panel")) {
auto *taskManager = findTaskManagerApplet(containment);
if (!taskManager) {
return false;
}
auto *taskManagerQuickItem = PlasmaQuick::AppletQuickItem::itemForApplet(taskManager);
if (!taskManagerQuickItem) {
return false;
}
bool ret;
QMetaObject::invokeMethod(taskManagerQuickItem,
"hasLauncher",
Q_RETURN_ARG(bool, ret),
Q_ARG(QUrl, QUrl(QLatin1String("applications:") + service->storageId())));
return ret;
}
return false;
}
void ContainmentInterface::addLauncher(QObject *appletInterface, ContainmentInterface::Target target, const QString &entryPath)
{
if (!appletInterface) {
return;
}
Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
Plasma::Containment *containment = applet->containment();
if (!containment) {
return;
}
Plasma::Corona *corona = containment->corona();
if (!corona) {
return;
}
switch (target) {
case Desktop: {
containment = corona->containmentForScreen(containment->screen(), QString(), QString());
if (!containment) {
return;
}
const QStringList &containmentProvides = containment->pluginMetaData().value(QStringLiteral("X-Plasma-Provides"), QStringList());
if (containmentProvides.contains(QLatin1String("org.kde.plasma.filemanagement"))) {
auto *folderQuickItem = PlasmaQuick::AppletQuickItem::itemForApplet(containment);
if (!folderQuickItem) {
return;
}
QMetaObject::invokeMethod(folderQuickItem, "addLauncher", Q_ARG(QVariant, QUrl::fromLocalFile(entryPath)));
} else {
containment->createApplet(QStringLiteral("org.kde.plasma.icon"), QVariantList() << QUrl::fromLocalFile(entryPath));
}
break;
}
case Panel: {
if (containment->pluginMetaData().pluginId() == QLatin1String("org.kde.panel")) {
containment->createApplet(QStringLiteral("org.kde.plasma.icon"), QVariantList() << QUrl::fromLocalFile(entryPath));
}
break;
}
case TaskManager: {
if (containment->pluginMetaData().pluginId() == QLatin1String("org.kde.panel")) {
auto *taskManager = findTaskManagerApplet(containment);
if (!taskManager) {
return;
}
auto *taskManagerQuickItem = PlasmaQuick::AppletQuickItem::itemForApplet(taskManager);
if (!taskManagerQuickItem) {
return;
}
QMetaObject::invokeMethod(taskManagerQuickItem, "addLauncher", Q_ARG(QUrl, QUrl::fromLocalFile(entryPath)));
}
break;
}
}
}
QObject *ContainmentInterface::screenContainment(QObject *appletInterface)
{
if (!appletInterface) {
return nullptr;
}
const Plasma::Applet *applet = appletInterface->property("_plasma_applet").value<Plasma::Applet *>();
Plasma::Containment *containment = applet->containment();
if (!containment) {
return nullptr;
}
Plasma::Corona *corona = containment->corona();
if (!corona) {
return nullptr;
}
return corona->containmentForScreen(containment->screen(), QString(), QString());
}
bool ContainmentInterface::screenContainmentMutable(QObject *appletInterface)
{
const Plasma::Containment *containment = static_cast<const Plasma::Containment *>(screenContainment(appletInterface));
if (containment) {
return (containment->immutability() == Plasma::Types::Mutable);
}
return false;
}
void ContainmentInterface::ensureMutable(Plasma::Containment *containment)
{
if (containment && containment->immutability() != Plasma::Types::Mutable) {
containment->internalAction(QStringLiteral("lock widgets"))->trigger();
}
}
Plasma::Applet *ContainmentInterface::findTaskManagerApplet(Plasma::Containment *containment)
{
const QList<Plasma::Applet *> applets = containment->applets();
const auto found = std::find_if(applets.cbegin(), applets.cend(), [](const Plasma::Applet *applet) {
return m_knownTaskManagers.contains(applet->pluginMetaData().pluginId());
});
return found != applets.cend() ? *found : nullptr;
}
#include "moc_containmentinterface.cpp"
|