qtui: Switch to tray-specific icons
[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     _associatedWidget(parent)
40 {
41     Q_ASSERT(parent);
42
43     NotificationSettings{}.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
44     UiStyleSettings{}.initAndNotify("Icons/InvertTray", this, SLOT(invertTrayIconChanged(QVariant)), false);
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
83
84 QWidget *SystemTray::associatedWidget() const
85 {
86     return _associatedWidget;
87 }
88
89
90 bool SystemTray::isSystemTrayAvailable() const
91 {
92     return false;
93 }
94
95
96 bool SystemTray::isVisible() const
97 {
98     return false;
99 }
100
101
102 bool SystemTray::shouldBeVisible() const
103 {
104     return _shouldBeVisible;
105 }
106
107
108 void SystemTray::setVisible(bool visible)
109 {
110     _shouldBeVisible = visible;
111 }
112
113
114 SystemTray::Mode SystemTray::mode() const
115 {
116     return _mode;
117 }
118
119
120 void SystemTray::setMode(Mode mode_)
121 {
122     if (mode_ != _mode) {
123         _mode = mode_;
124 #ifdef HAVE_KDE4
125         if (_trayMenu) {
126             if (_mode == Legacy) {
127                 _trayMenu->setWindowFlags(Qt::Popup);
128             }
129             else {
130                 _trayMenu->setWindowFlags(Qt::Window);
131             }
132         }
133 #endif
134     }
135 }
136
137
138 SystemTray::State SystemTray::state() const
139 {
140     return _state;
141 }
142
143
144 void SystemTray::setState(State state)
145 {
146     if (_state != state) {
147         _state = state;
148     }
149 }
150
151
152 QString SystemTray::iconName(State state) const
153 {
154     QString name;
155     switch (state) {
156     case State::Passive:
157         name = "inactive-quassel-tray";
158         break;
159     case State::Active:
160         name = "active-quassel-tray";
161         break;
162     case State::NeedsAttention:
163         name = "message-quassel-tray";
164         break;
165     }
166
167     if (_trayIconInverted) {
168         name += "-inverted";
169     }
170
171     return name;
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 void SystemTray::invertTrayIconChanged(const QVariant &v)
219 {
220     _trayIconInverted = v.toBool();
221 }
222
223
224 QString SystemTray::toolTipTitle() const
225 {
226     return _toolTipTitle;
227 }
228
229
230 QString SystemTray::toolTipSubTitle() const
231 {
232     return _toolTipSubTitle;
233 }
234
235
236 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
237 {
238     _toolTipTitle = title;
239     _toolTipSubTitle = subtitle;
240     emit toolTipChanged(title, subtitle);
241 }
242
243
244 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
245 {
246     Q_UNUSED(title)
247     Q_UNUSED(message)
248     Q_UNUSED(icon)
249     Q_UNUSED(millisecondsTimeoutHint)
250     Q_UNUSED(id)
251 }
252
253
254 void SystemTray::closeMessage(uint notificationId)
255 {
256     Q_UNUSED(notificationId)
257 }
258
259
260 void SystemTray::activate(SystemTray::ActivationReason reason)
261 {
262     emit activated(reason);
263 }
264
265
266 void SystemTray::minimizeRestore()
267 {
268     GraphicalUi::toggleMainWidget();
269 }