File indexing completed on 2025-01-19 04:24:08

0001 /****************************************************************************************
0002  * Copyright (c) 2007-2008 Leo Franchi <lfranchi@gmail.com>                             *
0003  * Copyright (c) 2008 Mark Kretschmann <kretschmann@kde.org>                            *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #define DEBUG_PREFIX "LyricsEngine"
0019 
0020 #include "LyricsEngine.h"
0021 
0022 #include "EngineController.h"
0023 #include "core/support/Amarok.h"
0024 #include "core/support/Debug.h"
0025 #include "lyrics/LyricsManager.h"
0026 
0027 #include <QFont>
0028 
0029 #include <KFontChooser>
0030 
0031 
0032 LyricsEngine::LyricsEngine( QObject* parent )
0033     : QObject( parent )
0034     , m_fetching( false )
0035     , m_isUpdateInProgress( false )
0036 {
0037 
0038     EngineController* engine = The::engineController();
0039     LyricsManager* lyricsManager = LyricsManager::instance();
0040 
0041     connect( engine, &EngineController::trackChanged,
0042              this, &LyricsEngine::update );
0043     connect( engine, &EngineController::trackMetadataChanged,
0044              this, &LyricsEngine::onTrackMetadataChanged );
0045     connect( engine, &EngineController::trackPositionChanged,
0046              this, &LyricsEngine::positionChanged );
0047     connect( lyricsManager, &LyricsManager::newLyrics, this, &LyricsEngine::newLyrics );
0048     connect( lyricsManager, &LyricsManager::newSuggestions, this, &LyricsEngine::newSuggestions );
0049 }
0050 
0051 void LyricsEngine::onTrackMetadataChanged( Meta::TrackPtr track )
0052 {
0053     DEBUG_BLOCK
0054 
0055     // Only update if the lyrics have changed.
0056     if( m_lyrics != track->cachedLyrics() )
0057         update();
0058 }
0059 
0060 void LyricsEngine::update()
0061 {
0062     Meta::TrackPtr track = The::engineController()->currentTrack();
0063     if( !track )
0064     {
0065         clearLyrics();
0066         return;
0067     }
0068 
0069     if( LyricsManager::instance()->isEmpty( track->cachedLyrics() ) )
0070     {
0071         clearLyrics();
0072         return;
0073     }
0074 
0075     newLyrics( track );
0076 }
0077 
0078 void LyricsEngine::newLyrics( const Meta::TrackPtr &track )
0079 {
0080     DEBUG_BLOCK
0081 
0082     if( track != The::engineController()->currentTrack() )
0083         return;
0084 
0085     m_lyrics = track->cachedLyrics();
0086     Q_EMIT lyricsChanged();
0087 
0088     m_fetching = false;
0089     Q_EMIT fetchingChanged();
0090 }
0091 
0092 void LyricsEngine::newSuggestions( const QVariantList &suggested )
0093 {
0094     DEBUG_BLOCK
0095 
0096     // each string is in "title - artist <url>" form
0097     m_suggestions = suggested;
0098     clearLyrics();
0099 }
0100 
0101 void LyricsEngine::lyricsMessage( const QString& key, const QString &val )
0102 {
0103     DEBUG_BLOCK
0104 
0105     clearLyrics();
0106     Q_EMIT newLyricsMessage( key, val );
0107 }
0108 
0109 void LyricsEngine::clearLyrics()
0110 {
0111     m_fetching = false;
0112     Q_EMIT fetchingChanged();
0113 
0114     m_lyrics.clear();
0115     Q_EMIT lyricsChanged();
0116 }
0117 
0118 qreal LyricsEngine::position() const
0119 {
0120     return (qreal)The::engineController()->trackPosition() * 1000 / The::engineController()->trackLength();
0121 }
0122 
0123 void LyricsEngine::refetchLyrics() const
0124 {
0125     Meta::TrackPtr currentTrack = The::engineController()->currentTrack();
0126     if( !currentTrack )
0127         return;
0128 
0129     LyricsManager::instance()->loadLyrics( currentTrack, true );
0130 }
0131 
0132 qreal LyricsEngine::fontSize() const
0133 {
0134     return Amarok::config( "Context" ).group( "Lyrics" ).readEntry( "fontSize", 18 );
0135 }
0136 
0137 void LyricsEngine::setFontSize(qreal fontSize)
0138 {
0139     DEBUG_BLOCK
0140 
0141     if( fontSize == this->fontSize() )
0142         return;
0143 
0144     Amarok::config( "Context" ).group( "Lyrics" ).writeEntry( "fontSize", fontSize );
0145     Q_EMIT fontSizeChanged();
0146 }
0147 
0148 int LyricsEngine::alignment() const
0149 {
0150     return Amarok::config( "Context" ).group( "Lyrics" ).readEntry( "alignment", 2 );
0151 }
0152 
0153 void LyricsEngine::setAlignment(int alignment)
0154 {
0155     DEBUG_BLOCK
0156 
0157     if( alignment == this->alignment() )
0158         return;
0159 
0160     Amarok::config( "Context" ).group( "Lyrics" ).writeEntry( "alignment", alignment );
0161     Q_EMIT alignmentChanged();
0162 }
0163 
0164 QString LyricsEngine::font() const
0165 {
0166     return Amarok::config( "Context" ).group( "Lyrics" ).readEntry( "font", QFont().family() );
0167 }
0168 
0169 void LyricsEngine::setFont(const QString& font)
0170 {
0171     DEBUG_BLOCK
0172 
0173     if( font == this->font() )
0174         return;
0175 
0176     Amarok::config( "Context" ).group( "Lyrics" ).writeEntry( "font", font );
0177     Q_EMIT fontChanged();
0178 }
0179 
0180 QStringList LyricsEngine::availableFonts() const
0181 {
0182     QStringList list;
0183 
0184     KFontChooser::getFontList( list, 0 );
0185 
0186     return list;
0187 }
0188