Autopsy  4.15.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-2019 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.ArrayList;
23 import java.util.Collection;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.TreeMap;
28 import java.util.concurrent.ExecutionException;
29 import java.util.logging.Level;
30 import javafx.application.Platform;
31 import javafx.scene.control.Alert;
32 import javax.swing.AbstractAction;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.SwingWorker;
36 import org.openide.util.NbBundle;
37 import org.openide.util.Utilities;
38 import org.openide.util.actions.Presenter;
44 import org.sleuthkit.datamodel.AbstractFile;
45 import org.sleuthkit.datamodel.ContentTag;
46 import org.sleuthkit.datamodel.TagName;
47 import org.sleuthkit.datamodel.TskCoreException;
48 
52 @NbBundle.Messages({
53  "DeleteFileContentTagAction.deleteTag=Remove File Tag"
54 })
55 public class DeleteFileContentTagAction extends AbstractAction implements Presenter.Popup {
56 
57  private static final Logger logger = Logger.getLogger(DeleteFileContentTagAction.class.getName());
58 
59  private static final long serialVersionUID = 1L;
60  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileContentTagAction.class,
61  "DeleteFileContentTagAction.deleteTag");
62 
63  // This class is a singleton to support multi-selection of nodes, since
64  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
65  // node in the array returns a reference to the same action object from Node.getActions(boolean).
67 
68  public static synchronized DeleteFileContentTagAction getInstance() {
69  if (null == instance) {
70  instance = new DeleteFileContentTagAction();
71  }
72  return instance;
73  }
74 
76  super(MENU_TEXT);
77  }
78 
79  @Override
80  public JMenuItem getPopupPresenter() {
81  return new TagMenu();
82  }
83 
93  public JMenuItem getMenuForFiles(Collection<AbstractFile> selectedFiles) {
94  return new TagMenu(selectedFiles);
95  }
96 
97  @Override
98  public void actionPerformed(ActionEvent e) {
99  }
100 
101  protected String getActionDisplayName() {
102  return MENU_TEXT;
103  }
104 
105  @NbBundle.Messages({
106  "# {0} - fileID",
107  "DeleteFileContentTagAction.deleteTag.alert=Unable to untag file {0}."})
108  protected void deleteTag(TagName tagName, ContentTag contentTag, long fileId) {
109  new SwingWorker<Void, Void>() {
110 
111  @Override
112  protected Void doInBackground() throws Exception {
113  TagsManager tagsManager;
114  try {
116  } catch (NoCurrentCaseException ex) {
117  logger.log(Level.SEVERE, "Error untagging file. No open case found.", ex); //NON-NLS
118  Platform.runLater(()
119  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
120  );
121  return null;
122  }
123 
124  try {
125  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), contentTag.getContent().getName()}); //NON-NLS
126  tagsManager.deleteContentTag(contentTag);
127  } catch (TskCoreException tskCoreException) {
128  logger.log(Level.SEVERE, "Error untagging file", tskCoreException); //NON-NLS
129  Platform.runLater(()
130  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
131  );
132  }
133  return null;
134  }
135 
136  @Override
137  protected void done() {
138  super.done();
139  try {
140  get();
141  } catch (InterruptedException | ExecutionException ex) {
142  logger.log(Level.SEVERE, "Unexpected exception while untagging file", ex); //NON-NLS
143  }
144  }
145  }.execute();
146  }
147 
153  private final class TagMenu extends JMenu {
154 
155  private static final long serialVersionUID = 1L;
156 
161  TagMenu() {
162  this(new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class)));
163  }
164 
169  TagMenu(Collection<AbstractFile> selectedFiles) {
170  super(getActionDisplayName());
171 
172  if (!selectedFiles.isEmpty()) {
173  AbstractFile file = selectedFiles.iterator().next();
174 
175  Map<String, TagName> tagNamesMap = null;
176  List<String> standardTagNames = TagsManager.getStandardTagNames();
177  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
178  try {
179  // Get the current set of tag names.
181 
182  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
183  } catch (TskCoreException | NoCurrentCaseException ex) {
184  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
185  }
186 
187  // Each tag name in the current set of tags gets its own menu item in
188  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
189  // a tag with the associated tag name.
190  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
191  try {
192  List<ContentTag> existingTagsList
193  = Case.getCurrentCaseThrows().getServices().getTagsManager()
194  .getContentTagsByContent(file);
195 
196  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
197  String tagDisplayName = entry.getKey();
198 
199  TagName tagName = entry.getValue();
200  for (ContentTag contentTag : existingTagsList) {
201  if (tagDisplayName.equals(contentTag.getName().getDisplayName())) {
202  JMenuItem tagNameItem = new JMenuItem(TagUtils.getDecoratedTagDisplayName(tagName));
203  tagNameItem.addActionListener((ActionEvent e) -> {
204  deleteTag(tagName, contentTag, file.getId());
205  });
206 
207  // Show custom tags before predefined tags in the menu
208  if (standardTagNames.contains(tagDisplayName)) {
209  standardTagMenuitems.add(tagNameItem);
210  } else {
211  add(tagNameItem);
212  }
213  }
214  }
215  }
216  } catch (TskCoreException | NoCurrentCaseException ex) {
217  Logger.getLogger(TagMenu.class.getName())
218  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
219  }
220  }
221 
222  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty()) {
223  addSeparator();
224  }
225  standardTagMenuitems.forEach((menuItem) -> {
226  add(menuItem);
227  });
228 
229  if (getItemCount() == 0) {
230  setEnabled(false);
231  }
232  }
233  }
234  }
235 
236 }
static synchronized DeleteFileContentTagAction getInstance()
void deleteTag(TagName tagName, ContentTag contentTag, long fileId)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
JMenuItem getMenuForFiles(Collection< AbstractFile > selectedFiles)

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.