Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RecentCases.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.casemodule;
20 
21 import java.awt.event.ActionEvent;
22 import java.io.File;
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;
40 
46 final class RecentCases extends CallableSystemAction implements Presenter.Menu {
47 
48  static final int LENGTH = 6;
49  static final String NAME_PROP_KEY = "LBL_RecentCase_Name"; //NON-NLS
50  static final String PATH_PROP_KEY = "LBL_RecentCase_Path"; //NON-NLS
51  static final RecentCase BLANK_RECENTCASE = new RecentCase("", "");
52 
53  private final static RecentCases INSTANCE = new RecentCases();
54 
55  private Deque<RecentCase> recentCases; // newest case is last case
56 
63  static public RecentCases getInstance() {
64  INSTANCE.refreshRecentCases();
65  return INSTANCE;
66  }
67 
71  private RecentCases() {
72 
73  for (int i = 0; i < LENGTH; i++) {
74  try {
75  if (ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i)) == null) {
76  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), "");
77  }
78  if (ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i)) == null) {
79  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), "");
80  }
81  } catch (Exception e) {
82 
83  }
84  }
85 
86  // Load recentCases from properties
87  recentCases = new LinkedList<RecentCase>();
88 
89  for (int i = 0; i < LENGTH; i++) {
90  final RecentCase rc = new RecentCase(getName(i), getPath(i));
91  if (!rc.equals(BLANK_RECENTCASE)) {
92  recentCases.add(rc);
93  }
94  }
95 
96  refreshRecentCases();
97  }
98 
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));
103  }
104  }
105 
106  private static String nameKey(int i) {
107  validateCaseIndex(i);
108  return NAME_PROP_KEY + Integer.toString(i + 1);
109  }
110 
111  private static String pathKey(int i) {
112  validateCaseIndex(i);
113  return PATH_PROP_KEY + Integer.toString(i + 1);
114  }
115 
116  private String getName(int i) {
117  try {
118  return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i));
119  } catch (Exception e) {
120  return null;
121  }
122  }
123 
124  private String getPath(int i) {
125  try {
126  return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i));
127  } catch (Exception e) {
128  return null;
129  }
130  }
131 
132  private void setName(int i, String name) {
133  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), name);
134  }
135 
136  private void setPath(int i, String path) {
137  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), path);
138  }
139 
140  private void setRecentCase(int i, RecentCase rc) {
141  setName(i, rc.name);
142  setPath(i, rc.path);
143  }
144 
145  private static final class RecentCase {
146 
147  String name, path;
148 
154  private RecentCase(String name, String path) {
155  this.name = name;
156  this.path = path;
157  }
158 
168  static RecentCase createSafe(String name, String unsafePath) {
169  return new RecentCase(name, FileUtil.normalizePath(unsafePath));
170  }
171 
177  boolean exists() {
178  return !(name.equals("") || path.equals("") || !new File(path).exists());
179  }
180 
181  // netbeans autogenerated hashCode
182  @Override
183  public int hashCode() {
184  int hash = 7;
185  hash = 13 * hash + (this.name != null ? this.name.hashCode() : 0);
186  hash = 13 * hash + (this.path != null ? this.path.hashCode() : 0);
187  return hash;
188  }
189 
190  @Override
191  public boolean equals(Object obj) {
192  if (obj == null) {
193  return false;
194  }
195  if (getClass() != obj.getClass()) {
196  return false;
197  }
198  final RecentCase other = (RecentCase) obj;
199  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
200  return false;
201  }
202  if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
203  return false;
204  }
205  return true;
206  }
207  }
208 
213  private void refreshRecentCases() {
214  List<RecentCase> toDelete = new ArrayList<RecentCase>();
215  for (RecentCase rc : recentCases) {
216  if (!rc.exists()) {
217  toDelete.add(rc);
218  }
219  }
220  for (RecentCase deleteMe : toDelete) {
221  removeRecentCase(deleteMe.name, deleteMe.path);
222  }
223  }
224 
225  private void storeRecentCases() throws IOException {
226  int i = 0;
227 
228  // store however many recent cases exist
229  for (RecentCase rc : recentCases) {
230  setRecentCase(i, rc);
231  i++;
232  }
233 
234  // set the rest to blanks
235  while (i < LENGTH) {
236  setRecentCase(i, BLANK_RECENTCASE);
237  i++;
238  }
239 
240  }
241 
247  @Override
248  public JMenuItem getMenuPresenter() {
249  return new UpdateRecentCases();
250  }
251 
257  @Override
258  public void actionPerformed(ActionEvent e) {
259  UpdateRecentCases.hasRecentCase = false;
260 
261  recentCases.clear();
262 
263  try {
264  // clear the properties file
265  storeRecentCases();
266  } catch (Exception ex) {
267  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not clear the properties file.", ex); //NON-NLS
268  }
269  }
270 
271  private void addRecentCase(RecentCase rc) {
272  // remove the case if it's already in the list
273  recentCases.remove(rc);
274 
275  // make space if it's needed
276  if (recentCases.size() == LENGTH) {
277  recentCases.remove();
278  }
279 
280  recentCases.add(rc);
281  }
282 
291  public void addRecentCase(String name, String unsafePath) {
292  RecentCase rc = RecentCase.createSafe(name, unsafePath);
293 
294  addRecentCase(rc);
295 
296  this.getMenuPresenter().setVisible(true); // invoke the contructor again
297 
298  try {
299  storeRecentCases();
300  } catch (Exception ex) {
301  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
302  }
303  }
304 
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);
318 
319  // remove all instances of the old recent case
320  recentCases.removeAll(Arrays.asList(oldRc));
321 
322  addRecentCase(newRc);
323 
324  this.getMenuPresenter().setVisible(true); // invoke the contructor again
325 
326  try {
327  storeRecentCases();
328  } catch (Exception ex) {
329  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
330  }
331  }
332 
338  public int getTotalRecentCases() {
339  return recentCases.size();
340  }
341 
349  public void removeRecentCase(String name, String path) {
350  RecentCase rc = RecentCase.createSafe(name, path);
351 
352  // remove all instances of the old recent case
353  recentCases.removeAll(Arrays.asList(rc));
354 
355  this.getMenuPresenter().setVisible(true); // invoke the contructor again
356 
357  // write the properties file
358  try {
359  storeRecentCases();
360  } catch (Exception ex) {
361  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
362  }
363  }
364 
371  public String[] getRecentCaseNames() {
372  String[] caseNames = new String[LENGTH];
373 
374  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
375  int i = 0;
376  String currentCaseName = null;
377  try {
378  currentCaseName = Case.getCurrentCase().getName();
379  } catch (IllegalStateException ex) {
380  // in case there is no current case.
381  }
382 
383  while (mostRecentFirst.hasNext()) {
384  String name = mostRecentFirst.next().name;
385  if ((currentCaseName != null && !name.equals(currentCaseName)) || currentCaseName == null) {
386  // exclude currentCaseName from the caseNames[]
387  caseNames[i] = name;
388  i++;
389  }
390  }
391 
392  while (i < caseNames.length) {
393  caseNames[i] = "";
394  i++;
395  }
396 
397  // return last 5 case names
398  return (String[]) ArrayUtils.subarray(caseNames, 0, LENGTH - 1);
399  }
400 
407  public String[] getRecentCasePaths() {
408  String[] casePaths = new String[LENGTH];
409  String currentCasePath = null;
410  try {
411  currentCasePath = Case.getCurrentCase().getConfigFilePath();
412  } catch (IllegalStateException ex) {
413  // in case there is no current case.
414  }
415 
416  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
417  int i = 0;
418  while (mostRecentFirst.hasNext()) {
419  String path = mostRecentFirst.next().path;
420  if ((currentCasePath != null && !path.equals(currentCasePath)) || currentCasePath == null) {
421  // exclude currentCasePath from the casePaths[]
422  casePaths[i] = path;
423  i++;
424  }
425  }
426 
427  while (i < casePaths.length) {
428  casePaths[i] = "";
429  i++;
430  }
431 
432  // return last 5 case paths
433  return (String[]) ArrayUtils.subarray(casePaths, 0, LENGTH - 1);
434  }
435 
439  @Override
440  public void performAction() {
441  }
442 
448  @Override
449  public String getName() {
450  //return NbBundle.getMessage(RecentCases.class, "CTL_RecentCases");
451  return NbBundle.getMessage(RecentCases.class, "RecentCases.getName.text");
452  }
453 
459  @Override
460  public HelpCtx getHelpCtx() {
461  return HelpCtx.DEFAULT_HELP;
462  }
463 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.