Removed unused exports and finished UpdateChecker.
[FinalKeyGui.git] / src / fkgui / UpdateChecker.java
1 package fkgui;
2
3 import java.io.BufferedReader;
4 import java.io.InputStreamReader;
5 import java.net.URL;
6 import java.util.Locale;
7
8 import org.eclipse.swt.widgets.Display;
9
10 import fkgui.UpdateChecker.UpdateCheckResultListener.AutoUpdaterResultEvent;
11 import fkgui.UpdateChecker.UpdateCheckResultListener.UpdateCheckResult;
12
13
14 public class UpdateChecker implements Runnable {
15         
16         //Change locale with -Duser.country=DK -Duser.language=da parms for the java command.
17         
18         static final String CHECK_URL="http://cyberstalker.dk/finalkey/gui/update.php";
19         static final String CUR_VER="0.1";
20         static final String PLATFORM=System.getProperty("os.name")+"_"+System.getProperty("os.arch");
21         static final String LANG = Locale.getDefault().getLanguage();
22
23         public static final String REQUEST_STRING = CHECK_URL + "?version="+CUR_VER+"&platform="+PLATFORM+"&lang="+LANG;
24         
25         public interface UpdateCheckResultListener {
26                 public class AutoUpdaterResultEvent
27                 {
28                         public String version;
29                         public String message;
30                         public UpdateCheckResult result;
31                         public AutoUpdaterResultEvent( UpdateCheckResult res, String ver, String msg )
32                         {
33                                 result=res;
34                                 version=ver;
35                                 message=msg;
36                         }
37                 }
38                 public enum UpdateCheckResult { NO_UPDATE, CHECK_FAILED, UPDATE_AVAILABLE };
39                 
40                 public void updateCheckFinished( AutoUpdaterResultEvent event );
41         }       
42         
43         UpdateCheckResultListener delegate;
44         public UpdateChecker( UpdateCheckResultListener del )
45         {
46                 delegate=del;
47         }
48
49         public class AutoUpdaterResultTask implements Runnable
50         {
51                 private AutoUpdaterResultEvent e;
52                 private UpdateCheckResultListener d;
53                 public AutoUpdaterResultTask( AutoUpdaterResultEvent event, UpdateCheckResultListener delegate )
54                 {
55                         e=event;
56                         d=delegate;
57                 }
58                 @Override
59                 public void run() {
60                         d.updateCheckFinished(e);                       
61                 }
62         }
63         
64         @Override
65         public void run() {
66                 UpdateCheckResult state = UpdateCheckResult.CHECK_FAILED;
67                 String res="";
68                 String ver="No Version";
69                 String msg="No Update";
70                 System.out.println("Checking for new version: "+REQUEST_STRING);
71                 try {
72                         URL url = new URL(REQUEST_STRING);
73                         BufferedReader  in = new BufferedReader ( new InputStreamReader( url.openStream() ) );
74                         res=in.readLine();
75                         in.close();
76                         state = UpdateCheckResult.NO_UPDATE;
77
78                         String[] lines = res.split( "," );
79
80                         if( lines[0].compareTo("1")==0 )
81                         {
82                                 state = UpdateCheckResult.UPDATE_AVAILABLE;
83                                 ver = lines[1];
84                                 msg = lines[2];
85                                 msg=msg.replace("<br>", "\n");
86                                 msg.trim();
87
88                         }
89
90                 } catch (Exception e) {
91                         System.out.println("Trouble checking for new version: "+e.getMessage() );
92                 }
93
94                 Display.getDefault().asyncExec( new AutoUpdaterResultTask(new AutoUpdaterResultEvent(state , ver, msg), delegate) );
95                 
96         }
97
98 }