Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
CasePreferences.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.beans.PropertyChangeEvent;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.util.EnumSet;
29 import java.util.Properties;
30 import java.util.logging.Level;
33 
37 public final class CasePreferences {
38 
39  private static final String SETTINGS_FILE = "CasePreferences.properties"; //NON-NLS
40  private static final String KEY_GROUP_BY_DATA_SOURCE = "groupByDataSource"; //NON-NLS
41  private static final String VALUE_TRUE = "true"; //NON-NLS
42  private static final String VALUE_FALSE = "false"; //NON-NLS
43 
44  private static final Logger logger = Logger.getLogger(CasePreferences.class.getName());
45 
46  private static Boolean groupItemsInTreeByDataSource = null;
47 
51  private CasePreferences() {
52  }
53 
54  static {
55  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), (PropertyChangeEvent evt) -> {
56  if (evt.getNewValue() != null) {
57  loadFromStorage((Case) evt.getNewValue());
58  } else {
59  saveToStorage((Case) evt.getOldValue());
60  clear();
61  }
62  });
63  try {
64  loadFromStorage(Case.getCurrentCaseThrows());
65  } catch (NoCurrentCaseException ex) {
66  logger.log(Level.SEVERE, "No current case open.", ex);
67  }
68  }
69 
76  public static Boolean getGroupItemsInTreeByDataSource() {
78  }
79 
85  public static void setGroupItemsInTreeByDataSource(boolean value) {
86  groupItemsInTreeByDataSource = value;
88  }
89 
93  private static void loadFromStorage(Case currentCase) {
94  Path settingsFile = Paths.get(currentCase.getConfigDirectory(), SETTINGS_FILE); //NON-NLS
95  if (settingsFile.toFile().exists()) {
96  // Read the settings
97  try (InputStream inputStream = Files.newInputStream(settingsFile)) {
98  Properties props = new Properties();
99  props.load(inputStream);
100  String groupByDataSourceValue = props.getProperty(KEY_GROUP_BY_DATA_SOURCE);
101  if (groupByDataSourceValue != null) {
102  switch (groupByDataSourceValue) {
103  case VALUE_TRUE:
104  groupItemsInTreeByDataSource = true;
105  break;
106  case VALUE_FALSE:
107  groupItemsInTreeByDataSource = false;
108  break;
109  default:
110  logger.log(Level.WARNING, String.format("Unexpected value '%s' for key '%s'. Using 'null' instead.",
111  groupByDataSourceValue, KEY_GROUP_BY_DATA_SOURCE));
112  groupItemsInTreeByDataSource = null;
113  break;
114  }
115  } else {
116  groupItemsInTreeByDataSource = null;
117  }
118  } catch (IOException ex) {
119  logger.log(Level.SEVERE, "Error reading settings file", ex);
120  }
121  }
122  }
123 
127  private static void clear() {
128  groupItemsInTreeByDataSource = null;
129  }
130 
134  private static void saveToStorage(Case currentCase) {
135  Path settingsFile = Paths.get(currentCase.getConfigDirectory(), SETTINGS_FILE); //NON-NLS
136  Properties props = new Properties();
137  if (groupItemsInTreeByDataSource != null) {
138  props.setProperty(KEY_GROUP_BY_DATA_SOURCE, (groupItemsInTreeByDataSource ? VALUE_TRUE : VALUE_FALSE));
139  }
140 
141  try (OutputStream fos = Files.newOutputStream(settingsFile)) {
142  props.store(fos, ""); //NON-NLS
143  } catch (IOException ex) {
144  logger.log(Level.SEVERE, "Error writing settings file", ex);
145  }
146  }
147 }
static void setGroupItemsInTreeByDataSource(boolean value)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static void addEventTypeSubscriber(Set< Events > eventTypes, PropertyChangeListener subscriber)
Definition: Case.java:429

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.