Add a module to export the playlist in HTML (feature requested on the forum).
[vlc.git] / modules / misc / playlist / html.c
1 /*****************************************************************************
2  * html.c : HTML playlist export module
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: RĂ©mi Duraffort <ivoire@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_playlist.h>
30 #include <vlc_input.h>
31 #include <vlc_meta.h>
32 #include <vlc_strings.h>
33
34
35
36 // Export the playlist in HTML
37 int Export_HTML( vlc_object_t *p_this );
38
39
40 /**
41  * Recursiveyy follow the playlist
42  * @param p_playlist: the playlist
43  * @param p_export: the export structure
44  * @param p_root: the current node
45  */
46 static void DoChildren( playlist_t *p_playlist, playlist_export_t *p_export,
47                         playlist_item_t *p_root )
48 {
49     /* Go through the playlist and add items */
50     for( int i = 0; i < p_root->i_children ; i++)
51     {
52         playlist_item_t *p_current = p_root->pp_children[i];
53         assert( p_current );
54
55         if( p_current->i_flags & PLAYLIST_SAVE_FLAG )
56             continue;
57
58         if( p_current->i_children >= 0 )
59         {
60             DoChildren( p_playlist, p_export, p_current );
61             continue;
62         }
63
64         char* psz_name = NULL;
65         char *psz_tmp = input_item_GetName( p_current->p_input );
66         if( psz_tmp )
67             psz_name = convert_xml_special_chars( psz_tmp );
68         free( psz_tmp );
69
70         if( psz_name )
71         {
72             char* psz_artist = NULL;
73             psz_tmp = input_item_GetArtist( p_current->p_input );
74             if( psz_tmp )
75                 psz_artist = convert_xml_special_chars( psz_tmp );
76             free( psz_tmp );
77
78             mtime_t i_duration = input_item_GetDuration( p_current->p_input );
79             int min = ( i_duration / 1000000 ) / 60;
80             int sec = ( i_duration / 1000000 ) - min * 60;
81
82             // Print the artist if we have one
83             if( psz_artist && *psz_artist )
84                 fprintf( p_export->p_file, "    <li>%s - %s (%02d:%02d)</li>\n", psz_artist, psz_name, min, sec );
85             else
86                 fprintf( p_export->p_file, "    <li>%s (%2d:%2d)</li>\n", psz_name, min, sec );
87
88             free( psz_artist );
89         }
90         free( psz_name );
91     }
92 }
93
94
95 /**
96  * Export the playlist as an HTML page
97  * @param p_this: the playlist
98  * @return VLC_SUCCESS if everything goes fine
99  */
100 int Export_HTML( vlc_object_t *p_this )
101 {
102     playlist_t *p_playlist = (playlist_t*)p_this;
103     playlist_export_t *p_export = (playlist_export_t *)p_playlist->p_private;
104
105     msg_Dbg( p_playlist, "saving using HTML format" );
106
107     /* Write header */
108     fprintf( p_export->p_file, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"
109 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
110 "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n"
111 "<head>\n"
112 "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
113 "  <meta name=\"Generator\" content=\"VLC media player\" />\n"
114 "  <meta name=\"Author\" content=\"videolan@videolan.org (VideoLAN team)\" />\n"
115 "  <title>VLC generated playlist</title>\n"
116 "  <style type=\"text/css\">\n"
117 "    body {\n"
118 "      background-color: #E4F3FF;\n"
119 "      font-family: sans-serif, Helvetica, Arial;\n"
120 "      font-size: 13px;\n"
121 "    }\n"
122 "    h1 {\n"
123 "      color: #2D58AE;\n"
124 "      font-size: 25px;\n"
125 "    }\n"
126 "    hr {\n"
127 "      color: #555555;\n"
128 "    }\n"
129 "  </style>\n"
130 "</head>\n\n"
131 "<body>\n"
132 "  <h1>Playlist</h1>\n"
133 "  <hr />\n"
134 "  <ol>\n" );
135
136     // Call the playlist constructor
137     DoChildren( p_playlist, p_export, p_export->p_root );
138
139     // Print the footer
140     fprintf( p_export->p_file, "  </ol>\n"
141 "  <hr />\n"
142 "</body>\n"
143 "</html>" );
144     return VLC_SUCCESS;
145 }
146