Handling playlists elements

Insertion

To add elements to the playlist you only need to insert them in the position you want. Notice that media items are represented by their object identifiers, as provided by the sources serving them.

{
        gchar *oid = get_oid();

	mafw_playlist_insert_item(playlist, index, oid, &error);
	g_free(oid);
	
	if (error != NULL)
	{
          /* Error handling */
	}
	else
	{
          /* Elements insertion */
	}
}

As in other cases it is recommended to connect the signal before inserting the elements and react to the signals accordingly because other applications can also insert elements in any playlist:

{
	g_signal_connect (playlist, "contents-changed",
			  (GCallback) on_mafw_playlist_contents_changed, NULL);
}

static void
on_mafw_playlist_contents_changed(MafwPlaylist *playlist, guint from,
				  guint nremoved, guint nreplaced)
{
  /* Update UI accordingly */
}

This MafwPlaylist::contents-changed signal is used to signal changes of any kind. The from parameter is the first element affected by the change, nremoved is the number of removed elements and nreplaced means the number of elements replaced. This method will be called in case of insertion with the insertion index as from, 0 as nremoved and 1 as nreplaced. More info is available in Mafw API reference.

Removal

As in the case of insertion, one only needs to call the method on the playlist with the index of the element to remove:

{
	mafw_playlist_remove_item (playlist, (guint) index, &error);

	if (error != NULL) {
         /* Error handling */
	}
}

The signal received when modifying the playlist is the same as when removing elements, but difference is that nremoved would be one and nreplaced would be the difference between the number of elements and the removed element index plus the number of removed items. you can find some examples in Mafw API reference.

Moving elements in the playlist

To move one element from one index to another it is only needed to call the method on the playlist with the index we are moving from and the index we are moving to:

{
	mafw_playlist_move_item (playlist, 3, 1, &error);

	if (error != NULL) {
          /* Error handling */
	}
}

The given example would move the element from position 3 to position 1 so previous elements in positions 1 and 2 would be moved forward to positions 2 and 3. The signal emitted in this case is item-moved:

{
	g_signal_connect (playlist, "item-moved",
			  (GCallback) on_mafw_playlist_item_moved, NULL);
}

static void
on_mafw_playlist_item_moved(MafwPlaylist *playlist, guint from, guint to)
{
	/* Move the playlist items in the view accordingly */
}