]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/CheckVersionScreen.java
Version 19, May 2018
[GpsPrune.git] / tim / prune / function / CheckVersionScreen.java
1 package tim.prune.function;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.text.DateFormat;
7 import java.text.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.Properties;
10 import javax.swing.JOptionPane;
11
12 import tim.prune.App;
13 import tim.prune.GenericFunction;
14 import tim.prune.GpsPrune;
15 import tim.prune.I18nManager;
16 import tim.prune.function.browser.BrowserLauncher;
17
18 /**
19  * Class to check the version of GpsPrune
20  * and show an appropriate dialog
21  */
22 public class CheckVersionScreen extends GenericFunction
23 {
24         /**
25          * Constructor
26          * @param inApp app object
27          */
28         public CheckVersionScreen(App inApp)
29         {
30                 super(inApp);
31         }
32
33         /**
34          * Get the name key
35          */
36         public String getNameKey() {
37                 return "function.checkversion";
38         }
39
40         /**
41          * Show the check version dialog
42          */
43         public void begin()
44         {
45                 final String filePathStart = "https://activityworkshop.net/software/gpsprune/gpsprune_versioncheck_";
46                 final String filePathEnd = ".txt";
47                 String latestVer = null;
48                 String nextVersion = null;
49                 String releaseDate = null;
50                 Properties props = new Properties();
51                 try
52                 {
53                         // Load properties from the url on the server
54                         InputStream inStream = new URL(filePathStart + GpsPrune.VERSION_NUMBER + filePathEnd).openStream();
55                         props.load(inStream);
56
57                         // Extract the three fields we want, ignore others
58                         latestVer = props.getProperty("prune.latestversion");
59                         nextVersion = props.getProperty("prune.nextversion");
60                         releaseDate = props.getProperty("prune.releasedate");
61                 }
62                 catch (IOException ioe) {
63                         System.err.println(ioe.getClass().getName() + " - " + ioe.getMessage());
64                 }
65
66                 if (latestVer == null) {
67                         // Couldn't get version number, show error message
68                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.checkversion.error"),
69                                 I18nManager.getText(getNameKey()), JOptionPane.ERROR_MESSAGE);
70                 }
71                 else if (latestVer.equals(GpsPrune.VERSION_NUMBER))
72                 {
73                         // Version on the server is the same as this one
74                         String displayMessage = I18nManager.getText("dialog.checkversion.uptodate");
75                         if (nextVersion != null && !nextVersion.equals(""))
76                         {
77                                 displayMessage += "\n\n" + nextVersion;
78                         }
79                         // Show information message that the current version is already running
80                         JOptionPane.showMessageDialog(_parentFrame, displayMessage,
81                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
82                 }
83                 else
84                 {
85                         // A new version is available!
86                         String displayMessage = I18nManager.getText("dialog.checkversion.newversion1") + " " + latestVer
87                                 + " " + I18nManager.getText("dialog.checkversion.newversion2");
88                         try
89                         {
90                                 if (releaseDate != null && !releaseDate.equals("")) {
91                                         displayMessage += "\n\n" + I18nManager.getText("dialog.checkversion.releasedate1") + " "
92                                                 + DateFormat.getDateInstance(DateFormat.LONG).format(new SimpleDateFormat("y-M-d").parse(releaseDate))
93                                                 + " " + I18nManager.getText("dialog.checkversion.releasedate2");
94                                 }
95                         }
96                         catch (ParseException pe) {
97                                 System.err.println("Oops, couldn't parse date: '" + releaseDate + "'");
98                         }
99                         displayMessage += "\n\n" + I18nManager.getText("dialog.checkversion.download");
100
101                         // Show information message to download the new version
102                         Object[] buttonTexts = {I18nManager.getText("button.showwebpage"), I18nManager.getText("button.cancel")};
103                         if (JOptionPane.showOptionDialog(_parentFrame, displayMessage,
104                                         I18nManager.getText(getNameKey()), JOptionPane.YES_NO_OPTION,
105                                         JOptionPane.INFORMATION_MESSAGE, null, buttonTexts, buttonTexts[1])
106                                 == JOptionPane.YES_OPTION)
107                         {
108                                 // User selected to launch home page
109                                 BrowserLauncher.launchBrowser("https://activityworkshop.net/software/gpsprune/download.html");
110                         }
111                 }
112         }
113 }