/* * QMPDClient - An MPD client written in Qt 4. * Copyright (C) 2005-2007 Håvard Tautra Knutsen * * 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; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "trayicon.h" #include "config.h" #include "iconmanager.h" #include "mpd.h" #include "mpdconnection.h" #include "mpdsong.h" #include "richtext.h" #include #include #include #include #ifndef Q_WS_X11 #include "macroexpander.h" #endif TrayIcon::TrayIcon(QWidget *parent) : QSystemTrayIcon(parent), m_mpd(MPD::instance()), m_connection(MPDConnection::instance()) { setObjectName("trayicon"); #ifndef Q_WS_X11 m_connected = QPixmap(":/icons/qmpdclient16.png"); m_disconnected = QPixmap(":/icons/qmpdclient16d.png"); #else m_connected = QPixmap(22, 22); m_connected.fill(parent->palette().background().color()); QPainter p(&m_connected); p.drawPixmap(0, 0, QPixmap(":/icons/qmpdclient22.png")); m_disconnected = QPixmap(m_connected.size()); m_disconnected.fill(parent->palette().background().color()); QPainter p2(&m_disconnected); p2.drawPixmap(0, 0, QPixmap(":/icons/qmpdclient22d.png")); #endif setIcon(m_disconnected); QMenu *menu = new QMenu(parent); m_showHideAction = menu->addAction("", parent, SLOT(showHide())); menu->addSeparator(); m_prevAction = menu->addAction("", m_mpd, SLOT(prev())); m_playAction = menu->addAction("", m_mpd, SLOT(play())); m_pauseAction = menu->addAction("", m_mpd, SLOT(pause())); m_stopAction = menu->addAction("", m_mpd, SLOT(stop())); m_nextAction = menu->addAction("", m_mpd, SLOT(next())); menu->addSeparator(); m_quitAction = menu->addAction("", qApp, SLOT(quit())); setContextMenu(menu); // For icon changes m_showHideAction->setObjectName("showHideAction"); m_prevAction->setObjectName("prevAction"); m_playAction->setObjectName("playAction"); m_pauseAction->setObjectName("pauseAction"); m_stopAction->setObjectName("stopAction"); m_nextAction->setObjectName("nextAction"); m_quitAction->setObjectName("quitAction"); connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(action(QSystemTrayIcon::ActivationReason))); connect(menu, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow())); connect(m_mpd, SIGNAL(playingSongUpdated(const MPDSong &)), this, SLOT(setSong(const MPDSong &))); connect(m_connection, SIGNAL(connected(const ServerInfo &)), this, SLOT(connected())); connect(m_connection, SIGNAL(disconnected(const QString &)), this, SLOT(disconnected())); m_connection->isConnected() ? connected() : disconnected(); setToolTip(NAMEVER); } void TrayIcon::menuAboutToShow() { Q_ASSERT(m_showHideAction); if (qobject_cast(parent())->isVisible()) m_showHideAction->setText(tr("Hide")); else m_showHideAction->setText(tr("Show")); } void TrayIcon::updateTranslation() { Q_ASSERT(m_prevAction); Q_ASSERT(m_playAction); Q_ASSERT(m_pauseAction); Q_ASSERT(m_stopAction); Q_ASSERT(m_nextAction); Q_ASSERT(m_quitAction); m_prevAction->setText(tr("Previuos")); m_playAction->setText(tr("Play")); m_pauseAction->setText(tr("Pause")); m_stopAction->setText(tr("Stop")); m_nextAction->setText(tr("Next")); m_quitAction->setText(tr("Quit")); } void TrayIcon::action(QSystemTrayIcon::ActivationReason reason) { if (reason != QSystemTrayIcon::Context) emit clicked(); } void TrayIcon::setSong(const MPDSong &s) { if (s.isNull()) { QString msg = tr("Not playing", "This is for the trayicon tooltip, indicating that no song is playing"); #ifndef Q_WS_X11 // Win32 and OSX does not seem to support richtext in tooltips setToolTip(msg); #else QString tooltip = ""; tooltip += "
"; tooltip += QString(" %1
").arg(NAMEVER); tooltip += QString("%1").arg(msg); tooltip += ""; setToolTip(tooltip); #endif return; } // Make nice tooltip int desktopWidth = QApplication::desktop()->width(); QString artist = elideRichText("", s.artist(), "", desktopWidth / 8); QString album = elideRichText("", s.album(), "", desktopWidth / 8); #ifndef Q_WS_X11 // Win32 and OSX does not seem to support richtext in tooltips setToolTip(expandMacros(s, Config::instance()->playlistPattern())); #else QString tooltip = ""; tooltip += "
"; tooltip += QString(" %1
").arg(NAMEVER); tooltip += elideRichText("", s.title(), "", desktopWidth / 4) + "
"; if (!artist.isEmpty()) tooltip += artist; if (!artist.isEmpty() && !album.isEmpty()) tooltip += " - "; if (!album.isEmpty()) tooltip += album; tooltip += ""; setToolTip(tooltip); #endif } void TrayIcon::connected() { setIcon(m_connected); } void TrayIcon::disconnected() { setIcon(m_disconnected); }