Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
ResetWindowsAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy
3  *
4  * Copyright 2021 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.apputils;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.charset.Charset;
24 import java.util.logging.Level;
25 import javax.swing.SwingUtilities;
26 import org.apache.commons.io.FileUtils;
27 import org.openide.LifecycleManager;
28 import org.openide.awt.ActionID;
29 import org.openide.awt.ActionReference;
30 import org.openide.awt.ActionReferences;
31 import org.openide.awt.ActionRegistration;
32 import org.openide.util.HelpCtx;
33 import org.openide.util.NbBundle;
34 import org.openide.util.actions.CallableSystemAction;
40 
45 @ActionID(category = "Tools", id = "org.sleuthkit.autopsy.apputils.ResetWindowsAction")
46 @ActionReferences(value = {
47  @ActionReference(path = "Menu/Window", position = 205)})
48 @ActionRegistration(displayName = "#CTL_ResetWindowsAction", lazy = false)
49 @NbBundle.Messages({"CTL_ResetWindowsAction=Reset Windows"})
50 public final class ResetWindowsAction extends CallableSystemAction {
51 
52  private static final String DISPLAY_NAME = Bundle.CTL_ResetWindowsAction();
53  private static final long serialVersionUID = 1L;
54  private final static Logger logger = Logger.getLogger(ResetWindowsAction.class.getName());
55  private final static String WINDOWS2LOCAL = "Windows2Local";
56  private final static String CASE_TO_REOPEN_FILE = "caseToOpen.txt";
57 
58  @Override
59  public boolean isEnabled() {
60  return true;
61  }
62 
63  @NbBundle.Messages({"ResetWindowAction.confirm.text=In order to perform the resetting of window locations the software will close and restart. "
64  + "If a case is currently open, it will be closed. If ingest or a search is currently running, it will be terminated. "
65  + "Are you sure you want to restart the software to reset all window locations?",
66  "ResetWindowAction.caseCloseFailure.text=Unable to close the current case, "
67  + "the software will restart and the windows locations will reset the next time the software is closed.",
68  "ResetWindowAction.caseSaveMetadata.text=Unable to save current case path, "
69  + "the software will restart and the windows locations will reset but the current case will not be opened upon restart."})
70 
71  @Override
72  public void performAction() {
73  SwingUtilities.invokeLater(() -> {
74  boolean response = MessageNotifyUtil.Message.confirm(Bundle.ResetWindowAction_confirm_text());
75  if (response) {
76  //adding the shutdown hook, closing the current case, and marking for restart can be re-ordered if slightly different behavior is desired
77  Runtime.getRuntime().addShutdownHook(new Thread() {
78  @Override
79  public void run() {
80  try {
81  FileUtils.deleteDirectory(new File(PlatformUtil.getUserConfigDirectory() + File.separator + WINDOWS2LOCAL));
82  } catch (IOException ex) {
83  //While we would like the user to be aware of this in the unlikely event that the directory can not be deleted
84  //Because our deletion is being attempted in a shutdown hook I don't know that we can pop up UI elements during the shutdown proces
85  logger.log(Level.SEVERE, "Unable to delete config directory, window locations will not be reset. To manually reset the windows please delete the following directory while the software is closed. " + PlatformUtil.getUserConfigDirectory() + File.separator + "Windows2Local", ex);
86  }
87  }
88  });
89  try {
90  if (Case.isCaseOpen()) {
91  String caseMetadataFilePath = Case.getCurrentCase().getMetadata().getFilePath().toString();
92  File caseToOpenFile = new File(ResetWindowsAction.getCaseToReopenFilePath());
93  Charset encoding = null; //prevents writeStringToFile from having ambiguous arguments
94  FileUtils.writeStringToFile(caseToOpenFile, caseMetadataFilePath, encoding);
96  }
97  // The method markForRestart can not be undone once it is called.
98  LifecycleManager.getDefault().markForRestart();
99  //we need to call exit last
100  LifecycleManager.getDefault().exit();
101  } catch (CaseActionException ex) {
102  logger.log(Level.WARNING, Bundle.ResetWindowAction_caseCloseFailure_text(), ex);
103  MessageNotifyUtil.Message.show(Bundle.ResetWindowAction_caseCloseFailure_text(), MessageNotifyUtil.MessageType.ERROR);
104  } catch (IOException ex) {
105  logger.log(Level.WARNING, Bundle.ResetWindowAction_caseSaveMetadata_text(), ex);
106  MessageNotifyUtil.Message.show(Bundle.ResetWindowAction_caseSaveMetadata_text(), MessageNotifyUtil.MessageType.ERROR);
107  }
108  }
109  });
110  }
111 
112  public static String getCaseToReopenFilePath(){
113  return PlatformUtil.getUserConfigDirectory() + File.separator + CASE_TO_REOPEN_FILE;
114  }
115 
121  @Override
122 
123  public void setEnabled(boolean value) {
124  super.setEnabled(value);
125  }
126 
127  @Override
128  public String getName() {
129  return DISPLAY_NAME;
130  }
131 
132  @Override
133  public HelpCtx getHelpCtx() {
134  return HelpCtx.DEFAULT_HELP;
135  }
136 
137  @Override
138  public boolean asynchronous() {
139  return false; // run on edt
140  }
141 }
static void show(String message, MessageType messageType)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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.