More systray refactoring
[quassel.git] / src / uisupport / graphicalui.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This contains code from KStatusNotifierItem, part of the KDE libs     *
6  *   Copyright (C) 2009 Marco Martin <notmart@gmail.com>                   *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) version 3.                                           *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #include "graphicalui.h"
25
26 #include "actioncollection.h"
27 #include "contextmenuactionprovider.h"
28
29 #ifdef Q_WS_X11
30 #  include <QX11Info>
31 #endif
32 #ifdef HAVE_KDE
33 #  include <KWindowInfo>
34 #  include <KWindowSystem>
35 #endif
36
37 GraphicalUi *GraphicalUi::_instance = 0;
38 QWidget *GraphicalUi::_mainWidget = 0;
39 QHash<QString, ActionCollection *> GraphicalUi::_actionCollections;
40 ContextMenuActionProvider *GraphicalUi::_contextMenuActionProvider = 0;
41 ToolBarActionProvider *GraphicalUi::_toolBarActionProvider = 0;
42 UiStyle *GraphicalUi::_uiStyle = 0;
43 bool GraphicalUi::_onAllDesktops = false;
44
45 GraphicalUi::GraphicalUi(QObject *parent) : AbstractUi(parent) {
46   Q_ASSERT(!_instance);
47   _instance = this;
48
49 #ifdef Q_WS_WIN
50   _dwTickCount = 0;
51   mainWidget()->installEventFilter(this);
52 #endif
53
54 }
55
56 GraphicalUi::~GraphicalUi() {
57 #ifdef Q_WS_WIN
58   mainWidget()->removeEventFilter(this);
59 #endif
60 }
61
62 ActionCollection *GraphicalUi::actionCollection(const QString &category) {
63   if(_actionCollections.contains(category))
64     return _actionCollections.value(category);
65   ActionCollection *coll = new ActionCollection(_mainWidget);
66   if(_mainWidget)
67     coll->addAssociatedWidget(_mainWidget);
68   _actionCollections.insert(category, coll);
69   return coll;
70 }
71
72 void GraphicalUi::setMainWidget(QWidget *widget) {
73   _mainWidget = widget;
74 }
75
76 void GraphicalUi::setContextMenuActionProvider(ContextMenuActionProvider *provider) {
77   _contextMenuActionProvider = provider;
78 }
79
80 void GraphicalUi::setToolBarActionProvider(ToolBarActionProvider *provider) {
81   _toolBarActionProvider = provider;
82 }
83
84 void GraphicalUi::setUiStyle(UiStyle *style) {
85   _uiStyle = style;
86 }
87
88 bool GraphicalUi::eventFilter(QObject *obj, QEvent *event) {
89 #ifdef Q_WS_WIN
90   if(obj == mainWidget() && event->type() == QEvent::ActivationChange) {
91     _dwTickCount = GetTickCount();
92   }
93 #endif
94   return AbstractUi::eventFilter(obj, event);
95 }
96
97 // Code taken from KStatusNotifierItem for handling minimize/restore
98
99 bool GraphicalUi::checkMainWidgetVisibility(bool perform) {
100 #ifdef Q_WS_WIN
101   // the problem is that we lose focus when the systray icon is activated
102   // and we don't know the former active window
103   // therefore we watch for activation event and use our stopwatch :)
104   if(GetTickCount() - _dwTickCount < 300) {
105     // we were active in the last 300ms -> hide it
106     minimizeRestore(false);
107   } else {
108     minimizeRestore(true);
109   }
110
111 #elif defined(HAVE_KDE) && defined(Q_WS_X11)
112   KWindowInfo info1 = KWindowSystem::windowInfo(mainWidget()->winId(), NET::XAWMState | NET::WMState | NET::WMDesktop);
113   // mapped = visible (but possibly obscured)
114   bool mapped = (info1.mappingState() == NET::Visible) && !info1.isMinimized();
115
116   //    - not mapped -> show, raise, focus
117   //    - mapped
118   //        - obscured -> raise, focus
119   //        - not obscured -> hide
120   //info1.mappingState() != NET::Visible -> window on another desktop?
121   if(!mapped) {
122     if(perform)
123       minimizeRestore(true);
124     return true;
125
126   } else {
127     QListIterator< WId > it (KWindowSystem::stackingOrder());
128     it.toBack();
129     while(it.hasPrevious()) {
130       WId id = it.previous();
131       if(id == mainWidget()->winId())
132         break;
133
134       KWindowInfo info2 = KWindowSystem::windowInfo(id, NET::WMDesktop | NET::WMGeometry | NET::XAWMState | NET::WMState | NET::WMWindowType);
135
136       if(info2.mappingState() != NET::Visible)
137         continue; // not visible on current desktop -> ignore
138
139       if(!info2.geometry().intersects(mainWidget()->geometry()))
140         continue; // not obscuring the window -> ignore
141
142       if(!info1.hasState(NET::KeepAbove) && info2.hasState(NET::KeepAbove))
143         continue; // obscured by window kept above -> ignore
144
145       NET::WindowType type = info2.windowType(NET::NormalMask | NET::DesktopMask
146                                               | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask
147                                               | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask);
148
149       if(type == NET::Dock || type == NET::TopMenu)
150         continue; // obscured by dock or topmenu -> ignore
151
152       if(perform) {
153         KWindowSystem::raiseWindow(mainWidget()->winId());
154         KWindowSystem::activateWindow(mainWidget()->winId());
155       }
156       return true;
157     }
158
159     //not on current desktop?
160     if(!info1.isOnCurrentDesktop()) {
161       if(perform)
162         KWindowSystem::activateWindow(mainWidget()->winId());
163       return true;
164     }
165
166     if(perform)
167       minimizeRestore(false); // hide
168     return false;
169   }
170 #else
171
172   if(!mainWidget()->isVisible() || mainWidget()->isMinimized()) {
173     if(perform)
174       minimizeRestore(true);
175     return true;
176   } else {
177     if(perform)
178       minimizeRestore(false);
179     return false;
180   }
181
182 #endif
183
184   return true;
185 }
186
187 void GraphicalUi::minimizeRestore(bool show) {
188   if(show)
189     activateMainWidget();
190   else
191     hideMainWidget();
192 }
193
194 void GraphicalUi::activateMainWidget() {
195 #ifdef HAVE_KDE
196 #  ifdef Q_WS_X11
197     KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
198     if(_onAllDesktops) {
199       KWindowSystem::setOnAllDesktops(mainWidget()->winId(), true);
200     } else {
201       KWindowSystem::setCurrentDesktop(info.desktop());
202     }
203
204     mainWidget()->move(info.frameGeometry().topLeft()); // avoid placement policies
205     mainWidget()->show();
206     mainWidget()->raise();
207     KWindowSystem::raiseWindow(mainWidget()->winId());
208     KWindowSystem::activateWindow(mainWidget()->winId());
209 #  else
210     mainWidget()->show();
211     KWindowSystem::raiseWindow(mainWidget()->winId());
212     KWindowSystem::forceActiveWindow(mainWidget()->winId());
213 #  endif
214
215 #else /* HAVE_KDE */
216
217 #ifdef Q_WS_X11
218   // Bypass focus stealing prevention
219   QX11Info::setAppUserTime(QX11Info::appTime());
220 #endif
221
222   if(mainWidget()->windowState() & Qt::WindowMinimized) {
223     // restore
224     mainWidget()->setWindowState((mainWidget()->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
225   }
226
227   // this does not actually work on all platforms... and causes more evil than good
228   // mainWidget()->move(mainWidget()->frameGeometry().topLeft()); // avoid placement policies
229   mainWidget()->show();
230   mainWidget()->raise();
231   mainWidget()->activateWindow();
232
233 #endif /* HAVE_KDE */
234 }
235
236 void GraphicalUi::hideMainWidget() {
237
238 #if defined(HAVE_KDE) && defined(Q_WS_X11)
239   KWindowInfo info = KWindowSystem::windowInfo(mainWidget()->winId(), NET::WMDesktop | NET::WMFrameExtents);
240   _onAllDesktops = info.onAllDesktops();
241 #endif
242
243   if(instance()->isHidingMainWidgetAllowed())
244     mainWidget()->hide();
245 }
246
247 void GraphicalUi::toggleMainWidget() {
248   instance()->checkMainWidgetVisibility(true);
249 }