File indexing completed on 2025-02-23 04:28:33

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Anmol Ahuja <darthcodus@gmail.com>                                *
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 "PlaylistProviderExporter.h"
0018 
0019 #include "playlistmanager/PlaylistManager.h"
0020 #include "core/playlists/PlaylistProvider.h"
0021 #include "core-impl/playlists/providers/user/UserPlaylistProvider.h"
0022 #include "scripting/scriptengine/ScriptingDefines.h"
0023 
0024 #include <QIcon>
0025 
0026 #include <QJSEngine>
0027 #include <QJSValue>
0028 
0029 using namespace AmarokScript;
0030 
0031 PlaylistProviderPrototype::PlaylistProviderPrototype( Playlists::PlaylistProvider *provider )
0032 : QObject( nullptr )
0033 , m_provider( provider )
0034 {}
0035 
0036 void
0037 PlaylistProviderPrototype::init( QJSEngine *engine )
0038 {
0039     qRegisterMetaType<Playlists::PlaylistProvider*>();
0040     QMetaType::registerConverter<Playlists::PlaylistProvider*,QJSValue>( [=] (Playlists::PlaylistProvider* providerPtr) {
0041         return toScriptValue<Playlists::PlaylistProvider*,PlaylistProviderPrototype>( engine, providerPtr);
0042 
0043     } );
0044     QMetaType::registerConverter<QJSValue,Playlists::PlaylistProvider*>( [] (QJSValue jsValue) {
0045         Playlists::PlaylistProvider* providerPtr;
0046         fromScriptValue<Playlists::PlaylistProvider*,PlaylistProviderPrototype>( jsValue, providerPtr );
0047         return providerPtr;
0048     } );
0049 
0050     qRegisterMetaType<PlaylistProviderList>();
0051     QMetaType::registerConverter<PlaylistProviderList,QJSValue>( [=] (PlaylistProviderList providerList) { return toScriptArray<PlaylistProviderList>( engine, providerList); } );
0052     QMetaType::registerConverter<QJSValue,PlaylistProviderList>( [] (QJSValue jsValue) {
0053         PlaylistProviderList providerList;
0054         fromScriptArray<PlaylistProviderList>( jsValue, providerList );
0055         return providerList;
0056     } );
0057 }
0058 
0059 // script invokable
0060 
0061 Playlists::PlaylistPtr
0062 PlaylistProviderPrototype::addPlaylist(Playlists::PlaylistPtr playlist)
0063 {
0064     if( m_provider )
0065         return m_provider->addPlaylist( playlist );
0066     return Playlists::PlaylistPtr();
0067 }
0068 
0069 int
0070 PlaylistProviderPrototype::category() const
0071 {
0072     if( m_provider )
0073         return m_provider->category();
0074     return -1;
0075 }
0076 
0077 bool
0078 PlaylistProviderPrototype::deletePlaylists( const Playlists::PlaylistList &playlistlist )
0079 {
0080     return m_provider && m_provider->deletePlaylists( playlistlist );
0081 }
0082 
0083 Playlists::PlaylistList
0084 PlaylistProviderPrototype::playlists()
0085 {
0086     if( m_provider )
0087         return m_provider->playlists();
0088     return Playlists::PlaylistList();
0089 }
0090 
0091 void
0092 PlaylistProviderPrototype::renamePlaylist( Playlists::PlaylistPtr playlist, const QString &newName )
0093 {
0094     if( m_provider )
0095         m_provider->renamePlaylist( playlist, newName );
0096 }
0097 
0098 Playlists::PlaylistPtr
0099 PlaylistProviderPrototype::save( const Meta::TrackList &tracks, const QString &name )
0100 {
0101     Playlists::UserPlaylistProvider* playlist = dynamic_cast<Playlists::UserPlaylistProvider*>( m_provider.data() );
0102     if( playlist )
0103         return playlist->save( tracks, name );
0104     return Playlists::PlaylistPtr();
0105 }
0106 
0107 QString PlaylistProviderPrototype::toString() const
0108 {
0109     if( m_provider )
0110         return m_provider->prettyName();
0111     return QStringLiteral( "Invalid" );
0112 }
0113 
0114 // private
0115 
0116 bool
0117 PlaylistProviderPrototype::isValid() const
0118 {
0119     return m_provider;
0120 }
0121 
0122 QIcon
0123 PlaylistProviderPrototype::icon() const
0124 {
0125     if( m_provider )
0126         return m_provider->icon();
0127     return QIcon();
0128 }
0129 
0130 bool
0131 PlaylistProviderPrototype::isWritable() const
0132 {
0133     return m_provider && m_provider->isWritable();
0134 }
0135 
0136 int
0137 PlaylistProviderPrototype::playlistCount() const
0138 {
0139     if( m_provider )
0140         return m_provider->playlistCount();
0141     return -1;
0142 }