Rework tray icon activation behavior yet again
[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   _passiveIcon(DesktopIcon("quassel_inactive")),
39   _activeIcon(DesktopIcon("quassel")),
40   _needsAttentionIcon(DesktopIcon("quassel_message")),
41   _trayMenu(0),
42   _associatedWidget(parent)
43 {
44   Q_ASSERT(parent);
45 }
46
47 SystemTray::~SystemTray() {
48   _trayMenu->deleteLater();
49 }
50
51 QWidget *SystemTray::associatedWidget() const {
52   return _associatedWidget;
53 }
54
55 void SystemTray::setTrayMenu(QMenu *menu) {
56   if(menu)
57     _trayMenu = menu;
58   else
59     _trayMenu = new QMenu();
60
61   ActionCollection *coll = QtUi::actionCollection("General");
62
63   _trayMenu->addAction(coll->action("ConnectCore"));
64   _trayMenu->addAction(coll->action("DisconnectCore"));
65   _trayMenu->addAction(coll->action("CoreInfo"));
66 #ifndef HAVE_KDE
67   _trayMenu->addSeparator();
68   _trayMenu->addAction(coll->action("Quit"));
69 #endif /* HAVE_KDE */
70 }
71
72 void SystemTray::setMode(Mode mode_) {
73   if(mode_ != _mode) {
74     _mode = mode_;
75     if(_mode == Legacy) {
76       _trayMenu->setWindowFlags(Qt::Popup);
77     } else {
78       _trayMenu->setWindowFlags(Qt::Window);
79     }
80   }
81 }
82
83 Icon SystemTray::stateIcon() const {
84   return stateIcon(state());
85 }
86
87 Icon SystemTray::stateIcon(State state) const {
88   switch(state) {
89   case Passive:
90     return _passiveIcon;
91   case Active:
92     return _activeIcon;
93   case NeedsAttention:
94     return _needsAttentionIcon;
95   }
96   return Icon();
97 }
98
99 void SystemTray::setState(State state) {
100   if(_state != state) {
101     _state = state;
102   }
103 }
104
105 void SystemTray::setAlert(bool alerted) {
106   if(alerted)
107     setState(NeedsAttention);
108   else
109     setState(Client::isConnected() ? Active : Passive);
110 }
111
112 void SystemTray::setVisible(bool visible) {
113   Q_UNUSED(visible)
114 }
115
116 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
117   _toolTipTitle = title;
118   _toolTipSubTitle = subtitle;
119   emit toolTipChanged(title, subtitle);
120 }
121
122 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) {
123   Q_UNUSED(title)
124   Q_UNUSED(message)
125   Q_UNUSED(icon)
126   Q_UNUSED(millisecondsTimeoutHint)
127 }
128
129 void SystemTray::activate(SystemTray::ActivationReason reason) {
130   emit activated(reason);
131 }