|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Kai Uwe Broulik <kde@privat.broulik.de> |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +#include "actions.h" |
| 21 | + |
| 22 | +#include <QDBusConnection> |
| 23 | +#include <QDBusMessage> |
| 24 | +#include <QDBusPendingCallWatcher> |
| 25 | +#include <QDBusPendingReply> |
| 26 | +#include <QDebug> |
| 27 | +#include <QStringList> |
| 28 | +#include <QVariantList> |
| 29 | + |
| 30 | +static const QString s_orgGtkActions = QStringLiteral("org.gtk.Actions"); |
| 31 | + |
| 32 | +Actions::Actions(const QString &serviceName, const QString &objectPath, QObject *parent) |
| 33 | + : QObject(parent) |
| 34 | + , m_serviceName(serviceName) |
| 35 | + , m_objectPath(objectPath) |
| 36 | +{ |
| 37 | + Q_ASSERT(!serviceName.isEmpty()); |
| 38 | + Q_ASSERT(!m_objectPath.isEmpty()); |
| 39 | + |
| 40 | + if (!QDBusConnection::sessionBus().connect(serviceName, |
| 41 | + objectPath, |
| 42 | + s_orgGtkActions, |
| 43 | + QStringLiteral("Changed"), |
| 44 | + this, |
| 45 | + SLOT(onActionsChanged(QStringList,StringBoolMap,QVariantMap,GMenuActionMap)))) { |
| 46 | + qDebug() << "Failed to subscribe to action changes for" << parent << "on" << serviceName << "at" << objectPath; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +Actions::~Actions() = default; |
| 51 | + |
| 52 | +void Actions::load() |
| 53 | +{ |
| 54 | + QDBusMessage msg = QDBusMessage::createMethodCall(m_serviceName, |
| 55 | + m_objectPath, |
| 56 | + s_orgGtkActions, |
| 57 | + QStringLiteral("DescribeAll")); |
| 58 | + |
| 59 | + QDBusPendingReply<GMenuActionMap> reply = QDBusConnection::sessionBus().asyncCall(msg); |
| 60 | + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); |
| 61 | + connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { |
| 62 | + QDBusPendingReply<GMenuActionMap> reply = *watcher; |
| 63 | + if (reply.isError()) { |
| 64 | + qDebug() << "Failed to get actions from" << m_serviceName << "at" << m_objectPath << reply.error(); |
| 65 | + emit failedToLoad(); |
| 66 | + } else { |
| 67 | + m_actions = reply.value(); |
| 68 | + emit loaded(); |
| 69 | + } |
| 70 | + watcher->deleteLater(); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +bool Actions::get(const QString &name, GMenuAction &action) const |
| 75 | +{ |
| 76 | + auto it = m_actions.find(name); |
| 77 | + if (it == m_actions.constEnd()) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + action = *it; |
| 82 | + return true; |
| 83 | +} |
| 84 | + |
| 85 | +GMenuActionMap Actions::getAll() const |
| 86 | +{ |
| 87 | + return m_actions; |
| 88 | +} |
| 89 | + |
| 90 | +void Actions::trigger(const QString &name, const QVariant &target, uint timestamp) |
| 91 | +{ |
| 92 | + if (!m_actions.contains(name)) { |
| 93 | + qDebug() << "Cannot invoke action" << name << "which doesn't exist"; |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + QDBusMessage msg = QDBusMessage::createMethodCall(m_serviceName, |
| 98 | + m_objectPath, |
| 99 | + s_orgGtkActions, |
| 100 | + QStringLiteral("Activate")); |
| 101 | + msg << name; |
| 102 | + |
| 103 | + QVariantList args; |
| 104 | + if (target.isValid()) { |
| 105 | + args << target; |
| 106 | + } |
| 107 | + msg << QVariant::fromValue(args); |
| 108 | + |
| 109 | + QVariantMap platformData; |
| 110 | + |
| 111 | + if (timestamp) { |
| 112 | + // From documentation: |
| 113 | + // If the startup notification id is not available, this can be just "_TIMEtime", where |
| 114 | + // time is the time stamp from the event triggering the call. |
| 115 | + // see also gtkwindow.c extract_time_from_startup_id and startup_id_is_fake |
| 116 | + platformData.insert(QStringLiteral("desktop-startup-id"), QStringLiteral("_TIME") + QString::number(timestamp)); |
| 117 | + } |
| 118 | + |
| 119 | + msg << platformData; |
| 120 | + |
| 121 | + QDBusPendingReply<void> reply = QDBusConnection::sessionBus().asyncCall(msg); |
| 122 | + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); |
| 123 | + connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, name](QDBusPendingCallWatcher *watcher) { |
| 124 | + QDBusPendingReply<void> reply = *watcher; |
| 125 | + if (reply.isError()) { |
| 126 | + qDebug() << "Failed to invoke action" << name << "on" << m_serviceName << "at" << m_objectPath << reply.error(); |
| 127 | + } |
| 128 | + watcher->deleteLater(); |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +bool Actions::isValid() const |
| 133 | +{ |
| 134 | + return !m_actions.isEmpty(); |
| 135 | +} |
| 136 | + |
| 137 | +void Actions::onActionsChanged(const QStringList &removed, |
| 138 | + const StringBoolMap &enabledChanges, |
| 139 | + const QVariantMap &stateChanges, |
| 140 | + const GMenuActionMap &added) |
| 141 | +{ |
| 142 | + // Collect the actions that we removed, altered, or added, so we can eventually signal changes for all menus that contain one of those actions |
| 143 | + QStringList dirtyActions; |
| 144 | + |
| 145 | + // TODO I bet for most of the loops below we could use a nice short std algorithm |
| 146 | + |
| 147 | + for (const QString &removedAction : removed) { |
| 148 | + if (m_actions.remove(removedAction)) { |
| 149 | + dirtyActions.append(removedAction); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + for (auto it = enabledChanges.constBegin(), end = enabledChanges.constEnd(); it != end; ++it) { |
| 154 | + const QString &actionName = it.key(); |
| 155 | + const bool enabled = it.value(); |
| 156 | + |
| 157 | + auto actionIt = m_actions.find(actionName); |
| 158 | + if (actionIt == m_actions.end()) { |
| 159 | + qDebug() << "Got enabled changed for action" << actionName << "which we don't know"; |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + GMenuAction &action = *actionIt; |
| 164 | + if (action.enabled != enabled) { |
| 165 | + action.enabled = enabled; |
| 166 | + dirtyActions.append(actionName); |
| 167 | + } else { |
| 168 | + qDebug() << "Got enabled change for action" << actionName << "which didn't change it"; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + for (auto it = stateChanges.constBegin(), end = stateChanges.constEnd(); it != end; ++it) { |
| 173 | + const QString &actionName = it.key(); |
| 174 | + const QVariant &state = it.value(); |
| 175 | + |
| 176 | + auto actionIt = m_actions.find(actionName); |
| 177 | + if (actionIt == m_actions.end()) { |
| 178 | + qDebug() << "Got state changed for action" << actionName << "which we don't know"; |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + GMenuAction &action = *actionIt; |
| 183 | + |
| 184 | + if (action.state.isEmpty()) { |
| 185 | + qDebug() << "Got new state for action" << actionName << "that didn't have any state before"; |
| 186 | + action.state.append(state); |
| 187 | + dirtyActions.append(actionName); |
| 188 | + } else { |
| 189 | + // Action state is a list but the state change only sends us a single variant, so just overwrite the first one |
| 190 | + QVariant &firstState = action.state.first(); |
| 191 | + if (firstState != state) { |
| 192 | + firstState = state; |
| 193 | + dirtyActions.append(actionName); |
| 194 | + } else { |
| 195 | + qDebug() << "Got state change for action" << actionName << "which didn't change it"; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + // unite() will result in keys being present multiple times, do it manually and overwrite existing ones |
| 201 | + for (auto it = added.constBegin(), end = added.constEnd(); it != end; ++it) { |
| 202 | + const QString &actionName = it.key(); |
| 203 | + |
| 204 | +// if (DBUSMENUPROXY().isInfoEnabled()) { |
| 205 | +// if (m_actions.contains(actionName)) { |
| 206 | +// qDebug() << "Got new action" << actionName << "that we already have, overwriting existing one"; |
| 207 | +// } |
| 208 | +// } |
| 209 | + |
| 210 | + m_actions.insert(actionName, it.value()); |
| 211 | + |
| 212 | + dirtyActions.append(actionName); |
| 213 | + } |
| 214 | + |
| 215 | + if (!dirtyActions.isEmpty()) { |
| 216 | + emit actionsChanged(dirtyActions); |
| 217 | + } |
| 218 | +} |
0 commit comments