Creating playlists

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 */
}

The method mafw_playlist_manager_create_playlist is responsible for requesting the creation of a new playlist to the playlist daemon. If the operation was successful, this operations returns a reference to a MafwProxyPlaylist that will act as a local representation of the remote playlist created in the playlist daemon. If the requested playlist name has already been used (this is, another playlist with that name already exists), no playlist is created and a reference to the already existing playlist is returned. In order to check if a certain playlist already exists, the developer can use mafw_playlist_manager_get_playlists or mafw_playlist_manager_list_playlists to list the playlist names that have been registered already.

It is important to consider the shared nature of the MAFW playlists. From the developer point of view, this means it should never be assumed that a certain MAFW based application is the only one interacting with a playlist.

The application can install a signal handler in order to be notified of the creation of new playlists. This can be done like this:

{
	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)
{
    /* Handle new playlist */
}

Although new playlists can be used right away after calling mafw_playlist_manager_create_playlist, it is to always handle newly created playlists through the "playlist-created" signal, because this will work also for playlists created by other MAFW based applications.

It is possible to import an existing playlist file or the contents of a particular container of a source extension as a MAFW playlist with the help of mafw_playlist_manager_import. If this method is passed a URI as parameter, it will try to parse the file pointed by that URI and populate a playlist with the entries of the playlist file. Likewise, if a source object identifier is used instead of a URI, and it points to a valid source container, the container will be browsed, and the contents will be used to populate the playlist. If the object identifier points to a single media item instead of a container, the associated media will be treated as as a playlist file, parsing the file it points to adding its entries to the playlist.

After a playlist has been imported, the framework will invoke the callback passed as parameter to mafw_playlist_manager_import, with a reference to the imported playlist proxy object. The name of the playlist will be the URI pointing to the file, or the source container title. If a playlist with this name existed already, then a number will be added after the name as a suffix to ensure a unique name is assigned. After this, the "playlist-created" signal will be emitted.

This is an example of importing a playlist file to the framework:


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) */
  }
}