File indexing completed on 2025-01-05 04:26:02

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  * Copyright (c) 2008 Bart Cerneels <bart.cerneels@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 #include "MediaDeviceUserPlaylistProvider.h"
0019 
0020 #include "SvgHandler.h"
0021 #include "browsers/playlistbrowser/UserPlaylistModel.h"
0022 #include "core/support/Amarok.h"
0023 #include "core/support/Debug.h"
0024 #include "core-impl/collections/mediadevicecollection/MediaDeviceCollection.h"
0025 #include "core-impl/collections/support/CollectionManager.h"
0026 #include "core-impl/playlists/types/file/PlaylistFileSupport.h"
0027 #include "core-impl/playlists/types/file/m3u/M3UPlaylist.h"
0028 #include "core-impl/playlists/types/file/pls/PLSPlaylist.h"
0029 #include "core-impl/playlists/types/file/xspf/XSPFPlaylist.h"
0030 #include "playlistmanager/PlaylistManager.h"
0031 
0032 #include <QIcon>
0033 #include <QInputDialog>
0034 #include <QUrl>
0035 
0036 #include <QMap>
0037 
0038 // static const int USERPLAYLIST_DB_VERSION = 2;
0039 static const QString key(QStringLiteral("AMAROK_USERPLAYLIST"));
0040 
0041 namespace Playlists {
0042 
0043 MediaDeviceUserPlaylistProvider::MediaDeviceUserPlaylistProvider( Collections::MediaDeviceCollection *collection )
0044     : Playlists::UserPlaylistProvider()
0045     , m_collection( collection )
0046 {
0047     DEBUG_BLOCK
0048 //    checkTables();
0049 //    m_root = Playlists::MediaDevicePlaylistGroupPtr( new Playlists::MediaDevicePlaylistGroup( "",
0050 //            Playlists::MediaDevicePlaylistGroupPtr() ) );
0051 //    The::playlistManager()->addProvider( this, category() );
0052 }
0053 
0054 MediaDeviceUserPlaylistProvider::~MediaDeviceUserPlaylistProvider()
0055 {
0056     DEBUG_BLOCK
0057 //     foreach( Playlists::MediaDevicePlaylistPtr playlist, m_playlists )
0058 //     {
0059 //         playlist->saveToDb( true );
0060 //     }
0061     m_playlists.clear();
0062 //    Q_EMIT updated();
0063 //    The::playlistManager()->removeProvider( this );
0064 
0065 }
0066 
0067 Playlists::PlaylistList
0068 MediaDeviceUserPlaylistProvider::playlists()
0069 {
0070     DEBUG_BLOCK
0071     Playlists::PlaylistList playlists;
0072 
0073     foreach( Playlists::MediaDevicePlaylistPtr mediadevicePlaylist, m_playlists )
0074     {
0075         playlists << Playlists::PlaylistPtr::staticCast( mediadevicePlaylist );
0076     }
0077 
0078     return playlists;
0079 }
0080 
0081 Playlists::PlaylistPtr
0082 MediaDeviceUserPlaylistProvider::save( const Meta::TrackList &tracks )
0083 {
0084     DEBUG_BLOCK
0085     // This provider can only save it's own tracks for now, filter out all the others.
0086     Meta::TrackList filteredTracks;
0087     foreach( const Meta::TrackPtr track, tracks )
0088         if( track->collection() == m_collection )
0089             filteredTracks << track;
0090 
0091     return save( filteredTracks,
0092                  QDateTime::currentDateTime().toString( QStringLiteral("ddd MMMM d yy hh-mm") ) );
0093 }
0094 
0095 Playlists::PlaylistPtr
0096 MediaDeviceUserPlaylistProvider::save( const Meta::TrackList &tracks, const QString& name )
0097 {
0098     DEBUG_BLOCK
0099     debug() << "saving " << tracks.count() << " tracks to device with name" << name;
0100     // NOTE: the playlist constructor tells the handler to make the playlist, save to db etc.
0101     Playlists::MediaDevicePlaylistPtr pl = Playlists::MediaDevicePlaylistPtr( new Playlists::MediaDevicePlaylist( name, tracks ) );
0102     //pl = 0;
0103 
0104     Q_EMIT playlistSaved( pl, name ); // inform handler of new playlist
0105 
0106     addMediaDevicePlaylist( pl );
0107 
0108     return Playlists::PlaylistPtr::dynamicCast( pl );
0109 }
0110 
0111 void
0112 MediaDeviceUserPlaylistProvider::renamePlaylist(PlaylistPtr playlist, const QString &newName )
0113 {
0114     DEBUG_BLOCK
0115     Playlists::MediaDevicePlaylistPtr pl = Playlists::MediaDevicePlaylistPtr::staticCast( playlist );
0116     if( pl )
0117     {
0118         debug() << "Setting name of playlist";
0119         pl->setName( newName );
0120 
0121         Q_EMIT playlistRenamed( pl );
0122     }
0123 }
0124 
0125 bool
0126 MediaDeviceUserPlaylistProvider::deletePlaylists( const Playlists::PlaylistList &playlistlist )
0127 {
0128     Playlists::MediaDevicePlaylistList pllist;
0129     foreach( Playlists::PlaylistPtr playlist, playlistlist )
0130     {
0131         Playlists::MediaDevicePlaylistPtr pl =
0132                 Playlists::MediaDevicePlaylistPtr::staticCast( playlist );
0133 
0134         if( pl )
0135         {
0136             debug() << "Deleting playlist: " << pl->name();
0137             removePlaylist( pl );
0138             pllist << pl;
0139         }
0140     }
0141 
0142     Q_EMIT playlistsDeleted( pllist );
0143 
0144     return true;
0145 }
0146 
0147 void
0148 MediaDeviceUserPlaylistProvider::addMediaDevicePlaylist( Playlists::MediaDevicePlaylistPtr &playlist )
0149 {
0150     m_playlists << playlist;
0151     Q_EMIT updated();
0152 }
0153 
0154 void
0155 MediaDeviceUserPlaylistProvider::removePlaylist( Playlists::MediaDevicePlaylistPtr &playlist )
0156 {
0157     m_playlists.removeOne( playlist );
0158     Q_EMIT updated();
0159 }
0160 
0161 } //namespace Playlists
0162