qtui: Clean up SystemTray
[quassel.git] / src / qtui / systemtray.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This file is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Library General Public License (LGPL)   *
7  *   as published by the Free Software Foundation; either version 2 of the *
8  *   License, or (at your option) any later version.                       *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QMenu>
23
24 #include "systemtray.h"
25
26 #include "action.h"
27 #include "actioncollection.h"
28 #include "client.h"
29 #include "qtui.h"
30
31 #ifdef HAVE_KDE4
32 #  include <KMenu>
33 #  include <KWindowInfo>
34 #  include <KWindowSystem>
35 #endif
36
37 SystemTray::SystemTray(QWidget *parent)
38     : QObject(parent),
39     _passiveIcon(QIcon::fromTheme("inactive-quassel", QIcon(":/icons/inactive-quassel.png"))),
40     _activeIcon(QIcon::fromTheme("quassel", QIcon(":/icons/quassel.png"))),
41     _needsAttentionIcon(QIcon::fromTheme("message-quassel", QIcon(":/icons/message-quassel.png"))),
42     _associatedWidget(parent)
43 {
44     Q_ASSERT(parent);
45 }
46
47
48 SystemTray::~SystemTray()
49 {
50     _trayMenu->deleteLater();
51 }
52
53
54 void SystemTray::init()
55 {
56     ActionCollection *coll = QtUi::actionCollection("General");
57     _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
58
59 #ifdef HAVE_KDE4
60     KMenu *kmenu;
61     _trayMenu = kmenu = new KMenu();
62     kmenu->addTitle(_activeIcon, "Quassel IRC");
63 #else
64     _trayMenu = new QMenu(associatedWidget());
65 #endif
66
67     _trayMenu->setTitle("Quassel IRC");
68
69 #ifndef HAVE_KDE4
70     _trayMenu->setAttribute(Qt::WA_Hover);
71 #endif
72
73     _trayMenu->addAction(coll->action("ConnectCore"));
74     _trayMenu->addAction(coll->action("DisconnectCore"));
75     _trayMenu->addAction(coll->action("CoreInfo"));
76     _trayMenu->addSeparator();
77     _trayMenu->addAction(_minimizeRestoreAction);
78     _trayMenu->addAction(coll->action("Quit"));
79
80     connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
81
82     NotificationSettings notificationSettings;
83     notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
84 }
85
86
87 QWidget *SystemTray::associatedWidget() const
88 {
89     return _associatedWidget;
90 }
91
92
93 bool SystemTray::isSystemTrayAvailable() const
94 {
95     return false;
96 }
97
98
99 bool SystemTray::isVisible() const
100 {
101     return false;
102 }
103
104
105 bool SystemTray::shouldBeVisible() const
106 {
107     return _shouldBeVisible;
108 }
109
110
111 void SystemTray::setVisible(bool visible)
112 {
113     _shouldBeVisible = visible;
114 }
115
116
117 SystemTray::Mode SystemTray::mode() const
118 {
119     return _mode;
120 }
121
122
123 void SystemTray::setMode(Mode mode_)
124 {
125     if (mode_ != _mode) {
126         _mode = mode_;
127 #ifdef HAVE_KDE4
128         if (_trayMenu) {
129             if (_mode == Legacy) {
130                 _trayMenu->setWindowFlags(Qt::Popup);
131             }
132             else {
133                 _trayMenu->setWindowFlags(Qt::Window);
134             }
135         }
136 #endif
137     }
138 }
139
140
141 SystemTray::State SystemTray::state() const
142 {
143     return _state;
144 }
145
146
147 void SystemTray::setState(State state)
148 {
149     if (_state != state) {
150         _state = state;
151     }
152 }
153
154
155 QIcon SystemTray::stateIcon() const
156 {
157     return stateIcon(state());
158 }
159
160
161 QIcon SystemTray::stateIcon(State state) const
162 {
163     switch (state) {
164     case Passive:
165         return _passiveIcon;
166     case Active:
167         return _activeIcon;
168     case NeedsAttention:
169         return _needsAttentionIcon;
170     }
171     return QIcon();
172 }
173
174
175 bool SystemTray::isAlerted() const
176 {
177     return state() == State::NeedsAttention;
178 }
179
180
181 void SystemTray::setAlert(bool alerted)
182 {
183     if (alerted)
184         setState(NeedsAttention);
185     else
186         setState(Client::isConnected() ? Active : Passive);
187 }
188
189
190 QMenu *SystemTray::trayMenu() const
191 {
192     return _trayMenu;
193 }
194
195
196 void SystemTray::trayMenuAboutToShow()
197 {
198     if (GraphicalUi::isMainWidgetVisible())
199         _minimizeRestoreAction->setText(tr("&Minimize"));
200     else
201         _minimizeRestoreAction->setText(tr("&Restore"));
202 }
203
204
205 bool SystemTray::animationEnabled() const
206 {
207     return _animationEnabled;
208 }
209
210
211 void SystemTray::enableAnimationChanged(const QVariant &v)
212 {
213     _animationEnabled = v.toBool();
214     emit animationEnabledChanged(v.toBool());
215 }
216
217
218 QString SystemTray::toolTipTitle() const
219 {
220     return _toolTipTitle;
221 }
222
223
224 QString SystemTray::toolTipSubTitle() const
225 {
226     return _toolTipSubTitle;
227 }
228
229
230 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
231 {
232     _toolTipTitle = title;
233     _toolTipSubTitle = subtitle;
234     emit toolTipChanged(title, subtitle);
235 }
236
237
238 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
239 {
240     Q_UNUSED(title)
241     Q_UNUSED(message)
242     Q_UNUSED(icon)
243     Q_UNUSED(millisecondsTimeoutHint)
244     Q_UNUSED(id)
245 }
246
247
248 void SystemTray::closeMessage(uint notificationId)
249 {
250     Q_UNUSED(notificationId)
251 }
252
253
254 void SystemTray::activate(SystemTray::ActivationReason reason)
255 {
256     emit activated(reason);
257 }
258
259
260 void SystemTray::minimizeRestore()
261 {
262     GraphicalUi::toggleMainWidget();
263 }