1 package SnowMailClient.gnupg.Views; 2 3 import SnowMailClient.gnupg.*; 4 import SnowMailClient.gnupg.Main.GnuPGCommands; 5 import SnowMailClient.Language.Language; 6 import SnowMailClient.SnowMailClientApp; 7 import SnowMailClient.crypto.PassphraseDialog; 8 import SnowMailClient.crypto.SecretKeyManager; 9 import snow.crypto.*; 10 import snow.sortabletable.*; 11 import snow.utils.gui.*; 12 13 14 import SnowMailClient.gnupg.model.*; 15 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.*; 19 import javax.swing.event.*; 20 import javax.swing.border.*; 21 import java.util.*; 22 23 24 public final class KeysViewer extends JDialog 25 { 26 final private GnuPGLink link; 27 final private KeysTableModel tableModel = new KeysTableModel(); 28 final private SortableTableModel sortableTableModel; 29 30 final private JTable table = new JTable(); 31 final private JButton closeButton = new JButton(Language.translate("Close")); 34 final private JButton refreshButton = new JButton(Language.translate("Refresh")); 35 36 38 final private JCheckBox advancedModeCB = new JCheckBox(Language.translate("Advanced Mode"), false); 39 40 public KeysViewer() 41 { 42 super(SnowMailClientApp.getInstance(), Language.translate("GnuPG explorer"), true); 43 44 link = SnowMailClientApp.getInstance().getGnuPGLink(); 45 try 46 { 47 link.setGPGPath(link.getPathToGPG()); 48 } catch(Exception ignored) {} 49 50 if(!link.isGPG_available()) 51 { 52 54 JOptionPane.showMessageDialog(SnowMailClientApp.getInstance(), 55 Language.translate("Please first set the GPG path."), 56 Language.translate("GPG not installed"), 57 JOptionPane.WARNING_MESSAGE); 58 59 sortableTableModel = null; 60 return; 61 } 62 63 65 tableModel.refreshModel(link); 66 67 69 sortableTableModel = new SortableTableModel(tableModel); 70 table.setModel(sortableTableModel); 71 sortableTableModel.installGUI(table); 72 table.setDefaultRenderer(Object .class, new KeysTableCellRenderer()); 73 74 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 75 76 this.getContentPane().setLayout(new BorderLayout()); 77 this.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); 78 79 AdvancedSearchPanel sp = new AdvancedSearchPanel(Language.translate("Search"), null, sortableTableModel, false); 81 this.getContentPane().add(sp, BorderLayout.NORTH); 82 sp.add(Box.createHorizontalStrut(100)); 83 sp.add(advancedModeCB); 84 advancedModeCB.addActionListener(new ActionListener() 85 { 86 public void actionPerformed(ActionEvent ae) 87 { 88 if(advancedModeCB.isSelected()) 89 { 90 int[] range = new int[tableModel.getColumnCount()]; 91 for(int i=0; i<range.length; i++) range[i] = i; 92 sortableTableModel.setVisibleColumns(range); 93 } 94 else 95 { 96 sortableTableModel.setVisibleColumns(new int[]{1,2,3,6}); 97 } 98 tableModel.setAdvancedMode(advancedModeCB.isSelected(), link); 99 sortableTableModel.setPreferredColumnSizesFromModel(); 100 } 101 }); 102 103 sortableTableModel.setVisibleColumns(new int[]{1,2,3,6}); 104 tableModel.setAdvancedMode(false, link); 105 sortableTableModel.setPreferredColumnSizesFromModel(); 106 107 JPanel southPanel = new JPanel(); southPanel.setBorder(new EmptyBorder(4,5,4,5)); 111 southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS)); 112 113 this.getContentPane().add(southPanel, BorderLayout.SOUTH); 114 southPanel.add(refreshButton); 118 southPanel.add(Box.createHorizontalGlue()); 119 southPanel.add(closeButton); 120 121 closeButton.setBackground(Color.orange); 122 closeButton.addActionListener(new ActionListener() 123 { 124 public void actionPerformed(ActionEvent ae) 125 { 126 setVisible(false); 127 } 128 }); 129 130 131 132 refreshButton.addActionListener(new ActionListener() 133 { 134 public void actionPerformed(ActionEvent ae) 135 { 136 try 137 { 138 tableModel.refreshModel(link); 139 } 140 catch(Exception ex) 141 { 142 JOptionPane.showMessageDialog(KeysViewer.this, 143 ex.getMessage(), 144 Language.translate("Cannot read the keys from GnuPG"), 145 JOptionPane.ERROR_MESSAGE); 146 } 147 } 148 }); 149 150 151 152 153 154 155 table.addMouseListener(new MouseAdapter() 156 { 157 @Override public void mousePressed(MouseEvent me) 158 { 159 if(me.isPopupTrigger()) 160 { 161 showPopup(me); 162 } 163 } 164 165 @Override public void mouseReleased(MouseEvent me) 166 { 167 if(me.isPopupTrigger()) 168 { 169 showPopup(me); 170 } 171 } 172 173 }); 174 175 this.createMenu(); 176 177 179 SnowMailClientApp.getInstance().getProperties().setComponentSizeFromINIFile(this, "gpgExplorer", 800,600,100,100); 180 SnowMailClientApp.centerComponentOnMainFrame(this); 181 this.setVisible(true); 182 183 SnowMailClientApp.getInstance().getProperties().saveComponentSizeInINIFile(this, "gpgExplorer"); 184 185 186 } 187 188 private void createMenu() 189 { 190 this.setJMenuBar(new JMenuBar()); 191 192 JMenu importMenu = new JMenu(Language.translate("Import")); 193 getJMenuBar().add(importMenu); 194 195 JMenuItem addKeyItem = new JMenuItem(Language.translate("Import key from file")); 196 importMenu.add(addKeyItem); 197 198 JMenuItem getKeyItem = new JMenuItem(Language.translate("Import a key from server")); 199 importMenu.add(getKeyItem); 200 201 JMenuItem refreshKeysItem = new JMenuItem(Language.translate("Refresh keys from server")); 202 importMenu.add(refreshKeysItem); 203 204 JMenu createMenu = new JMenu(Language.translate("Generate")); 205 getJMenuBar().add(createMenu); 206 207 JMenuItem generateKeyPairItem = new JMenuItem(Language.translate("Generate a new KeyPair")); 208 createMenu.add(generateKeyPairItem); 209 210 addKeyItem.addActionListener(new ActionListener() 211 { 212 public void actionPerformed(ActionEvent ae) 213 { 214 addKeyAction(); 215 } 216 }); 217 218 getKeyItem.addActionListener(new ActionListener() 219 { 220 public void actionPerformed(ActionEvent ae) 221 { 222 new GetKeyFromServerDialog(KeysViewer.this, tableModel); 223 } 224 }); 225 226 refreshKeysItem.addActionListener(new ActionListener() 227 { 228 public void actionPerformed(ActionEvent ae) 229 { 230 refreshKeysAction(); 231 } 232 }); 233 234 generateKeyPairItem.addActionListener(new ActionListener() 235 { 236 public void actionPerformed(ActionEvent ae) 237 { 238 try 239 { 240 new GenerateGPGKeypairDialog(KeysViewer.this, tableModel); 241 } 242 catch(Exception ex) 243 { 244 JOptionPane.showMessageDialog(KeysViewer.this, 245 ex.getMessage(), 246 Language.translate("Cannot generate new keypair"), 247 JOptionPane.ERROR_MESSAGE); 248 } 249 } 250 }); 251 } 252 253 254 private void showPopup(MouseEvent me) 255 { 256 JPopupMenu popup = new JPopupMenu("Key popup"); 257 int[] sels = table.getSelectedRows(); 258 if(sels.length!=1) return; 259 260 int pos = sortableTableModel.getIndexInUnsortedFromTablePos(sels[0]); 261 final GnuPGKeyID key = tableModel.getKeyAt(pos); 262 263 final boolean hasAssociatedSecretKey = !key.isSecret() && link.hasSecretKeyAssociated(key); 264 265 popup.add(key.getNames()+" / "+key.getKeyID()); 266 popup.addSeparator(); 267 268 269 if(!key.isSecret()) 270 { 271 JMenuItem viewTrustItem = new JMenuItem(Language.translate("View/set key trust")); 273 popup.add(viewTrustItem); 274 viewTrustItem.addActionListener(new ActionListener() 275 { 276 public void actionPerformed(ActionEvent ae) 277 { 278 new SetTrustDialog(KeysViewer.this, tableModel, key); 279 } 280 }); 281 282 JMenuItem viewPubKeyItem = new JMenuItem(Language.translate("View public key")); 283 popup.add(viewPubKeyItem); 284 viewPubKeyItem.addActionListener(new ActionListener() 285 { 286 public void actionPerformed(ActionEvent ae) 287 { 288 try 289 { 290 String pkc = link.getPublicKeyContent(key); 291 displayKeyContentTextArea(pkc, key, Language.translate("Public key of %",key.getKeyID())); 292 } 293 catch(Exception ex) 294 { 295 ex.printStackTrace(); 296 JOptionPane.showMessageDialog(KeysViewer.this, 297 ex.getMessage(), 298 Language.translate("Cannot display the public key"), 299 JOptionPane.ERROR_MESSAGE); 300 } 301 } 302 }); 303 304 JMenuItem sendPubKeyItem = new JMenuItem(Language.translate("Publish public key to keyserver")); 305 popup.add(sendPubKeyItem); 306 sendPubKeyItem.addActionListener(new ActionListener() 307 { 308 public void actionPerformed(ActionEvent ae) 309 { 310 311 final ProgressModalDialog progress = new ProgressModalDialog(KeysViewer.this, 312 Language.translate("Publishing public key to keyserver"), false); 313 SnowMailClientApp.centerComponentOnMainFrame(progress); 314 315 Thread t = new Thread () 316 { 317 public void run() 318 { 319 try 320 { 321 GnuPGCommands.sendPublicKey( 322 link.getPathToGPG(), 323 key, 324 progress); 325 } 326 catch(Exception ex) 327 { 328 ex.printStackTrace(); 329 JOptionPane.showMessageDialog(KeysViewer.this, 330 ex.getMessage(), 331 Language.translate("Cannot send the public key"), 332 JOptionPane.ERROR_MESSAGE); 333 } 334 finally 335 { 336 progress.closeDialog(); 337 } 338 } 339 }; 340 t.setPriority(Thread.NORM_PRIORITY-1); 341 t.start(); 342 343 progress.start(); 345 } 346 }); 347 348 } 349 else 350 { 351 JMenuItem viewSecretKeyItem = new JMenuItem(Language.translate("View secret key")); 352 popup.add(viewSecretKeyItem); 353 viewSecretKeyItem.addActionListener(new ActionListener() 354 { 355 public void actionPerformed(ActionEvent ae) 356 { 357 try 358 { 359 String skc = link.getSecretKeyContent(key); 360 displayKeyContentTextArea(skc, key, Language.translate("Secret key of %",key.getKeyID())); 361 } 362 catch(Exception ex) 363 { 364 ex.printStackTrace(); 365 JOptionPane.showMessageDialog(KeysViewer.this, 366 ex.getMessage(), 367 Language.translate("Cannot display the secret key"), 368 JOptionPane.ERROR_MESSAGE); 369 } 370 } 371 }); 372 373 if(link.getPasswordForKey(key)!=null) 374 { 375 JMenuItem viewPassItem = new JMenuItem(Language.translate("View password")); 376 popup.add(viewPassItem); 377 viewPassItem.addActionListener(new ActionListener() 378 { 379 public void actionPerformed(ActionEvent ae) 380 { 381 SecretKeyID skid = SecretKeyUtilities.computeSignature( 383 SecretKeyManager.getInstance().getUserKeyForEncryption()); 384 PassphraseDialog pd = new PassphraseDialog(KeysViewer.this, 385 Language.translate("Enter the SnowMail password"), true, skid, null, Language.translate("Security Check")); 386 387 if(pd.matchesID()) 388 { 389 JOptionPane.showMessageDialog( 390 KeysViewer.this, new String (link.getPasswordForKey(key)), 391 Language.translate("Password for %", key.getMails()), 392 JOptionPane.INFORMATION_MESSAGE); 393 } 394 } 395 }); 396 } 397 } 398 399 400 401 JMenuItem removeKeyItem = new JMenuItem(Language.translate("Remove key")); 403 popup.add(removeKeyItem); 404 removeKeyItem.addActionListener(new ActionListener() 405 { 406 public void actionPerformed(ActionEvent ae) 407 { 408 409 if(hasAssociatedSecretKey) 410 { 411 int rep = JOptionPane.showConfirmDialog(KeysViewer.this, 412 Language.translate("This will also remove the associated private key. Do you want to proceed ?"), 413 Language.translate("Removing public and private key"), 414 JOptionPane.YES_NO_OPTION); 415 if(rep!=JOptionPane.YES_OPTION) return; 416 } 417 418 419 try 420 { 421 if(hasAssociatedSecretKey) 422 { 423 link.removePublicAndPrivateKey(key); 424 } 425 else 426 { 427 link.removeKey(key); 428 } 429 430 tableModel.refreshModel(link); 432 } 433 catch(Exception ex) 434 { 435 ex.printStackTrace(); 436 JOptionPane.showMessageDialog(KeysViewer.this, 437 ex.getMessage(), 438 Language.translate("Cannot remove the key"), 439 JOptionPane.ERROR_MESSAGE); 440 441 } 442 } 443 }); 444 445 446 447 popup.show(table, me.getX(), me.getY()); 448 } 449 450 private void refreshKeysAction() 451 { 452 final ProgressModalDialog progress = new ProgressModalDialog(KeysViewer.this, 453 Language.translate("Refreshing all keys from keyserver"), false); 454 SnowMailClientApp.centerComponentOnMainFrame(progress); 455 456 Thread t = new Thread () 457 { 458 public void run() 459 { 460 try 461 { 462 GnuPGCommands.refreshPublicKeysFromServer(link.getPathToGPG(),progress); 463 EventQueue.invokeLater(new Runnable () 464 { 465 public void run() 466 { 467 tableModel.refreshModel(link); 468 } 469 }); 470 } 471 catch(Exception ex) 472 { 473 ex.printStackTrace(); 474 JOptionPane.showMessageDialog(KeysViewer.this, 475 ex.getMessage(), 476 Language.translate("Cannot refresh the keys"), 477 JOptionPane.ERROR_MESSAGE); 478 } 479 finally 480 { 481 progress.closeDialog(); 482 } 483 } 484 }; 485 t.setPriority(Thread.NORM_PRIORITY-1); 486 t.start(); 487 488 progress.start(); 490 } 491 492 private void addKeyAction() 493 { 494 JDialog dialog = new JDialog(this, Language.translate("Add a new key to the GPG keyring"), true); 495 JTextArea ta = new JTextArea(); 496 dialog.setLayout(new BorderLayout()); 497 dialog.add(new JScrollPane(ta), BorderLayout.CENTER); 498 CloseControlPanel ccp = new CloseControlPanel(dialog, false, false, Language.translate("Add the key in GPG")); 499 ccp.getOkButton().setIcon( Icons.PlusIcon.shared10 ); 500 dialog.add(ccp, BorderLayout.SOUTH); 501 502 504 SnowMailClientApp.getInstance().getProperties().setComponentSizeFromINIFile(dialog, "gpgExplorer.addKeyDialog", 600,650,120,120); 505 SnowMailClientApp.centerComponentOnMainFrame(dialog); 506 dialog.setVisible(true); 507 508 SnowMailClientApp.getInstance().getProperties().saveComponentSizeInINIFile(dialog, "gpgExplorer.addKeyDialog"); 509 if(ccp.getWasCancelled()) return; 510 511 String key = ta.getText(); 512 try 513 { 514 link.addKey(key); 515 tableModel.refreshModel(link); 516 } 517 catch(Exception ex) 518 { 519 ex.printStackTrace(); 520 JOptionPane.showMessageDialog(KeysViewer.this, 521 ex.getMessage(), 522 Language.translate("Cannot add the key"), 523 JOptionPane.ERROR_MESSAGE); 524 } 525 526 } 527 528 529 private void displayKeyContentTextArea(String content, GnuPGKeyID key, String title) 530 { 531 JDialog dialog = new JDialog(this, title, true); 532 StringBuffer tacont = new StringBuffer (); 533 tacont.append(Language.translate("Key fingerprint")+" = "+key.getFingerprint()+"\r\n"); 534 tacont.append(Language.translate("Key length")+" = "+key.getKeyLength()+"\r\n"); 535 536 if(!key.isSecret()) 537 { 538 tacont.append(Language.translate("Calculate key trust")+" = "+key.getCalculatedTrustMessage()+"\r\n"); 539 } 540 541 542 tacont.append("\r\n"); 543 tacont.append(content); 544 JTextArea ta = new JTextArea(tacont.toString()); 545 ta.setEditable(false); 546 dialog.setLayout(new BorderLayout()); 547 dialog.add(new JScrollPane(ta), BorderLayout.CENTER); 548 CloseControlPanel ccp = new CloseControlPanel(dialog, false, false, Language.translate("Close")); 549 dialog.add(ccp, BorderLayout.SOUTH); 550 551 553 SnowMailClientApp.getInstance().getProperties().setComponentSizeFromINIFile(dialog, "gpgExplorer.displayTextArea", 600,650,120,120); 554 SnowMailClientApp.centerComponentOnMainFrame(dialog); 555 dialog.setVisible(true); 556 557 SnowMailClientApp.getInstance().getProperties().saveComponentSizeInINIFile(dialog, "gpgExplorer.displayTextArea"); 558 } 559 560 561 562 563 564 } | Popular Tags |