Ticket #1848: Getting Started.patch

File Getting Started.patch, 8.5 KB (added by xeen, 17 years ago)
  • src/org/openstreetmap/josm/gui/GettingStarted.java

     
    44
    55import static org.openstreetmap.josm.tools.I18n.tr;
    66
     7import java.io.BufferedReader;
     8import java.io.BufferedWriter;
     9import java.io.File;
     10import java.io.FileReader;
     11import java.io.FileWriter;
    712import java.io.IOException;
     13import java.io.Writer;
    814import java.util.ArrayList;
     15import java.util.Date;
    916import java.util.concurrent.Executors;
    1017import java.util.concurrent.ExecutorService;
    1118import java.util.concurrent.Callable;
     
    5057            }
    5158        }
    5259    }
    53 
     60   
     61    /**
     62     * This class encapsulates the "reading URL" task and can be executed in background and in
     63     * parallel. Since the MOTD is many separate pages this speeds things up quite a lot. If no
     64     * localized version is available, it automatically falls back to the international one.
     65     */
    5466    public class readMOTD implements Callable<String> {
    5567        private boolean isLocalized;
    5668        private boolean isHelp;
     
    5870        private String urlIntl;
    5971        private String urlBase;
    6072
    61         readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean isHelp) {
    62           this.isLocalized = isLocalized;
    63           this.urlBase = urlBase;
    64           this.urlLoc = urlLoc;
    65           this.urlIntl = urlIntl;
    66           this.isHelp = isHelp;
     73        /**
     74         * Read a MOTD page
     75         * @param isLocalized If true, tries to get localized version as defined in urlLoc
     76         * @param urlBase Base URL (i.e. http://www.openstreetmap.de/wiki/)
     77         * @param urlLoc Part to append to base URL to receive localized version
     78         * @param urlIntl Part to append to base URL to receive international version
     79         * @param makeList If true, the URL's contents will be wrapped in a list (<ul><li>)
     80         */
     81        readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean makeList) {
     82            this.isLocalized = isLocalized;
     83            this.urlBase = urlBase;
     84            this.urlLoc = urlLoc;
     85            this.urlIntl = urlIntl;
     86            this.isHelp = makeList;
    6787        }
    68 
     88       
     89        /*
     90         * Does the actual work
     91         * @see java.util.concurrent.Callable#call()
     92         */
    6993        public String call() {
    7094            WikiReader wr = new WikiReader(urlBase);
    7195            String content = "";
     
    80104                    if(isHelp)
    81105                        content += message;
    82106                    else
    83                         content += "<ul><li>"+ message.substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
     107                        content += "<ul><li>"+ message.substring(8)
     108                                        .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    84109            } catch (IOException ioe) {
    85110                try {
    86111                    if(isHelp)
    87112                        content += wr.read(urlIntl);
    88113                    else
    89                         content += "<ul><li>"+wr.read(urlIntl).substring(8).replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
     114                        content += "<ul><li>"+wr.read(urlIntl).substring(8)
     115                                        .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    90116                } catch (IOException ioe2) {
    91117                }
    92118            }
     
    94120            return content;
    95121        }
    96122    }
    97 
     123   
     124    /**
     125     * This is where the MOTD will be cached
     126     */
     127    final static private File cacheDir = new File(Main.pref.getPreferencesDir(), "motd.html");
     128   
     129    /**
     130     * Grabs current MOTD from cache or webpage and parses, then caches it.
     131     */
    98132    private void assignContent() {
    99         if (content.length() > 0 && Main.pref.getBoolean("help.displaymotd", true)) return;
     133        if (content.length() > 0 || !Main.pref.getBoolean("motd.display", true)) return;
     134       
     135        int myVersion = AboutAction.getVersionNumber();
     136        String languageCode = Main.getLanguageCodeU();
     137       
     138        // Try loading it from cache and quit if successful
     139        if(loadFromDisk(myVersion))
     140            return;
    100141
    101142        String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
    102143        WikiReader wr = new WikiReader(baseurl);
     
    111152                ")</h2>";
    112153        }
    113154
    114         int myVersion = AboutAction.getVersionNumber();
    115         String languageCode = Main.getLanguageCodeU();
    116 
    117155        // Finds wiki links like (underscores inserted for readability): [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE]
    118156        // Langcode usually consists of two letters describing the language and may be omitted
    119157        // Condition may be one of the following: >  <  <=  =>
     
    174212            linkContent.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
    175213        }
    176214
    177         for(int i=0; i < linkContent.size(); i++) {
     215        linkContent.add(slave.submit(new readMOTD(false, baseurl, "", baseurl + "/latest?format=txt", true)));
     216
     217        for(int i=0; i < linkContent.size()-1; i++) {
    178218            try {
    179219                content += linkContent.get(i).get();
    180220            } catch (Exception e) {}
    181221        }
    182222       
     223        String version = "<div style=\"text-align:right;font-weight:normal;font-size:small\">";
     224        try {
     225            String v = linkContent.get(linkContent.size()-1).get().trim().substring(6);
     226            version += tr("(Current version: {0}. Your version: {1})", v, myVersion);
     227        } catch(Exception e) {}
     228        version += "</div>";
     229       
    183230        linkContent.clear();
    184231        try {
    185232            slave.shutdown();
    186233        } catch(SecurityException x) {}
    187234       
    188 
    189235        content = "<html>\n"+
    190236            styles +
    191             "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+
    192             content+"\n"+
     237            "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>"+
     238            content.replace("</html>", "")+
     239            version+
    193240            "</html>";
     241       
     242        saveToDisk(myVersion);
    194243    }
     244   
     245    /**
     246     * Tries to load cached MOTD from disk if the JOSM version is still the same and the last update
     247     * is younger than motd.updateinterval milliseconds (default: 24 hours).
     248     *
     249     * @param myVersion Currently running version
     250     * @return false if update is required, true if cache was valid.
     251     */
     252    private boolean loadFromDisk(int myVersion) {
     253        if(Main.pref.getInteger("motd.lastversion", 0) != myVersion)
     254            return false;
     255        if(Long.parseLong(Main.pref.get("motd.lastupdate", "0"))
     256            + Main.pref.getInteger("motd.updateinterval", 24*60*60*1000)
     257                < new Date().getTime())
     258            return false;
    195259
     260        StringBuilder c = new StringBuilder();
     261        try {
     262            BufferedReader input =  new BufferedReader(new FileReader(cacheDir));
     263            try {
     264                String line = null;
     265                while (( line = input.readLine()) != null){
     266                    c.append(line);
     267                    c.append("\n");
     268                }
     269            } finally {
     270                input.close();
     271            }
     272        }
     273        catch (IOException ex){
     274            ex.printStackTrace();
     275        }
     276       
     277        content = c.toString();
     278        return content.length() > 0;
     279    }
     280
     281    /**
     282     * Saves current MOTD to disk and updates "last update" variables
     283     * @param myVersion currently running version
     284     */
     285    private void saveToDisk(int myVersion) {
     286        try {
     287            Writer output = new BufferedWriter(new FileWriter(
     288                    new File(Main.pref.getPreferencesDir(), "motd.html")));
     289            try {
     290                output.write(content);
     291                Main.pref.put("motd.lastupdate", Long.toString(new Date().getTime()));
     292                Main.pref.putInteger("motd.lastversion", myVersion);
     293            } finally {
     294                output.close();
     295            }
     296        } catch(Exception e) {
     297            Main.pref.putInteger("motd.lastupdate", 0);
     298        }
     299    }
     300
     301    /**
     302     * Initializes getting the MOTD as well as enabling the FileDrop Listener.
     303     * Displays a message while the MOTD is downloading.
     304     */
    196305    public GettingStarted() {
    197306        super(new BorderLayout());
    198307        final LinkGeneral lg = new LinkGeneral(