Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DeleteFileContentTagAction.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017-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.actions;
20 
21 import java.awt.event.ActionEvent;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.concurrent.ExecutionException;
28 import java.util.logging.Level;
29 import javafx.application.Platform;
30 import javafx.scene.control.Alert;
31 import javax.swing.AbstractAction;
32 import javax.swing.JMenu;
33 import javax.swing.JMenuItem;
34 import javax.swing.SwingWorker;
35 import org.openide.util.NbBundle;
36 import org.openide.util.Utilities;
37 import org.openide.util.actions.Presenter;
42 import org.sleuthkit.datamodel.AbstractFile;
43 import org.sleuthkit.datamodel.ContentTag;
44 import org.sleuthkit.datamodel.TagName;
45 import org.sleuthkit.datamodel.TskCoreException;
46 import org.sleuthkit.datamodel.TskData;
47 
51 @NbBundle.Messages({
52  "DeleteFileContentTagAction.deleteTag=Remove File Tag"
53 })
54 public class DeleteFileContentTagAction extends AbstractAction implements Presenter.Popup {
55 
56  private static final Logger logger = Logger.getLogger(DeleteFileContentTagAction.class.getName());
57 
58  private static final long serialVersionUID = 1L;
59  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileContentTagAction.class,
60  "DeleteFileContentTagAction.deleteTag");
61 
62  // This class is a singleton to support multi-selection of nodes, since
63  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
64  // node in the array returns a reference to the same action object from Node.getActions(boolean).
66 
67  public static synchronized DeleteFileContentTagAction getInstance() {
68  if (null == instance) {
69  instance = new DeleteFileContentTagAction();
70  }
71  return instance;
72  }
73 
75  super(MENU_TEXT);
76  }
77 
78  @Override
79  public JMenuItem getPopupPresenter() {
80  return new TagMenu();
81  }
82 
83  @Override
84  public void actionPerformed(ActionEvent e) {
85  }
86 
87  protected String getActionDisplayName() {
88  return MENU_TEXT;
89  }
90 
91  @NbBundle.Messages({
92  "# {0} - fileID",
93  "DeleteFileContentTagAction.deleteTag.alert=Unable to untag file {0}."})
94  protected void deleteTag(TagName tagName, ContentTag contentTag, long fileId) {
95  new SwingWorker<Void, Void>() {
96 
97  @Override
98  protected Void doInBackground() throws Exception {
99  TagsManager tagsManager;
100  try {
102  } catch (NoCurrentCaseException ex) {
103  logger.log(Level.SEVERE, "Error untagging file. No open case found.", ex); //NON-NLS
104  Platform.runLater(() ->
105  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
106  );
107  return null;
108  }
109 
110  try {
111  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), contentTag.getContent().getName()}); //NON-NLS
112  tagsManager.deleteContentTag(contentTag);
113  } catch (TskCoreException tskCoreException) {
114  logger.log(Level.SEVERE, "Error untagging file", tskCoreException); //NON-NLS
115  Platform.runLater(() ->
116  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
117  );
118  }
119  return null;
120  }
121 
122  @Override
123  protected void done() {
124  super.done();
125  try {
126  get();
127  } catch (InterruptedException | ExecutionException ex) {
128  logger.log(Level.SEVERE, "Unexpected exception while untagging file", ex); //NON-NLS
129  }
130  }
131  }.execute();
132  }
133 
139  private class TagMenu extends JMenu {
140 
141  private static final long serialVersionUID = 1L;
142 
143  TagMenu() {
144  super(getActionDisplayName());
145 
146  final Collection<AbstractFile> selectedAbstractFilesList =
147  new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
148 
149  if(!selectedAbstractFilesList.isEmpty()) {
150  AbstractFile file = selectedAbstractFilesList.iterator().next();
151 
152  Map<String, TagName> tagNamesMap = null;
153  try {
154  // Get the current set of tag names.
156 
157  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
158  } catch (TskCoreException | NoCurrentCaseException ex) {
159  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
160  }
161 
162  // Each tag name in the current set of tags gets its own menu item in
163  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
164  // a tag with the associated tag name.
165  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
166  try {
167  List<ContentTag> existingTagsList =
170 
171  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
172  String tagDisplayName = entry.getKey();
173 
174  TagName tagName = entry.getValue();
175  for(ContentTag contentTag : existingTagsList) {
176  if(tagDisplayName.equals(contentTag.getName().getDisplayName())) {
177  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
178  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
179  tagNameItem.addActionListener((ActionEvent e) -> {
180  deleteTag(tagName, contentTag, file.getId());
181  });
182  add(tagNameItem);
183  }
184  }
185  }
186  } catch (TskCoreException | NoCurrentCaseException ex) {
187  Logger.getLogger(TagMenu.class.getName())
188  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
189  }
190  }
191 
192  if(getItemCount() == 0) {
193  setEnabled(false);
194  }
195  }
196  }
197  }
198 
199 }
List< ContentTag > getContentTagsByContent(Content content)
static synchronized DeleteFileContentTagAction getInstance()
void deleteTag(TagName tagName, ContentTag contentTag, long fileId)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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