001    /////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.tools;
018    
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    import java.net.MalformedURLException;
022    import java.net.URL;
023    import javax.swing.JFrame;
024    import javax.swing.JOptionPane;
025    import sun.net.www.content.text.PlainTextInputStream;
026    
027    /**
028     * Class Updater.
029     * <br><b>Summary:</b><br>
030     * This class permit to know if Sears has an available update.
031     */
032    public class Updater {
033        /**The update address.*/
034        private String address;
035    
036        /**The JFrame parent, used to display message dialog.*/
037        private JFrame parent;
038        
039        /**The default update address*/
040        public static final String DEFAULT_UPDATE_ADDRESS = "http://nico.rouviere.free.fr/Sears/version";
041    
042        /**
043         * Constructor Updater.
044         * <br><b>Summary:</b><br>
045         * Constructor of the class.
046         * This will launch a thread that try to read the given address and retrieve the version.
047         * If given version is lower that the one found at the given adress, it will show a message dialog.
048         *
049         * @param address           The address to look for the newest version number.
050         * @param currentVersion    The current version.
051         */
052        public Updater(JFrame parent, String address, double currentVersion) {
053            this.address = address;
054            this.parent = parent;
055            final double myVersion = currentVersion;
056            //construct a thread that will reach the version without bloking the GUI.
057            Thread updaterThread = new Thread("UpdaterThread") {
058                public void run() {
059                    double version = getAddressVersion();
060                    if (myVersion < version) {
061                        showUpdateAvailableMessage(version);
062                    }
063                }
064            };
065            updaterThread.start();
066        }
067    
068        /**
069         * Method showUpdateAvailableMessage.
070         * <br><b>Summary:</b><br>
071         * This method displays a message to the user about the new available version.
072         * @param version   The new version available.
073         */
074        protected void showUpdateAvailableMessage(double version) {
075            JOptionPane.showConfirmDialog(parent
076                                        , SearsResourceBundle.getResource("various_updateAvailable1")+" "+ version
077                                        + " "+SearsResourceBundle.getResource("various_updateAvailable2")+"\n"
078                                        + SearsResourceBundle.getResource("various_updateAvailable3")
079                                        ,SearsResourceBundle.getResource("various_updateAvailableTitle")
080                                        ,JOptionPane.DEFAULT_OPTION);
081        }
082    
083        /**
084         * Method getAddressVersion.
085         * <br><b>Summary:</b><br>
086         * This method retrieve the version at the updater address.
087         * @return  <b>double</b>      The found version, or -1 if an error occurs.
088         */
089        protected double getAddressVersion() {
090            //The result of the method.
091            double result = -1;
092            //Try to read a double in the found text.
093            try {
094                //retrieve the text at the given index.
095                String versionString = getAddressText();
096                result = Double.parseDouble(versionString);
097            } catch (NumberFormatException e) {
098                //we failed to get the version number return -1
099                Trace.trace("The text found is not a double at address "+address, Trace.ERROR_PRIORITY);
100            }
101            //return the result
102            return result;
103        }
104    
105        /**
106         * Method getAddressText.
107         * <br><b>Summary:</b><br>
108         * This method get the text at a given internet address.
109         * or "" if an error occurs.
110         * @return  <b>String</b>   The text found at the address.
111         */
112        public String getAddressText() {
113            //The result of the method
114            String result = "";
115            try {
116                //construc the URL to retrieve the text..
117                URL url = new URL(address);
118                //and retrieve the file at the given address.
119                InputStreamReader reader = new InputStreamReader(((PlainTextInputStream) url.getContent()));
120                int c = reader.read();
121                while (c != -1) {
122                    result += (char) c;
123                    c = reader.read();
124                }
125            } catch (MalformedURLException e) {
126                //The URL is not correct. do nothing.
127                Trace.trace("The given update address is not valid :"+address+" : "+e.getMessage(), Trace.ERROR_PRIORITY);
128            } catch (IOException e) {
129                //An error occurs during reading, do nothing.
130                Trace.trace("An exception occurs during update reading: "+e.getMessage() , Trace.ERROR_PRIORITY);
131            }
132            //return the result.
133            return result;
134        }
135    }