-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionManager.java
More file actions
111 lines (93 loc) · 4.01 KB
/
VersionManager.java
File metadata and controls
111 lines (93 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* VersionManager.java
*
* Checks for updated versions. Reads a Version object from the network in
* XMLEncoded format.
*
* @author Guy Wittig
* @version 09-Apr-2010
*
* This program is part of MV-Plan
* Copywrite 2006 Guy Wittig
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* The GNU General Public License can be read at http://www.gnu.org/licenses/licenses.html
*/
package mvplan.updater;
import mvplan.main.*;
import mvplan.util.Version;
import java.util.Calendar;
import java.io.*;
import java.net.*;
import java.beans.XMLDecoder;
public class VersionManager {
public final static int UNDEFINED=0;
public final static int CURRENT=1;
public final static int UPDATE=2;
public final static int ERROR=-1;
private int resultCode;
/** Creates a new instance of VersionManager */
public VersionManager() {
resultCode=UNDEFINED;
}
public int getResultCode() { return resultCode; }
public boolean updateRequired() {
// Check if we have done this recently
if(Mvplan.prefs.isUpdateCheckDisable()) return false;
Calendar cal=Calendar.getInstance();
cal.add(Calendar.DATE, -Mvplan.prefs.getUpdateCheckFrequency());
// Check when last done. Zero means always check.
if(Mvplan.prefs.getUpdateCheckFrequency()>0 && Mvplan.prefs.getLastUpdateCheck().after(cal.getTime())) {
if(Mvplan.DEBUG>0) System.out.println("Version Manager is skipping check for now.");
return false;
}
return true;
}
public void updateCheck() {
Version latestVersion;
String url;
// Build url of the form: "http://services.wittig.net.au/mvplan/Version?command=current"
url = Mvplan.prefs.getUpdateVersionURL()+"?command=current";
try {
latestVersion = readURL(url);
} catch (Exception e) {
if(Mvplan.DEBUG>0) System.err.println("Failed to read from network.\n");
resultCode=ERROR;
return;
}
if(Mvplan.DEBUG>0) System.out.println("Read version: "+latestVersion.toString()+'\n');
// If we get here we were successful. Update last check time
Mvplan.prefs.setLastUpdateCheck(Calendar.getInstance().getTime());
if(Mvplan.DEBUG>0) System.out.println("Current version is "+Mvplan.mvplanVersion.toString()+". Latest version is "+latestVersion.toString());
if(Mvplan.mvplanVersion.compareTo(latestVersion)>0 ) {
if(Mvplan.DEBUG>0) System.out.println("Version Manager has determined that a newer version is available.");
resultCode=UPDATE;
return;
} else {
if(Mvplan.DEBUG>0) System.out.println("Version Manager has determined that you are using the latest version.");
resultCode=CURRENT;
return;
}
}
public Version readURL(String url) throws Exception {
URL target = new URL(url);
URLConnection targetConnection = target.openConnection();
// Java 1.5 only
targetConnection.setConnectTimeout(15000);
XMLDecoder decoder = new XMLDecoder(
new BufferedInputStream(
targetConnection.getInputStream()));
Version v = (Version)decoder.readObject();
decoder.close();
return v;
}
}