1 /*
2  * Created on 20 mai 2004
3  * Created by Olivier Chalouhi
4  *
5  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details ( see the LICENSE file ).
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 package org.gudy.azureus2.ui.swt.updater2;
22 
23 import java.util.Map;
24 
25 import org.eclipse.swt.SWT;
26 
27 import org.gudy.azureus2.core3.config.COConfigurationManager;
28 import org.gudy.azureus2.core3.logging.*;
29 import org.gudy.azureus2.core3.util.AEVerifier;
30 import org.gudy.azureus2.core3.util.Constants;
31 import org.gudy.azureus2.platform.PlatformManager;
32 import org.gudy.azureus2.platform.PlatformManagerCapabilities;
33 import org.gudy.azureus2.platform.PlatformManagerFactory;
34 
35 import com.aelitis.azureus.core.versioncheck.VersionCheckClient;
36 
37 import org.gudy.azureus2.plugins.update.UpdateChecker;
38 
39 
40 /**
41  * @author Olivier Chalouhi
42  *
43  */
44 public class SWTVersionGetter {
45 	private static final LogIDs LOGID = LogIDs.GUI;
46 
47   private String platform;
48   private int currentVersion;
49   private int latestVersion;
50   private UpdateChecker	checker;
51 
52   private String[] mirrors;
53 
54 	private String infoURL;
55 
56   public
SWTVersionGetter( UpdateChecker _checker )57   SWTVersionGetter(
58   		UpdateChecker	_checker )
59   {
60     this.platform 		= COConfigurationManager.getStringParameter("ConfigView.section.style.swt.library.selection");
61     if (this.platform == null || this.platform.length() == 0) {
62     	this.platform = SWT.getPlatform();
63     }
64     this.currentVersion = SWT.getVersion();
65 
66 
67     this.latestVersion = 0;
68     checker	= _checker;
69   }
70 
needsUpdate()71   public boolean needsUpdate() {
72     try {
73       downloadLatestVersion();
74 
75       String msg = "SWT: current version = " + currentVersion + ", latest version = " + latestVersion;
76 
77       checker.reportProgress( msg );
78 
79       if (Logger.isEnabled())
80 				Logger.log(new LogEvent(LOGID, msg));
81 
82       return latestVersion > currentVersion;
83     } catch(Exception e) {
84       e.printStackTrace();
85       return false;
86     }
87   }
88 
downloadLatestVersion()89   private void downloadLatestVersion() {
90   	if (Logger.isEnabled())
91 			Logger.log(new LogEvent(LOGID, "Requesting latest SWT version "
92 					+ "and url from version check client."));
93 
94     Map reply = VersionCheckClient.getSingleton().getVersionCheckInfo(VersionCheckClient.REASON_CHECK_SWT);
95 
96     String msg = "SWT version check received:";
97 
98     boolean	done = false;
99 
100     if ( Constants.isOSX_10_5_OrHigher ){
101 
102     	String target_lib = COConfigurationManager.getStringParameter( "ConfigView.section.style.swt.library.selection" );
103 
104     	String current_lib = SWT.getPlatform();
105 
106     	if ( target_lib.equalsIgnoreCase( current_lib )){
107 
108     	    byte[] version_bytes = (byte[])reply.get( "swt_version_" + target_lib );
109 
110     	    if ( version_bytes != null ){
111 
112     	    	latestVersion = Integer.parseInt( new String( version_bytes ) );
113 
114     	    	msg += " version=" + latestVersion;
115 
116         		byte[] url_bytes = (byte[])reply.get( "swt_url_" + target_lib );
117 
118         		if ( url_bytes != null ){
119 
120            			mirrors = new String[] { new String( url_bytes ) };
121 
122         			msg += " url=" + mirrors[0];
123         		}
124 
125         		done = true;
126     	    }
127     	}else{
128 
129     		byte[] url_bytes = (byte[])reply.get( "swt_url_" + target_lib );
130 
131     		if ( url_bytes != null ){
132 
133     			msg += " (platform switch from " + current_lib + " to " + target_lib + ")";
134 
135     			mirrors = new String[] { new String( url_bytes ) };
136 
137     			msg += " url=" + mirrors[0];
138 
139     			latestVersion = Integer.MAX_VALUE;
140 
141     			done = true;
142     		}
143     	}
144     }
145 
146     if ( !done ){
147 
148 	    byte[] version_bytes = (byte[])reply.get( "swt_version" );
149 	    if( version_bytes != null ) {
150 	      latestVersion = Integer.parseInt( new String( version_bytes ) );
151 	      msg += " version=" + latestVersion;
152 	    }
153 
154 	    byte[] url_bytes = (byte[])reply.get( "swt_url" );
155 	    if( url_bytes != null ) {
156 	      mirrors = new String[] { new String( url_bytes ) };
157 	      msg += " url=" + mirrors[0];
158 	    }
159     }
160 
161     byte[] info_bytes = (byte[])reply.get( "swt_info_url" );
162 
163     if ( info_bytes != null ){
164 
165     	byte[] sig = (byte[])reply.get( "swt_info_sig" );
166 
167 		if ( sig == null ){
168 
169 			Logger.log( new LogEvent( LogIDs.LOGGER, "swt info signature check failed - missing signature" ));
170 
171 		}else{
172 
173 		  	try{
174 	    		infoURL = new String( info_bytes );
175 
176 	    		try{
177 					AEVerifier.verifyData( infoURL, sig );
178 
179 				}catch( Throwable e ){
180 
181 					Logger.log( new LogEvent( LogIDs.LOGGER, "swt info signature check failed", e  ));
182 
183 					infoURL = null;
184 				}
185 	    	}catch ( Exception e ){
186 
187 	    		Logger.log(new LogEvent(LOGID, "swt info_url", e));
188 	    	}
189 		}
190     }
191 
192     if (Logger.isEnabled())
193 			Logger.log(new LogEvent(LOGID, msg));
194   }
195 
196 
197 
198 
199   /**
200    * @return Returns the latestVersion.
201    */
getLatestVersion()202   public int getLatestVersion() {
203     return latestVersion;
204   }
205 
getCurrentVersion()206   public int getCurrentVersion() {
207 	    return currentVersion;
208   }
209   /**
210    * @return Returns the platform.
211    */
getPlatform()212   public String getPlatform() {
213     return platform;
214   }
215 
216   /**
217    * @return Returns the mirrors.
218    */
getMirrors()219   public String[] getMirrors() {
220     return mirrors;
221   }
222 
getInfoURL()223 	public String getInfoURL() {
224 		return infoURL;
225 	}
226 }
227