Snapshot before changing swt list to comboviewer
[FinalKeyGui.git] / src / fkgui / UpdateChecker.java
1 package fkgui;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.net.URL;
8
9 import org.eclipse.swt.widgets.Display;
10
11 import fkgui.UpdateChecker.AutoUpdaterResultListener.AutoUpdaterResultEvent;
12 import fkgui.UpdateChecker.AutoUpdaterResultListener.UpdateCheckResult;
13
14
15 public class UpdateChecker implements Runnable {
16         
17         static final String CHECK_URL="http://cyberstalker.dk/finalkey/gui/update.php";
18         static final String CUR_VER="0.0";
19         static final String PLATFORM="lin_x64";
20         static final String LANG = "US";
21
22         public static final String REQUEST_STRING = CHECK_URL + "?version="+CUR_VER+"&platform="+PLATFORM+"&lang="+LANG;
23         
24         public interface AutoUpdaterResultListener {
25                 public class AutoUpdaterResultEvent
26                 {
27                         public String version;
28                         public String message;
29                         public UpdateCheckResult result;
30                         public AutoUpdaterResultEvent( UpdateCheckResult res, String ver, String msg )
31                         {
32                                 result=res;
33                                 version=ver;
34                                 message=msg;
35                         }
36                 }
37                 public enum UpdateCheckResult { NO_UPDATE, CHECK_FAILED, UPDATE_AVAILABLE };
38                 
39                 public void updateCheckFinished( AutoUpdaterResultEvent event );
40         }       
41         
42         AutoUpdaterResultListener delegate;
43         public UpdateChecker( AutoUpdaterResultListener del )
44         {
45                 delegate=del;
46         }
47
48         public class AutoUpdaterResultTask implements Runnable
49         {
50                 private AutoUpdaterResultEvent e;
51                 private AutoUpdaterResultListener d;
52                 public AutoUpdaterResultTask( AutoUpdaterResultEvent event, AutoUpdaterResultListener delegate )
53                 {
54                         e=event;
55                         d=delegate;
56                 }
57                 @Override
58                 public void run() {
59                         d.updateCheckFinished(e);                       
60                 }
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
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 }