summaryrefslogtreecommitdiff
path: root/src/audacious/playlist-files.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/audacious/playlist-files.c')
-rw-r--r--src/audacious/playlist-files.c181
1 files changed, 100 insertions, 81 deletions
diff --git a/src/audacious/playlist-files.c b/src/audacious/playlist-files.c
index 795042e..8ebe521 100644
--- a/src/audacious/playlist-files.c
+++ b/src/audacious/playlist-files.c
@@ -1,6 +1,6 @@
/*
* playlist-files.c
- * Copyright 2010-2011 John Lindgren
+ * Copyright 2010-2013 John Lindgren
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@@ -27,80 +27,98 @@
#include "plugin.h"
#include "plugins.h"
-static PluginHandle * get_plugin_silent (const char * filename)
+typedef struct
{
- char buf[32];
- if (! uri_get_extension (filename, buf, sizeof buf))
- return NULL;
-
- return playlist_plugin_for_extension (buf);
+ const char * filename;
+ char * title;
+ Index * filenames;
+ Index * tuples;
+ bool_t plugin_found;
+ bool_t success;
}
+PlaylistData;
-bool_t filename_is_playlist (const char * filename)
+static void plugin_for_filename (const char * filename, PluginForEachFunc func, void * data)
{
- return (get_plugin_silent (filename) != NULL);
+ char ext[32];
+ if (uri_get_extension (filename, ext, sizeof ext))
+ playlist_plugin_for_ext (ext, func, data);
}
-static PluginHandle * get_plugin (const char * filename, bool_t saving)
+static bool_t plugin_found_cb (PluginHandle * plugin, void * data)
{
- PluginHandle * plugin = get_plugin_silent (filename);
-
- if (! plugin)
- {
- SPRINTF (error, _("Cannot %s %s: unsupported file extension."),
- saving ? _("save") : _("load"), filename);
- interface_show_error (error);
- return NULL;
- }
-
- return plugin;
+ * (PluginHandle * *) data = plugin;
+ return FALSE; /* stop when first plugin is found */
}
-bool_t playlist_load (const char * filename, char * * title,
- Index * * filenames_p, Index * * tuples_p)
+bool_t filename_is_playlist (const char * filename)
{
- AUDDBG ("Loading playlist %s.\n", filename);
+ PluginHandle * plugin = NULL;
+ plugin_for_filename (filename, plugin_found_cb, & plugin);
+ return (plugin != NULL);
+}
- PluginHandle * plugin = get_plugin (filename, FALSE);
- if (! plugin)
- return FALSE;
+static bool_t playlist_load_cb (PluginHandle * plugin, void * data_)
+{
+ PlaylistData * data = (PlaylistData *) data_;
PlaylistPlugin * pp = plugin_get_header (plugin);
- g_return_val_if_fail (pp && PLUGIN_HAS_FUNC (pp, load), FALSE);
+ if (! pp || ! PLUGIN_HAS_FUNC (pp, load))
+ return TRUE; /* try another plugin */
+
+ data->plugin_found = TRUE;
- VFSFile * file = vfs_fopen (filename, "r");
+ VFSFile * file = vfs_fopen (data->filename, "r");
if (! file)
- return FALSE;
+ return FALSE; /* stop if we can't open file */
- Index * filenames = index_new ();
- Index * tuples = index_new ();
- bool_t success = pp->load (filename, file, title, filenames, tuples);
+ data->success = pp->load (data->filename, file, & data->title, data->filenames, data->tuples);
vfs_fclose (file);
+ return ! data->success; /* stop when playlist is loaded */
+}
+
+bool_t playlist_load (const char * filename, char * * title, Index * * filenames, Index * * tuples)
+{
+ PlaylistData data =
+ {
+ .filename = filename,
+ .filenames = index_new (),
+ .tuples = index_new ()
+ };
+
+ AUDDBG ("Loading playlist %s.\n", filename);
+ plugin_for_filename (filename, playlist_load_cb, & data);
+
+ if (! data.plugin_found)
+ {
+ SPRINTF (error, _("Cannot load %s: unsupported file extension."), filename);
+ interface_show_error (error);
+ }
- if (! success)
+ if (! data.success)
{
- index_free (filenames);
- index_free (tuples);
+ str_unref (data.title);
+ index_free_full (data.filenames, (IndexFreeFunc) str_unref);
+ index_free_full (data.tuples, (IndexFreeFunc) tuple_unref);
return FALSE;
}
- if (index_count (tuples))
- g_return_val_if_fail (index_count (tuples) == index_count (filenames),
- FALSE);
+ if (index_count (data.tuples))
+ g_return_val_if_fail (index_count (data.tuples) == index_count (data.filenames), FALSE);
else
{
- index_free (tuples);
- tuples = NULL;
+ index_free (data.tuples);
+ data.tuples = NULL;
}
- * filenames_p = filenames;
- * tuples_p = tuples;
+ * title = data.title;
+ * filenames = data.filenames;
+ * tuples = data.tuples;
return TRUE;
}
-bool_t playlist_insert_playlist_raw (int list, int at,
- const char * filename)
+bool_t playlist_insert_playlist_raw (int list, int at, const char * filename)
{
char * title = NULL;
Index * filenames, * tuples;
@@ -117,59 +135,60 @@ bool_t playlist_insert_playlist_raw (int list, int at,
return TRUE;
}
-bool_t playlist_save (int list, const char * filename)
+static bool_t playlist_save_cb (PluginHandle * plugin, void * data_)
{
- AUDDBG ("Saving playlist %s.\n", filename);
-
- PluginHandle * plugin = get_plugin (filename, TRUE);
- if (! plugin)
- return FALSE;
+ PlaylistData * data = data_;
PlaylistPlugin * pp = plugin_get_header (plugin);
- g_return_val_if_fail (pp, FALSE);
+ if (! pp || ! PLUGIN_HAS_FUNC (pp, save))
+ return TRUE; /* try another plugin */
- if (! PLUGIN_HAS_FUNC (pp, save))
- {
- SPRINTF (error, _("Cannot save %s: plugin does not support saving."), filename);
- interface_show_error (error);
- return FALSE;
- }
+ data->plugin_found = TRUE;
- bool_t fast = get_bool (NULL, "metadata_on_play");
-
- VFSFile * file = vfs_fopen (filename, "w");
+ VFSFile * file = vfs_fopen (data->filename, "w");
if (! file)
- return FALSE;
+ return FALSE; /* stop if we can't open file */
- char * title = playlist_get_title (list);
+ data->success = pp->save (data->filename, file, data->title, data->filenames, data->tuples);
+
+ vfs_fclose (file);
+ return FALSE; /* stop after first attempt (successful or not) */
+}
+
+bool_t playlist_save (int list, const char * filename)
+{
+ PlaylistData data =
+ {
+ .filename = filename,
+ .title = playlist_get_title (list),
+ .filenames = index_new (),
+ .tuples = index_new ()
+ };
int entries = playlist_entry_count (list);
- Index * filenames = index_new ();
- index_allocate (filenames, entries);
- Index * tuples = index_new ();
- index_allocate (tuples, entries);
+ bool_t fast = get_bool (NULL, "metadata_on_play");
+
+ index_allocate (data.filenames, entries);
+ index_allocate (data.tuples, entries);
for (int i = 0; i < entries; i ++)
{
- index_append (filenames, playlist_entry_get_filename (list, i));
- index_append (tuples, playlist_entry_get_tuple (list, i, fast));
+ index_insert (data.filenames, -1, playlist_entry_get_filename (list, i));
+ index_insert (data.tuples, -1, playlist_entry_get_tuple (list, i, fast));
}
- bool_t success = pp->save (filename, file, title, filenames, tuples);
-
- vfs_fclose (file);
- str_unref (title);
+ AUDDBG ("Saving playlist %s.\n", filename);
+ plugin_for_filename (filename, playlist_save_cb, & data);
- for (int i = 0; i < entries; i ++)
+ if (! data.plugin_found)
{
- str_unref (index_get (filenames, i));
- Tuple * tuple = index_get (tuples, i);
- if (tuple)
- tuple_unref (tuple);
+ SPRINTF (error, _("Cannot save %s: unsupported file extension."), filename);
+ interface_show_error (error);
}
- index_free (filenames);
- index_free (tuples);
+ str_unref (data.title);
+ index_free_full (data.filenames, (IndexFreeFunc) str_unref);
+ index_free_full (data.tuples, (IndexFreeFunc) tuple_unref);
- return success;
+ return data.success;
}