Autopsy  4.19.1
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;
47 import org.sleuthkit.datamodel.AbstractFile;
48 import org.sleuthkit.datamodel.ContentTag;
49 import org.sleuthkit.datamodel.TagName;
50 import org.sleuthkit.datamodel.TskCoreException;
51 
55 @NbBundle.Messages({
56  "DeleteFileContentTagAction.deleteTag=Remove File Tag"
57 })
58 public class DeleteFileContentTagAction extends AbstractAction implements Presenter.Popup {
59 
60  private static final Logger logger = Logger.getLogger(DeleteFileContentTagAction.class.getName());
61 
62  private static final long serialVersionUID = 1L;
63  private static final String MENU_TEXT = NbBundle.getMessage(DeleteFileContentTagAction.class,
64  "DeleteFileContentTagAction.deleteTag");
65 
66  // This class is a singleton to support multi-selection of nodes, since
67  // org.openide.nodes.NodeOp.findActions(Node[] nodes) will only pick up an Action if every
68  // node in the array returns a reference to the same action object from Node.getActions(boolean).
70 
71  public static synchronized DeleteFileContentTagAction getInstance() {
72  if (null == instance) {
73  instance = new DeleteFileContentTagAction();
74  }
75  return instance;
76  }
77 
79  super(MENU_TEXT);
80  }
81 
82  @Override
83  public JMenuItem getPopupPresenter() {
84  return new TagMenu();
85  }
86 
96  public JMenuItem getMenuForFiles(Collection<AbstractFile> selectedFiles) {
97  return new TagMenu(selectedFiles);
98  }
99 
100  @Override
101  public void actionPerformed(ActionEvent e) {
102  }
103 
104  protected String getActionDisplayName() {
105  return MENU_TEXT;
106  }
107 
108  @NbBundle.Messages({
109  "# {0} - fileID",
110  "DeleteFileContentTagAction.deleteTag.alert=Unable to untag file {0}."})
111  protected void deleteTag(TagName tagName, ContentTag contentTag, long fileId) {
112  new SwingWorker<Void, Void>() {
113 
114  @Override
115  protected Void doInBackground() throws Exception {
116  TagsManager tagsManager;
117  try {
119  } catch (NoCurrentCaseException ex) {
120  logger.log(Level.SEVERE, "Error untagging file. No open case found.", ex); //NON-NLS
121  Platform.runLater(()
122  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
123  );
124  return null;
125  }
126 
127  try {
128  logger.log(Level.INFO, "Removing tag {0} from {1}", new Object[]{tagName.getDisplayName(), contentTag.getContent().getName()}); //NON-NLS
129 
130  // Check if there is an image tag before deleting the content tag.
132  if(imageTag != null) {
134  }
135 
136  tagsManager.deleteContentTag(contentTag);
137  } catch (TskCoreException tskCoreException) {
138  logger.log(Level.SEVERE, "Error untagging file", tskCoreException); //NON-NLS
139  Platform.runLater(()
140  -> new Alert(Alert.AlertType.ERROR, Bundle.DeleteFileContentTagAction_deleteTag_alert(fileId)).show()
141  );
142  }
143  return null;
144  }
145 
146  @Override
147  protected void done() {
148  super.done();
149  try {
150  get();
151  } catch (InterruptedException | ExecutionException ex) {
152  logger.log(Level.SEVERE, "Unexpected exception while untagging file", ex); //NON-NLS
153  }
154  }
155  }.execute();
156  }
157 
163  private final class TagMenu extends JMenu {
164 
165  private static final long serialVersionUID = 1L;
166 
171  TagMenu() {
172  this(new HashSet<>(Utilities.actionsGlobalContext().lookupAll(AbstractFile.class)));
173  }
174 
179  TagMenu(Collection<AbstractFile> selectedFiles) {
180  super(getActionDisplayName());
181 
182  if (!selectedFiles.isEmpty()) {
183  AbstractFile file = selectedFiles.iterator().next();
184 
185  Map<String, TagName> tagNamesMap = null;
186  List<String> standardTagNames = TagsManager.getStandardTagNames();
187  List<JMenuItem> standardTagMenuitems = new ArrayList<>();
188  try {
189  // Get the current set of tag names.
191 
192  tagNamesMap = new TreeMap<>(tagsManager.getDisplayNamesToTagNamesMap());
193  } catch (TskCoreException | NoCurrentCaseException ex) {
194  Logger.getLogger(TagsManager.class.getName()).log(Level.SEVERE, "Failed to get tag names", ex); //NON-NLS
195  }
196 
197  // Each tag name in the current set of tags gets its own menu item in
198  // the "Quick Tags" sub-menu. Selecting one of these menu items adds
199  // a tag with the associated tag name.
200  if (null != tagNamesMap && !tagNamesMap.isEmpty()) {
201  try {
202  List<ContentTag> existingTagsList
203  = Case.getCurrentCaseThrows().getServices().getTagsManager()
204  .getContentTagsByContent(file);
205 
206  for (Map.Entry<String, TagName> entry : tagNamesMap.entrySet()) {
207  String tagDisplayName = entry.getKey();
208 
209  TagName tagName = entry.getValue();
210  for (ContentTag contentTag : existingTagsList) {
211  if (tagDisplayName.equals(contentTag.getName().getDisplayName())) {
212  JMenuItem tagNameItem = new JMenuItem(TagUtils.getDecoratedTagDisplayName(tagName));
213  tagNameItem.addActionListener((ActionEvent e) -> {
214  deleteTag(tagName, contentTag, file.getId());
215  });
216 
217  // Show custom tags before predefined tags in the menu
218  if (standardTagNames.contains(tagDisplayName)) {
219  standardTagMenuitems.add(tagNameItem);
220  } else {
221  add(tagNameItem);
222  }
223  }
224  }
225  }
226  } catch (TskCoreException | NoCurrentCaseException ex) {
227  Logger.getLogger(TagMenu.class.getName())
228  .log(Level.SEVERE, "Error retrieving tags for TagMenu", ex); //NON-NLS
229  }
230  }
231 
232  if ((getItemCount() > 0) && !standardTagMenuitems.isEmpty()) {
233  addSeparator();
234  }
235  standardTagMenuitems.forEach((menuItem) -> {
236  add(menuItem);
237  });
238 
239  if (getItemCount() == 0) {
240  setEnabled(false);
241  }
242  }
243  }
244  }
245 
246 }
static< T > ContentViewerTag< T > getTag(ContentTag contentTag, Class< T > clazz)
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-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.