MAFW playlists

Creating playlists
Removing playlists
Handling playlists elements
Insertion
Removal
Moving elements in the playlist
Getting information from playlists
Synchronously without metadata
Asynchronously with metadata
Shuffle and unshuffle

Creating playlists

MAFW playlists are shared, this means they are stored in the mafw-playlist-daemon, so we always need to manipulate them using MafwPlaylistManager object:

gchar* name = "myplaylist";
MafwPlaylistManager *manager;
MafwProxyPlaylist *playlist;
GError *error = NULL;
		
manager = mafw_playlist_manager_get ();
		
playlist = mafw_playlist_manager_create_playlist (manager,
						  name,
						  &error);
		
if (error != NULL)
{
  /* Error management */
}
else if (playlist != NULL)
{
  /* Playlist created correctly */
}
else
{
  /* This should not happen */
}

As one can see, first we get a reference to the manager (which is singleton) and then we create the playlist. We get then a reference to a MafwProxyPlaylist (which is a proxy for the MafwPlaylist that handles DBUS communications transparently for the application developer). Because the playlists are shared, if you try to create a playlist and there is already one with that name, no new one is created and the old one is returned. If you want to be sure that your playlist was not already created, you would need to use either mafw_playlist_manager_get_playlists or mafw_playlist_manager_list_playlists previously to list the available playlists and check yours is not already there.

When manipulating playlists, you should take into account that playlists in MAFW are always shared, meaning that you should not assume that yours is the only application that is using or manipulating a particular playlist.

It is a good idea to install a signal handler in the MafwPlaylistManager to be notified of the creation of new playlists.

{
	g_signal_connect(manager, "playlist-created",
			 G_CALLBACK(on_mafw_playlist_created), NULL);
}

static void
on_mafw_playlist_created (MafwPlaylistManager* manager,
			  MafwProxyPlaylist *playlist, gpointer user_data)
{
  /* Code to handle addition of playlists. */
}

You can manage the playlists that you create just after creating them or wait for this signal. The latter is recommended because it also works for playlists created by applications other than yours.

It is possible to import an existing playlist-file to the framework as a playlist, or import a source-container's content as a playlist with the help of mafw_playlist_manager_import. If you pass an URI as parameter to this function, it will try to parse the file and populate a playlist with the entries of the playlist file. If you pass an object-id as a parameter, and that points to a valid source container, the container will be browsed non-recursively, and the contents will be used to populate the playlist. If the object-id points to a single media file, the file will be parsed and added to the playlist.

After the playlist-importing is done, the framework will call the passed callback, with the new playlist. The name of the playlist will be the URI pointing to the file, or the source container title. If the name existed already, a number will be added after the name. After this, the "playlist-created" signal will be emitted too.

An example of importing a playlist-file to the framework is this:


static void
on_mafw_playlist_import_done (MafwPlaylistManager *self,
					  guint import_id,
					  MafwProxyPlaylist *new_playlist,
					  gpointer user_data,
					  const GError *error)
{
  /* Code to handle playlist import or check for errors */
}

static void import_test_file(void)
{
  gchar* uri = "file:///home/test/test.pls";
  MafwPlaylistManager *manager;
  GError *error = NULL;
		
  manager = mafw_playlist_manager_get ();
		
  if (!mafw_playlist_manager_import (manager,
					  uri,
					  NULL,
					  on_mafw_playlist_import_done,
					  NULL,
					  &error))
  {
  	/* Error management (preconditions violated) */
  }
}