4a68dd64114e5e1bffcb2b7915b766e2e36622c3
[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     ActionCollection *coll = QtUi::actionCollection("General");
47     _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
48
49 #ifdef HAVE_KDE4
50     KMenu *kmenu;
51     _trayMenu = kmenu = new KMenu();
52     kmenu->addTitle(_activeIcon, "Quassel IRC");
53 #else
54     _trayMenu = new QMenu(associatedWidget());
55 #endif
56
57     _trayMenu->setTitle("Quassel IRC");
58
59 #ifndef HAVE_KDE4
60     _trayMenu->setAttribute(Qt::WA_Hover);
61 #endif
62
63     _trayMenu->addAction(coll->action("ConnectCore"));
64     _trayMenu->addAction(coll->action("DisconnectCore"));
65     _trayMenu->addAction(coll->action("CoreInfo"));
66     _trayMenu->addSeparator();
67     _trayMenu->addAction(_minimizeRestoreAction);
68     _trayMenu->addAction(coll->action("Quit"));
69
70     connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
71 }
72
73
74 SystemTray::~SystemTray()
75 {
76     _trayMenu->deleteLater();
77 }
78
79
80 QWidget *SystemTray::associatedWidget() const
81 {
82     return _associatedWidget;
83 }
84
85
86 bool SystemTray::isSystemTrayAvailable() const
87 {
88     return false;
89 }
90
91
92 bool SystemTray::isVisible() const
93 {
94     return _isVisible;
95 }
96
97
98 void SystemTray::setVisible(bool visible)
99 {
100     if (visible != _isVisible) {
101         _isVisible = visible;
102         emit visibilityChanged(visible);
103     }
104 }
105
106
107 SystemTray::Mode SystemTray::mode() const
108 {
109     return _mode;
110 }
111
112
113 void SystemTray::setMode(Mode mode)
114 {
115     if (mode != _mode) {
116         _mode = mode;
117 #ifdef HAVE_KDE4
118         if (_trayMenu) {
119             if (mode == Mode::Legacy) {
120                 _trayMenu->setWindowFlags(Qt::Popup);
121             }
122             else {
123                 _trayMenu->setWindowFlags(Qt::Window);
124             }
125         }
126 #endif
127         emit modeChanged(mode);
128     }
129 }
130
131
132 SystemTray::State SystemTray::state() const
133 {
134     return _state;
135 }
136
137
138 void SystemTray::setState(State state)
139 {
140     if (_state != state) {
141         _state = state;
142         emit stateChanged(state);
143     }
144 }
145
146
147 QString SystemTray::iconName(State state) const
148 {
149     QString name;
150     switch (state) {
151     case State::Passive:
152         name = "inactive-quassel-tray";
153         break;
154     case State::Active:
155         name = "active-quassel-tray";
156         break;
157     case State::NeedsAttention:
158         name = "message-quassel-tray";
159         break;
160     }
161
162     if (_trayIconInverted) {
163         name += "-inverted";
164     }
165
166     return name;
167 }
168
169
170 bool SystemTray::isAlerted() const
171 {
172     return state() == State::NeedsAttention;
173 }
174
175
176 void SystemTray::setAlert(bool alerted)
177 {
178     if (alerted)
179         setState(NeedsAttention);
180     else
181         setState(Client::isConnected() ? Active : Passive);
182 }
183
184
185 QMenu *SystemTray::trayMenu() const
186 {
187     return _trayMenu;
188 }
189
190
191 void SystemTray::trayMenuAboutToShow()
192 {
193     if (GraphicalUi::isMainWidgetVisible())
194         _minimizeRestoreAction->setText(tr("&Minimize"));
195     else
196         _minimizeRestoreAction->setText(tr("&Restore"));
197 }
198
199
200 bool SystemTray::animationEnabled() const
201 {
202     return _animationEnabled;
203 }
204
205
206 void SystemTray::enableAnimationChanged(const QVariant &v)
207 {
208     _animationEnabled = v.toBool();
209     emit animationEnabledChanged(v.toBool());
210 }
211
212
213 void SystemTray::invertTrayIconChanged(const QVariant &v)
214 {
215     _trayIconInverted = v.toBool();
216 }
217
218
219 QString SystemTray::toolTipTitle() const
220 {
221     return _toolTipTitle;
222 }
223
224
225 QString SystemTray::toolTipSubTitle() const
226 {
227     return _toolTipSubTitle;
228 }
229
230
231 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
232 {
233     _toolTipTitle = title;
234     _toolTipSubTitle = subtitle;
235     emit toolTipChanged(title, subtitle);
236 }
237
238
239 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
240 {
241     Q_UNUSED(title)
242     Q_UNUSED(message)
243     Q_UNUSED(icon)
244     Q_UNUSED(millisecondsTimeoutHint)
245     Q_UNUSED(id)
246 }
247
248
249 void SystemTray::closeMessage(uint notificationId)
250 {
251     Q_UNUSED(notificationId)
252 }
253
254
255 void SystemTray::activate(SystemTray::ActivationReason reason)
256 {
257     emit activated(reason);
258 }
259
260
261 void SystemTray::minimizeRestore()
262 {
263     GraphicalUi::toggleMainWidget();
264 }