Audacious $Id:Doxyfile42802007-03-2104:39:00Znenolod$
playlist-files.c
Go to the documentation of this file.
00001 /*
00002  * playlist-files.c
00003  * Copyright 2010-2011 John Lindgren
00004  *
00005  * This file is part of Audacious.
00006  *
00007  * Audacious is free software: you can redistribute it and/or modify it under
00008  * the terms of the GNU General Public License as published by the Free Software
00009  * Foundation, version 2 or version 3 of the License.
00010  *
00011  * Audacious is distributed in the hope that it will be useful, but WITHOUT ANY
00012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00013  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along with
00016  * Audacious. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * The Audacious team does not consider modular code linking to Audacious or
00019  * using our public API to be a derived work.
00020  */
00021 
00022 #include <glib.h>
00023 #include <libaudcore/audstrings.h>
00024 
00025 #include "config.h"
00026 #include "debug.h"
00027 #include "i18n.h"
00028 #include "misc.h"
00029 #include "playlist.h"
00030 #include "plugin.h"
00031 #include "plugins.h"
00032 
00033 static const char * get_extension (const char * filename)
00034 {
00035     const char * ext;
00036     uri_parse (filename, NULL, & ext, NULL, NULL);
00037     return (ext && ext[0] == '.') ? ext + 1 : NULL;
00038 }
00039 
00040 bool_t filename_is_playlist (const char * filename)
00041 {
00042     const char * ext = get_extension (filename);
00043     return (ext && playlist_plugin_for_extension (ext)) ? TRUE : FALSE;
00044 }
00045 
00046 static PluginHandle * get_plugin (const char * filename, bool_t saving)
00047 {
00048     const char * ext = get_extension (filename);
00049     PluginHandle * plugin = ext ? playlist_plugin_for_extension (ext) : NULL;
00050 
00051     if (! plugin)
00052     {
00053         char * error = str_printf (_("Cannot %s %s: unsupported file "
00054          "extension."), saving ? _("save") : _("load"), filename);
00055         interface_show_error (error);
00056         str_unref (error);
00057         return NULL;
00058     }
00059 
00060     return plugin;
00061 }
00062 
00063 bool_t playlist_load (const char * filename, char * * title,
00064  Index * * filenames_p, Index * * tuples_p)
00065 {
00066     AUDDBG ("Loading playlist %s.\n", filename);
00067 
00068     PluginHandle * plugin = get_plugin (filename, FALSE);
00069     if (! plugin)
00070         return FALSE;
00071 
00072     PlaylistPlugin * pp = plugin_get_header (plugin);
00073     g_return_val_if_fail (pp && PLUGIN_HAS_FUNC (pp, load), FALSE);
00074 
00075     VFSFile * file = vfs_fopen (filename, "r");
00076     if (! file)
00077         return FALSE;
00078 
00079     Index * filenames = index_new ();
00080     Index * tuples = index_new ();
00081     bool_t success = pp->load (filename, file, title, filenames, tuples);
00082 
00083     vfs_fclose (file);
00084 
00085     if (! success)
00086     {
00087         index_free (filenames);
00088         index_free (tuples);
00089         return FALSE;
00090     }
00091 
00092     if (index_count (tuples))
00093         g_return_val_if_fail (index_count (tuples) == index_count (filenames),
00094          FALSE);
00095     else
00096     {
00097         index_free (tuples);
00098         tuples = NULL;
00099     }
00100 
00101     * filenames_p = filenames;
00102     * tuples_p = tuples;
00103     return TRUE;
00104 }
00105 
00106 bool_t playlist_insert_playlist_raw (int list, int at,
00107  const char * filename)
00108 {
00109     char * title = NULL;
00110     Index * filenames, * tuples;
00111 
00112     if (! playlist_load (filename, & title, & filenames, & tuples))
00113         return FALSE;
00114 
00115     if (title && ! playlist_entry_count (list))
00116         playlist_set_title (list, title);
00117 
00118     playlist_entry_insert_batch_raw (list, at, filenames, tuples, NULL);
00119 
00120     str_unref (title);
00121     return TRUE;
00122 }
00123 
00124 bool_t playlist_save (int list, const char * filename)
00125 {
00126     AUDDBG ("Saving playlist %s.\n", filename);
00127 
00128     PluginHandle * plugin = get_plugin (filename, TRUE);
00129     if (! plugin)
00130         return FALSE;
00131 
00132     PlaylistPlugin * pp = plugin_get_header (plugin);
00133     g_return_val_if_fail (pp && PLUGIN_HAS_FUNC (pp, load), FALSE);
00134 
00135     bool_t fast = get_bool (NULL, "metadata_on_play");
00136 
00137     VFSFile * file = vfs_fopen (filename, "w");
00138     if (! file)
00139         return FALSE;
00140 
00141     char * title = playlist_get_title (list);
00142 
00143     int entries = playlist_entry_count (list);
00144     Index * filenames = index_new ();
00145     index_allocate (filenames, entries);
00146     Index * tuples = index_new ();
00147     index_allocate (tuples, entries);
00148 
00149     for (int i = 0; i < entries; i ++)
00150     {
00151         index_append (filenames, playlist_entry_get_filename (list, i));
00152         index_append (tuples, playlist_entry_get_tuple (list, i, fast));
00153     }
00154 
00155     bool_t success = pp->save (filename, file, title, filenames, tuples);
00156 
00157     vfs_fclose (file);
00158     str_unref (title);
00159 
00160     for (int i = 0; i < entries; i ++)
00161     {
00162         str_unref (index_get (filenames, i));
00163         Tuple * tuple = index_get (tuples, i);
00164         if (tuple)
00165             tuple_unref (tuple);
00166     }
00167 
00168     index_free (filenames);
00169     index_free (tuples);
00170 
00171     return success;
00172 }