Autopsy  4.5.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;
41 import org.sleuthkit.datamodel.AbstractFile;
42 import org.sleuthkit.datamodel.ContentTag;
43 import org.sleuthkit.datamodel.TagName;
44 import org.sleuthkit.datamodel.TskCoreException;
45 import org.sleuthkit.datamodel.TskData;
46 
50 @NbBundle.Messages({
51  "DeleteFileContentTagAction.deleteTag=Remove File Tag"
52 })
53 public class DeleteFileContentTagAction extends AbstractAction implements Presenter.Popup {
54 
55  private static final Logger logger = Logger.getLogger(DeleteFileContentTagAction.class.getName());
56 
57  private static final long serialVersionUID = 1L;
58  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileContentTagAction.class,
59  "DeleteFileContentTagAction.deleteTag");
60 
61  // This class is a singleton to support multi-selection of nodes, since
62  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
63  // node in the array returns a reference to the same action object from Node.getActions(boolean).
65 
66  public static synchronized DeleteFileContentTagAction getInstance() {
67  if (null == instance) {
68  instance = new DeleteFileContentTagAction();
69  }
70  return instance;
71  }
72 
74  super(MENU_TEXT);
75  }
76 
77  @Override
78  public JMenuItem getPopupPresenter() {
79  return new TagMenu();
80  }
81 
82  @Override
83  public void actionPerformed(ActionEvent e) {
84  }
85 
86  protected String getActionDisplayName() {
87  return MENU_TEXT;
88  }
89 
90  @NbBundle.Messages({
91  "# {0} - fileID",
92  "DeleteFileContentTagAction.deleteTag.alert=Unable to untag file {0}."})
93  protected void deleteTag(TagName tagName, ContentTag contentTag, long fileId) {
94  new SwingWorker<Void, Void>() {
95 
96  @Override
97  protected Void doInBackground() throws Exception {
99 
100  try {
101  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), contentTag.getContent().getName()}); //NON-NLS
102  tagsManager.deleteContentTag(contentTag);
103  } catch (TskCoreException tskCoreException) {
104  logger.log(Level.SEVERE, "Error untagging file", tskCoreException); //NON-NLS
105  Platform.runLater(() ->
106  new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
107  );
108  }
109  return null;
110  }
111 
112  @Override
113  protected void done() {
114  super.done();
115  try {
116  get();
117  } catch (InterruptedException | ExecutionException ex) {
118  logger.log(Level.SEVERE, "Unexpected exception while untagging file", ex); //NON-NLS
119  }
120  }
121  }.execute();
122  }
123 
129  private class TagMenu extends JMenu {
130 
131  private static final long serialVersionUID = 1L;
132 
133  TagMenu() {
134  super(getActionDisplayName());
135 
136  final Collection<AbstractFile> selectedAbstractFilesList =
137  new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class));
138 
139  if(!selectedAbstractFilesList.isEmpty()) {
140  AbstractFile file = selectedAbstractFilesList.iterator().next();
141 
142  // Get the current set of tag names.
144 
145  Map<String, TagName> tagNamesMap = null;
146  try {
147  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
148  } catch (TskCoreException ex) {
149  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
150  }
151 
152  // Each tag name in the current set of tags gets its own menu item in
153  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
154  // a tag with the associated tag name.
155  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
156  try {
157  List<ContentTag> existingTagsList =
160 
161  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
162  String tagDisplayName = entry.getKey();
163 
164  TagName tagName = entry.getValue();
165  for(ContentTag contentTag : existingTagsList) {
166  if(tagDisplayName.equals(contentTag.getName().getDisplayName())) {
167  String notableString = tagName.getKnownStatus() == TskData.FileKnown.BAD ? TagsManager.getNotableTagLabel() : "";
168  JMenuItem tagNameItem = new JMenuItem(tagDisplayName + notableString);
169  tagNameItem.addActionListener((ActionEvent e) -> {
170  deleteTag(tagName, contentTag, file.getId());
171  });
172  add(tagNameItem);
173  }
174  }
175  }
176  } catch (TskCoreException ex) {
177  Logger.getLogger(TagMenu.class.getName())
178  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
179  }
180  }
181 
182  if(getItemCount() == 0) {
183  setEnabled(false);
184  }
185  }
186  }
187  }
188 
189 }
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: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.