Autopsy  3.1
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 
20 package org.sleuthkit.autopsy.casemodule;
21 
22 import java.awt.event.ActionEvent;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Deque;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.logging.Level;
32 import javax.swing.JMenuItem;
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 = 5;
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 
57 
64  static public RecentCases getInstance() {
65  INSTANCE.refreshRecentCases();
66  return INSTANCE;
67  }
68 
70  private RecentCases() {
71 
72  for (int i = 0; i < LENGTH; i++) {
73  try{
74  if(ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i)) == null)
75  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), "");
76  if(ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i)) == null)
77  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), "");
78  }
79  catch(Exception e ){
80 
81  }
82  }
83 
84  // Load recentCases from properties
85  recentCases = new LinkedList<RecentCase>();
86 
87  for (int i = 0; i < LENGTH; i++) {
88  final RecentCase rc = new RecentCase(getName(i), getPath(i));
89  if (!rc.equals(BLANK_RECENTCASE)) {
90  recentCases.add(rc);
91  }
92  }
93 
94  refreshRecentCases();
95  }
96 
97  private static void validateCaseIndex(int i) {
98  if (i < 0 || i >= LENGTH) {
99  throw new IllegalArgumentException(
100  NbBundle.getMessage(RecentCases.class, "RecentCases.exception.caseIdxOutOfRange.msg", i));
101  }
102  }
103 
104  private static String nameKey(int i) {
105  validateCaseIndex(i);
106  return NAME_PROP_KEY + Integer.toString(i + 1);
107  }
108 
109  private static String pathKey(int i) {
110  validateCaseIndex(i);
111  return PATH_PROP_KEY + Integer.toString(i + 1);
112  }
113 
114  private String getName(int i) {
115  try{
116  return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i));
117  }
118  catch(Exception e){
119  return null;
120  }
121  }
122 
123  private String getPath(int i) {
124  try{
125  return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i));
126  }
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 
153  private RecentCase(String name, String path) {
154  this.name = name;
155  this.path = path;
156  }
157 
165  static RecentCase createSafe(String name, String unsafePath) {
166  return new RecentCase(name, FileUtil.normalizePath(unsafePath));
167  }
168 
173  boolean exists() {
174  return !(name.equals("") || path.equals("") || !new File(path).exists());
175  }
176 
177  // netbeans autogenerated hashCode
178  @Override
179  public int hashCode() {
180  int hash = 7;
181  hash = 13 * hash + (this.name != null ? this.name.hashCode() : 0);
182  hash = 13 * hash + (this.path != null ? this.path.hashCode() : 0);
183  return hash;
184  }
185 
186  @Override
187  public boolean equals(Object obj) {
188  if (obj == null) {
189  return false;
190  }
191  if (getClass() != obj.getClass()) {
192  return false;
193  }
194  final RecentCase other = (RecentCase) obj;
195  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
196  return false;
197  }
198  if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
199  return false;
200  }
201  return true;
202  }
203  }
204 
209  private void refreshRecentCases() {
210  List<RecentCase> toDelete = new ArrayList<RecentCase>();
211  for (RecentCase rc : recentCases) {
212  if(!rc.exists()) {
213  toDelete.add(rc);
214  }
215  }
216  for (RecentCase deleteMe : toDelete) {
217  removeRecentCase(deleteMe.name, deleteMe.path);
218  }
219  }
220 
221  private void storeRecentCases() throws IOException {
222  int i = 0;
223 
224  // store however many recent cases exist
225  for (RecentCase rc : recentCases) {
226  setRecentCase(i, rc);
227  i++;
228  }
229 
230  // set the rest to blanks
231  while (i < LENGTH) {
232  setRecentCase(i, BLANK_RECENTCASE);
233  i++;
234  }
235 
236  }
237 
243  @Override
244  public JMenuItem getMenuPresenter() {
245  return new UpdateRecentCases();
246  }
247 
253  @Override
254  public void actionPerformed(ActionEvent e) {
255  UpdateRecentCases.hasRecentCase = false;
256 
257  recentCases.clear();
258 
259  try {
260  // clear the properties file
261  storeRecentCases();
262  } catch (Exception ex) {
263  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not clear the properties file.", ex); //NON-NLS
264  }
265  }
266 
267  private void addRecentCase(RecentCase rc) {
268  // remove the case if it's already in the list
269  recentCases.remove(rc);
270 
271  // make space if it's needed
272  if (recentCases.size() == LENGTH) recentCases.remove();
273 
274  recentCases.add(rc);
275  }
276 
285  public void addRecentCase(String name, String unsafePath) {
286  RecentCase rc = RecentCase.createSafe(name, unsafePath);
287 
288  addRecentCase(rc);
289 
290  this.getMenuPresenter().setVisible(true); // invoke the contructor again
291 
292  try {
293  storeRecentCases();
294  } catch (Exception ex) {
295  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
296  }
297  }
298 
308  public void updateRecentCase(String oldName, String oldPath, String newName, String newPath) throws Exception {
309  RecentCase oldRc = RecentCase.createSafe(oldName, oldPath);
310  RecentCase newRc = RecentCase.createSafe(newName, newPath);
311 
312  // remove all instances of the old recent case
313  recentCases.removeAll(Arrays.asList(oldRc));
314 
315  addRecentCase(newRc);
316 
317  this.getMenuPresenter().setVisible(true); // invoke the contructor again
318 
319  try {
320  storeRecentCases();
321  } catch (Exception ex) {
322  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
323  }
324  }
325 
331  public int getTotalRecentCases() {
332  return recentCases.size();
333  }
334 
341  public void removeRecentCase(String name, String path) {
342  RecentCase rc = RecentCase.createSafe(name, path);
343 
344  // remove all instances of the old recent case
345  recentCases.removeAll(Arrays.asList(rc));
346 
347 
348  this.getMenuPresenter().setVisible(true); // invoke the contructor again
349 
350  // write the properties file
351  try {
352  storeRecentCases();
353  } catch (Exception ex) {
354  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
355  }
356  }
357 
364  public String[] getRecentCaseNames() {
365  String[] caseNames = new String[LENGTH];
366 
367  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
368  int i = 0;
369  while (mostRecentFirst.hasNext()) {
370  caseNames[i] = mostRecentFirst.next().name;
371  i++;
372  }
373 
374  while (i < caseNames.length) {
375  caseNames[i] = "";
376  i++;
377  }
378 
379  return caseNames;
380  }
381 
388  public String[] getRecentCasePaths() {
389  String[] casePaths = new String[LENGTH];
390 
391  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
392  int i = 0;
393  while (mostRecentFirst.hasNext()) {
394  casePaths[i] = mostRecentFirst.next().path;
395  i++;
396  }
397 
398  while (i < casePaths.length) {
399  casePaths[i] = "";
400  i++;
401  }
402 
403  return casePaths;
404  }
405 
409  @Override
410  public void performAction() {
411  }
412 
418  @Override
419  public String getName() {
420  //return NbBundle.getMessage(RecentCases.class, "CTL_RecentCases");
421  return NbBundle.getMessage(this.getClass(), "RecentCases.getName.text");
422  }
423 
429  @Override
430  public HelpCtx getHelpCtx() {
431  return HelpCtx.DEFAULT_HELP;
432  }
433 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.