Commit c5a50a6a authored by Bart Cerneels's avatar Bart Cerneels
Browse files

Enabled editing of M3U playlists

As a bonus implemented a more efficient save system for playlist-files.

BUG:245963
parent 7270986f
......@@ -19,10 +19,11 @@
#define _PREFIX "M3UPlaylist"
#include "core/support/Amarok.h"
#include "core-impl/collections/support/CollectionManager.h"
#include "core/support/Debug.h"
#include "PlaylistManager.h"
#include "core-impl/collections/support/CollectionManager.h"
#include "core-impl/playlists/types/file/PlaylistFileSupport.h"
#include "playlistmanager/file/PlaylistFileProvider.h"
#include "PlaylistManager.h"
#include <KMimeType>
#include <KUrl>
......@@ -113,6 +114,50 @@ M3UPlaylist::triggerTrackLoad()
}
}
void
M3UPlaylist::addTrack( Meta::TrackPtr track, int position )
{
if( !m_tracksLoaded )
triggerTrackLoad();
int trackPos = position < 0 ? m_tracks.count() : position;
if( trackPos > m_tracks.count() )
trackPos = m_tracks.count();
m_tracks.insert( trackPos, track );
//set in case no track was in the playlist before
m_tracksLoaded = true;
notifyObserversTrackAdded( track, trackPos );
if( !m_url.isEmpty() )
saveLater();
}
void
M3UPlaylist::removeTrack( int position )
{
if( position < 0 || position >= m_tracks.count() )
return;
m_tracks.removeAt( position );
notifyObserversTrackRemoved( position );
if( !m_url.isEmpty() )
saveLater();
}
void
M3UPlaylist::saveLater()
{
if( !m_provider )
return;
PlaylistFileProvider *playlistFileProvider =
qobject_cast<PlaylistFileProvider *>( m_provider );
if( !playlistFileProvider )
return;
playlistFileProvider->saveLater( PlaylistFilePtr( this ) );
}
bool
M3UPlaylist::loadM3u( QTextStream &stream )
{
......
......@@ -52,6 +52,9 @@ class AMAROK_EXPORT_TESTS M3UPlaylist : public PlaylistFile
virtual Meta::TrackList tracks();
virtual void triggerTrackLoad();
virtual void addTrack( Meta::TrackPtr track, int position = -1 );
virtual void removeTrack( int position );
/* the following has been copied from Meta.h
* it is my hope that we can integrate Playlists
* better into the rest of the Meta framework someday ~Bart Cerneels
......@@ -69,6 +72,7 @@ class AMAROK_EXPORT_TESTS M3UPlaylist : public PlaylistFile
bool load( QTextStream &stream ) { return loadM3u( stream ); }
private:
void saveLater();
bool loadM3u( QTextStream &stream );
KUrl m_url;
......
......@@ -35,6 +35,7 @@
#include <QAction>
#include <QLabel>
#include <QString>
#include <QTimer>
//For removing multiple tracks from different playlists with one QAction
typedef QMultiMap<Playlists::PlaylistPtr, Meta::TrackPtr> PlaylistTrackMap;
......@@ -51,6 +52,7 @@ PlaylistFileProvider::PlaylistFileProvider()
, m_renameAction( 0 )
, m_deleteAction( 0 )
, m_removeTrackAction( 0 )
, m_saveLaterTimer( 0 )
{
//playlists are lazy loaded
}
......@@ -401,6 +403,39 @@ PlaylistFileProvider::loadPlaylists()
m_playlistsLoaded = true;
}
void
PlaylistFileProvider::saveLater( Playlists::PlaylistFilePtr playlist )
{
//WARNING: this assumes the playlistfile uses it's m_url for uidUrl
if( playlist->uidUrl().isEmpty() )
return;
if( !m_saveLaterPlaylists.contains( playlist ) )
m_saveLaterPlaylists << playlist;
if( !m_saveLaterTimer )
{
m_saveLaterTimer = new QTimer( this );
m_saveLaterTimer->setSingleShot( true );
m_saveLaterTimer->setInterval( 0 );
connect( m_saveLaterTimer, SIGNAL(timeout()), SLOT(slotSaveLater()) );
}
m_saveLaterTimer->start();
}
void
PlaylistFileProvider::slotSaveLater() //SLOT
{
foreach( Playlists::PlaylistFilePtr playlist, m_saveLaterPlaylists )
{
KUrl url = playlist->uidUrl();
playlist->save( url, true ); //TODO: read relative type when loading
}
m_saveLaterPlaylists.clear();
}
void
PlaylistFileProvider::slotDelete()
{
......
......@@ -17,10 +17,10 @@
#ifndef PLAYLISTFILEPROVIDER_H
#define PLAYLISTFILEPROVIDER_H
#include "core-impl/playlists/providers/user/UserPlaylistProvider.h"
#include "core/playlists/PlaylistFormat.h"
#include "core-impl/playlists/types/file/PlaylistFileSupport.h"
#include "core/playlists/PlaylistProvider.h"
#include "core-impl/playlists/providers/user/UserPlaylistProvider.h"
#include "core-impl/playlists/types/file/PlaylistFileSupport.h"
#include <kicon.h>
......@@ -28,9 +28,9 @@ class KConfigGroup;
class KUrl;
class QAction;
class QTimer;
namespace Playlists {
/**
@author Bart Cerneels <bart.cerneels@kde.org>
*/
......@@ -67,7 +67,15 @@ class PlaylistFileProvider : public Playlists::UserPlaylistProvider
virtual void loadPlaylists();
/* PlaylistFileProvider methods */
/** Schedules a PlaylistFile to be saved on the next iteration of the mainloop.
* Each playlist will be scheduled and saved only once.
*/
void saveLater( Playlists::PlaylistFilePtr playlist );
private slots:
void slotSaveLater();
void slotDelete();
void slotRename();
void slotRemove();
......@@ -84,6 +92,9 @@ class PlaylistFileProvider : public Playlists::UserPlaylistProvider
QAction *m_renameAction;
QAction *m_deleteAction;
QAction *m_removeTrackAction;
QTimer *m_saveLaterTimer;
QList<Playlists::PlaylistFilePtr> m_saveLaterPlaylists;
};
} //namespace Playlists
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment