package filesystem; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Calendar; import java.util.GregorianCalendar; import java.io.*; import java.nio.channels.*; /** * Utilities class to allow various commonly used file operations. * * @author ablackbu * @version 11-27-10 */ public class FSUtil { // singelton private static FSUtil instance; // true if the operation is copy private boolean isCopy; // the clipboard for copy/cut/paste private File clipBoard; private FSUtil() { isCopy = false; clipBoard = null; } /** * Public access for the singleton. * * @return the singleton object. */ public static FSUtil getInstance() { if (instance == null) { instance = new FSUtil(); } return instance; } /** * Renames the given file to the given name. * * @param file * the file which will be renamed * @param name * the new name * @return true if successful else false */ public boolean rename(File file, String name) { String pathToFile = file.getAbsolutePath(); String oldFileName = file.getName(); int endingIndex = pathToFile.length() - (oldFileName.length()); pathToFile = pathToFile.substring(0, endingIndex); // Destination directory File dir = new File(pathToFile); // Move file to new directory return file.renameTo(new File(dir, name)); } /** * Sets the current file onto the clipboard for pasting. Will copy on paste. * * @param file * file to copy */ public void copy(File file) { isCopy = true; clipBoard = file; } /** * Sets the current file onto the clipboard for pasting. Will move on paste. * * @param file * file to cut */ public void cut(File file) { isCopy = false; clipBoard = file; } /** * Moves or Copies the clipboard to the given directory. * * @param file * directory to paste into * @return true if successful else false */ public boolean paste(File file) { if (clipBoard == null) return false; String fullFileName = fileNameSetter(file); if (isCopy) { // make copy try { File f = new File(file.getPath() + "/" + fullFileName); copyFile(clipBoard, f); } catch (IOException e) { e.printStackTrace(); } return true; } else { // move return move(clipBoard, file.getAbsolutePath()); } } /** * Mutator method for the clipboard. * * @param f * file to set the clipboard to. */ public void setClipboard(File f) { clipBoard = f; } /** * Method to check if the clipboard is set. * * @return true if clipboard is null */ public boolean isClipBoardEmpty() { if (clipBoard == null) return true; return false; } /** * Copy backend which does the actual copying. * * @param in * input file * @param out * output file * @throws IOException */ private void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } /** * Delete the file to the recycle bin/trash based on OS * * note currently on windows this is does not move to the recycle bin * because the recycle bin does not have a specific area on the FS. * * @param the * file to be "deleted" * @return true if successful, else false */ public boolean delete(File file) { String fileName = file.getName(); if (FSInfo.getInstance().getOS().equals("Linux")) { try { BufferedWriter out = new BufferedWriter( new FileWriter(FSInfo.getInstance().getHomeDir() .toString() + "/.local/share/Trash/info/" + fileName + ".trashinfo")); Calendar calendar = new GregorianCalendar(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); int hour = calendar.get(Calendar.HOUR); if (calendar.get(Calendar.AM_PM) == Calendar.PM) hour += 12; int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); out.write("[Trash Info]\n"); out.write("Path=" + file.getAbsolutePath() + "\n"); out.write("DeletionDate=" + year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second); out.close(); } catch (IOException e) { System.err.println("Duplicate files in trash."); e.getStackTrace(); } move(file, FSInfo.getInstance().getHomeDir().toString() + "/.local/share/Trash/files"); } else if (FSInfo.getInstance().getOS().equals("Windows")) { System.err.println("Windows"); file.delete(); // TODO send to recycle bin } else if (FSInfo.getInstance().getOS().equals("OS X")) { System.err.println("OS X"); move(file, "~/.Trash"); } else { return false; } return true; } /** * Moves the given file to the given path. * * @param file * the file to move * @param path * the path to move the file to * @return true if successful, else false */ public boolean move(File file, String path) { // Destination directory File dir = new File(path); // Move file to new directory return file.renameTo(new File(dir, file.getName())); } /** * * @param dir * @return */ private String fileNameSetter(File dir) { File f = new File(dir.getAbsolutePath() + "/" + clipBoard.getName()); try { if(f.createNewFile()) return clipBoard.getName(); } catch (IOException e) { e.printStackTrace(); } int dotPos = clipBoard.getName().lastIndexOf('.'); String extension = clipBoard.getName().substring(dotPos); String name = clipBoard.getName().substring(0, dotPos); String cpString = " (copy)"; String fullFileName = name + cpString + extension; return fileNameSetter(dir, fullFileName); } /** * * @param dir * @return the fileName */ private String fileNameSetter(File dir, String fileName) { File f = new File(dir.getAbsolutePath() + "/" + fileName); try { if(f.createNewFile()) return f.getName(); } catch (IOException e) { e.printStackTrace(); } int dotPos = fileName.lastIndexOf('.'); String extension = fileName.substring(dotPos); String name = fileName.substring(0, dotPos); int counter = 2; String fullFileName = name + counter + extension; return fileNameSetter(dir, fullFileName, counter); } /** * * @param dir * @param fileName * @param counter * @return */ private String fileNameSetter(File dir, String fileName, int counter) { counter++; File f = new File(dir.getAbsolutePath() + "/" + fileName); try { if(f.createNewFile()) return f.getName(); } catch (IOException e) { e.printStackTrace(); } int dotPos = fileName.lastIndexOf('.'); String extension = fileName.substring(dotPos); String name = fileName.substring(0, dotPos-1); String fullFileName = name + counter + extension; return fileNameSetter(dir, fullFileName, counter); } }