Autopsy  4.18.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-2018 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.filesystems.FileUtil;
34 import org.openide.util.HelpCtx;
35 import org.openide.util.NbBundle;
36 import org.openide.util.actions.CallableSystemAction;
37 import org.openide.util.actions.Presenter;
40 
46 final class RecentCases extends CallableSystemAction implements Presenter.Menu {
47 
48  private static final long serialVersionUID = 1L;
49  private static final int LENGTH = 6;
50  private static final String NAME_PROP_KEY = "LBL_RecentCase_Name"; //NON-NLS
51  private static final String PATH_PROP_KEY = "LBL_RecentCase_Path"; //NON-NLS
52  private static final RecentCase BLANK_RECENTCASE = new RecentCase("", "");
53  private final static RecentCases instance = new RecentCases();
54  private final Deque<RecentCase> recentCases; // newest case is last case
55 
62  static public RecentCases getInstance() {
63  instance.refreshRecentCases();
64  return instance;
65  }
66 
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  }
77  if (ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i)) == null) {
78  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), "");
79  }
80  } catch (Exception e) {
81 
82  }
83  }
84 
85  // Load recentCases from properties
86  recentCases = new LinkedList<>();
87 
88  for (int i = 0; i < LENGTH; i++) {
89  final RecentCase rc = new RecentCase(getName(i), getPath(i));
90  if (!rc.equals(BLANK_RECENTCASE)) {
91  recentCases.add(rc);
92  }
93  }
94 
95  refreshRecentCases();
96  }
97 
98  private static void validateCaseIndex(int i) {
99  if (i < 0 || i >= LENGTH) {
100  throw new IllegalArgumentException(
101  NbBundle.getMessage(RecentCases.class, "RecentCases.exception.caseIdxOutOfRange.msg", i));
102  }
103  }
104 
105  private static String nameKey(int i) {
106  validateCaseIndex(i);
107  return NAME_PROP_KEY + Integer.toString(i + 1);
108  }
109 
110  private static String pathKey(int i) {
111  validateCaseIndex(i);
112  return PATH_PROP_KEY + Integer.toString(i + 1);
113  }
114 
115  private String getName(int i) {
116  try {
117  return ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i));
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  } catch (Exception e) {
127  return null;
128  }
129  }
130 
131  private void setName(int i, String name) {
132  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, nameKey(i), name);
133  }
134 
135  private void setPath(int i, String path) {
136  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, pathKey(i), path);
137  }
138 
139  private void setRecentCase(int i, RecentCase rc) {
140  setName(i, rc.name);
141  setPath(i, rc.path);
142  }
143 
144  private static final class RecentCase {
145 
146  String name, path;
147 
153  private RecentCase(String name, String path) {
154  this.name = name;
155  this.path = path;
156  }
157 
167  static RecentCase createSafe(String name, String unsafePath) {
168  return new RecentCase(name, FileUtil.normalizePath(unsafePath));
169  }
170 
176  boolean exists() {
177  return !(name.isEmpty() || path.isEmpty() || !new File(path).exists());
178  }
179 
180  // netbeans autogenerated hashCode
181  @Override
182  public int hashCode() {
183  int hash = 7;
184  hash = 13 * hash + (this.name != null ? this.name.hashCode() : 0);
185  hash = 13 * hash + (this.path != null ? this.path.hashCode() : 0);
186  return hash;
187  }
188 
189  @Override
190  public boolean equals(Object obj) {
191  if (obj == null) {
192  return false;
193  }
194  if (getClass() != obj.getClass()) {
195  return false;
196  }
197  final RecentCase other = (RecentCase) obj;
198  if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
199  return false;
200  }
201  if ((this.path == null) ? (other.path != null) : !this.path.equals(other.path)) {
202  return false;
203  }
204  return true;
205  }
206  }
207 
212  private void refreshRecentCases() {
213  List<RecentCase> toDelete = new ArrayList<>();
214  for (RecentCase rc : recentCases) {
215  if (!rc.exists()) {
216  toDelete.add(rc);
217  }
218  }
219  for (RecentCase deleteMe : toDelete) {
220  removeRecentCase(deleteMe.name, deleteMe.path);
221  }
222  }
223 
224  private void storeRecentCases() throws IOException {
225  int i = 0;
226 
227  // store however many recent cases exist
228  for (RecentCase rc : recentCases) {
229  setRecentCase(i, rc);
230  i++;
231  }
232 
233  // set the rest to blanks
234  while (i < LENGTH) {
235  setRecentCase(i, BLANK_RECENTCASE);
236  i++;
237  }
238 
239  }
240 
246  @Override
247  public JMenuItem getMenuPresenter() {
248  return new UpdateRecentCases();
249  }
250 
256  @Override
257  public void actionPerformed(ActionEvent e) {
258  UpdateRecentCases.setHasRecentCase(false);
259 
260  recentCases.clear();
261 
262  try {
263  // clear the properties file
264  storeRecentCases();
265  } catch (IOException ex) {
266  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not clear the properties file.", ex); //NON-NLS
267  }
268  }
269 
270  private void addRecentCase(RecentCase rc) {
271  // remove the case if it's already in the list
272  recentCases.remove(rc);
273 
274  // make space if it's needed
275  if (recentCases.size() == LENGTH) {
276  recentCases.remove();
277  }
278 
279  recentCases.add(rc);
280  }
281 
290  public void addRecentCase(String name, String unsafePath) {
291  RecentCase rc = RecentCase.createSafe(name, unsafePath);
292 
293  addRecentCase(rc);
294 
295  this.getMenuPresenter().setVisible(true); // invoke the contructor again
296 
297  try {
298  storeRecentCases();
299  } catch (IOException ex) {
300  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
301  }
302  }
303 
314  public void updateRecentCase(String oldName, String oldPath, String newName, String newPath) throws Exception {
315  RecentCase oldRc = RecentCase.createSafe(oldName, oldPath);
316  RecentCase newRc = RecentCase.createSafe(newName, newPath);
317 
318  // remove all instances of the old recent case
319  recentCases.removeAll(Arrays.asList(oldRc));
320 
321  addRecentCase(newRc);
322 
323  this.getMenuPresenter().setVisible(true); // invoke the contructor again
324 
325  try {
326  storeRecentCases();
327  } catch (IOException ex) {
328  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
329  }
330  }
331 
337  public int getTotalRecentCases() {
338  return recentCases.size();
339  }
340 
348  public void removeRecentCase(String name, String path) {
349  RecentCase rc = RecentCase.createSafe(name, path);
350 
351  // remove all instances of the old recent case
352  recentCases.removeAll(Arrays.asList(rc));
353 
354  this.getMenuPresenter().setVisible(true); // invoke the contructor again
355 
356  // write the properties file
357  try {
358  storeRecentCases();
359  } catch (IOException ex) {
360  Logger.getLogger(RecentCases.class.getName()).log(Level.WARNING, "Error: Could not update the properties file.", ex); //NON-NLS
361  }
362  }
363 
370  public String[] getRecentCaseNames() {
371  String[] caseNames = new String[LENGTH];
372 
373  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
374  int i = 0;
375  String currentCaseName = null;
376  try {
377  currentCaseName = Case.getCurrentCaseThrows().getDisplayName();
378  } catch (NoCurrentCaseException ex) {
379  // in case there is no current case.
380  }
381 
382  while (mostRecentFirst.hasNext()) {
383  String name = mostRecentFirst.next().name;
384  if ((currentCaseName != null && !name.equals(currentCaseName)) || currentCaseName == null) {
385  // exclude currentCaseName from the caseNames[]
386  caseNames[i] = name;
387  i++;
388  }
389  }
390 
391  while (i < caseNames.length) {
392  caseNames[i] = "";
393  i++;
394  }
395 
396  // return last 5 case names
397  return (String[]) ArrayUtils.subarray(caseNames, 0, LENGTH - 1);
398  }
399 
406  public String[] getRecentCasePaths() {
407  String[] casePaths = new String[LENGTH];
408  String currentCasePath = null;
409  try {
410  currentCasePath = Case.getCurrentCaseThrows().getMetadata().getFilePath().toString();
411  } catch (NoCurrentCaseException ex) {
412  /*
413  * There may be no current case.
414  */
415  }
416 
417  Iterator<RecentCase> mostRecentFirst = recentCases.descendingIterator();
418  int i = 0;
419  while (mostRecentFirst.hasNext()) {
420  String path = mostRecentFirst.next().path;
421  if ((currentCasePath != null && !path.equals(currentCasePath)) || currentCasePath == null) {
422  // exclude currentCasePath from the casePaths[]
423  casePaths[i] = path;
424  i++;
425  }
426  }
427 
428  while (i < casePaths.length) {
429  casePaths[i] = "";
430  i++;
431  }
432 
433  // return last 5 case paths
434  return (String[]) ArrayUtils.subarray(casePaths, 0, LENGTH - 1);
435  }
436 
440  @Override
441  public void performAction() {
442  }
443 
449  @Override
450  public String getName() {
451  //return NbBundle.getMessage(RecentCases.class, "CTL_RecentCases");
452  return NbBundle.getMessage(RecentCases.class, "RecentCases.getName.text");
453  }
454 
460  @Override
461  public HelpCtx getHelpCtx() {
462  return HelpCtx.DEFAULT_HELP;
463  }
464 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.