/
qtsystemtrayicon.cpp
296 lines (232 loc) · 8.2 KB
/
qtsystemtrayicon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include <QObject>
#include <QCoreApplication>
#include <QSystemTrayIcon>
#include <QAction>
#include <QIODevice>
#include <QFile>
#include <QMenu>
#include <QIcon>
#include <QString>
#include <QUrl>
#include <QtEvents>
#include <QSettings>
#include "song.h"
#include "iconloader.h"
#include "utilities.h"
#include "systemtrayicon.h"
#include "qtsystemtrayicon.h"
#include "settings/behavioursettingspage.h"
QtSystemTrayIcon::QtSystemTrayIcon(QObject *parent)
: SystemTrayIcon(parent),
tray_(new QSystemTrayIcon(this)),
menu_(new QMenu),
app_name_(QCoreApplication::applicationName()),
icon_(IconLoader::Load("strawberry")),
normal_icon_(icon_.pixmap(48, QIcon::Normal)),
grey_icon_(icon_.pixmap(48, QIcon::Disabled)),
action_play_pause_(nullptr),
action_stop_(nullptr),
action_stop_after_this_track_(nullptr),
action_mute_(nullptr) {
app_name_[0] = app_name_[0].toUpper();
tray_->setIcon(normal_icon_);
tray_->installEventFilter(this);
ClearNowPlaying();
#ifndef Q_OS_WIN
de_ = Utilities::DesktopEnvironment().toLower();
QFile pattern_file;
if (de_ == "kde") {
pattern_file.setFileName(":/html/playing-tooltip-plain.html");
}
else {
pattern_file.setFileName(":/html/playing-tooltip-table.html");
}
pattern_file.open(QIODevice::ReadOnly);
pattern_ = QString::fromLatin1(pattern_file.readAll());
pattern_file.close();
#endif
connect(tray_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(Clicked(QSystemTrayIcon::ActivationReason)));
}
QtSystemTrayIcon::~QtSystemTrayIcon() {
delete menu_;
}
bool QtSystemTrayIcon::eventFilter(QObject *object, QEvent *event) {
if (QObject::eventFilter(object, event)) return true;
if (object != tray_) return false;
if (event->type() == QEvent::Wheel) {
QWheelEvent *e = static_cast<QWheelEvent*>(event);
if (e->modifiers() == Qt::ShiftModifier) {
if (e->delta() > 0) {
emit SeekForward();
}
else {
emit SeekBackward();
}
}
else if (e->modifiers() == Qt::ControlModifier) {
if (e->delta() < 0) {
emit NextTrack();
}
else {
emit PreviousTrack();
}
}
else {
QSettings s;
s.beginGroup(BehaviourSettingsPage::kSettingsGroup);
bool prev_next_track = s.value("scrolltrayicon").toBool();
s.endGroup();
if (prev_next_track) {
if (e->delta() < 0) {
emit NextTrack();
}
else {
emit PreviousTrack();
}
}
else {
emit ChangeVolume(e->delta());
}
}
return true;
}
return false;
}
void QtSystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit) {
// Creating new actions and connecting them to old ones.
// This allows us to use old actions without displaying shortcuts that can not be used when Strawberry's window is hidden
menu_->addAction(previous->icon(), previous->text(), previous, SLOT(trigger()));
action_play_pause_ = menu_->addAction(play->icon(), play->text(), play, SLOT(trigger()));
action_stop_ = menu_->addAction(stop->icon(), stop->text(), stop, SLOT(trigger()));
action_stop_after_this_track_ = menu_->addAction(stop_after->icon(), stop_after->text(), stop_after, SLOT(trigger()));
menu_->addAction(next->icon(), next->text(), next, SLOT(trigger()));
menu_->addSeparator();
action_mute_ = menu_->addAction(mute->icon(), mute->text(), mute, SLOT(trigger()));
action_mute_->setCheckable(true);
action_mute_->setChecked(mute->isChecked());
menu_->addSeparator();
action_love_ = menu_->addAction(love->icon(), love->text(), love, SLOT(trigger()));
action_love_->setVisible(love->isVisible());
action_love_->setEnabled(love->isEnabled());
menu_->addSeparator();
menu_->addAction(quit->icon(), quit->text(), quit, SLOT(trigger()));
tray_->setContextMenu(menu_);
}
void QtSystemTrayIcon::Clicked(QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::Trigger:
emit ShowHide();
break;
case QSystemTrayIcon::MiddleClick:
emit PlayPause();
break;
default:
break;
}
}
void QtSystemTrayIcon::ShowPopup(const QString &summary, const QString &message, int timeout) {
tray_->showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout);
}
void QtSystemTrayIcon::UpdateIcon() {
tray_->setIcon(CreateIcon(normal_icon_, grey_icon_));
}
void QtSystemTrayIcon::SetPaused() {
SystemTrayIcon::SetPaused();
action_stop_->setEnabled(true);
action_stop_after_this_track_->setEnabled(true);
action_play_pause_->setIcon(IconLoader::Load("media-playback-start"));
action_play_pause_->setText(tr("Play"));
action_play_pause_->setEnabled(true);
}
void QtSystemTrayIcon::SetPlaying(bool enable_play_pause) {
SystemTrayIcon::SetPlaying();
action_stop_->setEnabled(true);
action_stop_after_this_track_->setEnabled(true);
action_play_pause_->setIcon(IconLoader::Load("media-playback-pause"));
action_play_pause_->setText(tr("Pause"));
action_play_pause_->setEnabled(enable_play_pause);
}
void QtSystemTrayIcon::SetStopped() {
SystemTrayIcon::SetStopped();
action_stop_->setEnabled(false);
action_stop_after_this_track_->setEnabled(false);
action_play_pause_->setIcon(IconLoader::Load("media-playback-start"));
action_play_pause_->setText(tr("Play"));
action_play_pause_->setEnabled(true);
action_love_->setEnabled(false);
}
void QtSystemTrayIcon::MuteButtonStateChanged(bool value) {
if (action_mute_) action_mute_->setChecked(value);
}
bool QtSystemTrayIcon::IsVisible() const {
return tray_->isVisible();
}
void QtSystemTrayIcon::SetVisible(bool visible) {
tray_->setVisible(visible);
}
void QtSystemTrayIcon::SetNowPlaying(const Song &song, const QUrl &cover_url) {
#ifdef Q_OS_WIN
Q_UNUSED(song);
Q_UNUSED(cover_url);
// Windows doesn't support HTML in tooltips, so just show something basic
tray_->setToolTip(song.PrettyTitleWithArtist());
#else
int columns = cover_url.isEmpty() ? 1 : 2;
QString tooltip(pattern_);
tooltip.replace("%columns" , QString::number(columns));
tooltip.replace("%appName" , app_name_);
tooltip.replace("%titleKey" , tr("Title") % ":");
tooltip.replace("%titleValue" , song.PrettyTitle().toHtmlEscaped());
tooltip.replace("%artistKey" , tr("Artist") % ":");
tooltip.replace("%artistValue", song.artist().toHtmlEscaped());
tooltip.replace("%albumKey" , tr("Album") % ":");
tooltip.replace("%albumValue" , song.album().toHtmlEscaped());
tooltip.replace("%lengthKey" , tr("Length") % ":");
tooltip.replace("%lengthValue", song.PrettyLength().toHtmlEscaped());
if (columns == 2) {
QString final_path = cover_url.isLocalFile() ? cover_url.path() : cover_url.toString();
if (de_ == "kde") {
tooltip.replace("%image", "<img src=\"" % final_path % "\" />");
}
else {
tooltip.replace("%image", " <td> <img src=\"" % final_path % "\" /> </td>");
}
}
else {
tooltip.replace("<td>%image</td>", "");
tooltip.replace("%image", "");
}
// TODO: we should also repaint this
tray_->setToolTip(tooltip);
#endif
}
void QtSystemTrayIcon::ClearNowPlaying() {
tray_->setToolTip(app_name_);
}
void QtSystemTrayIcon::LoveVisibilityChanged(bool value) {
action_love_->setVisible(value);
}
void QtSystemTrayIcon::LoveStateChanged(bool value) {
action_love_->setEnabled(value);
}