SandiskPlaylistFolder.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package convert_rhythmbox_player_to_sandisk;
  2. // To figure out which Rhythmbox playlist songs are in the folder, or not
  3. import java.util.ArrayList;
  4. // To get all the songs from the folder
  5. import java.io.File;
  6. // To copy the songs from the Music folder to the SanDisk playlist folder
  7. import java.nio.file.Files;
  8. import java.nio.file.Path;
  9. import java.io.IOException;
  10. public class SandiskPlaylistFolder {
  11. private String name; // SanDisk playlist folder name (e.g., Anime, Rap)
  12. // Absolute path to the SanDisk playlist folder, from user's root directory
  13. private String path;
  14. // To search for a filename to delete
  15. private ArrayList<String> rhythmboxPlaylistSongFilenames;
  16. // To contain all the song names that are in the SanDisk playlist folder
  17. private ArrayList<String> sandiskPlaylistSongFilenames;
  18. // To contain all the song names from the Rhythmbox playlist
  19. private ArrayList<File> rhythmboxPlaylistSongFiles;
  20. public SandiskPlaylistFolder(String name, RhythmboxPlaylist playlist) {
  21. this.name = name;
  22. this.path = "/media/elijah/16GB Rockbo/SD CARD MUSIC/" + name + "/";
  23. sandiskPlaylistSongFilenames = this.getSongFilenamesFromSandiskFolder();
  24. rhythmboxPlaylistSongFiles = playlist.getPlaylistSongs();
  25. rhythmboxPlaylistSongFilenames = playlist.getPlaylistSongFilenames();
  26. }
  27. private ArrayList<String> getSongFilenamesFromSandiskFolder() {
  28. ArrayList<String> songNames = new ArrayList<String>();
  29. // To get the File objects from the playlist folder's path
  30. File[] files = new File(this.path).listFiles();
  31. for (File file : files) {
  32. songNames.add( file.getName() );
  33. }
  34. return songNames;
  35. }
  36. // Helper function to delete songs from the SanDisk playlist folder
  37. private void deleteSong(String songName) {
  38. File fileToDelete = new File(this.path + songName);
  39. try {
  40. fileToDelete.delete();
  41. } catch(Exception err) {
  42. System.out.println("File doesn't exist, so it can't be deleted.");
  43. }
  44. }
  45. /* Add any songs from Rhythmbox playlist to the SanDisk folder that are
  46. not already there */
  47. public void copyMissingSongsToFolder() {
  48. for (File file : rhythmboxPlaylistSongFiles) {
  49. if ( !sandiskPlaylistSongFilenames.contains(file.getName()) ) {
  50. File musicFolderSong = new File( "/home/elijah/Music/" + file.toPath() );
  51. // To get the file path for the file in the SanDisk folder
  52. File sandiskSong = new File( this.path + file.getName() );
  53. // Copy the files
  54. System.out.println( "Copying the following song: " + file.getName() );
  55. try {
  56. Files.copy( musicFolderSong.toPath(), sandiskSong.toPath() );
  57. } catch(IOException err) {
  58. System.out.println("Song could not be copied to folder: " + err);
  59. }
  60. }
  61. else
  62. continue;
  63. }
  64. }
  65. /* Delete any songs not found in the Rhythmbox playlist from the SanDisk
  66. playlist folder */
  67. public void deleteExtraSongsFromFolder() {
  68. for (String filename : sandiskPlaylistSongFilenames) {
  69. // If the song is not in the playlist, delete it from the filesystem
  70. if ( !rhythmboxPlaylistSongFilenames.contains(filename) ) {
  71. System.out.println("Deleting the following song: " + filename);
  72. this.deleteSong(filename);
  73. }
  74. // If it's in the playlist, move to the next file
  75. else
  76. continue;
  77. }
  78. }
  79. }