2
* Copyright (C) 2013 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Pete Woods <pete.woods@canonical.com>
19
#include <common/DBusTypes.h>
20
#include <common/Localisation.h>
21
#include <service/DBusMenuCollector.h>
22
#include <service/AppmenuRegistrarInterface.h>
24
#include <dbusmenuimporter.h>
26
#include <columbus.hh>
28
using namespace hud::common;
29
using namespace hud::service;
31
DBusMenuCollector::DBusMenuCollector(unsigned int windowId,
32
QSharedPointer<ComCanonicalAppMenuRegistrarInterface> registrar) :
33
m_windowId(windowId), m_registrar(registrar) {
35
connect(registrar.data(),
36
SIGNAL(WindowRegistered(uint, const QString &, const QDBusObjectPath &)),
38
SLOT( WindowRegistered(uint, const QString &, const QDBusObjectPath &)));
40
connect(registrar.data(), SIGNAL(
41
WindowUnregistered(uint)), this, SLOT(WindowUnregistered(uint)));
43
QDBusPendingReply<QString, QDBusObjectPath> windowReply =
44
registrar->GetMenuForWindow(m_windowId);
46
windowReply.waitForFinished();
47
if (windowReply.isError()) {
51
windowRegistered(windowReply.argumentAt<0>(), windowReply.argumentAt<1>());
54
DBusMenuCollector::~DBusMenuCollector() {
57
void DBusMenuCollector::windowRegistered(const QString &service,
58
const QDBusObjectPath &menuObjectPath) {
60
m_path = menuObjectPath;
62
if (m_service.isEmpty()) {
63
m_menuImporter.reset();
67
m_menuImporter.reset(new DBusMenuImporter(m_service, m_path.path()));
69
CollectorToken::Ptr collectorToken(m_collectorToken);
71
collectorToken->changed();
75
bool DBusMenuCollector::isValid() const {
76
return !m_menuImporter.isNull();
79
inline uint qHash(const QStringList &key, uint seed) {
81
for (const QString &s : key) {
82
hash ^= qHash(s, seed);
87
static void openMenu(QMenu *menu) {
94
for (int i(0); i < menu->actions().size(); ++i) {
95
QAction *action = menu->actions().at(i);
96
if (!action->isEnabled()) {
99
if (action->isSeparator()) {
103
QMenu *child(action->menu());
110
static void hideMenu(QMenu *menu) {
111
for (int i(0); i < menu->actions().size(); ++i) {
112
QAction *action = menu->actions().at(i);
113
QMenu *child(action->menu());
122
CollectorToken::Ptr DBusMenuCollector::activate() {
123
CollectorToken::Ptr collectorToken(m_collectorToken);
125
if (collectorToken.isNull()) {
126
openMenu(m_menuImporter->menu());
127
collectorToken.reset(
128
new CollectorToken(shared_from_this(), m_menuImporter->menu()));
129
m_collectorToken = collectorToken;
132
return collectorToken;
135
void DBusMenuCollector::deactivate() {
136
hideMenu(m_menuImporter->menu());
139
void DBusMenuCollector::WindowRegistered(uint windowId, const QString &service,
140
const QDBusObjectPath &menuObjectPath) {
141
// Simply ignore updates for other windows
142
if (windowId != m_windowId) {
146
windowRegistered(service, menuObjectPath);
149
void DBusMenuCollector::WindowUnregistered(uint windowId) {
150
// Simply ignore updates for other windows
151
if (windowId != m_windowId) {
155
m_menuImporter.reset();