File indexing completed on 2024-05-19 04:50:11

0001 /****************************************************************************************
0002  * Copyright (c) 2007 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 #define DEBUG_PREFIX "AmpacheService"
0018 
0019 #include "AmpacheService.h"
0020 
0021 #include "AmpacheConfig.h"
0022 #include "AmpacheAccountLogin.h"
0023 
0024 #include "core/support/Amarok.h"
0025 #include "core/support/Components.h"
0026 #include "core/logger/Logger.h"
0027 #include "browsers/SingleCollectionTreeItemModel.h"
0028 #include "core-impl/collections/support/CollectionManager.h"
0029 #include <config.h>
0030 #include "core/support/Debug.h"
0031 
0032 #ifdef HAVE_LIBLASTFM
0033   #include "LastfmInfoParser.h"
0034 #endif
0035 
0036 #include <QStandardPaths>
0037 
0038 
0039 AmpacheServiceFactory::AmpacheServiceFactory()
0040     : ServiceFactory()
0041 {}
0042 
0043 void AmpacheServiceFactory::init()
0044 {
0045     //read config and create the needed number of services
0046     AmpacheConfig config;
0047     AmpacheServerList servers = config.servers();
0048     m_initialized = true;
0049 
0050     for( int i = 0; i < servers.size(); i++ )
0051     {
0052         AmpacheServerEntry server = servers.at( i );
0053         ServiceBase* service = new AmpacheService( this, "Ampache (" + server.name + ')', server.url, server. username, server.password );
0054         Q_EMIT newService( service );
0055     }
0056 }
0057 
0058 QString
0059 AmpacheServiceFactory::name()
0060 {
0061     return QStringLiteral("Ampache");
0062 }
0063 
0064 KConfigGroup
0065 AmpacheServiceFactory::config()
0066 {
0067     return Amarok::config( QStringLiteral("Service_Ampache") );
0068 }
0069 
0070 bool
0071 AmpacheServiceFactory::possiblyContainsTrack(const QUrl &url) const
0072 {
0073     AmpacheConfig config;
0074     foreach( const AmpacheServerEntry &server, config.servers() )
0075     {
0076         if ( server.url.isParentOf( url ) )
0077             return true;
0078     }
0079 
0080     return false;
0081 }
0082 
0083 AmpacheService::AmpacheService( AmpacheServiceFactory* parent, const QString & name, const QUrl &url, const QString &username, const QString &password )
0084     : ServiceBase( name,  parent )
0085     , m_infoParser( nullptr )
0086     , m_collection( nullptr )
0087     , m_ampacheLogin( new AmpacheAccountLogin( url, username, password, this ) )
0088 {
0089     DEBUG_BLOCK
0090     connect( m_ampacheLogin, &AmpacheAccountLogin::loginSuccessful, this, &AmpacheService::onLoginSuccessful );
0091     setShortDescription( i18n( "Amarok frontend for your Ampache server" ) );
0092     setIcon( QIcon::fromTheme( "view-services-ampache-amarok" ) );
0093     setLongDescription( i18n( "Use Amarok as a seamless frontend to your Ampache server. This lets you browse and play all the Ampache contents from within Amarok." ) );
0094     setImagePath( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/hover_info_ampache.png" ) );
0095 #ifdef HAVE_LIBLASTFM
0096     m_infoParser = new LastfmInfoParser();
0097 #endif
0098 }
0099 
0100 AmpacheService::~AmpacheService()
0101 {
0102     CollectionManager::instance()->removeTrackProvider( m_collection );
0103     delete m_collection;
0104     m_ampacheLogin->deleteLater();
0105 }
0106 
0107 void
0108 AmpacheService::polish()
0109 {
0110     m_bottomPanel->hide();
0111     setInfoParser( m_infoParser );
0112 
0113     /*if ( !m_authenticated )
0114         authenticate( );*/
0115 }
0116 
0117 void
0118 AmpacheService::reauthenticate()
0119 {
0120     m_ampacheLogin->reauthenticate();
0121     // it would make sense here to clean the complete cache
0122     // information from a server might get outdated.
0123 }
0124 
0125 void
0126 AmpacheService::onLoginSuccessful()
0127 {
0128     m_collection = new Collections::AmpacheServiceCollection( this, m_ampacheLogin->server(), m_ampacheLogin->sessionId() );
0129     // connect( m_collection, SIGNAL(authenticationNeeded()), SLOT(authenticate()) );
0130 
0131     CollectionManager::instance()->addTrackProvider( m_collection );
0132     QList<CategoryId::CatMenuId> levels;
0133     levels << CategoryId::Artist << CategoryId::Album;
0134     setModel( new SingleCollectionTreeItemModel( m_collection, levels ) );
0135     setServiceReady( true );
0136 }