Autopsy  4.21.0
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 = false;
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;
87  if (Case.isCaseOpen()) {
89  }
90  }
91 
95  private static void loadFromStorage(Case currentCase) {
96  Path settingsFile = Paths.get(currentCase.getConfigDirectory(), SETTINGS_FILE); //NON-NLS
97  if (settingsFile.toFile().exists()) {
98  // Read the settings
99  try (InputStream inputStream = Files.newInputStream(settingsFile)) {
100  Properties props = new Properties();
101  props.load(inputStream);
102  String groupByDataSourceValue = props.getProperty(KEY_GROUP_BY_DATA_SOURCE);
103  if (groupByDataSourceValue != null) {
104  switch (groupByDataSourceValue) {
105  case VALUE_TRUE:
106  groupItemsInTreeByDataSource = true;
107  break;
108  case VALUE_FALSE:
109  groupItemsInTreeByDataSource = false;
110  break;
111  default:
112  logger.log(Level.WARNING, String.format("Unexpected value '%s' for key '%s'. Using 'null' instead.",
113  groupByDataSourceValue, KEY_GROUP_BY_DATA_SOURCE));
114  groupItemsInTreeByDataSource = false;
115  break;
116  }
117  } else {
118  groupItemsInTreeByDataSource = false;
119  }
120  } catch (IOException ex) {
121  logger.log(Level.SEVERE, "Error reading settings file", ex);
122  }
123  }
124  }
125 
129  private static void clear() {
130  groupItemsInTreeByDataSource = false;
131  }
132 
136  private static void saveToStorage(Case currentCase) {
137  Path settingsFile = Paths.get(currentCase.getConfigDirectory(), SETTINGS_FILE); //NON-NLS
138  Properties props = new Properties();
139  if (groupItemsInTreeByDataSource != null) {
140  props.setProperty(KEY_GROUP_BY_DATA_SOURCE, (groupItemsInTreeByDataSource ? VALUE_TRUE : VALUE_FALSE));
141  }
142 
143  try (OutputStream fos = Files.newOutputStream(settingsFile)) {
144  props.store(fos, ""); //NON-NLS
145  } catch (IOException ex) {
146  logger.log(Level.SEVERE, "Error writing settings file", ex);
147  }
148  }
149 }
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:708

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.