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
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "trayitem.h"
#include "config.h"
#include "currentcover.h"
#include "mainwindow.h"
#include "mpd-interface/song.h"
#include "settings.h"
#include "stdactions.h"
#include "support/action.h"
#include "support/utils.h"
#include "widgets/icons.h"
#include <QMenu>
#include <QWheelEvent>
#include <knotification.h>
#ifndef Q_OS_MAC
class VolumeSliderEventHandler : public QObject {
public:
VolumeSliderEventHandler(QObject* p) : QObject(p) {}
protected:
bool eventFilter(QObject* obj, QEvent* event) override
{
if (QEvent::Wheel == event->type()) {
int numDegrees = static_cast<QWheelEvent*>(event)->angleDelta().y() / 8;
int numSteps = numDegrees / 15;
if (numSteps > 0) {
for (int i = 0; i < numSteps; ++i) {
StdActions::self()->increaseVolumeAction->trigger();
}
}
else {
for (int i = 0; i > numSteps; --i) {
StdActions::self()->decreaseVolumeAction->trigger();
}
}
return true;
}
return QObject::eventFilter(obj, event);
}
};
#endif
TrayItem::TrayItem(MainWindow* p)
: QObject(p)
#ifndef Q_OS_MAC
,
mw(p), trayItem(nullptr), trayItemMenu(nullptr), connectionsAction(nullptr), partitionsAction(nullptr), outputsAction(nullptr)
#endif
,
songNotif(nullptr)
{
}
Q_DECL_UNUSED static Action* copyAction(Action* orig)
{
Action* newAction = new Action(orig->parent());
newAction->setText(Utils::strippedText(orig->text()));
newAction->setIcon(orig->icon());
QObject::connect(newAction, SIGNAL(triggered()), orig, SIGNAL(triggered()));
QObject::connect(newAction, SIGNAL(triggered(bool)), orig, SIGNAL(triggered(bool)));
return newAction;
}
void TrayItem::setup()
{
#ifndef Q_OS_MAC
if (!Settings::self()->useSystemTray() || !Utils::useSystemTray()) {
if (trayItem) {
trayItem->setVisible(false);
trayItem->deleteLater();
trayItem = nullptr;
trayItemMenu->deleteLater();
trayItemMenu = nullptr;
}
return;
}
if (trayItem) {
return;
}
#ifndef Q_OS_MAC
connectionsAction = new Action(Utils::strippedText(mw->connectionsAction->text()), this);
connectionsAction->setVisible(false);
partitionsAction = new Action(Utils::strippedText(mw->partitionsAction->text()), this);
partitionsAction->setVisible(false);
outputsAction = new Action(Utils::strippedText(mw->outputsAction->text()), this);
outputsAction->setVisible(false);
updateConnections();
updatePartitions();
updateOutputs();
#endif
// What systems DONT have a system tray? Also, isSytemTrayAvailable is checked in config dialog, so
// useSystemTray should not be set if there is none.
// Checking here seems to cause the icon not to appear if Cantata is autostarted in Plasma5 - #759
//if (!QSystemTrayIcon::isSystemTrayAvailable()) {
// return;
//}
trayItem = new QSystemTrayIcon(this);
trayItem->installEventFilter(new VolumeSliderEventHandler(this));
trayItemMenu = new QMenu(nullptr);
trayItemMenu->addAction(StdActions::self()->prevTrackAction);
trayItemMenu->addAction(StdActions::self()->playPauseTrackAction);
trayItemMenu->addAction(StdActions::self()->stopPlaybackAction);
trayItemMenu->addAction(StdActions::self()->stopAfterCurrentTrackAction);
trayItemMenu->addAction(StdActions::self()->nextTrackAction);
#ifndef Q_OS_MAC
trayItemMenu->addSeparator();
trayItemMenu->addAction(connectionsAction);
trayItemMenu->addAction(partitionsAction);
trayItemMenu->addAction(outputsAction);
#endif
trayItemMenu->addSeparator();
trayItemMenu->addAction(mw->restoreAction);
trayItemMenu->addSeparator();
trayItemMenu->addAction(copyAction(mw->quitAction));
trayItem->setContextMenu(trayItemMenu);
#if defined Q_OS_MAC || defined Q_OS_WIN
QIcon icon;
icon.addFile(CANTATA_SYS_ICONS_DIR + PROJECT_REV_ID ".png");
#else
QIcon icon = QIcon::fromTheme(Utils::Gnome == Utils::currentDe() ? PROJECT_REV_ID "-symbolic" : PROJECT_REV_ID);
// Bug: 660 If installed to non-standard folder, QIcon::fromTheme does not seem to find icon. Therefore
// add icon files here...
if (icon.isNull()) {
QStringList sizes = QStringList() << "16"
<< "22"
<< "24"
<< "32"
<< "48"
<< "64";
for (const QString& s : sizes) {
icon.addFile(QLatin1String(ICON_INSTALL_PREFIX "/") + s + QLatin1Char('x') + s + QLatin1String("/apps/" PROJECT_REV_ID ".png"));
}
icon.addFile(QLatin1String(ICON_INSTALL_PREFIX "/scalable/apps/" PROJECT_REV_ID ".svg"));
}
#endif
trayItem->setIcon(icon);
trayItem->setToolTip(tr("Cantata"));
trayItem->show();
connect(trayItem, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayItemClicked(QSystemTrayIcon::ActivationReason)));
#endif
}
void TrayItem::trayItemClicked(QSystemTrayIcon::ActivationReason reason)
{
#ifdef Q_OS_MAC
Q_UNUSED(reason)
#else
switch (reason) {
case QSystemTrayIcon::Trigger:
if (mw->isHidden()) {
mw->restoreWindow();
}
else {
mw->hideWindow();
}
break;
case QSystemTrayIcon::MiddleClick:
mw->playPauseTrack();
break;
default:
break;
}
#endif
}
void TrayItem::songChanged(const Song& song, bool isPlaying)
{
if (Settings::self()->showPopups()) {
bool useable = song.isStandardStream()
? !song.title.isEmpty() && !song.name().isEmpty()
: !song.title.isEmpty() && !song.artist.isEmpty() && !song.album.isEmpty();
if (useable && isPlaying) {
if (songNotif == nullptr) {
songNotif = new KNotification("newSong");
songNotif->setAutoDelete(false);
}
songNotif->setTitle(song.mainText());
songNotif->setText(song.subText());
songNotif->setImage(CurrentCover::self()->image().scaledToHeight(512, Qt::SmoothTransformation));
songNotif->setUrgency(KNotification::LowUrgency);
songNotif->sendEvent();
}
}
}
#ifndef Q_OS_MAC
static void copyMenu(Action* from, Action* to)
{
if (!to) {
return;
}
to->setVisible(from->isVisible());
if (to->isVisible()) {
if (!to->menu()) {
to->setMenu(new QMenu(nullptr));
}
QMenu* m = to->menu();
m->clear();
for (QAction* act : from->menu()->actions()) {
m->addAction(act);
}
}
}
#endif
void TrayItem::updateConnections()
{
#ifndef Q_OS_MAC
copyMenu(mw->connectionsAction, connectionsAction);
#endif
}
void TrayItem::updatePartitions()
{
#ifndef Q_OS_MAC
copyMenu(mw->partitionsAction, partitionsAction);
#endif
}
void TrayItem::updateOutputs()
{
#ifndef Q_OS_MAC
copyMenu(mw->outputsAction, outputsAction);
#endif
}
#include "moc_trayitem.cpp"
|