/**
* ExclusionsList.java
*
* Copyright (c) 2009 Brian Gibowski <brian@brgib.com>. All rights reserved.
*
* This file is part of iTunesDSM.
*
* iTunesDSM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTunesDSM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with iTunesDSM. If not, see <http://www.gnu.org/licenses/>.
*/
package data;
import display.Display;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This class maintains a list of files that have already been added to iTunes
* that are outside of the iTunes Media Folder. Keeping a list of exclusions
* is necessary as programmatically adding tracks to iTunes from outside the music
* folder that already exist in the iTunes Media folder will result in
* duplicates being created in the iTunes media folder.
* @author Brian Gibowski brian@brgib.com
*/
public class OutputStringArrayList extends ArrayList<String>{
/**
* The name and location of the Exclusions file.
*/
public static final String EXCLUSIONS_FILE_NAME = Data.DATA_FOLDER_PATH + "exclusions.dat";
/**
* The name and location of the all new tracks added to iTunes file.
*/
public static final String ADDED_TRACKS_FILENAME = Data.DATA_FOLDER_PATH + "added_tracks.txt";
private String fileName;
private Logger log = Logger.getLogger("global");
/**
* Creates a new ExclusionsList object.
* @param fileName The file to output the array list to.
*/
public OutputStringArrayList(String fileName){
this.fileName = fileName;
}
/**
* Writes the list of exclusions to a data file for later reference when
* tracks are added to iTunes programmatically.
*
* @param fileName The name of the file to output the array list to.
*/
public void write(String fileName){
checkFileExists(fileName);
ArrayList<String> old = read(fileName);
for(String a : old){
if(!contains(a)){
this.add(a);
}
}
PrintWriter printWriter = null;
FileWriter fw = null;
try{
fw = new FileWriter(fileName);
printWriter = new PrintWriter(fw, true);
for(String a : this){
printWriter.println(a);
printWriter.flush();
}
}
catch(IOException e){
log.log(Level.SEVERE, this.getClass().getName(), e);
}
finally{
if(fw != null){
try{
fw.close();
}
catch(IOException e){
log.log(Level.SEVERE, this.getClass().getName(), e);
}
}
if(printWriter != null){
printWriter.close();
}
}
}
/**
* Reads a list of exclusions from the exclusions data file.
*
* @param fileName The name of the file to read the array list from.
*
* @return the ExclusionsList containing strings of file locations of Tracks
* that were previously added to iTunes.
*
*
*/
public static OutputStringArrayList read(String fileName){
try{
checkFileExists(fileName);
FileReader reader = new FileReader(new File(fileName));
Scanner in = new Scanner(reader);
OutputStringArrayList list = new OutputStringArrayList(fileName);
while(in.hasNextLine()){
list.add(in.nextLine());
}
in.close();
reader.close();
return list;
}
catch(FileNotFoundException ignored){
//log.log(Level.SEVERE, this.getClass().getName(), e);
}
catch(IOException ignored){
}
return null;
}
/**
* Simply checks that the exclusions file exists. If it does not
* a new exclusions data file is created.
*/
private static void checkFileExists(String fileName){
try{
boolean folderSuccess = Data.checkDataFolderExists();
if(!new File(fileName).exists()){
File file = new File(fileName);
boolean fileSuccess = file.createNewFile();
if(!folderSuccess || !fileSuccess){
Display.newWarningMessage("Unable to create exclusions file",
"Sorry, iTunesDSM was unable to create the exclusions file in the" +
" directory " + file.getPath() + ".\n\nUnfornately," +
" iTunesDSM will not be able to record tracks you've added to iTunes" +
"from outside the iTunes music directory. " +
"If you are not sure why this occurred, contact the developer at " +
Display.DEVELOPER_EMAIL);
}
}
}
catch(SecurityException e){
//log.log(Level.SEVERE, this.getClass().getName(), e);
}
catch(IOException e){
//log.log(Level.SEVERE, this.getClass().getName(), e);
}
}
}
|