KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ProfileWindow


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.*;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.swing.tree.*;
8 import javax.accessibility.Accessible JavaDoc;
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 /** Window that shows a profile.*/
17 public final class ProfileWindow extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, 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 JavaDoc ID;
30     
31     final BtnClick click=new BtnClick();
32     
33     final ProfileWindow thisDialog=this;
34     
35     public ProfileWindow(String JavaDoc id,Component c){
36         super(WhisperIM.MainWindow,"",false);
37         //this.ID=StringUtils.parseBareAddress(id.toLowerCase());
38
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         // see if profile is in cache
61
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); //NB only valid keys are cached so it is safe to assume true
66
}
67         // help key
68
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
69
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         // cache profile
94
WhisperIM.MainWindow.Profiles.put("jabber"+ID,p);
95         boolean keyvalid=false;
96         // get the key
97
PublicKey key=vcard.getKey();
98         // see if already had a key
99
PublicKey oldkey=(PublicKey)WhisperIM.MainWindow.Keys.get("jabber"+ID);
100         if (oldkey==null){ // no old key
101
if(key!=null){
102                 // check valid and cache if so
103
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){ //no new key leave as is
121
profilePane.set(p,false,oldkey,true);
122             wait.dispose();
123             return;
124         }
125         
126         // Ok, we have a old key in cache and a different key in vcard
127
// After check the new key is valid,
128
// we compare the DSS signing elements.
129
// If they're the same and the date stamp match we'll accept the new key
130
// and inform the user.
131
// Otherwise we alert the user and recommend not to accept the new key.
132

133         //check basic validity of new key
134
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         // compare dates
148
java.util.GregorianCalendar JavaDoc olddate;
149         java.util.GregorianCalendar JavaDoc newdate;
150         try{
151             olddate=whisper.Util.parseTime(oldkey.date());
152             newdate=whisper.Util.parseTime(key.date());
153         }
154         catch(Exception JavaDoc e){ //shouldnt happen - isKeyValid checks for bad format of timestamp
155
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         //compare DSS values
168
if(key.getG().equals(oldkey.getG()) && key.getP().equals(oldkey.getP()) && key.getQ().equals(oldkey.getQ()) && key.getY().equals(oldkey.getY()) ){
169             // update cache
170
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         // show warning
179
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); //accept new key
183
saveNewKey(key);
184         }
185         
186         //use old key
187
profilePane.set(p,false,oldkey,true);
188     }
189     
190     
191     private void saveNewKey(PublicKey key){
192         //get the filename
193
String JavaDoc filename=null;
194         Vector ids=whisper.Parse.getTags(key.getID());
195         String JavaDoc protocol;
196         String JavaDoc uid;
197         boolean foundfilename=false;
198         int i=0;
199         String JavaDoc tag;
200         while (i<ids.size() && (!foundfilename)){ //iterate thru the id's
201
tag=((String JavaDoc)ids.elementAt(i)).trim();
202             protocol=tag.substring(1,tag.indexOf(">"));
203             uid=whisper.Parse.getTag(key.getID(),protocol);
204             filename=(String JavaDoc)WhisperIM.MainWindow.keyFilenames.get(protocol+uid);
205             if(filename!=null){
206                 foundfilename=true;
207             }
208             i++;
209         }
210         if(filename==null){ //shouldn't happen
211
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             //get button
238
String JavaDoc b=ae.getActionCommand();
239             // call method depending on button clicked
240
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 JavaDoc{
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 JavaDoc(){
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 JavaDoc(){
275                 public void run(){
276                     doFetched(finalvcard);
277                 }
278             });
279         }
280     }
281 }
Popular Tags