19 package org.sleuthkit.autopsy.casemodule;
 
   21 import java.awt.event.ActionEvent;
 
   23 import java.io.IOException;
 
   24 import java.util.ArrayList;
 
   25 import java.util.Arrays;
 
   26 import java.util.Deque;
 
   27 import java.util.Iterator;
 
   28 import java.util.LinkedList;
 
   29 import java.util.List;
 
   30 import java.util.logging.Level;
 
   31 import javax.swing.JMenuItem;
 
   32 import org.apache.commons.lang.ArrayUtils;
 
   33 import org.openide.util.HelpCtx;
 
   34 import org.openide.util.NbBundle;
 
   35 import org.openide.util.actions.CallableSystemAction;
 
   36 import org.openide.util.actions.Presenter;
 
   37 import org.openide.filesystems.FileUtil;
 
   46 final class RecentCases 
extends CallableSystemAction implements Presenter.Menu {
 
   48     static final int LENGTH = 6;
 
   49     static final String NAME_PROP_KEY = 
"LBL_RecentCase_Name"; 
 
   50     static final String PATH_PROP_KEY = 
"LBL_RecentCase_Path"; 
 
   51     static final RecentCase BLANK_RECENTCASE = 
new RecentCase(
"", 
"");
 
   53     private final static RecentCases INSTANCE = 
new RecentCases();
 
   55     private Deque<RecentCase> recentCases; 
 
   63     static public RecentCases getInstance() {
 
   64         INSTANCE.refreshRecentCases();
 
   71     private RecentCases() {
 
   73         for (
int i = 0; i < LENGTH; i++) {
 
   75                 if (ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i)) == null) {
 
   76                     ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), 
"");
 
   78                 if (ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i)) == null) {
 
   79                     ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), 
"");
 
   81             } 
