Refactor the system tray's context menu
[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 <QApplication>
21 #include <QMenu>
22
23 #include "systemtray.h"
24
25 #include "action.h"
26 #include "actioncollection.h"
27 #include "client.h"
28 #include "iconloader.h"
29 #include "qtui.h"
30
31 #ifdef HAVE_KDE
32 #  include <KMenu>
33 #  include <KWindowInfo>
34 #  include <KWindowSystem>
35 #endif
36
37 SystemTray::SystemTray(QWidget *parent)
38 : QObject(parent),
39   _mode(Invalid),
40   _state(Passive),
41   _passiveIcon(DesktopIcon("quassel_inactive")),
42   _activeIcon(DesktopIcon("quassel")),
43   _needsAttentionIcon(DesktopIcon("quassel_message")),
44   _trayMenu(0),
45   _associatedWidget(parent)
46 {
47   Q_ASSERT(parent);
48 }
49
50 SystemTray::~SystemTray() {
51   _trayMenu->deleteLater();
52 }
53
54 QWidget *SystemTray::associatedWidget() const {
55   return _associatedWidget;
56 }
57
58 void SystemTray::init() {
59   ActionCollection *coll = QtUi::actionCollection("General");
60   _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
61
62 #ifdef HAVE_KDE
63   KMenu *kmenu;
64   _trayMenu = kmenu = new KMenu();
65   kmenu->addTitle(qApp->windowIcon(), "Quassel IRC");
66 #else
67   _trayMenu = new QMenu(associatedWidget());
68 #endif
69
70   _trayMenu->setTitle("Quassel IRC");
71
72 #ifndef HAVE_KDE
73   _trayMenu->setAttribute(Qt::WA_Hover);
74 #endif
75
76   _trayMenu->addAction(coll->action("ConnectCore"));
77   _trayMenu->addAction(coll->action("DisconnectCore"));
78   _trayMenu->addAction(coll->action("CoreInfo"));
79   _trayMenu->addSeparator();
80   _trayMenu->addAction(_minimizeRestoreAction);
81   _trayMenu->addAction(coll->action("Quit"));
82
83   connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
84 }
85
86 void SystemTray::trayMenuAboutToShow() {
87   if(GraphicalUi::isMainWidgetVisible())
88     _minimizeRestoreAction->setText(tr("&Minimize"));
89   else
90     _minimizeRestoreAction->setText(tr("&Restore"));
91 }
92
93 void SystemTray::setMode(Mode mode_) {
94   if(mode_ != _mode) {
95     _mode = mode_;
96 #ifdef HAVE_KDE
97     if(_trayMenu) {
98       if(_mode == Legacy) {
99         _trayMenu->setWindowFlags(Qt::Popup);
100       } else {
101         _trayMenu->setWindowFlags(Qt::Window);
102       }
103     }
104 #endif
105   }
106 }
107
108 Icon SystemTray::stateIcon() const {
109   return stateIcon(state());
110 }
111
112 Icon SystemTray::stateIcon(State state) const {
113   switch(state) {
114   case Passive:
115     return _passiveIcon;
116   case Active:
117     return _activeIcon;
118   case NeedsAttention:
119     return _needsAttentionIcon;
120   }
121   return Icon();
122 }
123
124 void SystemTray::setState(State state) {
125   if(_state != state) {
126     _state = state;
127   }
128 }
129
130 void SystemTray::setAlert(bool alerted) {
131   if(alerted)
132     setState(NeedsAttention);
133   else
134     setState(Client::isConnected() ? Active : Passive);
135 }
136
137 void SystemTray::setVisible(bool visible) {
138   Q_UNUSED(visible)
139 }
140
141 void SystemTray::setToolTip(const QString &title, const QString &subtitle) {
142   _toolTipTitle = title;
143   _toolTipSubTitle = subtitle;
144   emit toolTipChanged(title, subtitle);
145 }
146
147 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint) {
148   Q_UNUSED(title)
149   Q_UNUSED(message)
150   Q_UNUSED(icon)
151   Q_UNUSED(millisecondsTimeoutHint)
152 }
153
154 void SystemTray::activate(SystemTray::ActivationReason reason) {
155   emit activated(reason);
156 }
157
158 void SystemTray::minimizeRestore() {
159   GraphicalUi::toggleMainWidget();
160 }