Ejemplo n.º 1
0
/**
 * rb_playlist_source_save_playlist:
 * @source: a #RBPlaylistSource
 * @uri: destination URI
 * @export_type: format to save in
 *
 * Saves the playlist to an external file in a standard
 * format (M3U, PLS, or XSPF).
 */
void
rb_playlist_source_save_playlist (RBPlaylistSource *source,
				  const char *uri,
				  RBPlaylistExportType export_type)
{
	TotemPlParser *pl;
	GError *error = NULL;
	char *name;
	gint totem_format;
#if TOTEM_PL_PARSER_CHECK_VERSION(2,29,1)
	TotemPlPlaylist *playlist;
	GFile *file;
#endif

	g_return_if_fail (RB_IS_PLAYLIST_SOURCE (source));

	rb_debug ("saving playlist");
	pl = totem_pl_parser_new ();

	g_object_get (source, "name", &name, NULL);

	switch (export_type) {
	case RB_PLAYLIST_EXPORT_TYPE_XSPF:
		totem_format = TOTEM_PL_PARSER_XSPF;
		break;
	case RB_PLAYLIST_EXPORT_TYPE_M3U:
		totem_format = TOTEM_PL_PARSER_M3U;
		break;
	case RB_PLAYLIST_EXPORT_TYPE_PLS:
	default:
		totem_format = TOTEM_PL_PARSER_PLS;
		break;
	}

#if TOTEM_PL_PARSER_CHECK_VERSION(2,29,1)
	file = g_file_new_for_uri (uri);
	playlist = totem_pl_playlist_new ();

	gtk_tree_model_foreach (GTK_TREE_MODEL (source->priv->model),
				(GtkTreeModelForeachFunc)playlist_iter_foreach,
				playlist);
	totem_pl_parser_save (pl, playlist, file, name, totem_format, &error);
	g_object_unref (playlist);
	g_object_unref (file);
#else
	totem_pl_parser_write_with_title (pl, GTK_TREE_MODEL (source->priv->model),
					  playlist_iter_func, uri, name,
					  totem_format,
					  NULL, &error);
#endif
	g_object_unref (pl);
	g_free (name);
	if (error != NULL) {
		rb_error_dialog (NULL, _("Couldn't save playlist"),
				 "%s", error->message);
		g_error_free (error);
	}
}
static gboolean
save_playlist (RBGenericPlayerPlaylistSource *source)
{
	TotemPlParser *parser;
	TotemPlParserType playlist_type;
	RhythmDBQueryModel *query_model;
	char *name;
	char *temp_path;
	GError *error = NULL;
	RBGenericPlayerPlaylistSourcePrivate *priv = GET_PRIVATE (source);
	GFile *file;
	gboolean result;
	SavePlaylistData data;

	priv->save_playlist_id = 0;
	playlist_type = rb_generic_player_source_get_playlist_format (priv->player_source);

	g_object_get (source,
		      "name", &name,
		      "base-query-model", &query_model,
		      NULL);

	/* if we don't already have a name for this playlist, make one now */
	if (priv->playlist_path == NULL) {
		char *playlist_dir;
		char *mount_uri;
		char *filename;
		const char *ext;
		GFile *dir;
		GFile *playlist;

		ext = playlist_format_extension (playlist_type);

		if (name == NULL || name[0] == '\0') {
			/* now what? */
			filename = g_strdup_printf ("unnamed%s", ext);
		} else {
			filename = g_strdup_printf ("%s%s", name, ext);
		}

		playlist_dir = rb_generic_player_source_get_playlist_path (priv->player_source);
		mount_uri = rb_generic_player_source_get_mount_path (priv->player_source);

		dir = g_file_new_for_uri (mount_uri);
		if (playlist_dir != NULL) {
			GFile *pdir;

			pdir = g_file_resolve_relative_path (dir, playlist_dir);
			g_object_unref (dir);
			dir = pdir;
		}

		playlist = g_file_resolve_relative_path (dir, filename);
		priv->playlist_path = g_file_get_path (playlist);
		
		g_free (mount_uri);
		g_free (playlist_dir);

		g_object_unref (dir);
	}

	temp_path = g_strdup_printf ("%s%06X", priv->playlist_path, g_random_int_range (0, 0xFFFFFF));
	file = g_file_new_for_path (temp_path);

	parser = totem_pl_parser_new ();
	data.source = source;
	data.playlist_type = playlist_type;
#if TOTEM_PL_PARSER_CHECK_VERSION(2,29,1)
	data.playlist = totem_pl_playlist_new ();

	gtk_tree_model_foreach (GTK_TREE_MODEL (query_model),
				(GtkTreeModelForeachFunc) save_playlist_foreach,
				&data);
	if (rb_debug_matches ("totem_pl_parser_save", "totem-pl-parser.c")) {
		g_object_set (parser, "debug", TRUE, NULL);
	}

	result = totem_pl_parser_save (parser, data.playlist, file, name, playlist_type, &error);
	g_object_unref (data.playlist);
	data.playlist = NULL;
#else
	if (rb_debug_matches ("totem_pl_parser_write_with_title", "totem-pl-parser.c")) {
		g_object_set (parser, "debug", TRUE, NULL);
	}

	result = totem_pl_parser_write_with_title (parser,
						   GTK_TREE_MODEL (query_model),
						   (TotemPlParserIterFunc) save_playlist_entry,
						   temp_path,
						   name,
						   playlist_type,
						   &data,
						   &error);
#endif
	if (result == FALSE) {
		/* XXX report this more usefully */
		g_warning ("Playlist save failed: %s", error ? error->message : "<no error>");
	} else {
		GFile *dest;

		dest = g_file_new_for_path (priv->playlist_path);
		g_file_move (file, dest, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NO_FALLBACK_FOR_MOVE, NULL, NULL, NULL, &error);
		if (error != NULL) {
			/* XXX report this more usefully */
			g_warning ("moving %s => %s failed: %s", temp_path, priv->playlist_path, error->message);
		}

		g_object_unref (dest);
	}

	g_clear_error (&error);
	g_free (name);
	g_free (temp_path);
	g_object_unref (query_model);
	g_object_unref (parser);
	g_object_unref (file);

	return FALSE;
}
Ejemplo n.º 3
0
gboolean
brasero_project_save_audio_project_playlist (BraseroBurnSession *session,
					     const gchar *uri,
					     BraseroProjectSave type)
{
	TotemPlParserType pl_type;
	TotemPlParser *parser;
	TotemPlPlaylist *playlist;
	TotemPlPlaylistIter pl_iter;
	gboolean result;
	GFile *file;
	GSList *iter;

	file = g_file_new_for_uri (uri);
	parser = totem_pl_parser_new ();
	playlist = totem_pl_playlist_new ();

	/* populate playlist */
	iter = brasero_burn_session_get_tracks (session);
	for (; iter; iter = iter->next) {
		BraseroTrackStream *track;
		const gchar *title;
		gchar *uri;

		track = iter->data;

		uri = brasero_track_stream_get_source (track, TRUE);
		title = brasero_track_tag_lookup_string (BRASERO_TRACK (track), BRASERO_TRACK_STREAM_TITLE_TAG);

		totem_pl_playlist_append (playlist, &pl_iter);
		totem_pl_playlist_set (playlist, &pl_iter,
				       TOTEM_PL_PARSER_FIELD_URI, uri,
				       TOTEM_PL_PARSER_FIELD_TITLE, title,
				       NULL);
		g_free (uri);
	}

	switch (type) {
		case BRASERO_PROJECT_SAVE_PLAYLIST_M3U:
			pl_type = TOTEM_PL_PARSER_M3U;
			break;
		case BRASERO_PROJECT_SAVE_PLAYLIST_XSPF:
			pl_type = TOTEM_PL_PARSER_XSPF;
			break;
		case BRASERO_PROJECT_SAVE_PLAYLIST_IRIVER_PLA:
			pl_type = TOTEM_PL_PARSER_IRIVER_PLA;
			break;

		case BRASERO_PROJECT_SAVE_PLAYLIST_PLS:
		default:
			pl_type = TOTEM_PL_PARSER_PLS;
			break;
	}

	result = totem_pl_parser_save (parser, playlist, file,
				       brasero_burn_session_get_label (session),
				       pl_type, NULL);

	g_object_unref (playlist);
	g_object_unref (parser);
	g_object_unref (file);

	return result;
}