19 package org.sleuthkit.autopsy.modules.hashdatabase;
 
   21 import java.awt.Dimension;
 
   22 import java.awt.Toolkit;
 
   24 import java.io.IOException;
 
   25 import java.nio.file.Paths;
 
   26 import java.util.logging.Level;
 
   27 import javax.swing.JFileChooser;
 
   28 import javax.swing.JFrame;
 
   29 import javax.swing.JOptionPane;
 
   30 import javax.swing.filechooser.FileNameExtensionFilter;
 
   31 import org.apache.commons.io.FilenameUtils;
 
   32 import org.openide.util.NbBundle;
 
   33 import org.openide.windows.WindowManager;
 
   46 final class HashDbImportDatabaseDialog 
extends javax.swing.JDialog {
 
   48     private JFileChooser fileChooser = 
new JFileChooser();
 
   49     private String selectedFilePath = 
"";
 
   50     private HashDb selectedHashDb = null;
 
   51     private final static String LAST_FILE_PATH_KEY = 
"HashDbImport_Path";
 
   58     HashDbImportDatabaseDialog() {
 
   59         super((JFrame) WindowManager.getDefault().getMainWindow(),
 
   60                 NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.importHashDbMsg"),
 
   72     HashDb getHashDatabase() {
 
   73         return selectedHashDb;
 
   76     private void initFileChooser() {
 
   77         fileChooser.setDragEnabled(
false);
 
   78         fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 
   79         String[] EXTENSION = 
new String[]{
"txt", 
"kdb", 
"idx", 
"hash", 
"Hash", 
"hsh"}; 
 
   80         FileNameExtensionFilter filter = 
new FileNameExtensionFilter(
 
   81                 NbBundle.getMessage(
this.getClass(), 
"HashDbImportDatabaseDialog.fileNameExtFilter.text"), EXTENSION);
 
   82         fileChooser.setFileFilter(filter);
 
   83         fileChooser.setMultiSelectionEnabled(
false);
 
   86     private void display() {
 
   87         Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
 
   88         setLocation((screenDimension.width - getSize().width) / 2, (screenDimension.height - getSize().height) / 2);
 
   92     private static String shortenPath(String path) {
 
   93         String shortenedPath = path;
 
   94         if (shortenedPath.length() > 50) {
 
   95             shortenedPath = shortenedPath.substring(0, 10 + shortenedPath.substring(10).indexOf(File.separator) + 1) + 
"..." + shortenedPath.substring((shortenedPath.length() - 20) + shortenedPath.substring(shortenedPath.length() - 20).indexOf(File.separator));
 
  105     @SuppressWarnings(
"unchecked")
 
  107     private 
void initComponents() {
 
  109         buttonGroup1 = 
new javax.swing.ButtonGroup();
 
  110         okButton = 
new javax.swing.JButton();
 
  111         cancelButton = 
new javax.swing.JButton();
 
  112         databasePathTextField = 
new javax.swing.JTextField();
 
  113         openButton = 
new javax.swing.JButton();
 
  114         knownRadioButton = 
new javax.swing.JRadioButton();
 
  115         knownBadRadioButton = 
new javax.swing.JRadioButton();
 
  116         jLabel1 = 
new javax.swing.JLabel();
 
  117         hashSetNameTextField = 
new javax.swing.JTextField();
 
  118         jLabel2 = 
new javax.swing.JLabel();
 
  119         sendIngestMessagesCheckbox = 
new javax.swing.JCheckBox();
 
  120         jLabel3 = 
new javax.swing.JLabel();
 
  122         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
 
  124         org.openide.awt.Mnemonics.setLocalizedText(okButton, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.okButton.text")); 
 
  125         okButton.addActionListener(
new java.awt.event.ActionListener() {
 
  126             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  127                 okButtonActionPerformed(evt);
 
  131         org.openide.awt.Mnemonics.setLocalizedText(cancelButton, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.cancelButton.text")); 
 
  132         cancelButton.addActionListener(
new java.awt.event.ActionListener() {
 
  133             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  134                 cancelButtonActionPerformed(evt);
 
  138         databasePathTextField.setEditable(
false);
 
  139         databasePathTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.databasePathTextField.text")); 
 
  141         org.openide.awt.Mnemonics.setLocalizedText(openButton, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.openButton.text")); 
 
  142         openButton.addActionListener(
new java.awt.event.ActionListener() {
 
  143             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  144                 openButtonActionPerformed(evt);
 
  148         buttonGroup1.add(knownRadioButton);
 
  149         org.openide.awt.Mnemonics.setLocalizedText(knownRadioButton, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.knownRadioButton.text")); 
 
  150         knownRadioButton.addActionListener(
new java.awt.event.ActionListener() {
 
  151             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  152                 knownRadioButtonActionPerformed(evt);
 
  156         buttonGroup1.add(knownBadRadioButton);
 
  157         knownBadRadioButton.setSelected(
true);
 
  158         org.openide.awt.Mnemonics.setLocalizedText(knownBadRadioButton, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.knownBadRadioButton.text")); 
 
  159         knownBadRadioButton.addActionListener(
new java.awt.event.ActionListener() {
 
  160             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  161                 knownBadRadioButtonActionPerformed(evt);
 
  165         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.jLabel1.text")); 
 
  167         hashSetNameTextField.setText(
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.hashSetNameTextField.text")); 
 
  169         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.jLabel2.text")); 
 
  171         sendIngestMessagesCheckbox.setSelected(
true);
 
  172         org.openide.awt.Mnemonics.setLocalizedText(sendIngestMessagesCheckbox, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.sendIngestMessagesCheckbox.text")); 
 
  173         sendIngestMessagesCheckbox.addActionListener(
new java.awt.event.ActionListener() {
 
  174             public void actionPerformed(java.awt.event.ActionEvent evt) {
 
  175                 sendIngestMessagesCheckboxActionPerformed(evt);
 
  179         org.openide.awt.Mnemonics.setLocalizedText(jLabel3, 
org.openide.util.NbBundle.getMessage(HashDbImportDatabaseDialog.class, 
"HashDbImportDatabaseDialog.jLabel3.text")); 
 
  181         javax.swing.GroupLayout layout = 
new javax.swing.GroupLayout(getContentPane());
 
  182         getContentPane().setLayout(layout);
 
  183         layout.setHorizontalGroup(
 
  184             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  185             .addGroup(layout.createSequentialGroup()
 
  187                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  188                     .addGroup(layout.createSequentialGroup()
 
  189                         .addGap(0, 0, Short.MAX_VALUE)
 
  190                         .addComponent(okButton)
 
  191                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  192                         .addComponent(cancelButton))
 
  193                     .addGroup(layout.createSequentialGroup()
 
  194                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
 
  195                             .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
 
  196                                 .addComponent(jLabel1)
 
  197                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  198                                 .addComponent(hashSetNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE))
 
  199                             .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
 
  200                                 .addComponent(jLabel3)
 
  201                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  202                                 .addComponent(databasePathTextField)))
 
  203                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  204                         .addComponent(openButton))
 
  205                     .addGroup(layout.createSequentialGroup()
 
  206                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  207                             .addComponent(jLabel2)
 
  208                             .addGroup(layout.createSequentialGroup()
 
  210                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  211                                     .addComponent(knownRadioButton)
 
  212                                     .addComponent(knownBadRadioButton)))
 
  213                             .addComponent(sendIngestMessagesCheckbox))
 
  214                         .addGap(0, 0, Short.MAX_VALUE)))
 
  218         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, 
new java.awt.Component[] {cancelButton, okButton});
 
  220         layout.setVerticalGroup(
 
  221             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  222             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
 
  224                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
  225                     .addComponent(openButton)
 
  226                     .addComponent(databasePathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
 
  227                     .addComponent(jLabel3))
 
  228                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  229                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
  230                     .addComponent(jLabel1)
 
  231                     .addComponent(hashSetNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
 
  232                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 
  233                     .addGroup(layout.createSequentialGroup()
 
  234                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 119, Short.MAX_VALUE)
 
  235                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
 
  236                             .addComponent(okButton)
 
  237                             .addComponent(cancelButton))
 
  239                     .addGroup(layout.createSequentialGroup()
 
  240                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
 
  241                         .addComponent(jLabel2)
 
  242                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  243                         .addComponent(knownRadioButton)
 
  244                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
 
  245                         .addComponent(knownBadRadioButton)
 
  247                         .addComponent(sendIngestMessagesCheckbox)
 
  248                         .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
 
  254     private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  255         String lastBaseDirectory = Paths.get(PlatformUtil.getUserConfigDirectory(), 
"HashDatabases").toString();
 
  256         if (ModuleSettings.settingExists(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY)) {
 
  257             lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY);
 
  259         File hashDbFolder = 
new File(lastBaseDirectory);
 
  261         if (!hashDbFolder.exists()) {
 
  262             hashDbFolder.mkdir();
 
  264         fileChooser.setCurrentDirectory(hashDbFolder);
 
  265         if (fileChooser.showOpenDialog(
this) == JFileChooser.APPROVE_OPTION) {
 
  266             File databaseFile = fileChooser.getSelectedFile();
 
  268                 selectedFilePath = databaseFile.getCanonicalPath();
 
  269                 databasePathTextField.setText(shortenPath(selectedFilePath));
 
  270                 hashSetNameTextField.setText(FilenameUtils.removeExtension(databaseFile.getName()));
 
  271                 if (hashSetNameTextField.getText().toLowerCase().contains(
"nsrl")) { 
 
  272                     knownRadioButton.setSelected(
true);
 
  273                     knownRadioButtonActionPerformed(null);
 
  275                 ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, LAST_FILE_PATH_KEY, databaseFile.getParent());
 
  276             } 
catch (IOException ex) {
 
  277                 Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.SEVERE, 
"Failed to get path of selected database", ex); 
 
  278                 JOptionPane.showMessageDialog(
this,
 
  279                         NbBundle.getMessage(
this.getClass(),
 
  280                                 "HashDbImportDatabaseDialog.failedToGetDbPathMsg"));
 
  285     private void knownRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  286         sendIngestMessagesCheckbox.setSelected(
false);
 
  287         sendIngestMessagesCheckbox.setEnabled(
false);
 
  290     private void knownBadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  291         sendIngestMessagesCheckbox.setSelected(
true);
 
  292         sendIngestMessagesCheckbox.setEnabled(
true);
 
  295     private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  299     private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
 
  303         if (hashSetNameTextField.getText().isEmpty()) {
 
  304             JOptionPane.showMessageDialog(
this,
 
  305                     NbBundle.getMessage(
this.getClass(),
 
  306                             "HashDbCreateDatabaseDialog.mustEnterHashSetNameMsg"),
 
  307                     NbBundle.getMessage(
this.getClass(),
 
  308                             "HashDbImportDatabaseDialog.importHashDbErr"),
 
  309                     JOptionPane.ERROR_MESSAGE);
 
  313         if (selectedFilePath.isEmpty()) {
 
  314             JOptionPane.showMessageDialog(
this,
 
  315                     NbBundle.getMessage(
this.getClass(),
 
  316                             "HashDbImportDatabaseDialog.mustSelectHashDbFilePathMsg"),
 
  317                     NbBundle.getMessage(
this.getClass(),
 
  318                             "HashDbImportDatabaseDialog.importHashDbErr"),
 
  319                     JOptionPane.ERROR_MESSAGE);
 
  322         File file = 
new File(selectedFilePath);
 
  323         if (!file.exists()) {
 
  324             JOptionPane.showMessageDialog(
this,
 
  325                     NbBundle.getMessage(
this.getClass(),
 
  326                             "HashDbImportDatabaseDialog.hashDbDoesNotExistMsg"),
 
  327                     NbBundle.getMessage(
this.getClass(),
 
  328                             "HashDbImportDatabaseDialog.importHashDbErr"),
 
  329                     JOptionPane.ERROR_MESSAGE);
 
  334         if (knownRadioButton.isSelected()) {
 
  335             type = KnownFilesType.KNOWN;
 
  337             type = KnownFilesType.KNOWN_BAD;
 
  340         String errorMessage = NbBundle.getMessage(this.getClass(),
 
  341                 "HashDbImportDatabaseDialog.errorMessage.failedToOpenHashDbMsg",
 
  344             selectedHashDb = HashDbManager.getInstance().addExistingHashDatabaseNoSave(hashSetNameTextField.getText(), selectedFilePath, 
true, sendIngestMessagesCheckbox.isSelected(), type);
 
  345         } 
catch (HashDbManagerException ex) {
 
  346             Logger.getLogger(HashDbImportDatabaseDialog.class.getName()).log(Level.WARNING, errorMessage, ex);
 
  347             JOptionPane.showMessageDialog(
this,
 
  349                     NbBundle.getMessage(this.getClass(),
 
  350                             "HashDbImportDatabaseDialog.importHashDbErr"),
 
  351                     JOptionPane.ERROR_MESSAGE);
 
  358     private void sendIngestMessagesCheckboxActionPerformed(java.awt.event.ActionEvent evt) {
 
  363     private javax.swing.ButtonGroup buttonGroup1;
 
  364     private javax.swing.JButton cancelButton;
 
  365     private javax.swing.JTextField databasePathTextField;
 
  366     private javax.swing.JTextField hashSetNameTextField;
 
  367     private javax.swing.JLabel jLabel1;
 
  368     private javax.swing.JLabel jLabel2;
 
  369     private javax.swing.JLabel jLabel3;
 
  370     private javax.swing.JRadioButton knownBadRadioButton;
 
  371     private javax.swing.JRadioButton knownRadioButton;
 
  372     private javax.swing.JButton okButton;
 
  373     private javax.swing.JButton openButton;
 
  374     private javax.swing.JCheckBox sendIngestMessagesCheckbox;