2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2021-03-20 21:14:47 +01:00
|
|
|
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QSystemTrayIcon>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QString>
|
2019-07-07 21:14:24 +02:00
|
|
|
#include <QUrl>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtEvents>
|
2019-01-11 20:21:45 +01:00
|
|
|
#include <QSettings>
|
2018-05-01 00:41:33 +02:00
|
|
|
|
|
|
|
#include "song.h"
|
|
|
|
#include "iconloader.h"
|
2019-02-13 20:04:05 +01:00
|
|
|
#include "utilities.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
|
|
|
#include "systemtrayicon.h"
|
|
|
|
#include "qtsystemtrayicon.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-11 20:21:45 +01:00
|
|
|
#include "settings/behavioursettingspage.h"
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
QtSystemTrayIcon::QtSystemTrayIcon(QObject *parent)
|
|
|
|
: SystemTrayIcon(parent),
|
|
|
|
tray_(new QSystemTrayIcon(this)),
|
|
|
|
menu_(new QMenu),
|
2019-02-10 21:25:36 +01:00
|
|
|
app_name_(QCoreApplication::applicationName()),
|
2019-06-29 20:18:54 +02:00
|
|
|
icon_(IconLoader::Load("strawberry")),
|
2019-02-10 21:25:36 +01:00
|
|
|
normal_icon_(icon_.pixmap(48, QIcon::Normal)),
|
|
|
|
grey_icon_(icon_.pixmap(48, QIcon::Disabled)),
|
2018-02-27 18:06:05 +01:00
|
|
|
action_play_pause_(nullptr),
|
|
|
|
action_stop_(nullptr),
|
|
|
|
action_stop_after_this_track_(nullptr),
|
2018-09-30 15:33:27 +02:00
|
|
|
action_mute_(nullptr) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-02-13 20:04:05 +01:00
|
|
|
app_name_[0] = app_name_[0].toUpper();
|
2020-11-04 21:01:04 +01:00
|
|
|
QIcon theme_icon_grey = IconLoader::Load("strawberry-grey");
|
2020-11-11 22:55:56 +01:00
|
|
|
if (!theme_icon_grey.isNull()) {
|
2020-11-04 21:01:04 +01:00
|
|
|
grey_icon_ = theme_icon_grey.pixmap(48, QIcon::Disabled);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
tray_->setIcon(normal_icon_);
|
|
|
|
tray_->installEventFilter(this);
|
|
|
|
ClearNowPlaying();
|
|
|
|
|
2021-01-26 16:48:04 +01:00
|
|
|
QObject::connect(tray_, &QSystemTrayIcon::activated, this, &QtSystemTrayIcon::Clicked);
|
2019-02-10 21:25:36 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QtSystemTrayIcon::~QtSystemTrayIcon() {
|
|
|
|
delete menu_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QtSystemTrayIcon::eventFilter(QObject *object, QEvent *event) {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
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) {
|
2020-05-29 17:42:40 +02:00
|
|
|
if (e->angleDelta().y() > 0) {
|
2018-02-27 18:06:05 +01:00
|
|
|
emit SeekForward();
|
2018-05-01 00:41:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-02-27 18:06:05 +01:00
|
|
|
emit SeekBackward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (e->modifiers() == Qt::ControlModifier) {
|
2020-05-29 17:42:40 +02:00
|
|
|
if (e->angleDelta().y() < 0) {
|
2018-02-27 18:06:05 +01:00
|
|
|
emit NextTrack();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit PreviousTrack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-01-11 20:21:45 +01:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(BehaviourSettingsPage::kSettingsGroup);
|
|
|
|
bool prev_next_track = s.value("scrolltrayicon").toBool();
|
|
|
|
s.endGroup();
|
|
|
|
if (prev_next_track) {
|
2020-05-29 17:42:40 +02:00
|
|
|
if (e->angleDelta().y() < 0) {
|
2019-01-11 20:21:45 +01:00
|
|
|
emit NextTrack();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
emit PreviousTrack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-05-29 17:42:40 +02:00
|
|
|
emit ChangeVolume(e->angleDelta().y());
|
2019-01-11 20:21:45 +01:00
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
void QtSystemTrayIcon::SetupMenu(QAction *previous, QAction *play, QAction *stop, QAction *stop_after, QAction *next, QAction *mute, QAction *love, QAction *quit) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
// 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
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_->addAction(previous->icon(), previous->text(), previous, &QAction::trigger);
|
|
|
|
action_play_pause_ = menu_->addAction(play->icon(), play->text(), play, &QAction::trigger);
|
|
|
|
action_stop_ = menu_->addAction(stop->icon(), stop->text(), stop, &QAction::trigger);
|
|
|
|
action_stop_after_this_track_ = menu_->addAction(stop_after->icon(), stop_after->text(), stop_after, &QAction::trigger);
|
|
|
|
menu_->addAction(next->icon(), next->text(), next, &QAction::trigger);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
menu_->addSeparator();
|
2021-01-26 16:48:04 +01:00
|
|
|
action_mute_ = menu_->addAction(mute->icon(), mute->text(), mute, &QAction::trigger);
|
2018-02-27 18:06:05 +01:00
|
|
|
action_mute_->setCheckable(true);
|
|
|
|
action_mute_->setChecked(mute->isChecked());
|
|
|
|
|
|
|
|
menu_->addSeparator();
|
2021-01-26 16:48:04 +01:00
|
|
|
action_love_ = menu_->addAction(love->icon(), love->text(), love, &QAction::trigger);
|
2019-06-12 00:38:52 +02:00
|
|
|
action_love_->setVisible(love->isVisible());
|
|
|
|
action_love_->setEnabled(love->isEnabled());
|
2018-02-27 18:06:05 +01:00
|
|
|
menu_->addSeparator();
|
2021-01-26 16:48:04 +01:00
|
|
|
menu_->addAction(quit->icon(), quit->text(), quit, &QAction::trigger);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
tray_->setContextMenu(menu_);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::Clicked(QSystemTrayIcon::ActivationReason reason) {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
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);
|
2019-10-20 00:17:28 +02:00
|
|
|
action_play_pause_->setIcon(IconLoader::Load("media-playback-start"));
|
2018-02-27 18:06:05 +01:00
|
|
|
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);
|
2019-10-20 00:17:28 +02:00
|
|
|
action_play_pause_->setIcon(IconLoader::Load("media-playback-pause"));
|
2018-02-27 18:06:05 +01:00
|
|
|
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);
|
2019-10-20 00:17:28 +02:00
|
|
|
action_play_pause_->setIcon(IconLoader::Load("media-playback-start"));
|
2018-02-27 18:06:05 +01:00
|
|
|
action_play_pause_->setText(tr("Play"));
|
|
|
|
|
|
|
|
action_play_pause_->setEnabled(true);
|
|
|
|
|
2019-06-12 00:38:52 +02:00
|
|
|
action_love_->setEnabled(false);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-10-11 01:29:26 +02:00
|
|
|
void QtSystemTrayIcon::SetNowPlaying(const Song &song, const QUrl&) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
tray_->setToolTip(song.PrettyTitleWithArtist());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::ClearNowPlaying() {
|
2019-02-10 21:25:36 +01:00
|
|
|
tray_->setToolTip(app_name_);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2019-06-12 00:38:52 +02:00
|
|
|
|
|
|
|
void QtSystemTrayIcon::LoveVisibilityChanged(bool value) {
|
|
|
|
action_love_->setVisible(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::LoveStateChanged(bool value) {
|
|
|
|
action_love_->setEnabled(value);
|
|
|
|
}
|