import javax.swing.Action import java.awt.* import java.io.FileWriter import java.io.IOException import java.io.Writer import java.util.SortedSet import java.util.TreeSet import org.slf4j.LoggerFactory import com.tagtraum.beatunes.action.standard.LibraryBatchAction import com.tagtraum.beatunes.action.standard.LibraryBatchAction.EachSongProcessor import com.tagtraum.beatunes.MessageDialog import javax.swing.JOptionPane import com.tagtraum.audiokern.AudioSong import com.tagtraum.core.OperatingSystem import com.tagtraum.audiokern.AudioMetaData // An action that allows to do something (print tags) for each song in the library. // The corresponding menu item can be found in the 'Tools' menu. class EmbedRatings extends LibraryBatchAction { // Inner class that implements the // com.tagtraum.beatunes.action.standard.LibraryBatchAction.EachSongProcessor // interface. Its process method is called for each song. class Embedder implements EachSongProcessor { static log = LoggerFactory.getLogger("EmbedRatings.groovy") String file // Called once, before processing starts. def void startProcessing(int count) { log.info "We are expecting to embed ratings for ${count} songs." } // Called for each song. def void process(AudioSong song, int index) { int rating = song.getRating() if (rating > 0) { // get the AudioMetaData (allows writing to the file directly) AudioSong songFile = song.getImplementation(AudioMetaData.class) if (songFile != null) { try { songFile.setRating(rating) log.info("Embedded rating ${rating} into " + song) } catch (Exception e) { log.error("Failed to embed rating for " + song, e) } } else { log.info("Failed to get song file for " + song) } } else { log.info("No rating set in " + song) } } // Called once all songs were processed. def void finishProcessing() { log.info "Done." new MessageDialog(getApplication().getMainWindow(), "Done.", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION).showDialog() } // Message to be shown in progress dialog. def String getProgressDialogMessage(AudioSong song) { "Embedding rating into ${song.getName()}" } // Title for progress dialog. def String getProgressDialogTitle() { "Embedding ratings ..." } } // Unique id def String getId() { "Groovy.EmbedRatings" } // Is called by beaTunes as part of the lifecycle after instantiation. // At this point all other plugins are instantiated and registered. // We use this to set the menu item's (i.e. action's) name. def void init() { putValue(Action.NAME, "Embed Ratings into Files") } // We need to ask the user, whether he really wants to do this. // How we ask is defined here. def String getConfirmationMessage() { "Do you really want to embed iTunes ratings into all your files?" } // Factory method that creates the processor for each song. def EachSongProcessor createEachSongProcessor() { new Embedder() } }