File indexing completed on 2024-05-05 04:47:20

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
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 "AmarokUrlHandler.h"
0018 
0019 #include "GlobalCurrentTrackActions.h"
0020 #include "amarokurls/BookmarkMetaActions.h"
0021 #include "amarokurls/BookmarkModel.h"
0022 #include "amarokurls/ContextUrlGenerator.h"
0023 #include "amarokurls/NavigationUrlGenerator.h"
0024 #include "amarokurls/NavigationUrlRunner.h"
0025 #include "amarokurls/PlayUrlGenerator.h"
0026 #include "amarokurls/PlayUrlRunner.h"
0027 #include <core/storage/SqlStorage.h>
0028 #include "core/meta/Meta.h"
0029 #include "core/support/Debug.h"
0030 #include "core-impl/storage/StorageManager.h"
0031 #include "core-impl/meta/timecode/TimecodeObserver.h"
0032 #include "playlist/PlaylistViewUrlGenerator.h"
0033 
0034 #include <QIcon>
0035 #include <KLocalizedString>
0036 
0037 namespace The {
0038     static AmarokUrlHandler* s_AmarokUrlHandler_instance = nullptr;
0039 
0040     AmarokUrlHandler* amarokUrlHandler()
0041     {
0042         if( !s_AmarokUrlHandler_instance )
0043             s_AmarokUrlHandler_instance = new AmarokUrlHandler();
0044 
0045         return s_AmarokUrlHandler_instance;
0046     }
0047 }
0048 
0049 AmarokUrlHandler::AmarokUrlHandler()
0050     : QObject()
0051     , m_navigationRunner( nullptr )
0052     , m_playRunner ( nullptr )
0053     , m_timecodeObserver( nullptr )
0054 {
0055     DEBUG_BLOCK
0056 
0057     //init the bookmark model to make sure that db tables are created/updated if needed.
0058     BookmarkModel::instance();
0059     
0060     //we init some of the default runners here.
0061     m_navigationRunner = new NavigationUrlRunner();
0062     m_playlistViewRunner = new Playlist::ViewUrlRunner();
0063     m_playRunner = new PlayUrlRunner();
0064     m_timecodeObserver = new TimecodeObserver( this );
0065     registerRunner( m_navigationRunner, m_navigationRunner->command() );
0066     registerRunner( m_playRunner, m_playRunner->command() );
0067     registerRunner( m_playlistViewRunner, m_playlistViewRunner->command() );
0068 
0069     registerGenerator( ContextUrlGenerator::instance() );
0070     registerGenerator( NavigationUrlGenerator::instance() );
0071     registerGenerator( Playlist::ViewUrlGenerator::instance() );
0072     registerGenerator( PlayUrlGenerator::instance() );
0073 }
0074 
0075 
0076 AmarokUrlHandler::~AmarokUrlHandler()
0077 {
0078     delete m_navigationRunner;
0079     delete m_playlistViewRunner;
0080 }
0081 
0082 void AmarokUrlHandler::registerRunner( AmarokUrlRunnerBase * runner, const QString & command )
0083 {
0084     m_registeredRunners.insert( command, runner );
0085 }
0086 
0087 void AmarokUrlHandler::unRegisterRunner( AmarokUrlRunnerBase * runner )
0088 {
0089     //get the key of the runner
0090     QString key = m_registeredRunners.key( runner, QString() );
0091 
0092     if ( !key.isEmpty() )
0093         m_registeredRunners.remove( key );
0094 }
0095 
0096 void AmarokUrlHandler::registerGenerator( AmarokUrlGenerator * generator )
0097 {
0098     if( !m_registeredGenerators.contains( generator ) )
0099         m_registeredGenerators.append( generator );
0100 }
0101 
0102 void AmarokUrlHandler::unRegisterGenerator( AmarokUrlGenerator * generator )
0103 {
0104     m_registeredGenerators.removeAll( generator );
0105 }
0106 
0107 bool AmarokUrlHandler::run( const AmarokUrl &url )
0108 {
0109 
0110     DEBUG_BLOCK
0111 
0112     QString command = url.command();
0113 
0114     debug() << "command: " << command;
0115     debug() << "registered commands: " << m_registeredRunners.keys();
0116 
0117     if ( m_registeredRunners.contains( command ) )
0118         return m_registeredRunners.value( command )->run( url );
0119     else
0120         return false;
0121 
0122 }
0123 
0124 void AmarokUrlHandler::bookmarkAlbum( const Meta::AlbumPtr &album ) //slot
0125 {
0126     NavigationUrlGenerator::instance()->urlFromAlbum( album ).saveToDb();
0127     BookmarkModel::instance()->reloadFromDb();
0128 }
0129 
0130 void AmarokUrlHandler::bookmarkArtist( const Meta::ArtistPtr &artist ) //slot
0131 {
0132     NavigationUrlGenerator::instance()->urlFromArtist( artist ).saveToDb();
0133     BookmarkModel::instance()->reloadFromDb();
0134 }
0135 
0136 BookmarkList AmarokUrlHandler::urlsByCommand( const QString &command )
0137 {
0138     DEBUG_BLOCK
0139 
0140     QString query = QStringLiteral("SELECT id, parent_id, name, url, description, custom FROM bookmarks where url like 'amarok://%1%' ORDER BY name;");
0141     query = query.arg( command );
0142     QStringList result = StorageManager::instance()->sqlStorage()->query( query );
0143 
0144     debug() << "Result: " << result;
0145     int resultRows = result.count() / 6;
0146 
0147     BookmarkList resultList;
0148     for( int i = 0; i < resultRows; i++ )
0149     {
0150         QStringList row = result.mid( i*6, 6 );
0151         resultList << AmarokUrlPtr( new AmarokUrl( row ) );
0152     }
0153 
0154     return resultList;
0155 }
0156 
0157 AmarokUrl
0158 AmarokUrlHandler::createBrowserViewBookmark()
0159 {
0160     return NavigationUrlGenerator::instance()->CreateAmarokUrl();
0161 }
0162 
0163 AmarokUrl
0164 AmarokUrlHandler::createPlaylistViewBookmark()
0165 {
0166     return Playlist::ViewUrlGenerator::instance()->createUrl();
0167 }
0168 
0169 AmarokUrl
0170 AmarokUrlHandler::createContextViewBookmark()
0171 {
0172     return ContextUrlGenerator::instance()->createContextBookmark();
0173 }
0174 
0175 void AmarokUrlHandler::bookmarkCurrentBrowserView()
0176 {
0177     AmarokUrl url = createBrowserViewBookmark();
0178     url.saveToDb();
0179     BookmarkModel::instance()->reloadFromDb();
0180 }
0181 
0182 void
0183 AmarokUrlHandler::bookmarkCurrentPlaylistView()
0184 {
0185     AmarokUrl url = createPlaylistViewBookmark();
0186     url.saveToDb();
0187     BookmarkModel::instance()->reloadFromDb();
0188 }
0189 
0190 void
0191 AmarokUrlHandler::bookmarkCurrentContextView()
0192 {
0193     AmarokUrl url = createContextViewBookmark();
0194     url.saveToDb();
0195     BookmarkModel::instance()->reloadFromDb(); 
0196 }
0197 
0198 QIcon
0199 AmarokUrlHandler::iconForCommand( const QString &command )
0200 {
0201     if( m_registeredRunners.keys().contains( command ) )
0202         return m_registeredRunners.value( command )->icon();
0203 
0204     return QIcon::fromTheme( QStringLiteral("unknown") );
0205 }
0206 
0207 void AmarokUrlHandler::updateTimecodes(const QString* BookmarkName)
0208 {
0209     Q_EMIT timecodesUpdated( BookmarkName );
0210 }
0211 
0212 void
0213 AmarokUrlHandler::paintNewTimecode( const QString &name, int pos )
0214 {
0215     Q_EMIT timecodeAdded( name, pos );
0216 }
0217 
0218 QString
0219 AmarokUrlHandler::prettyCommand( const QString &command )
0220 {
0221     if( m_registeredRunners.keys().contains( command ) )
0222         return m_registeredRunners.value( command )->prettyCommand();
0223 
0224     return i18nc( "The command type of this url is not known", "Unknown" );
0225 }
0226 
0227 
0228