More systray refactoring
[quassel.git] / src / qtui / systemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include <QMenu>
21
22 #include "systemtray.h"
23
24 #include "actioncollection.h"
25 #include "client.h"
26 #include "iconloader.h"
27 #include "qtui.h"
28
29 #ifdef HAVE_KDE
30 #  include <KWindowInfo>
31 #  include <KWindowSystem>
32 #endif
33
34 SystemTray::SystemTray(QWidget *parent)
35 : QObject(parent),
36   _mode(Invalid),
37   _state(Passive),
38   _inhibitActivation(false),
39   _passiveIcon(DesktopIcon("quassel_inactive")),
40   _activeIcon(DesktopIcon("quassel")),
41   _needsAttentionIcon(DesktopIcon("quassel_message")),
42   _trayMenu(0),
43   _associatedWidget(parent)
44 {
45   Q_ASSERT(parent);
46
47   qApp->installEventFilter(this);
48 }
49
50 SystemTray::~SystemTray() {
51   _trayMenu->deleteLater();
52 }
53
54 QWidget *SystemTray::associatedWidget() const {
55   return _associatedWidget;
56 }
57
58 void SystemTray::setTrayMenu(QMenu *menu) {
59   if(menu)
60     _trayMenu = menu;
61   else
62     _trayMenu = new QMenu();
63
64   ActionCollection *coll = QtUi::actionCollection("General");
65
66   _trayMenu->addAction(coll->action("ConnectCore"));
67   _trayMenu->addAction(coll->action("DisconnectCore"));
68   _trayMenu->addAction(coll->action("CoreInfo"));
69 #ifndef HAVE_KDE
70   _trayMenu->addSeparator();
71   _trayMenu->addAction(coll->action("Quit"));
72 #endif /* HAVE_KDE */
73 }
74
75 void SystemTray::setMode(Mode mode_) {
76   if(mode_ != _mode) {
77     _mode = mode_;
78     if(_mode == Legacy) {
79       _trayMenu->setWindowFlags(Qt::Popup);
80     } else {
81       _trayMenu->setWindowFlags(Qt::Window);
82     }
83   }
84 }
85
86 Icon SystemTray::stateIcon() const {
87   return stateIcon(state());
88 }
89
90 Icon SystemTray::stateIcon(State state) const {
91   switch(state) {
92   case Passive:
93     return _passiveIcon;
94   case Active:
95     return _activeIcon;
96   case NeedsAttention:
97     return _needsAttentionIcon;
98   }
99   return Icon();
100 }
101
102 void SystemTray::setState(State state) {
103   if(_state != state) {
104     _state = state;
105   }
106 }
107
108 void SystemTray::setAlert(bool alerted) {
109   if(alerted)
110     setState(NeedsAttention);
111   else
112     setState(Client::isConnected() ? Active : Passive);
113 }
114
115 void SystemTray::setVisible(bool visible) {
116   Q_UNUSED(visible)
117 }
118
119 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
120   _toolTipTitle = title;
121   _toolTipSubTitle = subtitle;
122   emit toolTipChanged(title, subtitle);
123 }
124
125 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) {
126   Q_UNUSED(title)
127   Q_UNUSED(message)
128   Q_UNUSED(icon)
129   Q_UNUSED(millisecondsTimeoutHint)
130 }
131
132 void SystemTray::activate(SystemTray::ActivationReason reason) {
133
134   emit activated(reason);
135
136   if(reason == Trigger && !isActivationInhibited()) {
137     GraphicalUi::toggleMainWidget();
138   }
139 }
140
141 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
142   if(event->type() == QEvent::MouseMove || event->type() == QEvent::MouseButtonRelease) {
143     _inhibitActivation = false;
144   }
145   return QObject::eventFilter(obj, event);
146 }