File indexing completed on 2024-04-21 04:47:50

0001 /****************************************************************************************
0002  * Copyright (c) 2009-2011 Kevin Funk <krf@electrostorm.net>                            *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "KNotificationBackend.h"
0018 
0019 #include "EngineController.h"
0020 #include "SvgHandler.h"
0021 #include "core/meta/Meta.h"
0022 #include "core/support/Debug.h"
0023 
0024 #include <KIconLoader>
0025 #include <KLocalizedString>
0026 #include <KNotification>
0027 #include <KWindowSystem>
0028 
0029 using namespace Amarok;
0030 
0031 KNotificationBackend *
0032 KNotificationBackend::s_instance = nullptr;
0033 
0034 KNotificationBackend *
0035 KNotificationBackend::instance()
0036 {
0037     if( !s_instance )
0038         s_instance = new KNotificationBackend();
0039     return s_instance;
0040 }
0041 
0042 void
0043 KNotificationBackend::destroy()
0044 {
0045     delete s_instance;
0046     s_instance = nullptr;
0047 }
0048 
0049 KNotificationBackend::KNotificationBackend()
0050     : m_enabled( false )
0051 {
0052     EngineController *engine = The::engineController();
0053     connect( engine, &EngineController::trackPlaying, this, &KNotificationBackend::showCurrentTrack );
0054     connect( engine, &EngineController::trackMetadataChanged, this, &KNotificationBackend::showCurrentTrack );
0055     connect( engine, &EngineController::albumMetadataChanged, this, &KNotificationBackend::showCurrentTrack );
0056 
0057     if( engine->isPlaying() )
0058         showCurrentTrack();
0059 }
0060 
0061 KNotificationBackend::~KNotificationBackend()
0062 {
0063     if( m_notify )
0064         m_notify->close();
0065 }
0066 
0067 void
0068 KNotificationBackend::setEnabled( bool enable )
0069 {
0070     m_enabled = enable;
0071 }
0072 
0073 bool
0074 KNotificationBackend::isEnabled() const
0075 {
0076     return m_enabled;
0077 }
0078 
0079 bool
0080 KNotificationBackend::isFullscreenWindowActive() const
0081 {
0082     // Get information of the active window.
0083     KWindowInfo activeWindowInfo( KWindowSystem::activeWindow(), NET::WMState );
0084 
0085     // Check if it is running in fullscreen mode.
0086     return activeWindowInfo.hasState( NET::FullScreen );
0087 }
0088 
0089 void
0090 KNotificationBackend::show( const QString &title, const QString &body, const QPixmap &pixmap )
0091 {
0092     QPixmap icon;
0093     if( pixmap.isNull() )
0094     {
0095         KIconLoader loader;
0096         icon = loader.loadIcon( QStringLiteral("amarok"), KIconLoader::Desktop );
0097     }
0098     else
0099         icon = pixmap;
0100 
0101     KNotification *notify = new KNotification( "message" );
0102     notify->setTitle( title );
0103     notify->setText( body );
0104     notify->setPixmap( icon );
0105     notify->sendEvent();
0106 }
0107 
0108 void
0109 KNotificationBackend::showCurrentTrack( bool force )
0110 {
0111     if( !m_enabled && !force )
0112         return;
0113 
0114     EngineController *engine = The::engineController();
0115     Meta::TrackPtr track = engine->currentTrack();
0116     if( !track )
0117     {
0118         warning() << __PRETTY_FUNCTION__ << "null track!";
0119         return;
0120     }
0121 
0122     const QString title = i18n( "Now playing" );
0123     const QString text = engine->prettyNowPlaying();
0124     Meta::AlbumPtr album = track->album();
0125     const QPixmap pixmap = album ? The::svgHandler()->imageWithBorder( album, 80 ) : QPixmap();
0126 
0127     KNotification *notify = m_notify.data();
0128     if( !notify )
0129         notify = new KNotification( "trackChange" );
0130     notify->setTitle( title );
0131     notify->setText( text );
0132     notify->setPixmap( pixmap );
0133 
0134     if( m_notify ) // existing notification already shown
0135         notify->update();
0136     notify->sendEvent(); // (re)start timeout in both cases
0137     m_notify = notify;
0138 }