Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
NewCaseWizardPanel1.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 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.io.File;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import javax.swing.event.ChangeEvent;
27 import javax.swing.event.ChangeListener;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31 import org.openide.WizardDescriptor;
32 import org.openide.WizardValidationException;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
39 
43 class NewCaseWizardPanel1 implements WizardDescriptor.ValidatingPanel<WizardDescriptor> {
44 
45  private static final Logger logger = Logger.getLogger(NewCaseWizardPanel1.class.getName());
46  private static final String PROP_BASECASE = "LBL_BaseCase_PATH"; //NON-NLS
47  private static String createdDirectory;
48  private final Set<ChangeListener> listeners = new HashSet<>(1);
49  private NewCaseVisualPanel1 component;
50  private boolean isFinish;
51 
57  @Override
58  public NewCaseVisualPanel1 getComponent() {
59  if (component == null) {
60  component = new NewCaseVisualPanel1(this);
61  }
62  return component;
63  }
64 
71  @Override
72  public HelpCtx getHelp() {
73  /*
74  * Currently, no help is provided for this panel.
75  */
76  return HelpCtx.DEFAULT_HELP;
77  }
78 
86  @Override
87  public boolean isValid() {
88  return isFinish;
89  }
90 
96  @Override
97  public final void addChangeListener(ChangeListener listener) {
98  synchronized (listeners) {
99  listeners.add(listener);
100  }
101  }
102 
108  @Override
109  public final void removeChangeListener(ChangeListener listener) {
110  synchronized (listeners) {
111  listeners.remove(listener);
112  }
113  }
114 
118  protected final void fireChangeEvent() {
119  Iterator<ChangeListener> it;
120  synchronized (listeners) {
121  it = new HashSet<>(listeners).iterator();
122  }
123  ChangeEvent ev = new ChangeEvent(this);
124  while (it.hasNext()) {
125  it.next().stateChanged(ev);
126  }
127  }
128 
135  public void setIsFinish(Boolean isFinish) {
136  this.isFinish = isFinish;
137  fireChangeEvent();
138  }
139 
148  @Override
149  public void readSettings(WizardDescriptor settings) {
150  NewCaseVisualPanel1 panel = getComponent();
151  try {
152  String lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE);
153  panel.setCaseParentDir(lastBaseDirectory);
154  panel.readSettings();
155  createdDirectory = (String) settings.getProperty("createdDirectory"); //NON-NLS
156  if (createdDirectory != null && !createdDirectory.isEmpty()) {
157  logger.log(Level.INFO, "Deleting a case dir in readSettings(): {0}", createdDirectory); //NON-NLS
158  FileUtil.deleteDir(new File(createdDirectory));
159  }
160  } catch (Exception e) {
161  logger.log(Level.WARNING, "Could not read wizard settings in NewCaseWizardPanel1, ", e); //NON-NLS
162  }
163  }
164 
174  @Override
175  public void storeSettings(WizardDescriptor settings) {
176  CaseType caseType = getComponent().getCaseType();
177  settings.putProperty("caseName", getComponent().getCaseName()); //NON-NLS
178  settings.putProperty("caseParentDir", getComponent().getCaseParentDir()); //NON-NLS
179  settings.putProperty("createdDirectory", createdDirectory); //NON-NLS
180  settings.putProperty("caseType", caseType.ordinal()); //NON-NLS
181  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, ModuleSettings.CURRENT_CASE_TYPE, caseType.toString());
182  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE, getComponent().getCaseParentDir());
183  }
184 
185  @Override
186  public void validate() throws WizardValidationException {
187  /*
188  * Check whether or not the case name is valid. To be valid, the case
189  * name must not contain any characters that are not allowed in file
190  * names, since it will be used as the name of the case directory.
191  */
192  String caseName = getComponent().getCaseName();
193  if (!Case.isValidName(caseName)) {
194  String errorMsg = NbBundle
195  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidSymbols");
196  validationError(errorMsg);
197  } else {
198 
199  String caseParentDir = getComponent().getCaseParentDir();
200  String caseDirPath = caseParentDir + caseName;
201 
202  // check if the directory exist
203  if (new File(caseDirPath).exists()) {
204  // throw a warning to enter new data or delete the existing directory
205  String errorMsg = NbBundle
206  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.dirExists", caseDirPath);
207  validationError(errorMsg);
208  } else {
209  // check if the "base" directory path is absolute
210  File baseDir = new File(caseParentDir);
211  if (baseDir.isAbsolute()) {
212  // when the base directory doesn't exist
213  if (!baseDir.exists()) {
214  // get confirmation to create directory
215  String confMsg = NbBundle
216  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.confMsg.createDir.msg",
217  caseParentDir);
218  NotifyDescriptor d2 = new NotifyDescriptor.Confirmation(confMsg,
219  NbBundle.getMessage(this.getClass(),
220  "NewCaseWizardPanel1.validate.confMsg.createDir.title"),
221  NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
222  d2.setValue(NotifyDescriptor.NO_OPTION);
223 
224  Object res2 = DialogDisplayer.getDefault().notify(d2);
225  if (res2 != null && res2 == DialogDescriptor.YES_OPTION) {
226  // if user says yes
227  try {
228  createDirectory(caseDirPath, getComponent().getCaseType());
229  } catch (WizardValidationException ex) {
230  String errorMsg = NbBundle.getMessage(this.getClass(),
231  "NewCaseWizardPanel1.validate.errMsg.cantCreateParDir.msg",
232  caseParentDir);
233  logger.log(Level.WARNING, errorMsg, ex);
234  validationError(errorMsg);
235  }
236  }
237  if (res2 != null && res2 == DialogDescriptor.NO_OPTION) {
238  // if user says no
239  validationError(NbBundle.getMessage(this.getClass(),
240  "NewCaseWizardPanel1.validate.errMsg.prevCreateBaseDir.msg",
241  caseDirPath));
242  }
243  } else {
244  try {
245  createDirectory(caseDirPath, getComponent().getCaseType());
246  } catch (WizardValidationException ex) {
247  String errorMsg = NbBundle
248  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.cantCreateDir");
249  logger.log(Level.WARNING, errorMsg, ex);
250  validationError(errorMsg);
251  }
252  }
253  } else {
254  // throw a notification
255  String errorMsg = NbBundle
256  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidBaseDir.msg");
257  validationError(errorMsg);
258  }
259  }
260  }
261  }
262 
263  private void validationError(String errorMsg) throws WizardValidationException {
264  throw new WizardValidationException(this.getComponent(), errorMsg, null);
265  }
266 
267  /*
268  * create the directory and create a new case
269  */
270  private void createDirectory(final String caseDirPath, CaseType caseType) throws WizardValidationException {
271  // try to create the directory with the case name in the chosen parent directory
272  boolean success = false;
273  try {
274  Case.createCaseDirectory(caseDirPath, caseType);
275  success = true;
276  } catch (CaseActionException ex) {
277  logger.log(Level.SEVERE, "Could not createDirectory for the case, ", ex); //NON-NLS
278  }
279 
280  // check if the directory is successfully created
281  if (!success) {
282 
283  // delete the folder if we already created the folder and the error shows up
284  if (new File(caseDirPath).exists()) {
285  FileUtil.deleteDir(new File(caseDirPath));
286  }
287 
288  String errorMsg = NbBundle.getMessage(this.getClass(),
289  "NewCaseWizardPanel1.createDir.errMsg.cantCreateDir.msg");
290 
291  validationError(errorMsg);
292 
293  } // the new case directory is successfully created
294  else {
295  createdDirectory = caseDirPath;
296  // try to close Startup window if there's one
297  try {
298  StartupWindowProvider.getInstance().close();
299  } catch (Exception ex) {
300  logger.log(Level.WARNING, "Startup window didn't close as expected.", ex); //NON-NLS
301 
302  }
303  }
304  }
305 
306 }

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