1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.image.ImageObserver ; 4 import java.io.*; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.tree.*; 8 import javax.accessibility.Accessible ; 9 import org.jivesoftware.smack.*; 10 import org.jivesoftware.smackx.*; 11 import org.jivesoftware.smack.packet.*; 12 import org.jivesoftware.smack.filter.*; 13 import org.jivesoftware.smack.util.StringUtils; 14 import whisper.PublicKey; 15 16 17 public final class ProfileWindow extends JDialog implements Accessible , ImageObserver , MenuContainer, RootPaneContainer, Serializable, WindowConstants{ 18 19 private static final int GAP=10; 20 21 final JProfilePane profilePane=new JProfilePane(); 22 23 final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP)); 24 final JButton refreshBtn=new JButton(Lang.gs("refresh"),Icons.REFRESH); 25 final JButton closeBtn=new JButton(Lang.gs("close"),Icons.CLOSE); 26 27 JWaitDialog wait; 28 29 private String ID; 30 31 final BtnClick click=new BtnClick(); 32 33 final ProfileWindow thisDialog=this; 34 35 public ProfileWindow(String id,Component c){ 36 super(WhisperIM.MainWindow,"",false); 37 this.ID=id.toLowerCase(); 39 setTitle(Lang.gs("profile")+" "+ID); 40 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 41 42 Container cp=getContentPane(); 43 cp.setLayout(new BorderLayout()); 44 45 profilePane.setEditable(false); 46 47 48 refreshBtn.addActionListener(click); 49 closeBtn.addActionListener(click); 50 closeBtn.setDefaultCapable(true); 51 refreshBtn.setToolTipText(Lang.gs("refresh_tt")); 52 refreshBtn.setMnemonic(Lang.s2k("refresh_mn")); 53 closeBtn.setMnemonic(Lang.s2k("close_mn")); 54 btnPanel.add(refreshBtn); 55 btnPanel.add(closeBtn); 56 57 cp.add(btnPanel,BorderLayout.SOUTH); 58 cp.add(profilePane,BorderLayout.CENTER); 59 getRootPane().setDefaultButton(closeBtn); 60 boolean gotprofile=false; 62 Profile p=(Profile)WhisperIM.MainWindow.Profiles.get("jabber"+ID); 63 if(p!=null){ 64 gotprofile=true; 65 profilePane.set(p,false,(PublicKey)WhisperIM.MainWindow.Keys.get("jabber"+ID) ,true); } 67 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); getRootPane().getActionMap().put("help",new HelpAction(this,"contacts.html#profiles")); 70 71 pack(); 72 setLocationRelativeTo(c); 73 addKeyListener(new KeyAction()); 74 addMouseListener(new MouseAction()); 75 wait=new JWaitDialog(this,"getting profile"); 76 show(); 77 if(!gotprofile){ 78 doRefresh(); 79 } 80 } 81 82 public void doRefresh(){ 83 if(!WhisperIM.MainWindow.Conn.isConnected()){ 84 GUI.showWarning(this,"profile","need to be connected"); 85 return; 86 } 87 wait.show(); 88 (new FetchThread()).start(); 89 } 90 91 public void doFetched(VCard vcard){ 92 Profile p=new Profile(vcard); 93 WhisperIM.MainWindow.Profiles.put("jabber"+ID,p); 95 boolean keyvalid=false; 96 PublicKey key=vcard.getKey(); 98 PublicKey oldkey=(PublicKey)WhisperIM.MainWindow.Keys.get("jabber"+ID); 100 if (oldkey==null){ if(key!=null){ 102 if(WhisperIM.MainWindow.isKeyValid(key,ID,"jabber")){ 104 keyvalid=true; 105 WhisperIM.MainWindow.Keys.put("jabber"+ID,key); 106 } 107 else{ 108 WhisperIM.MainWindow.noKey("jabber",ID); 109 JOptionPane.showMessageDialog(this,Lang.gs("invalid new key")+"\r\n"+ID+"\r\n"+Lang.gs("key_fingerprint")+StringUtils.encodeHex(key.getKeyID()),Lang.gs("profile")+" "+ID,JOptionPane.WARNING_MESSAGE); 110 } 111 } 112 else{ 113 WhisperIM.MainWindow.noKey("jabber",ID); 114 } 115 profilePane.set(p,false,key,keyvalid); 116 wait.dispose(); 117 return; 118 } 119 120 if(key==null){ profilePane.set(p,false,oldkey,true); 122 wait.dispose(); 123 return; 124 } 125 126 133 if( !WhisperIM.MainWindow.isKeyValid(key,ID,"jabber") ) { 135 profilePane.set(p,false,oldkey,true); 136 wait.dispose(); 137 JOptionPane.showMessageDialog(this,Lang.gs("invalid new key")+"\r\n"+ID+"\r\n"+Lang.gs("key_fingerprint")+StringUtils.encodeHex(key.getKeyID()),Lang.gs("profile")+" "+ID,JOptionPane.WARNING_MESSAGE); 138 return; 139 } 140 141 if(areEqual(oldkey.getKeyID(),key.getKeyID()) ){ 142 profilePane.set(p,false,oldkey,true); 143 wait.dispose(); 144 return; 145 } 146 147 java.util.GregorianCalendar olddate; 149 java.util.GregorianCalendar newdate; 150 try{ 151 olddate=whisper.Util.parseTime(oldkey.date()); 152 newdate=whisper.Util.parseTime(key.date()); 153 } 154 catch(Exception e){ wait.dispose(); 156 e.printStackTrace(); 157 GUI.showError(this,"error","error",e.getMessage()); 158 return; 159 } 160 if(newdate.before(olddate)){ 161 profilePane.set(p,false,oldkey,true); 162 wait.dispose(); 163 JOptionPane.showMessageDialog(this,Lang.gs("invalid new key")+"\r\n"+ID+"\r\n"+Lang.gs("key_fingerprint")+StringUtils.encodeHex(key.getKeyID()),Lang.gs("profile")+" "+ID,JOptionPane.WARNING_MESSAGE); 164 return; 165 } 166 167 if(key.getG().equals(oldkey.getG()) && key.getP().equals(oldkey.getP()) && key.getQ().equals(oldkey.getQ()) && key.getY().equals(oldkey.getY()) ){ 169 WhisperIM.MainWindow.Keys.put("jabber"+ID,key); 171 saveNewKey(key); 172 profilePane.set(p,false,key,true); 173 wait.dispose(); 174 GUI.showInfo(this,"profile","new key",Lang.gs("key_fingerprint")+StringUtils.encodeHex(key.getKeyID()) ); 175 return; 176 } 177 178 wait.dispose(); 180 if( JOptionPane.showConfirmDialog(this,Lang.gs("different new key")+"\r\n"+ID+" "+StringUtils.encodeHex(key.getKeyID()),Lang.gs("profile")+" "+ID, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)==JOptionPane.YES_OPTION ){ 181 profilePane.set(p,false,key,true); 182 WhisperIM.MainWindow.Keys.put("jabber"+ID,key); saveNewKey(key); 184 } 185 186 profilePane.set(p,false,oldkey,true); 188 } 189 190 191 private void saveNewKey(PublicKey key){ 192 String filename=null; 194 Vector ids=whisper.Parse.getTags(key.getID()); 195 String protocol; 196 String uid; 197 boolean foundfilename=false; 198 int i=0; 199 String tag; 200 while (i<ids.size() && (!foundfilename)){ tag=((String )ids.elementAt(i)).trim(); 202 protocol=tag.substring(1,tag.indexOf(">")); 203 uid=whisper.Parse.getTag(key.getID(),protocol); 204 filename=(String )WhisperIM.MainWindow.keyFilenames.get(protocol+uid); 205 if(filename!=null){ 206 foundfilename=true; 207 } 208 i++; 209 } 210 if(filename==null){ filename="wpk"+WhisperIM.MainWindow.getNextKeyID()+".dat"; 212 } 213 WhisperIM.MainWindow.saveKey(key,filename); 214 } 215 216 217 private boolean areEqual(byte[] one,byte[] two){ 218 if(one==null && two==null){ 219 return true; 220 } 221 if( (one==null && two!=null) || (one!=null && two==null) ){ 222 return false; 223 } 224 if(one.length!=two.length){ 225 return false; 226 } 227 for(int i=0;i<one.length;i++){ 228 if(one[i]!=two[i]){ 229 return false; 230 } 231 } 232 return true; 233 } 234 235 final class BtnClick implements ActionListener{ 236 public void actionPerformed(ActionEvent ae){ 237 String b=ae.getActionCommand(); 239 if(b.equals(closeBtn.getText())){ 241 dispose(); 242 return; 243 } 244 if(b.equals(refreshBtn.getText())){ 245 doRefresh(); 246 return; 247 } 248 } 249 } 250 251 final class FetchThread extends Thread { 252 253 public FetchThread(){ 254 super("FetchThread in ProfileWindow class"); 255 setDaemon(true); 256 setPriority(Thread.MIN_PRIORITY); 257 } 258 259 public void run(){ 260 VCard vcard; 261 try{ 262 vcard=VCard.fetch(WhisperIM.MainWindow.Conn,ID); 263 } 264 catch (final XMPPException xe){ 265 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 266 public void run(){ 267 wait.dispose(); 268 GUI.showError(thisDialog,"profile","could not get profile",ID,xe); 269 } 270 }); 271 return; 272 } 273 final VCard finalvcard=vcard; 274 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 275 public void run(){ 276 doFetched(finalvcard); 277 } 278 }); 279 } 280 } 281 } | Popular Tags |