catch (Exception e) {
 
   87         recentCases = 
new LinkedList<RecentCase>();
 
   89         for (
int i = 0; i < LENGTH; i++) {
 
   90             final RecentCase rc = 
new RecentCase(getName(i), getPath(i));
 
   91             if (!rc.equals(BLANK_RECENTCASE)) {
 
   99     private static void validateCaseIndex(
int i) {
 
  100         if (i < 0 || i >= LENGTH) {
 
  101             throw new IllegalArgumentException(
 
  102                     NbBundle.getMessage(RecentCases.class, 
"RecentCases.exception.caseIdxOutOfRange.msg", i));
 
  106     private static String nameKey(
int i) {
 
  107         validateCaseIndex(i);
 
  108         return NAME_PROP_KEY + Integer.toString(i + 1);
 
  111     private static String pathKey(
int i) {
 
  112         validateCaseIndex(i);
 
  113         return PATH_PROP_KEY + Integer.toString(i + 1);
 
  116     private String getName(
int i) {
 
  118             return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i));
 
  119         } 
catch (Exception e) {
 
  124     private String getPath(
int i) {
 
  126             return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i));
 
  127         } 
catch (Exception e) {
 
  132     private void setName(
int i, String name) {
 
  133         ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), name);
 
  136     private void setPath(
int i, String path) {
 
  137         ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), path);
 
  140     private void setRecentCase(
int i, RecentCase rc) {
 
  168         static RecentCase createSafe(String name, String unsafePath) {
 
  178             return !(name.equals(
"") || path.equals(
"") || !
new File(path).exists());
 
  185             hash = 13 * hash + (this.name != null ? this.name.hashCode() : 0);
 
  186             hash = 13 * hash + (this.path != null ? this.path.hashCode() : 0);
 
  195             if (getClass() != obj.getClass()) {
 
  199             if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
 
  202             if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
 
  213     private void refreshRecentCases() {
 
  214         List<RecentCase> toDelete = 
new ArrayList<RecentCase>();
 
  215         for (RecentCase rc : recentCases) {
 
  220         for (RecentCase deleteMe : toDelete) {
 
  221             removeRecentCase(deleteMe.name, deleteMe.path);
 
  225     private void storeRecentCases() throws IOException {
 
  229         for (RecentCase rc : recentCases) {
 
  230             setRecentCase(i, rc);
 
  236             setRecentCase(i, BLANK_RECENTCASE);
 
  248     public JMenuItem getMenuPresenter() {
 
  249         return new UpdateRecentCases();
 
  258     public void actionPerformed(ActionEvent e) {
 
  259         UpdateRecentCases.hasRecentCase = 
false;
 
  266         } 
catch (Exception ex) {
 
  267             Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, 
"Error: Could not clear the properties file.", ex); 
 
  271     private void addRecentCase(RecentCase rc) {
 
  273         recentCases.remove(rc);
 
  276         if (recentCases.size() == LENGTH) {
 
  277             recentCases.remove();
 
  291     public void addRecentCase(String name, String unsafePath) {
 
  292         RecentCase rc = RecentCase.createSafe(name, unsafePath);
 
  296         this.getMenuPresenter().setVisible(
true); 
 
  300         } 
catch (Exception ex) {
 
  301             Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, 
"Error: Could not update the properties file.", ex); 
 
  315     public void updateRecentCase(String oldName, String oldPath, String newName, String newPath) 
throws Exception {
 
  316         RecentCase oldRc = RecentCase.createSafe(oldName, oldPath);
 
  317         RecentCase newRc = RecentCase.createSafe(newName, newPath);
 
  320         recentCases.removeAll(Arrays.asList(oldRc));
 
  322         addRecentCase(newRc);
 
  324         this.getMenuPresenter().setVisible(
true); 
 
  328         } 
catch (Exception ex) {
 
  329             Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, 
"Error: Could not update the properties file.", ex); 
 
  338     public int getTotalRecentCases() {
 
  339         return recentCases.size();
 
  349     public void removeRecentCase(String name, String path) {
 
  350         RecentCase rc = RecentCase.createSafe(name, path);
 
  353         recentCases.removeAll(Arrays.asList(rc));
 
  355         this.getMenuPresenter().setVisible(
true); 
 
  360         } 
catch (Exception ex) {
 
  361             Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, 
"Error: Could not update the properties file.", ex); 
 
  371     public String[] getRecentCaseNames() {
 
  372         String[] caseNames = 
new String[LENGTH];
 
  374         Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
 
  376         String currentCaseName = null;
 
  378             currentCaseName = Case.getCurrentCase().getName();
 
  379         } 
catch (IllegalStateException ex) {
 
  383         while (mostRecentFirst.hasNext()) {
 
  384             String name = mostRecentFirst.next().name;
 
  385             if ((currentCaseName != null && !name.equals(currentCaseName)) || currentCaseName == null) {
 
  392         while (i < caseNames.length) {
 
  398         return (String[]) ArrayUtils.subarray(caseNames, 0, LENGTH - 1);
 
  407     public String[] getRecentCasePaths() {
 
  408         String[] casePaths = 
new String[LENGTH];
 
  409         String currentCasePath = null;
 
  411             currentCasePath = Case.getCurrentCase().getCaseMetadata().getFilePath().toString();
 
  412         } 
catch (IllegalStateException ex) {
 
  416         Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
 
  418         while (mostRecentFirst.hasNext()) {
 
  419             String path = mostRecentFirst.next().path;
 
  420             if ((currentCasePath != null && !path.equals(currentCasePath)) || currentCasePath == null) {
 
  427         while (i < casePaths.length) {
 
  433         return (String[]) ArrayUtils.subarray(casePaths, 0, LENGTH - 1);
 
  440     public void performAction() {
 
  449     public String getName() {
 
  451         return NbBundle.getMessage(RecentCases.class, 
"RecentCases.getName.text");
 
  460     public HelpCtx getHelpCtx() {
 
  461         return HelpCtx.DEFAULT_HELP;
 
boolean equals(Object obj)
RecentCase(String name, String path)