static gchar * Picture_Info (Picture *pic) { const gchar *format, *desc, *type; gchar *r, *size_str; GString *s; format = Picture_Format_String(Picture_Format_From_Data(pic)); if (pic->description) desc = pic->description; else desc = ""; type = Picture_Type_String(pic->type); size_str = Convert_Size_1((gfloat)pic->size); s = g_string_new(0); // Behaviour following the tag type... switch (ETCore->ETFileDisplayed->ETFileDescription->TagType) { case MP4_TAG: { g_string_printf (s, "%s (%s - %d×%d %s)\n%s: %s", format, size_str, pic->width, pic->height, _("pixels"), _("Type"), type); break; } // Other tag types default: { g_string_printf (s, "%s (%s - %d×%d %s)\n%s: %s\n%s: %s", format, size_str, pic->width, pic->height, _("pixels"), _("Type"), type, _("Description"), desc); break; } } r = Try_To_Validate_Utf8_String(s->str); g_string_free(s, TRUE); // TRUE to free also 's->str'! g_free(size_str); return r; }
/* * Write Flac tag, using the level 2 flac interface */ gboolean flac_tag_write_file_tag (const ET_File *ETFile, GError **error) { const File_Tag *FileTag; GFile *file; GFileIOStream *iostream; EtFlacWriteState state; FLAC__IOCallbacks callbacks = { et_flac_read_func, et_flac_write_func, et_flac_seek_func, et_flac_tell_func, et_flac_eof_func, et_flac_write_close_func }; const gchar *filename; const gchar *filename_utf8; const gchar *flac_error_msg; FLAC__Metadata_Chain *chain; FLAC__Metadata_Iterator *iter; FLAC__StreamMetadata_VorbisComment_Entry vce_field_vendor_string; // To save vendor string gboolean vce_field_vendor_string_found = FALSE; g_return_val_if_fail (ETFile != NULL && ETFile->FileTag != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); FileTag = (File_Tag *)ETFile->FileTag->data; filename = ((File_Name *)ETFile->FileNameCur->data)->value; filename_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8; /* libFLAC is able to detect (and skip) ID3v2 tags by itself */ /* Create a new chain instance to get all blocks in one time. */ chain = FLAC__metadata_chain_new (); if (chain == NULL) { flac_error_msg = FLAC__Metadata_ChainStatusString[FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR]; g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Error while opening file ‘%s’ as FLAC: %s"), filename_utf8, flac_error_msg); return FALSE; } file = g_file_new_for_path (filename); state.file = file; state.error = NULL; /* TODO: Fallback to an in-memory copy of the file for non-local files, * where creation of the GFileIOStream may fail. */ iostream = g_file_open_readwrite (file, NULL, &state.error); if (iostream == NULL) { FLAC__metadata_chain_delete (chain); g_propagate_error (error, state.error); g_object_unref (file); return FALSE; } state.istream = G_FILE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (iostream))); state.ostream = G_FILE_OUTPUT_STREAM (g_io_stream_get_output_stream (G_IO_STREAM (iostream))); state.seekable = G_SEEKABLE (iostream); state.iostream = iostream; if (!FLAC__metadata_chain_read_with_callbacks (chain, &state, callbacks)) { const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status (chain); flac_error_msg = FLAC__Metadata_ChainStatusString[status]; FLAC__metadata_chain_delete (chain); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Error while opening file ‘%s’ as FLAC: %s"), filename_utf8, flac_error_msg); et_flac_write_close_func (&state); return FALSE; } /* Create a new iterator instance for the chain. */ iter = FLAC__metadata_iterator_new (); if (iter == NULL) { flac_error_msg = FLAC__Metadata_ChainStatusString[FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR]; g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Error while opening file ‘%s’ as FLAC: %s"), filename_utf8, flac_error_msg); FLAC__metadata_chain_delete (chain); et_flac_write_close_func (&state); return FALSE; } FLAC__metadata_iterator_init (iter, chain); while (FLAC__metadata_iterator_next (iter)) { const FLAC__MetadataType block_type = FLAC__metadata_iterator_get_block_type (iter); /* TODO: Modify the blocks directly, rather than deleting and * recreating. */ if (block_type == FLAC__METADATA_TYPE_VORBIS_COMMENT) { // Delete the VORBIS_COMMENT block and convert to padding. But before, save the original vendor string. /* Get block data. */ FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iter); FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment; if (vc->vendor_string.entry != NULL) { // Get initial vendor string, to don't alterate it by FLAC__VENDOR_STRING when saving file vce_field_vendor_string.entry = (FLAC__byte *)g_strdup ((gchar *)vc->vendor_string.entry); vce_field_vendor_string.length = strlen ((gchar *)vce_field_vendor_string.entry); vce_field_vendor_string_found = TRUE; } /* Free block data. */ FLAC__metadata_iterator_delete_block (iter, true); } else if (block_type == FLAC__METADATA_TYPE_PICTURE) { /* Delete all the PICTURE blocks, and convert to padding. */ FLAC__metadata_iterator_delete_block (iter, true); } } // // Create and insert a new VORBISCOMMENT block // { FLAC__StreamMetadata *vc_block; // For vorbis comments GList *l; // Allocate a block for Vorbis comments vc_block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); // Set the original vendor string, else will be use the version of library if (vce_field_vendor_string_found) { // must set 'copy' param to false, because the API will reuse the pointer of an empty // string (yet still return 'true', indicating it was copied); the string is free'd during // metadata_chain_delete routine FLAC__metadata_object_vorbiscomment_set_vendor_string(vc_block, vce_field_vendor_string, /*copy=*/false); } /********* * Title * *********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_TITLE, FileTag->title, g_settings_get_boolean (MainSettings, "ogg-split-title")); /********** * Artist * **********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_ARTIST, FileTag->artist, g_settings_get_boolean (MainSettings, "ogg-split-artist")); /**************** * Album Artist * ****************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_ALBUM_ARTIST, FileTag->album_artist, g_settings_get_boolean (MainSettings, "ogg-split-artist")); /********* * Album * *********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_ALBUM, FileTag->album, g_settings_get_boolean (MainSettings, "ogg-split-album")); /****************************** * Disc Number and Disc Total * ******************************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_DISC_NUMBER, FileTag->disc_number, FALSE); vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_DISC_TOTAL, FileTag->disc_total, FALSE); /******** * Year * ********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_DATE, FileTag->year, FALSE); /************************* * Track and Total Track * *************************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_TRACK_NUMBER, FileTag->track, FALSE); vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_TRACK_TOTAL, FileTag->track_total, FALSE); /********* * Genre * *********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_GENRE, FileTag->genre, g_settings_get_boolean (MainSettings, "ogg-split-genre")); /*********** * Comment * ***********/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_DESCRIPTION, FileTag->comment, g_settings_get_boolean (MainSettings, "ogg-split-comment")); /************ * Composer * ************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_COMPOSER, FileTag->composer, g_settings_get_boolean (MainSettings, "ogg-split-composer")); /******************* * Original artist * *******************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_PERFORMER, FileTag->orig_artist, g_settings_get_boolean (MainSettings, "ogg-split-original-artist")); /************* * Copyright * *************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_COPYRIGHT, FileTag->copyright, FALSE); /******* * URL * *******/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_CONTACT, FileTag->url, FALSE); /************** * Encoded by * **************/ vc_block_append_tag (vc_block, ET_VORBIS_COMMENT_FIELD_ENCODED_BY, FileTag->encoded_by, FALSE); /************************** * Set unsupported fields * **************************/ for (l = FileTag->other; l != NULL; l = g_list_next (l)) { vc_block_append_other_tag (vc_block, (gchar *)l->data); } // Add the block to the the chain (so we don't need to free the block) FLAC__metadata_iterator_insert_block_after(iter, vc_block); } // // Create and insert PICTURE blocks // /*********** * Picture * ***********/ { EtPicture *pic = FileTag->picture; while (pic) { /* TODO: Can this ever be NULL? */ if (pic->bytes) { const gchar *violation; FLAC__StreamMetadata *picture_block; // For picture data Picture_Format format; gconstpointer data; gsize data_size; // Allocate block for picture data picture_block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE); // Type picture_block->data.picture.type = pic->type; // Mime type format = Picture_Format_From_Data(pic); /* Safe to pass a const string, according to the FLAC API * reference. */ FLAC__metadata_object_picture_set_mime_type(picture_block, (gchar *)Picture_Mime_Type_String(format), TRUE); // Description if (pic->description) { FLAC__metadata_object_picture_set_description(picture_block, (FLAC__byte *)pic->description, TRUE); } // Resolution picture_block->data.picture.width = pic->width; picture_block->data.picture.height = pic->height; picture_block->data.picture.depth = 0; /* Picture data. */ data = g_bytes_get_data (pic->bytes, &data_size); /* Safe to pass const data, if the last argument (copy) is * TRUE, according the the FLAC API reference. */ FLAC__metadata_object_picture_set_data (picture_block, (FLAC__byte *)data, (FLAC__uint32)data_size, true); if (!FLAC__metadata_object_picture_is_legal (picture_block, &violation)) { g_critical ("Created an invalid picture block: ‘%s’", violation); FLAC__metadata_object_delete (picture_block); } else { // Add the block to the the chain (so we don't need to free the block) FLAC__metadata_iterator_insert_block_after(iter, picture_block); } } pic = pic->next; } } FLAC__metadata_iterator_delete (iter); // // Prepare for writing tag // FLAC__metadata_chain_sort_padding (chain); /* Write tag. */ if (FLAC__metadata_chain_check_if_tempfile_needed (chain, true)) { EtFlacWriteState temp_state; GFile *temp_file; GFileIOStream *temp_iostream; GError *temp_error = NULL; temp_file = g_file_new_tmp ("easytag-XXXXXX", &temp_iostream, &temp_error); if (temp_file == NULL) { FLAC__metadata_chain_delete (chain); g_propagate_error (error, temp_error); et_flac_write_close_func (&state); return FALSE; } temp_state.file = temp_file; temp_state.error = NULL; temp_state.istream = G_FILE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (temp_iostream))); temp_state.ostream = G_FILE_OUTPUT_STREAM (g_io_stream_get_output_stream (G_IO_STREAM (temp_iostream))); temp_state.seekable = G_SEEKABLE (temp_iostream); temp_state.iostream = temp_iostream; if (!FLAC__metadata_chain_write_with_callbacks_and_tempfile (chain, true, &state, callbacks, &temp_state, callbacks)) { const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status (chain); flac_error_msg = FLAC__Metadata_ChainStatusString[status]; FLAC__metadata_chain_delete (chain); et_flac_write_close_func (&temp_state); et_flac_write_close_func (&state); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to write comments to file ‘%s’: %s"), filename_utf8, flac_error_msg); return FALSE; } if (!g_file_move (temp_file, file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &state.error)) { FLAC__metadata_chain_delete (chain); et_flac_write_close_func (&temp_state); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to write comments to file ‘%s’: %s"), filename_utf8, state.error->message); et_flac_write_close_func (&state); return FALSE; } et_flac_write_close_func (&temp_state); } else { if (!FLAC__metadata_chain_write_with_callbacks (chain, true, &state, callbacks)) { const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status (chain); flac_error_msg = FLAC__Metadata_ChainStatusString[status]; FLAC__metadata_chain_delete (chain); et_flac_write_close_func (&state); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to write comments to file ‘%s’: %s"), filename_utf8, flac_error_msg); return FALSE; } } FLAC__metadata_chain_delete (chain); et_flac_write_close_func (&state); #ifdef ENABLE_MP3 { // Delete the ID3 tags (create a dummy ETFile for the Id3tag_... function) ET_File *ETFile_tmp = ET_File_Item_New(); File_Name *FileName_tmp = et_file_name_new (); File_Tag *FileTag_tmp = et_file_tag_new (); // Same file... FileName_tmp->value = g_strdup(filename); FileName_tmp->value_utf8 = g_strdup(filename_utf8); // Not necessary to fill 'value_ck' ETFile_tmp->FileNameList = g_list_append(NULL,FileName_tmp); ETFile_tmp->FileNameCur = ETFile_tmp->FileNameList; // With empty tag... ETFile_tmp->FileTagList = g_list_append(NULL,FileTag_tmp); ETFile_tmp->FileTag = ETFile_tmp->FileTagList; id3tag_write_file_tag (ETFile_tmp, NULL); ET_Free_File_List_Item(ETFile_tmp); } #endif return TRUE; }
/* * Write Flac tag, using the level 2 flac interface */ gboolean Flac_Tag_Write_File_Tag (ET_File *ETFile) { File_Tag *FileTag; gchar *filename_utf8, *filename; gchar *basename_utf8; FLAC__Metadata_Chain *chain; FLAC__Metadata_Iterator *iter; FLAC__StreamMetadata_VorbisComment_Entry vce_field_vendor_string; // To save vendor string gboolean vce_field_vendor_string_found = FALSE; if (!ETFile || !ETFile->FileTag) return FALSE; FileTag = (File_Tag *)ETFile->FileTag->data; filename = ((File_Name *)ETFile->FileNameCur->data)->value; filename_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8; flac_error_msg = NULL; /* libFLAC is able to detect (and skip) ID3v2 tags by itself */ // Create a new chain instance to get all blocks in one time chain = FLAC__metadata_chain_new(); if (chain == NULL || !FLAC__metadata_chain_read(chain,filename)) { if (chain == NULL) { // Error with "FLAC__metadata_chain_new" flac_error_msg = FLAC__Metadata_ChainStatusString[FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR]; } else { // Error with "FLAC__metadata_chain_read" FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain); flac_error_msg = FLAC__Metadata_ChainStatusString[status]; FLAC__metadata_chain_delete(chain); } Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' as FLAC (%s)."),filename_utf8,flac_error_msg); return FALSE; } // Create a new iterator instance for the chain iter = FLAC__metadata_iterator_new(); if (iter == NULL) { flac_error_msg = FLAC__Metadata_ChainStatusString[FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR]; Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' as FLAC (%s)."),filename_utf8,flac_error_msg); return FALSE; } // Initialize the iterator to point to the first metadata block in the given chain. FLAC__metadata_iterator_init(iter,chain); while (FLAC__metadata_iterator_next(iter)) { //g_print("Write: %d %s -> block type: %d\n",j++,g_path_get_basename(filename),FLAC__metadata_iterator_get_block_type(iter)); // Action to do according the type switch ( FLAC__metadata_iterator_get_block_type(iter) ) { // // Delete the VORBIS_COMMENT block and convert to padding. But before, save the original vendor string. // case FLAC__METADATA_TYPE_VORBIS_COMMENT: { // Get block data FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iter); FLAC__StreamMetadata_VorbisComment *vc = &block->data.vorbis_comment; if (vc->vendor_string.entry != NULL) { // Get initial vendor string, to don't alterate it by FLAC__VENDOR_STRING when saving file vce_field_vendor_string.entry = (FLAC__byte *)g_strdup((gchar *)vc->vendor_string.entry); vce_field_vendor_string.length = strlen((gchar *)vce_field_vendor_string.entry); vce_field_vendor_string_found = TRUE; } // Free block data FLAC__metadata_iterator_delete_block(iter,true); break; } // // Delete all the PICTURE blocks, and convert to padding // #ifndef LEGACY_FLAC // For FLAC >= 1.1.3 case FLAC__METADATA_TYPE_PICTURE: { FLAC__metadata_iterator_delete_block(iter,true); break; } #endif default: break; } } // // Create and insert a new VORBISCOMMENT block // { FLAC__StreamMetadata *vc_block; // For vorbis comments FLAC__StreamMetadata_VorbisComment_Entry field; gchar *string; GList *list; // Allocate a block for Vorbis comments vc_block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); // Set the original vendor string, else will be use the version of library if (vce_field_vendor_string_found) { // must set 'copy' param to false, because the API will reuse the pointer of an empty // string (yet still return 'true', indicating it was copied); the string is free'd during // metadata_chain_delete routine FLAC__metadata_object_vorbiscomment_set_vendor_string(vc_block, vce_field_vendor_string, /*copy=*/false); } /********* * Title * *********/ if ( FileTag->title ) { Flac_Write_Delimetered_Tag(vc_block,"TITLE=",FileTag->title); } /********** * Artist * **********/ if ( FileTag->artist ) { Flac_Write_Delimetered_Tag(vc_block,"ARTIST=",FileTag->artist); } /********* * Album * *********/ if ( FileTag->album ) { Flac_Write_Delimetered_Tag(vc_block,"ALBUM=",FileTag->album); } /*************** * Disc Number * ***************/ if ( FileTag->disc_number ) { string = g_strconcat("DISCNUMBER=",FileTag->disc_number,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /******** * Year * ********/ if ( FileTag->year ) { string = g_strconcat("DATE=",FileTag->year,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /************************* * Track and Total Track * *************************/ if ( FileTag->track ) { string = g_strconcat("TRACKNUMBER=",FileTag->track,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } if ( FileTag->track_total /*&& strlen(FileTag->track_total)>0*/ ) { string = g_strconcat("TRACKTOTAL=",FileTag->track_total,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /********* * Genre * *********/ if ( FileTag->genre ) { Flac_Write_Delimetered_Tag(vc_block,"GENRE=",FileTag->genre); } /*********** * Comment * ***********/ // We write the comment using the "both" format if ( FileTag->comment ) { Flac_Write_Delimetered_Tag(vc_block,"DESCRIPTION=",FileTag->comment); Flac_Write_Delimetered_Tag(vc_block,"COMMENT=",FileTag->comment); } /************ * Composer * ************/ if ( FileTag->composer ) { Flac_Write_Delimetered_Tag(vc_block,"COMPOSER=",FileTag->composer); } /******************* * Original artist * *******************/ if ( FileTag->orig_artist ) { Flac_Write_Delimetered_Tag(vc_block,"PERFORMER=",FileTag->orig_artist); } /************* * Copyright * *************/ if ( FileTag->copyright ) { string = g_strconcat("COPYRIGHT=",FileTag->copyright,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /******* * URL * *******/ if ( FileTag->url ) { string = g_strconcat("LICENSE=",FileTag->url,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /************** * Encoded by * **************/ if ( FileTag->encoded_by ) { string = g_strconcat("ENCODED-BY=",FileTag->encoded_by,NULL); field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); g_free(string); } /************************** * Set unsupported fields * **************************/ list = FileTag->other; while (list) { if (list->data) { string = (gchar*)list->data; field.entry = (FLAC__byte *)string; field.length = strlen(string); FLAC__metadata_object_vorbiscomment_insert_comment(vc_block,vc_block->data.vorbis_comment.num_comments,field,true); } list = list->next; } // Add the block to the the chain (so we don't need to free the block) FLAC__metadata_iterator_insert_block_after(iter, vc_block); } // // Create and insert PICTURE blocks // /*********** * Picture * ***********/ // For FLAC >= 1.1.3 #ifndef LEGACY_FLAC { Picture *pic = FileTag->picture; while (pic) { if (pic->data) { const gchar *violation; FLAC__StreamMetadata *picture_block; // For picture data Picture_Format format; // Allocate block for picture data picture_block = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PICTURE); // Type picture_block->data.picture.type = pic->type; // Mime type format = Picture_Format_From_Data(pic); FLAC__metadata_object_picture_set_mime_type(picture_block, (gchar *)Picture_Mime_Type_String(format), TRUE); // Description if (pic->description) { FLAC__metadata_object_picture_set_description(picture_block, (FLAC__byte *)pic->description, TRUE); } // Resolution picture_block->data.picture.width = pic->width; picture_block->data.picture.height = pic->height; picture_block->data.picture.depth = 0; // Picture data FLAC__metadata_object_picture_set_data(picture_block, (FLAC__byte *)pic->data, (FLAC__uint32) pic->size, TRUE); if (!FLAC__metadata_object_picture_is_legal(picture_block, &violation)) { Log_Print(LOG_ERROR,_("Picture block isn't valid: '%s'"),violation); FLAC__metadata_object_delete(picture_block); } else { // Add the block to the the chain (so we don't need to free the block) FLAC__metadata_iterator_insert_block_after(iter, picture_block); } } pic = pic->next; } } #endif // Free iter FLAC__metadata_iterator_delete(iter); // // Prepare for writing tag // // Move all PADDING blocks to the end on the metadata, and merge them into a single block. FLAC__metadata_chain_sort_padding(chain); // Write tag if ( !FLAC__metadata_chain_write(chain, /*padding*/TRUE, PRESERVE_MODIFICATION_TIME) ) { // Error with "FLAC__metadata_chain_write" FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain); flac_error_msg = FLAC__Metadata_ChainStatusString[status]; FLAC__metadata_chain_delete(chain); Log_Print(LOG_ERROR,_("ERROR: Failed to write comments to file '%s' (%s)."),filename_utf8,flac_error_msg); return FALSE; } else { basename_utf8 = g_path_get_basename(filename_utf8); Log_Print(LOG_OK,_("Written tag of '%s'"),basename_utf8); g_free(basename_utf8); } FLAC__metadata_chain_delete(chain); #ifdef ENABLE_MP3 /* * Write also the ID3 tags (ID3v1 and/or ID3v2) if wanted (as needed by some players) */ if (WRITE_ID3_TAGS_IN_FLAC_FILE) { Id3tag_Write_File_Tag(ETFile); } else { // Delete the ID3 tags (create a dummy ETFile for the Id3tag_... function) ET_File *ETFile_tmp = ET_File_Item_New(); File_Name *FileName_tmp = ET_File_Name_Item_New(); File_Tag *FileTag_tmp = ET_File_Tag_Item_New(); // Same file... FileName_tmp->value = g_strdup(filename); FileName_tmp->value_utf8 = g_strdup(filename_utf8); // Not necessary to fill 'value_ck' ETFile_tmp->FileNameList = g_list_append(NULL,FileName_tmp); ETFile_tmp->FileNameCur = ETFile_tmp->FileNameList; // With empty tag... ETFile_tmp->FileTagList = g_list_append(NULL,FileTag_tmp); ETFile_tmp->FileTag = ETFile_tmp->FileTagList; Id3tag_Write_File_Tag(ETFile_tmp); ET_Free_File_List_Item(ETFile_tmp); } #endif return TRUE; }
gboolean Ogg_Tag_Write_File_Tag (ET_File *ETFile) { File_Tag *FileTag; gchar *filename; gchar *filename_utf8; gchar *basename_utf8; FILE *file_in; vcedit_state *state; vorbis_comment *vc; gchar *string; GList *list; Picture *pic; if (!ETFile || !ETFile->FileTag) return FALSE; FileTag = (File_Tag *)ETFile->FileTag->data; filename = ((File_Name *)ETFile->FileNameCur->data)->value; filename_utf8 = ((File_Name *)ETFile->FileNameCur->data)->value_utf8; ogg_error_msg = NULL; /* Test to know if we can write into the file */ if ( (file_in=fopen(filename,"rb"))==NULL ) { Log_Print(LOG_ERROR,_("ERROR while opening file: '%s' (%s)."),filename_utf8,g_strerror(errno)); return FALSE; } { // Skip the id3v2 tag guchar tmp_id3[4]; gulong id3v2size; // Check if there is an ID3v2 tag... fseek(file_in, 0L, SEEK_SET); if (fread(tmp_id3, 1, 4, file_in) == 4) { // Calculate ID3v2 length if (tmp_id3[0] == 'I' && tmp_id3[1] == 'D' && tmp_id3[2] == '3' && tmp_id3[3] < 0xFF) { // id3v2 tag skipeer $49 44 33 yy yy xx zz zz zz zz [zz size] fseek(file_in, 2, SEEK_CUR); // Size is 6-9 position if (fread(tmp_id3, 1, 4, file_in) == 4) { id3v2size = 10 + ( (long)(tmp_id3[3]) | ((long)(tmp_id3[2]) << 7) | ((long)(tmp_id3[1]) << 14) | ((long)(tmp_id3[0]) << 21) ); fseek(file_in, id3v2size, SEEK_SET); }else { fseek(file_in, 0L, SEEK_SET); } }else { fseek(file_in, 0L, SEEK_SET); } }else { fseek(file_in, 0L, SEEK_SET); } } state = vcedit_new_state(); // Allocate memory for 'state' if ( vcedit_open(state,file_in) < 0 ) { Log_Print(LOG_ERROR,_("ERROR: Failed to open file: '%s' as vorbis (%s)."),filename_utf8,vcedit_error(state)); ogg_error_msg = vcedit_error(state); fclose(file_in); vcedit_clear(state); return FALSE; } /* Get data from tag */ vc = vcedit_comments(state); vorbis_comment_clear(vc); vorbis_comment_init(vc); /********* * Title * *********/ Ogg_Set_Tag(vc,"TITLE=",FileTag->title,VORBIS_SPLIT_FIELD_TITLE); /********** * Artist * **********/ Ogg_Set_Tag(vc,"ARTIST=",FileTag->artist, VORBIS_SPLIT_FIELD_ARTIST); /**************** * Album Artist * ****************/ Ogg_Set_Tag(vc,"ALBUMARTIST=",FileTag->album_artist, VORBIS_SPLIT_FIELD_ARTIST); /********* * Album * *********/ Ogg_Set_Tag(vc,"ALBUM=",FileTag->album, VORBIS_SPLIT_FIELD_ALBUM); /*************** * Disc Number * ***************/ Ogg_Set_Tag(vc,"DISCNUMBER=",FileTag->disc_number,FALSE); /******** * Year * ********/ Ogg_Set_Tag(vc,"DATE=",FileTag->year,FALSE); /************************* * Track and Total Track * *************************/ Ogg_Set_Tag(vc,"TRACKNUMBER=",FileTag->track,FALSE); Ogg_Set_Tag(vc,"TRACKTOTAL=",FileTag->track_total,FALSE); /********* * Genre * *********/ Ogg_Set_Tag(vc,"GENRE=",FileTag->genre,VORBIS_SPLIT_FIELD_GENRE); /*********** * Comment * ***********/ // We write the comment using the two formats "DESCRIPTION" and "COMMENT" to be compatible with old versions // Format of new specification Ogg_Set_Tag(vc,"DESCRIPTION=",FileTag->comment,VORBIS_SPLIT_FIELD_COMMENT); // Format used in winamp plugin Ogg_Set_Tag(vc,"COMMENT=",FileTag->comment,VORBIS_SPLIT_FIELD_COMMENT); if (OGG_TAG_WRITE_XMMS_COMMENT) { // Format used into xmms-1.2.5 Ogg_Set_Tag(vc,"=",FileTag->comment,VORBIS_SPLIT_FIELD_COMMENT); } /************ * Composer * ************/ Ogg_Set_Tag(vc,"COMPOSER=",FileTag->composer,VORBIS_SPLIT_FIELD_COMPOSER); /******************* * Original artist * *******************/ Ogg_Set_Tag(vc,"PERFORMER=",FileTag->orig_artist,VORBIS_SPLIT_FIELD_ORIG_ARTIST); /************* * Copyright * *************/ Ogg_Set_Tag(vc,"COPYRIGHT=",FileTag->copyright,FALSE); /******* * URL * *******/ Ogg_Set_Tag(vc,"LICENSE=",FileTag->url,FALSE); /************** * Encoded by * **************/ Ogg_Set_Tag(vc,"ENCODED-BY=",FileTag->encoded_by,FALSE); /*********** * Picture * ***********/ pic = FileTag->picture; while (pic) { if (pic->data) { gchar *data_encoded = NULL; gint size; Picture_Format format = Picture_Format_From_Data(pic); string = g_strdup_printf("COVERARTMIME=%s",Picture_Mime_Type_String(format)); vorbis_comment_add(vc,string); g_free(string); if (pic->type) { string = g_strdup_printf("COVERARTTYPE=%d",pic->type); vorbis_comment_add(vc,string); g_free(string); } if (pic->description) { string = g_strdup_printf("COVERARTDESCRIPTION=%s",pic->description); vorbis_comment_add(vc,string); g_free(string); } size = base64_encode(pic->data, pic->size, &data_encoded); string = g_strdup_printf("COVERART=%s",data_encoded); vorbis_comment_add(vc,string); g_free(data_encoded); g_free(string); } pic = pic->next; } /************************** * Set unsupported fields * **************************/ list = FileTag->other; while (list) { if (list->data) vorbis_comment_add(vc,(gchar *)list->data); list = list->next; } /* Write tag, and close also 'file_in' in all cases */ if ( Ogg_Tag_Write_File(file_in,filename,state) == FALSE ) { ogg_error_msg = vcedit_error(state); Log_Print(LOG_ERROR,_("ERROR: Failed to write comments to file '%s' (%s)."),filename_utf8,ogg_error_msg == NULL ? "" : ogg_error_msg); vcedit_clear(state); return FALSE; }else { basename_utf8 = g_path_get_basename(filename_utf8); Log_Print(LOG_OK,_("Written tag of '%s'"),basename_utf8); vcedit_clear(state); } return TRUE; }
void Picture_Save_Button_Clicked (GObject *object) { GtkWidget *FileSelectionWindow; GtkFileFilter *filter; GtkWindow *parent_window = NULL; static gchar *init_dir = NULL; GtkTreeSelection *selection; GList *selection_list = NULL; GList *l; GtkTreeModel *model; gint selection_nbr, selection_i = 1; g_return_if_fail (PictureEntryView != NULL); parent_window = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(object))); if (!gtk_widget_is_toplevel(GTK_WIDGET(parent_window))) { g_warning("Could not get parent window\n"); return; } model = gtk_tree_view_get_model(GTK_TREE_VIEW(PictureEntryView)); selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(PictureEntryView)); selection_list = gtk_tree_selection_get_selected_rows(selection, NULL); selection_nbr = gtk_tree_selection_count_selected_rows(GTK_TREE_SELECTION(selection)); for (l = selection_list; l != NULL; l = g_list_next (l)) { GtkTreePath *path = l->data; GtkTreeIter iter; Picture *pic; gchar *title; gboolean valid; gint response; // Get corresponding picture valid = gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path); if (valid) gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, PICTURE_COLUMN_DATA, &pic, -1); title = g_strdup_printf (_("Save Image %d/%d"), selection_i++, selection_nbr); FileSelectionWindow = gtk_file_chooser_dialog_new(title, parent_window, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_OK, NULL); g_free(title); // Add files filters // "All files" filter filter = gtk_file_filter_new (); gtk_file_filter_set_name(GTK_FILE_FILTER(filter), _("All Files")); gtk_file_filter_add_pattern(GTK_FILE_FILTER(filter), "*"); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(FileSelectionWindow), GTK_FILE_FILTER(filter)); // "PNG and JPEG" filter filter = gtk_file_filter_new (); gtk_file_filter_set_name(GTK_FILE_FILTER(filter), _("PNG and JPEG")); gtk_file_filter_add_mime_type(GTK_FILE_FILTER(filter), "image/jpeg"); gtk_file_filter_add_mime_type(GTK_FILE_FILTER(filter), "image/png"); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER (FileSelectionWindow), GTK_FILE_FILTER(filter)); // Make this filter the default gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(FileSelectionWindow), GTK_FILE_FILTER(filter)); // Set the default folder if defined if (init_dir) gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(FileSelectionWindow),init_dir); // Suggest a filename to the user if ( pic->description && strlen(pic->description) ) { gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(FileSelectionWindow), pic->description); //filename in UTF8 }else { gchar *image_name = NULL; switch (Picture_Format_From_Data(pic)) { case PICTURE_FORMAT_JPEG : image_name = g_strdup("image_name.jpg"); break; case PICTURE_FORMAT_PNG : image_name = g_strdup("image_name.png"); break; case PICTURE_FORMAT_UNKNOWN : image_name = g_strdup("image_name.ext"); break; } gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(FileSelectionWindow), image_name); //filename in UTF8 g_free(image_name); } gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (FileSelectionWindow), TRUE); gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (FileSelectionWindow), FALSE); response = gtk_dialog_run(GTK_DIALOG(FileSelectionWindow)); if (response == GTK_RESPONSE_OK) { GFile *file; GError *error = NULL; // Save the directory selected for initialize next time g_free(init_dir); init_dir = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(FileSelectionWindow)); file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (FileSelectionWindow)); if (!et_picture_save_file_data (pic, file, &error)) { Log_Print (LOG_ERROR, _("Image file not saved: %s"), error->message); g_error_free (error); } g_object_unref (file); } gtk_widget_destroy(FileSelectionWindow); } g_list_free_full (selection_list, (GDestroyNotify)gtk_tree_path_free); }