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