qtui: Use icon names in tray implementations
[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
44
45 SystemTray::~SystemTray()
46 {
47     _trayMenu->deleteLater();
48 }
49
50
51 void SystemTray::init()
52 {
53     ActionCollection *coll = QtUi::actionCollection("General");
54     _minimizeRestoreAction = new Action(tr("&Minimize"), this, this, SLOT(minimizeRestore()));
55
56 #ifdef HAVE_KDE4
57     KMenu *kmenu;
58     _trayMenu = kmenu = new KMenu();
59     kmenu->addTitle(_activeIcon, "Quassel IRC");
60 #else
61     _trayMenu = new QMenu(associatedWidget());
62 #endif
63
64     _trayMenu->setTitle("Quassel IRC");
65
66 #ifndef HAVE_KDE4
67     _trayMenu->setAttribute(Qt::WA_Hover);
68 #endif
69
70     _trayMenu->addAction(coll->action("ConnectCore"));
71     _trayMenu->addAction(coll->action("DisconnectCore"));
72     _trayMenu->addAction(coll->action("CoreInfo"));
73     _trayMenu->addSeparator();
74     _trayMenu->addAction(_minimizeRestoreAction);
75     _trayMenu->addAction(coll->action("Quit"));
76
77     connect(_trayMenu, SIGNAL(aboutToShow()), SLOT(trayMenuAboutToShow()));
78
79     NotificationSettings notificationSettings;
80     notificationSettings.initAndNotify("Systray/Animate", this, SLOT(enableAnimationChanged(QVariant)), true);
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     switch (state) {
155     case State::Passive:
156         return "inactive-quassel";
157     case State::Active:
158         return "quassel";
159     case State::NeedsAttention:
160         return "message-quassel";
161     }
162     return {};
163 }
164
165
166 bool SystemTray::isAlerted() const
167 {
168     return state() == State::NeedsAttention;
169 }
170
171
172 void SystemTray::setAlert(bool alerted)
173 {
174     if (alerted)
175         setState(NeedsAttention);
176     else
177         setState(Client::isConnected() ? Active : Passive);
178 }
179
180
181 QMenu *SystemTray::trayMenu() const
182 {
183     return _trayMenu;
184 }
185
186
187 void SystemTray::trayMenuAboutToShow()
188 {
189     if (GraphicalUi::isMainWidgetVisible())
190         _minimizeRestoreAction->setText(tr("&Minimize"));
191     else
192         _minimizeRestoreAction->setText(tr("&Restore"));
193 }
194
195
196 bool SystemTray::animationEnabled() const
197 {
198     return _animationEnabled;
199 }
200
201
202 void SystemTray::enableAnimationChanged(const QVariant &v)
203 {
204     _animationEnabled = v.toBool();
205     emit animationEnabledChanged(v.toBool());
206 }
207
208
209 QString SystemTray::toolTipTitle() const
210 {
211     return _toolTipTitle;
212 }
213
214
215 QString SystemTray::toolTipSubTitle() const
216 {
217     return _toolTipSubTitle;
218 }
219
220
221 void SystemTray::setToolTip(const QString &title, const QString &subtitle)
222 {
223     _toolTipTitle = title;
224     _toolTipSubTitle = subtitle;
225     emit toolTipChanged(title, subtitle);
226 }
227
228
229 void SystemTray::showMessage(const QString &title, const QString &message, MessageIcon icon, int millisecondsTimeoutHint, uint id)
230 {
231     Q_UNUSED(title)
232     Q_UNUSED(message)
233     Q_UNUSED(icon)
234     Q_UNUSED(millisecondsTimeoutHint)
235     Q_UNUSED(id)
236 }
237
238
239 void SystemTray::closeMessage(uint notificationId)
240 {
241     Q_UNUSED(notificationId)
242 }
243
244
245 void SystemTray::activate(SystemTray::ActivationReason reason)
246 {
247     emit activated(reason);
248 }
249
250
251 void SystemTray::minimizeRestore()
252 {
253     GraphicalUi::toggleMainWidget();
254 }