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

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