File indexing completed on 2024-05-19 04:50:12
0001 /**************************************************************************************** 0002 * Copyright (c) 2010 - 2011 Stefan Derkits <stefan@derkits.at> * 0003 * Copyright (c) 2010 - 2011 Christian Wagner <christian.wagner86@gmx.at> * 0004 * Copyright (c) 2010 - 2011 Felix Winter <ixos01@gmail.com> * 0005 * * 0006 * This program is free software; you can redistribute it and/or modify it under * 0007 * the terms of the GNU General Public License as published by the Free Software * 0008 * Foundation; either version 2 of the License, or (at your option) any later * 0009 * version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0013 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License along with * 0016 * this program. If not, see <http://www.gnu.org/licenses/>. * 0017 ****************************************************************************************/ 0018 0019 #define DEBUG_PREFIX "GpodderService" 0020 0021 #include "GpodderService.h" 0022 0023 #include "core/podcasts/PodcastProvider.h" 0024 #include "core/support/Debug.h" 0025 #include "GpodderPodcastTreeItem.h" 0026 #include "GpodderServiceConfig.h" 0027 #include "GpodderServiceModel.h" 0028 #include "GpodderServiceView.h" 0029 #include "GpodderSortFilterProxyModel.h" 0030 #include <mygpo-qt5/ApiRequest.h> 0031 #include <mygpo-qt5/Podcast.h> 0032 #include "playlistmanager/PlaylistManager.h" 0033 #include "widgets/SearchWidget.h" 0034 0035 #include <QHostInfo> 0036 #include <QStandardPaths> 0037 #include <QUrl> 0038 0039 0040 GpodderServiceFactory::GpodderServiceFactory() 0041 : ServiceFactory() 0042 {} 0043 0044 GpodderServiceFactory::~GpodderServiceFactory() 0045 {} 0046 0047 void 0048 GpodderServiceFactory::init() 0049 { 0050 ServiceBase *service = createGpodderService(); 0051 if( service ) 0052 { 0053 m_initialized = true; 0054 emit newService( service ); 0055 } 0056 } 0057 0058 QString 0059 GpodderServiceFactory::name() 0060 { 0061 return QStringLiteral("gpodder.net"); 0062 } 0063 0064 KConfigGroup 0065 GpodderServiceFactory::config() 0066 { 0067 return Amarok::config( GpodderServiceConfig::configSectionName() ); 0068 } 0069 0070 void 0071 GpodderServiceFactory::slotCreateGpodderService() 0072 { 0073 //Until we can remove a service when networking gets disabled, only create it the first time. 0074 if( !m_initialized ) 0075 { 0076 ServiceBase *service = createGpodderService(); 0077 if( service ) 0078 { 0079 m_initialized = true; 0080 emit newService( service ); 0081 } 0082 } 0083 } 0084 0085 void 0086 GpodderServiceFactory::slotRemoveGpodderService() 0087 { 0088 if( activeServices().isEmpty() ) 0089 return; 0090 0091 m_initialized = false; 0092 emit removeService( activeServices().first() ); 0093 } 0094 0095 ServiceBase * 0096 GpodderServiceFactory::createGpodderService() 0097 { 0098 ServiceBase *service = new GpodderService( this, QLatin1String( "gpodder" ) ); 0099 return service; 0100 } 0101 0102 GpodderService::GpodderService( GpodderServiceFactory *parent, const QString &name ) 0103 : ServiceBase( name, parent, false ) 0104 , m_inited( false ) 0105 , m_apiRequest( nullptr ) 0106 , m_podcastProvider( nullptr ) 0107 , m_proxyModel( nullptr ) 0108 , m_subscribeButton( nullptr ) 0109 , m_selectionModel( nullptr ) 0110 { 0111 DEBUG_BLOCK 0112 0113 setShortDescription( i18n( "gpodder.net: Podcast Directory Service" ) ); 0114 setIcon( QIcon::fromTheme( QStringLiteral("view-services-gpodder-amarok") ) ); 0115 setLongDescription( 0116 i18n( "gpodder.net is an online Podcast Directory & Synchonisation Service." ) ); 0117 setImagePath( QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/images/mygpo.png") ) ); 0118 0119 init(); 0120 } 0121 0122 GpodderService::~GpodderService() 0123 { 0124 DEBUG_BLOCK 0125 0126 if( m_podcastProvider ) 0127 { 0128 //Remove the provider 0129 The::playlistManager()->removeProvider( m_podcastProvider ); 0130 delete m_podcastProvider; 0131 } 0132 0133 if ( m_apiRequest ) 0134 delete m_apiRequest; 0135 } 0136 0137 //This Method should only contain the most necessary things for initilazing the Service 0138 void 0139 GpodderService::init() 0140 { 0141 DEBUG_BLOCK 0142 0143 GpodderServiceConfig config; 0144 0145 const QString &username = config.username(); 0146 const QString &password = config.password(); 0147 0148 if ( m_apiRequest ) 0149 delete m_apiRequest; 0150 0151 //We have to check this here too, since KWallet::openWallet() doesn't 0152 //guarantee that it will always return a wallet. 0153 //Notice that LastFm service does the same verification. 0154 if ( !config.isDataLoaded() ) 0155 { 0156 debug() << "Failed to read gpodder credentials."; 0157 m_apiRequest = new mygpo::ApiRequest( The::networkAccessManager() ); 0158 } 0159 else 0160 { 0161 if( config.enableProvider() ) 0162 { 0163 m_apiRequest = new mygpo::ApiRequest( username, 0164 password, 0165 The::networkAccessManager() ); 0166 if( m_podcastProvider ) 0167 delete m_podcastProvider; 0168 0169 enableGpodderProvider( username ); 0170 } 0171 else 0172 m_apiRequest = new mygpo::ApiRequest( The::networkAccessManager() ); 0173 } 0174 0175 setServiceReady( true ); 0176 m_inited = true; 0177 } 0178 0179 //This Method should contain the rest of the Service Initialization (not soo necessary things, that 0180 //can be done after the Object was created) 0181 void 0182 GpodderService::polish() 0183 { 0184 DEBUG_BLOCK 0185 0186 generateWidgetInfo(); 0187 0188 if( m_polished ) 0189 return; 0190 0191 //do not allow this content to get added to the playlist. At least not for now 0192 setPlayableTracks( false ); 0193 0194 GpodderServiceView *view = new GpodderServiceView( this ); 0195 view->setHeaderHidden( true ); 0196 view->setFrameShape( QFrame::NoFrame ); 0197 0198 // Was set true in OpmlDirectoryService, but I think we won't need this on true 0199 view->setDragEnabled( false ); 0200 view->setItemsExpandable( true ); 0201 0202 view->setSortingEnabled( false ); 0203 view->setEditTriggers( QAbstractItemView::NoEditTriggers ); 0204 view->setDragDropMode( QAbstractItemView::NoDragDrop ); 0205 0206 setView( view ); 0207 0208 GpodderServiceModel *sourceModel = new GpodderServiceModel( m_apiRequest, this ); 0209 0210 m_proxyModel = new GpodderSortFilterProxyModel( this ); 0211 m_proxyModel->setDynamicSortFilter( true ); 0212 m_proxyModel->setFilterCaseSensitivity( Qt::CaseInsensitive ); 0213 0214 m_proxyModel->setSourceModel( sourceModel ); 0215 0216 setModel( m_proxyModel ); 0217 0218 m_selectionModel = view->selectionModel(); 0219 0220 m_subscribeButton = new QPushButton(); 0221 m_subscribeButton->setParent( m_bottomPanel ); 0222 m_subscribeButton->setText( i18n( "Subscribe" ) ); 0223 m_subscribeButton->setObjectName( "subscribeButton" ); 0224 m_subscribeButton->setIcon( QIcon::fromTheme( QStringLiteral("get-hot-new-stuff-amarok") ) ); 0225 0226 m_subscribeButton->setEnabled( true ); 0227 0228 connect( m_subscribeButton, &QPushButton::clicked, this, &GpodderService::subscribe ); 0229 0230 connect( m_searchWidget, &SearchWidget::filterChanged, 0231 m_proxyModel, &QSortFilterProxyModel::setFilterWildcard ); 0232 0233 m_polished = true; 0234 } 0235 0236 void 0237 GpodderService::itemSelected( CollectionTreeItem * selectedItem ) 0238 { 0239 Q_UNUSED( selectedItem ) 0240 DEBUG_BLOCK 0241 return; 0242 } 0243 0244 void 0245 GpodderService::subscribe() 0246 { 0247 QModelIndex index = m_proxyModel->mapToSource( m_selectionModel->currentIndex() ); 0248 GpodderTreeItem *treeItem = static_cast<GpodderTreeItem*>( index.internalPointer() ); 0249 0250 if( GpodderPodcastTreeItem *podcastTreeItem = qobject_cast<GpodderPodcastTreeItem*>( treeItem ) ) 0251 { 0252 Podcasts::PodcastProvider *podcastProvider = The::playlistManager()->defaultPodcasts(); 0253 QUrl kUrl( podcastTreeItem->podcast()->url() ); 0254 podcastProvider->addPodcast( kUrl ); 0255 } 0256 } 0257 0258 void 0259 GpodderService::enableGpodderProvider( const QString &username ) 0260 { 0261 DEBUG_BLOCK 0262 0263 QString deviceName = QLatin1String( "amarok-" ) % QHostInfo::localHostName(); 0264 0265 debug() << QString( "Enabling GpodderProvider( Username: %1 - Device: %1 )" ) 0266 .arg( username ) 0267 .arg( deviceName ); 0268 0269 m_podcastProvider = new Podcasts::GpodderProvider( username, deviceName, m_apiRequest ); 0270 0271 //Add the gpodder's provider to the playlist manager 0272 The::playlistManager()->addProvider( m_podcastProvider, PlaylistManager::PodcastChannel ); 0273 0274 }