2010-06-22 13:52:55 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-10-14 21:46:11 +02:00
|
|
|
#include "iconloader.h"
|
2010-06-22 13:52:55 +02:00
|
|
|
#include "qtsystemtrayicon.h"
|
|
|
|
|
2010-11-21 22:36:27 +01:00
|
|
|
#include "core/song.h"
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
#include <QCoreApplication>
|
2010-12-06 00:41:41 +01:00
|
|
|
#include <QTextDocument>
|
|
|
|
#include <QStringBuilder>
|
2010-06-22 13:52:55 +02:00
|
|
|
#include <QMenu>
|
2010-12-06 00:41:41 +01:00
|
|
|
#include <QFile>
|
2010-06-22 13:52:55 +02:00
|
|
|
#include <QSystemTrayIcon>
|
|
|
|
#include <QWheelEvent>
|
|
|
|
|
|
|
|
QtSystemTrayIcon::QtSystemTrayIcon(QObject* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: SystemTrayIcon(parent),
|
|
|
|
tray_(new QSystemTrayIcon(this)),
|
|
|
|
menu_(new QMenu),
|
|
|
|
action_play_pause_(nullptr),
|
|
|
|
action_stop_(nullptr),
|
|
|
|
action_stop_after_this_track_(nullptr),
|
|
|
|
action_mute_(nullptr),
|
2014-03-27 18:55:58 +01:00
|
|
|
action_love_(nullptr) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QIcon theme_icon = IconLoader::Load("clementine-panel");
|
2010-10-14 21:46:11 +02:00
|
|
|
QIcon theme_icon_grey = IconLoader::Load("clementine-panel-grey");
|
|
|
|
|
|
|
|
if (theme_icon.isNull() || theme_icon_grey.isNull()) {
|
|
|
|
// Load the default icon
|
|
|
|
QIcon icon(":icon.png");
|
|
|
|
orange_icon_ = icon.pixmap(48, QIcon::Normal);
|
|
|
|
grey_icon_ = icon.pixmap(48, QIcon::Disabled);
|
|
|
|
} else {
|
|
|
|
// Use the icons from the theme
|
|
|
|
orange_icon_ = theme_icon.pixmap(48);
|
|
|
|
grey_icon_ = theme_icon_grey.pixmap(48);
|
|
|
|
}
|
2010-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
tray_->setIcon(orange_icon_);
|
|
|
|
tray_->installEventFilter(this);
|
2010-11-21 22:36:27 +01:00
|
|
|
ClearNowPlaying();
|
2010-06-22 13:52:55 +02:00
|
|
|
|
2010-12-06 00:41:41 +01:00
|
|
|
QFile pattern_file(":/now_playing_tooltip.txt");
|
|
|
|
pattern_file.open(QIODevice::ReadOnly);
|
|
|
|
pattern_ = QString::fromAscii(pattern_file.readAll());
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
connect(tray_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
|
|
|
SLOT(Clicked(QSystemTrayIcon::ActivationReason)));
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QtSystemTrayIcon::~QtSystemTrayIcon() { delete menu_; }
|
2010-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
bool QtSystemTrayIcon::eventFilter(QObject* object, QEvent* event) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (QObject::eventFilter(object, event)) return true;
|
2010-06-22 13:52:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (object != tray_) return false;
|
2010-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
if (event->type() == QEvent::Wheel) {
|
|
|
|
QWheelEvent* e = static_cast<QWheelEvent*>(event);
|
2011-03-21 16:15:17 +01:00
|
|
|
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 {
|
|
|
|
emit ChangeVolume(e->delta());
|
|
|
|
}
|
2010-06-22 13:52:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QtSystemTrayIcon::SetupMenu(QAction* previous, QAction* play,
|
|
|
|
QAction* stop, QAction* stop_after,
|
|
|
|
QAction* next, QAction* mute, QAction* love,
|
2014-03-27 18:55:58 +01:00
|
|
|
QAction* quit) {
|
2011-03-07 22:33:01 +01: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
|
|
|
|
// Clementine's window is hidden
|
2014-02-07 16:34:20 +01:00
|
|
|
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()));
|
2011-03-07 22:33:01 +01:00
|
|
|
menu_->addAction(next->icon(), next->text(), next, SLOT(trigger()));
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
menu_->addSeparator();
|
2014-02-07 16:34:20 +01:00
|
|
|
action_mute_ =
|
|
|
|
menu_->addAction(mute->icon(), mute->text(), mute, SLOT(trigger()));
|
2011-06-22 23:50:25 +02:00
|
|
|
action_mute_->setCheckable(true);
|
|
|
|
action_mute_->setChecked(mute->isChecked());
|
2011-03-07 22:33:01 +01:00
|
|
|
|
2010-11-21 22:36:27 +01:00
|
|
|
menu_->addSeparator();
|
2011-03-07 22:33:01 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
2014-02-07 16:34:20 +01:00
|
|
|
action_love_ =
|
|
|
|
menu_->addAction(love->icon(), love->text(), love, SLOT(trigger()));
|
2011-03-07 22:33:01 +01:00
|
|
|
action_love_->setVisible(love->isVisible());
|
2011-04-22 00:19:28 +02:00
|
|
|
action_love_->setEnabled(love->isEnabled());
|
2011-03-07 22:33:01 +01:00
|
|
|
#endif
|
|
|
|
|
2010-06-22 13:52:55 +02:00
|
|
|
menu_->addSeparator();
|
2011-03-07 22:33:01 +01:00
|
|
|
menu_->addAction(quit->icon(), quit->text(), quit, SLOT(trigger()));
|
2010-06-22 13:52:55 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QtSystemTrayIcon::ShowPopup(const QString& summary, const QString& message,
|
|
|
|
int timeout) {
|
2010-06-22 13:52:55 +02:00
|
|
|
tray_->showMessage(summary, message, QSystemTrayIcon::NoIcon, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::UpdateIcon() {
|
|
|
|
tray_->setIcon(CreateIcon(orange_icon_, grey_icon_));
|
|
|
|
}
|
|
|
|
|
2011-03-07 22:33:01 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-03-27 18:55:58 +01:00
|
|
|
void QtSystemTrayIcon::SetPlaying(bool enable_play_pause, bool enable_love) {
|
2011-03-07 22:33:01 +01:00
|
|
|
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);
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
action_love_->setEnabled(enable_love);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
action_love_->setEnabled(false);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-22 00:19:28 +02:00
|
|
|
void QtSystemTrayIcon::LastFMButtonVisibilityChanged(bool value) {
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
action_love_->setVisible(value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::LastFMButtonLoveStateChanged(bool value) {
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
action_love_->setEnabled(value);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-22 23:50:25 +02:00
|
|
|
void QtSystemTrayIcon::MuteButtonStateChanged(bool value) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (action_mute_) action_mute_->setChecked(value);
|
2011-06-22 23:50:25 +02:00
|
|
|
}
|
2011-04-22 00:19:28 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool QtSystemTrayIcon::IsVisible() const { return tray_->isVisible(); }
|
2010-06-22 13:52:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QtSystemTrayIcon::SetVisible(bool visible) { tray_->setVisible(visible); }
|
2010-11-21 22:36:27 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void QtSystemTrayIcon::SetNowPlaying(const Song& song,
|
|
|
|
const QString& image_path) {
|
2010-12-11 18:23:05 +01:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
// Windows doesn't support HTML in tooltips, so just show something basic
|
|
|
|
tray_->setToolTip(song.PrettyTitleWithArtist());
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2014-02-06 16:49:49 +01:00
|
|
|
int columns = image_path == nullptr ? 1 : 2;
|
2010-12-06 00:41:41 +01:00
|
|
|
|
|
|
|
QString clone = pattern_;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
clone.replace("%columns", QString::number(columns));
|
|
|
|
clone.replace("%appName", QCoreApplication::applicationName());
|
2010-12-06 00:41:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
clone.replace("%titleKey", tr("Title") % ":");
|
|
|
|
clone.replace("%titleValue", Qt::escape(song.PrettyTitle()));
|
|
|
|
clone.replace("%artistKey", tr("Artist") % ":");
|
2010-12-06 00:41:41 +01:00
|
|
|
clone.replace("%artistValue", Qt::escape(song.artist()));
|
2014-02-07 16:34:20 +01:00
|
|
|
clone.replace("%albumKey", tr("Album") % ":");
|
|
|
|
clone.replace("%albumValue", Qt::escape(song.album()));
|
2010-12-06 00:41:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
clone.replace("%lengthKey", tr("Length") % ":");
|
2010-12-06 00:41:41 +01:00
|
|
|
clone.replace("%lengthValue", Qt::escape(song.PrettyLength()));
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (columns == 2) {
|
|
|
|
QString final_path =
|
|
|
|
image_path.startsWith("file://") ? image_path.mid(7) : image_path;
|
|
|
|
clone.replace("%image",
|
|
|
|
" <td>"
|
|
|
|
" <img src=\"" %
|
|
|
|
final_path %
|
|
|
|
"\" />"
|
|
|
|
" </td>");
|
2010-12-06 00:41:41 +01:00
|
|
|
} else {
|
|
|
|
clone.replace("%image", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we should also repaint this
|
|
|
|
tray_->setToolTip(clone);
|
2010-11-21 22:36:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void QtSystemTrayIcon::ClearNowPlaying() {
|
|
|
|
tray_->setToolTip(QCoreApplication::applicationName());
|
2010-12-11 18:23:05 +01:00
|
|
|
}
|