| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2009 Kevin Funk <krf@electrostorm.net> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#include "KNotificationBackend.h" |
| 18 |
|
| 19 |
#include "core/support/Amarok.h" |
| 20 |
#include "SvgHandler.h" |
| 21 |
#include "core/support/Debug.h" |
| 22 |
#include "EngineController.h" |
| 23 |
#include "core/meta/Meta.h" |
| 24 |
|
| 25 |
#include <QTimer> |
| 26 |
|
| 27 |
|
| 28 |
namespace Amarok |
| 29 |
{ |
| 30 |
KNotificationBackend* |
| 31 |
KNotificationBackend::s_instance = 0; |
| 32 |
|
| 33 |
KNotificationBackend* |
| 34 |
KNotificationBackend::instance() |
| 35 |
{ |
| 36 |
return s_instance ? s_instance : s_instance = new KNotificationBackend(); |
| 37 |
} |
| 38 |
|
| 39 |
void |
| 40 |
KNotificationBackend::destroy() |
| 41 |
{ |
| 42 |
if( s_instance ) { delete s_instance; s_instance = 0; } |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
Amarok::KNotificationBackend::KNotificationBackend() |
| 47 |
: EngineObserver( The::engineController() ) |
| 48 |
, m_notify( 0 ) |
| 49 |
, m_enabled( false ) |
| 50 |
{ |
| 51 |
DEBUG_BLOCK |
| 52 |
|
| 53 |
m_timer = new QTimer( this ); |
| 54 |
m_timer->setSingleShot( true ); |
| 55 |
connect( m_timer, SIGNAL( timeout() ), this, SLOT( showCurrentTrack() ) ); |
| 56 |
} |
| 57 |
|
| 58 |
Amarok::KNotificationBackend::~KNotificationBackend() |
| 59 |
{ |
| 60 |
DEBUG_BLOCK |
| 61 |
|
| 62 |
if (m_notify) |
| 63 |
m_notify->close(); |
| 64 |
} |
| 65 |
|
| 66 |
void |
| 67 |
Amarok::KNotificationBackend::setEnabled(bool enable) |
| 68 |
{ |
| 69 |
m_enabled = enable; |
| 70 |
} |
| 71 |
|
| 72 |
bool |
| 73 |
Amarok::KNotificationBackend::isEnabled() const |
| 74 |
{ |
| 75 |
return m_enabled; |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
Amarok::KNotificationBackend::notificationClosed() |
| 80 |
{ |
| 81 |
if( sender() == m_notify ) |
| 82 |
m_notify = 0; |
| 83 |
} |
| 84 |
|
| 85 |
void |
| 86 |
Amarok::KNotificationBackend::engineStateChanged( Phonon::State state, Phonon::State /*oldState*/ ) |
| 87 |
{ |
| 88 |
switch( state ) |
| 89 |
{ |
| 90 |
case Phonon::PlayingState: |
| 91 |
case Phonon::StoppedState: |
| 92 |
case Phonon::PausedState: |
| 93 |
case Phonon::LoadingState: |
| 94 |
break; |
| 95 |
case Phonon::ErrorState: |
| 96 |
if( m_timer->isActive() ) |
| 97 |
m_timer->stop(); // Do not notify if track cannot be played |
| 98 |
case Phonon::BufferingState: |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void |
| 104 |
Amarok::KNotificationBackend::engineNewTrackPlaying() |
| 105 |
{ |
| 106 |
DEBUG_BLOCK |
| 107 |
|
| 108 |
if( !m_enabled ) |
| 109 |
return; // KNotify is disabled, so don't start timer |
| 110 |
|
| 111 |
m_timer->start( 3000 ); // Wait some time to display the correct cover |
| 112 |
} |
| 113 |
|
| 114 |
void |
| 115 |
Amarok::KNotificationBackend::showCurrentTrack() // slot |
| 116 |
{ |
| 117 |
DEBUG_BLOCK |
| 118 |
|
| 119 |
if( !m_enabled ) |
| 120 |
return; |
| 121 |
|
| 122 |
Meta::TrackPtr track = The::engineController()->currentTrack(); |
| 123 |
if( track ) |
| 124 |
{ |
| 125 |
if( m_notify ) { |
| 126 |
m_notify->close(); // Close old notification (when switching quickly between tracks) |
| 127 |
} |
| 128 |
|
| 129 |
m_notify = new KNotification( "trackChange" ); |
| 130 |
connect( m_notify, SIGNAL(closed()), this, SLOT(notificationClosed()) ); |
| 131 |
|
| 132 |
if( track->album() ) |
| 133 |
m_notify->setPixmap( The::svgHandler()->imageWithBorder( track->album(), 80 ) ); |
| 134 |
|
| 135 |
m_notify->setTitle( i18n( "Now playing" ) ); |
| 136 |
|
| 137 |
m_notify->setText( The::engineController()->prettyNowPlaying() ); |
| 138 |
m_notify->sendEvent(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
#include "KNotificationBackend.moc" |