+ else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
+ {
+ if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
+ ReadMetaFromXiph( ogg_flac->tag(), p_demux, p_demux_meta, p_meta );
+ else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
+ ReadMetaFromXiph( ogg_speex->tag(), p_demux, p_demux_meta, p_meta );
+ else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
+ ReadMetaFromXiph( ogg_vorbis->tag(), p_demux, p_demux_meta, p_meta );
+ }
+ else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
+ {
+ if( trueaudio->ID3v2Tag() )
+ ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux, p_demux_meta, p_meta );
+ }
+ else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
+ {
+ if( wavpack->APETag() )
+ ReadMetaFromAPE( wavpack->APETag(), p_demux, p_demux_meta, p_meta );
+ }
+
+ return VLC_SUCCESS;
+}
+
+
+
+/**
+ * Write meta informations to APE tags
+ * @param tag: the APE tag
+ * @param p_item: the input item
+ */
+static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
+{
+ char* psz_meta;
+#define WRITE( metaName, keyName ) \
+ psz_meta = input_item_Get##metaName( p_item ); \
+ if( psz_meta ) \
+ { \
+ String key( keyName, String::UTF8 ); \
+ String value( psz_meta, String::UTF8 ); \
+ tag->addValue( key, value, true ); \
+ } \
+ free( psz_meta );
+
+ WRITE( Copyright, "COPYRIGHT" );
+ WRITE( Language, "LANGUAGE" );
+ WRITE( Publisher, "PUBLISHER" );
+
+#undef WRITE
+}
+
+
+
+/**
+ * Write meta information to id3v2 tags
+ * @param tag: the id3v2 tag
+ * @param p_input: the input item
+ */
+static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
+{
+ char* psz_meta;
+#define WRITE( metaName, tagName ) \
+ psz_meta = input_item_Get##metaName( p_item ); \
+ if( psz_meta ) \
+ { \
+ ByteVector p_byte( tagName, 4 ); \
+ tag->removeFrames( p_byte ); \
+ ID3v2::TextIdentificationFrame* p_frame = \
+ new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
+ p_frame->setText( psz_meta ); \
+ tag->addFrame( p_frame ); \
+ } \
+ free( psz_meta );
+
+ WRITE( Copyright, "TCOP" );
+ WRITE( EncodedBy, "TENC" );
+ WRITE( Language, "TLAN" );
+ WRITE( Publisher, "TPUB" );
+
+#undef WRITE
+}
+
+
+
+/**
+ * Write the meta informations to XiphComments
+ * @param tag: the Xiph Comment
+ * @param p_input: the input item
+ */
+static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
+{
+ char* psz_meta;
+#define WRITE( metaName, keyName ) \
+ psz_meta = input_item_Get##metaName( p_item ); \
+ if( psz_meta ) \
+ { \
+ String key( keyName, String::UTF8 ); \
+ String value( psz_meta, String::UTF8 ); \
+ tag->addField( key, value, true ); \
+ } \
+ free( psz_meta );
+
+ WRITE( Copyright, "COPYRIGHT" );
+
+#undef WRITE
+}
+
+
+
+/**
+ * Set the tags to the file using TagLib
+ * @param p_this: the demux object
+ * @return VLC_SUCCESS if the operation success
+ */
+
+static int WriteMeta( vlc_object_t *p_this )
+{
+ playlist_t *p_playlist = (playlist_t *)p_this;
+ meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
+ input_item_t *p_item = p_export->p_item;
+ FileRef f;
+
+ if( !p_item )
+ {
+ msg_Err( p_this, "Can't save meta data of an empty input" );
+ return VLC_EGENERIC;
+ }
+
+ char *export_file = strdup(p_export->psz_file);
+ if( decode_URI( export_file ) == NULL )
+ {
+ free( export_file );
+ return VLC_EGENERIC;
+ }
+
+#if defined(WIN32) || defined (UNDER_CE)
+ wchar_t wpath[MAX_PATH + 1];
+ if( !MultiByteToWideChar( CP_UTF8, 0, export_file , -1, wpath, MAX_PATH) )
+ return VLC_EGENERIC;
+ wpath[MAX_PATH] = L'\0';
+ f = FileRef( wpath );
+#else
+ const char* local_name = ToLocale( export_file );
+ if( !local_name )
+ {
+ free( export_file );
+ return VLC_EGENERIC;
+ }
+ f = FileRef( local_name );
+ LocaleFree( local_name );
+#endif
+
+ if( f.isNull() || !f.tag() || f.file()->readOnly() )
+ {
+ msg_Err( p_this, "File %s can't be opened for tag writing",
+ export_file );
+ free( export_file );
+ return VLC_EGENERIC;
+ }
+
+ msg_Dbg( p_this, "Writing metadata for %s", export_file );
+ free( export_file );
+
+ Tag *p_tag = f.tag();
+
+ char *psz_meta;
+
+#define SET( a, b ) \
+ if( b ) \
+ { \
+ String* psz_tmp = new String( b, String::UTF8 ); \
+ p_tag->set##a( *psz_tmp ); \
+ delete psz_tmp; \
+ }
+
+ // Saving all common fields
+ // If the title is empty, use the name
+ psz_meta = input_item_GetTitleFbName( p_item );
+ SET( Title, psz_meta );
+ free( psz_meta );
+
+ psz_meta = input_item_GetArtist( p_item );
+ SET( Artist, psz_meta );
+ free( psz_meta );
+
+ psz_meta = input_item_GetAlbum( p_item );
+ SET( Album, psz_meta );
+ free( psz_meta );
+
+ psz_meta = input_item_GetDescription( p_item );
+ SET( Comment, psz_meta );
+ free( psz_meta );
+
+ psz_meta = input_item_GetGenre( p_item );
+ SET( Genre, psz_meta );
+ free( psz_meta );
+
+#undef SET
+
+ psz_meta = input_item_GetDate( p_item );
+ if( psz_meta ) p_tag->setYear( atoi( psz_meta ) );
+ free( psz_meta );
+
+ psz_meta = input_item_GetTrackNum( p_item );
+ if( psz_meta ) p_tag->setTrack( atoi( psz_meta ) );
+ free( psz_meta );
+
+
+ // Try now to write special tags
+ if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
+ {
+ if( flac->ID3v2Tag() )
+ WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
+ else if( flac->xiphComment() )
+ WriteMetaToXiph( flac->xiphComment(), p_item );
+ }
+ else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
+ {
+ if( mpc->APETag() )
+ WriteMetaToAPE( mpc->APETag(), p_item );
+ }
+ else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
+ {
+ if( mpeg->ID3v2Tag() )
+ WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
+ else if( mpeg->APETag() )
+ WriteMetaToAPE( mpeg->APETag(), p_item );
+ }
+ else if( Ogg::File* ogg = dynamic_cast<Ogg::File*>(f.file()) )
+ {
+ if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
+ WriteMetaToXiph( ogg_flac->tag(), p_item );
+ else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
+ WriteMetaToXiph( ogg_speex->tag(), p_item );
+ else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
+ WriteMetaToXiph( ogg_vorbis->tag(), p_item );
+ }
+ else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
+ {
+ if( trueaudio->ID3v2Tag() )
+ WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
+ }
+ else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
+ {
+ if( wavpack->APETag() )
+ WriteMetaToAPE( wavpack->APETag(), p_item );
+ }
+
+ // Save the meta data
+ f.save();
+
+ return VLC_SUCCESS;