SystemTray refactoring in preparation of supporting StatusNotifier
[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 #include "qtuisettings.h"
29
30 SystemTray::SystemTray(QObject *parent)
31 : QObject(parent),
32   _mode(Invalid),
33   _state(Passive),
34   _inhibitActivation(false),
35   _passiveIcon(DesktopIcon("quassel_inactive")),
36   _activeIcon(DesktopIcon("quassel")),
37   _needsAttentionIcon(DesktopIcon("quassel_message")),
38   _trayMenu(0)
39 {
40   qApp->installEventFilter(this);
41 }
42
43 SystemTray::~SystemTray() {
44   _trayMenu->deleteLater();
45 }
46
47 void SystemTray::setTrayMenu(QMenu *menu) {
48   if(menu)
49     _trayMenu = menu;
50   else
51     _trayMenu = new QMenu();
52
53   ActionCollection *coll = QtUi::actionCollection("General");
54
55   _trayMenu->addAction(coll->action("ConnectCore"));
56   _trayMenu->addAction(coll->action("DisconnectCore"));
57   _trayMenu->addAction(coll->action("CoreInfo"));
58 #ifndef HAVE_KDE
59   _trayMenu->addSeparator();
60   _trayMenu->addAction(coll->action("Quit"));
61 #endif /* HAVE_KDE */
62
63 }
64
65 void SystemTray::setMode(Mode mode_) {
66   if(mode_ != _mode) {
67     _mode = mode_;
68     if(_mode == Legacy) {
69       _trayMenu->setWindowFlags(Qt::Popup);
70     } else {
71       _trayMenu->setWindowFlags(Qt::Window);
72     }
73   }
74 }
75
76 Icon SystemTray::stateIcon() const {
77   return stateIcon(state());
78 }
79
80 Icon SystemTray::stateIcon(State state) const {
81   switch(state) {
82   case Passive:
83     return _passiveIcon;
84   case Active:
85     return _activeIcon;
86   case NeedsAttention:
87     return _needsAttentionIcon;
88   }
89   return Icon();
90 }
91
92 void SystemTray::setState(State state) {
93   if(_state != state) {
94     _state = state;
95   }
96 }
97
98 void SystemTray::setAlert(bool alerted) {
99   if(alerted)
100     setState(NeedsAttention);
101   else
102     setState(Client::isConnected() ? Active : Passive);
103 }
104
105 void SystemTray::setVisible(bool visible) {
106   Q_UNUSED(visible)
107 }
108
109 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
110   _toolTipTitle = title;
111   _toolTipSubTitle = subtitle;
112   emit toolTipChanged(title, subtitle);
113 }
114
115 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) {
116   Q_UNUSED(title)
117   Q_UNUSED(message)
118   Q_UNUSED(icon)
119   Q_UNUSED(millisecondsTimeoutHint)
120 }
121
122 bool SystemTray::eventFilter(QObject *obj, QEvent *event) {
123   Q_UNUSED(obj);
124   if(event->type() == QEvent::MouseButtonRelease) {
125     _inhibitActivation = false;
126   }
127   return false;
128 }