1/********************************************************************
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5Copyright (c) 2011 Lionel Chauvin <megabigbug@yahoo.fr>
6Copyright (c) 2011,2012 Cédric Bellegarde <gnumdk@gmail.com>
7Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program. If not, see <http://www.gnu.org/licenses/>.
21*********************************************************************/
22#include "appmenu.h"
23#include "client.h"
24#include "workspace.h"
25// Qt
26#include <QtDBus/QDBusConnection>
27#include <QtDBus/QDBusMessage>
28#include <QtDBus/QDBusPendingCall>
29
30namespace KWin {
31
32static const char *KDED_SERVICE = "org.kde.kded";
33static const char *KDED_APPMENU_PATH = "/modules/appmenu";
34static const char *KDED_INTERFACE = "org.kde.kded";
35
36KWIN_SINGLETON_FACTORY(ApplicationMenu)
37
38ApplicationMenu::ApplicationMenu(QObject *parent)
39 : QObject(parent)
40{
41 QDBusConnection dbus = QDBusConnection::sessionBus();
42 dbus.connect(KDED_SERVICE, KDED_APPMENU_PATH, KDED_INTERFACE, "showRequest",
43 this, SLOT(slotShowRequest(qulonglong)));
44 dbus.connect(KDED_SERVICE, KDED_APPMENU_PATH, KDED_INTERFACE, "menuAvailable",
45 this, SLOT(slotMenuAvailable(qulonglong)));
46 dbus.connect(KDED_SERVICE, KDED_APPMENU_PATH, KDED_INTERFACE, "menuHidden",
47 this, SLOT(slotMenuHidden(qulonglong)));
48 dbus.connect(KDED_SERVICE, KDED_APPMENU_PATH, KDED_INTERFACE, "clearMenus",
49 this, SLOT(slotClearMenus()));
50}
51
52ApplicationMenu::~ApplicationMenu()
53{
54 s_self = NULL;
55}
56
57bool ApplicationMenu::hasMenu(xcb_window_t window)
58{
59 return m_windowsMenu.removeOne(window);
60}
61
62void ApplicationMenu::slotShowRequest(qulonglong wid)
63{
64 if (Client *c = Workspace::self()->findClient(WindowMatchPredicate(wid)))
65 c->emitShowRequest();
66}
67
68void ApplicationMenu::slotMenuAvailable(qulonglong wid)
69{
70 if (Client *c = Workspace::self()->findClient(WindowMatchPredicate(wid)))
71 c->setAppMenuAvailable();
72 else
73 m_windowsMenu.append(wid);
74}
75
76void ApplicationMenu::slotMenuHidden(qulonglong wid)
77{
78 if (Client *c = Workspace::self()->findClient(WindowMatchPredicate(wid)))
79 c->emitMenuHidden();
80}
81
82void ApplicationMenu::slotClearMenus()
83{
84 foreach (Client *c, Workspace::self()->clientList()) {
85 c->setAppMenuUnavailable();
86 }
87}
88
89void ApplicationMenu::showApplicationMenu(const QPoint &p, const xcb_window_t id)
90{
91 QList<QVariant> args = QList<QVariant>() << p.x() << p.y() << qulonglong(id);
92 QDBusMessage method = QDBusMessage::createMethodCall(KDED_SERVICE, KDED_APPMENU_PATH, KDED_INTERFACE, "showMenu");
93 method.setArguments(args);
94 QDBusConnection::sessionBus().asyncCall(method);
95}
96
97} // namespace
98