1 /***************************************************************************
2 * The original file is part of the KDE libraries *
3 * Copyright (C) 2009 by Marco Martin <notmart@gmail.com> *
4 * Quasselfied 2010 by Manuel Nickschas <sputnick@quassel-irc.org> *
6 * This file is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU Library General Public License (LGPL) *
8 * as published by the Free Software Foundation; either version 2 of the *
9 * License, or (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
24 #include "statusnotifieritemdbus.h"
25 #include "statusnotifieritem.h"
27 #include <QDBusConnection>
30 #include <QApplication>
35 # include <KWindowInfo>
36 # include <KWindowSystem>
39 #include "statusnotifierwatcher.h"
40 #include "statusnotifieritemadaptor.h"
43 __inline int toInt(WId wid)
45 return (int)((__int64)wid);
50 __inline int toInt(WId wid)
58 // Marshall the ImageStruct data into a D-BUS argument
59 const QDBusArgument &operator<<(QDBusArgument &argument, const DBusImageStruct &icon)
61 argument.beginStructure();
62 argument << icon.width;
63 argument << icon.height;
64 argument << icon.data;
65 argument.endStructure();
70 // Retrieve the ImageStruct data from the D-BUS argument
71 const QDBusArgument &operator>>(const QDBusArgument &argument, DBusImageStruct &icon)
77 argument.beginStructure();
81 argument.endStructure();
91 // Marshall the ImageVector data into a D-BUS argument
92 const QDBusArgument &operator<<(QDBusArgument &argument, const DBusImageVector &iconVector)
94 argument.beginArray(qMetaTypeId<DBusImageStruct>());
95 for (int i = 0; i < iconVector.size(); ++i) {
96 argument << iconVector[i];
103 // Retrieve the ImageVector data from the D-BUS argument
104 const QDBusArgument &operator>>(const QDBusArgument &argument, DBusImageVector &iconVector)
106 argument.beginArray();
109 while (!argument.atEnd()) {
110 DBusImageStruct element;
112 iconVector.append(element);
121 // Marshall the ToolTipStruct data into a D-BUS argument
122 const QDBusArgument &operator<<(QDBusArgument &argument, const DBusToolTipStruct &toolTip)
124 argument.beginStructure();
125 argument << toolTip.icon;
126 argument << toolTip.image;
127 argument << toolTip.title;
128 argument << toolTip.subTitle;
129 argument.endStructure();
134 // Retrieve the ToolTipStruct data from the D-BUS argument
135 const QDBusArgument &operator>>(const QDBusArgument &argument, DBusToolTipStruct &toolTip)
138 DBusImageVector image;
142 argument.beginStructure();
146 argument >> subTitle;
147 argument.endStructure();
150 toolTip.image = image;
151 toolTip.title = title;
152 toolTip.subTitle = subTitle;
158 int StatusNotifierItemDBus::s_serviceCount = 0;
160 StatusNotifierItemDBus::StatusNotifierItemDBus(StatusNotifierItem *parent)
162 m_statusNotifierItem(parent),
163 m_service(QString("org.kde.StatusNotifierItem-%1-%2")
164 .arg(QCoreApplication::applicationPid())
165 .arg(++s_serviceCount)),
166 m_dbus(QDBusConnection::connectToBus(QDBusConnection::SessionBus, m_service))
168 new StatusNotifierItemAdaptor(this);
169 //qDebug() << "service is" << m_service;
174 StatusNotifierItemDBus::~StatusNotifierItemDBus()
180 QDBusConnection StatusNotifierItemDBus::dbusConnection() const
186 // FIXME: prevent double registrations, also test this on platforms != KDE
188 void StatusNotifierItemDBus::registerService()
190 //qDebug() << "registering to" << m_service;
191 m_dbus.registerService(m_service);
192 m_dbus.registerObject("/StatusNotifierItem", this);
197 void StatusNotifierItemDBus::unregisterService()
199 //qDebug() << "unregistering from" << m_service;
200 if (m_dbus.isConnected()) {
201 m_dbus.unregisterObject("/StatusNotifierItem");
202 m_dbus.unregisterService(m_service);
207 QString StatusNotifierItemDBus::service() const
214 //Values and calls have been adapted to Quassel
216 QString StatusNotifierItemDBus::Category() const
218 return QString("Communications"); // no need to make this configurable for Quassel
222 QString StatusNotifierItemDBus::Title() const
224 return m_statusNotifierItem->title();
228 QString StatusNotifierItemDBus::Id() const
230 return QString("QuasselIRC");
234 QString StatusNotifierItemDBus::Status() const
236 return m_statusNotifierItem->metaObject()->enumerator(m_statusNotifierItem->metaObject()->indexOfEnumerator("State")).valueToKey(m_statusNotifierItem->state());
240 int StatusNotifierItemDBus::WindowId() const
242 return toInt(QtUi::mainWindow()->winId());
247 //We don't need to support serialized icon data in Quassel
249 QString StatusNotifierItemDBus::IconName() const
251 return m_statusNotifierItem->iconName();
255 DBusImageVector StatusNotifierItemDBus::IconPixmap() const
257 return DBusImageVector();
261 QString StatusNotifierItemDBus::OverlayIconName() const
267 DBusImageVector StatusNotifierItemDBus::OverlayIconPixmap() const
269 return DBusImageVector();
273 //Requesting attention icon and movie
275 QString StatusNotifierItemDBus::AttentionIconName() const
277 return m_statusNotifierItem->attentionIconName();
281 DBusImageVector StatusNotifierItemDBus::AttentionIconPixmap() const
283 return DBusImageVector();
287 QString StatusNotifierItemDBus::AttentionMovieName() const
295 DBusToolTipStruct StatusNotifierItemDBus::ToolTip() const
297 DBusToolTipStruct toolTip;
298 toolTip.icon = m_statusNotifierItem->toolTipIconName();
299 toolTip.image = DBusImageVector();
300 toolTip.title = m_statusNotifierItem->toolTipTitle();
301 toolTip.subTitle = m_statusNotifierItem->toolTipSubTitle();
307 QString StatusNotifierItemDBus::IconThemePath() const
309 return m_statusNotifierItem->iconThemePath();
315 QDBusObjectPath StatusNotifierItemDBus::Menu() const
317 return QDBusObjectPath(m_statusNotifierItem->menuObjectPath());
323 void StatusNotifierItemDBus::ContextMenu(int x, int y)
325 if (!m_statusNotifierItem->trayMenu()) {
329 //TODO: nicer placement, possible?
330 if (!m_statusNotifierItem->trayMenu()->isVisible()) {
332 m_statusNotifierItem->trayMenu()->setWindowFlags(Qt::Window|Qt::FramelessWindowHint);
334 m_statusNotifierItem->trayMenu()->popup(QPoint(x, y));
336 KWindowSystem::setState(m_statusNotifierItem->trayMenu()->winId(), NET::SkipTaskbar|NET::SkipPager|NET::KeepAbove);
337 KWindowSystem::setType(m_statusNotifierItem->trayMenu()->winId(), NET::PopupMenu);
338 KWindowSystem::forceActiveWindow(m_statusNotifierItem->trayMenu()->winId());
342 m_statusNotifierItem->trayMenu()->hide();
347 void StatusNotifierItemDBus::Activate(int x, int y)
349 m_statusNotifierItem->activated(QPoint(x, y));
353 void StatusNotifierItemDBus::SecondaryActivate(int x, int y)
357 // emit m_statusNotifierItem->secondaryActivateRequested(QPoint(x,y));
361 void StatusNotifierItemDBus::Scroll(int delta, const QString &orientation)
364 Q_UNUSED(orientation)
365 // Qt::Orientation dir = (orientation.toLower() == "horizontal" ? Qt::Horizontal : Qt::Vertical);
366 // emit m_statusNotifierItem->scrollRequested(delta, dir);