4ee7b728b356c64920ae77de17a5c0381cf32c19
[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.UpdateCheckResult;
11 import fkgui.UpdateChecker.UpdateCheckResultListener.UpdateResultEvent;
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://finalkey.net/gui/update.php"; //$NON-NLS-1$
19         static final String CUR_VER="0.5.1"; //$NON-NLS-1$
20         static final String PLATFORM=System.getProperty("os.name")+"_"+System.getProperty("os.arch"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
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; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
24
25         public interface UpdateCheckResultListener {
26                 public class UpdateResultEvent
27                 {
28                         public String version;
29                         public String message;
30                         public UpdateCheckResult result;
31                         public UpdateResultEvent( 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( UpdateResultEvent 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 UpdateResultEvent e;
52                 private UpdateCheckResultListener d;
53                 public AutoUpdaterResultTask( UpdateResultEvent 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
67                 UpdateCheckResult state = UpdateCheckResult.CHECK_FAILED;
68                 String res=""; //$NON-NLS-1$
69                 String ver="No Version"; //$NON-NLS-1$
70                 String msg="No Update"; //$NON-NLS-1$
71                 System.out.println(Messages.UpdateChecker_11+REQUEST_STRING);
72                 try {
73                         URL url = new URL(REQUEST_STRING);
74                         BufferedReader  in = new BufferedReader ( new InputStreamReader( url.openStream() ) );
75                         res=in.readLine();
76                         in.close();
77                         state = UpdateCheckResult.NO_UPDATE;
78
79                         String[] lines = res.split( "," ); //$NON-NLS-1$
80
81                         if( lines[0].compareTo("1")==0 ) //$NON-NLS-1$
82                         {
83                                 state = UpdateCheckResult.UPDATE_AVAILABLE;
84                                 ver = lines[1];
85                                 msg = lines[2];
86                                 msg=msg.replace("<br>", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
87                                 msg.trim();
88
89                         }
90
91                         in.close();
92
93                 } catch (Exception e) {
94                         System.out.println(Messages.UpdateChecker_16+e.getMessage() );
95                 }
96
97                 Display.getDefault().asyncExec( new AutoUpdaterResultTask(new UpdateResultEvent(state , ver, msg), delegate) );
98
99         }
100
101 }