Formatting.
[jSite.git] / src / de / todesbaum / jsite / gui / UpdateChecker.java
index d151ed0..44be635 100644 (file)
@@ -25,8 +25,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
-import javax.swing.JFrame;
-
 import de.todesbaum.jsite.application.Freenet7Interface;
 import de.todesbaum.jsite.main.Main;
 import de.todesbaum.jsite.main.Version;
@@ -70,9 +68,6 @@ public class UpdateChecker implements Runnable {
        /** Last found version. */
        private Version lastVersion = Main.getVersion();
 
-       /** The parent of the dialog. */
-       private final JFrame parent;
-
        /** The freenet interface. */
        private final Freenet7Interface freenetInterface;
 
@@ -80,13 +75,10 @@ public class UpdateChecker implements Runnable {
         * Creates a new update checker that uses the given frame as its parent and
         * communications via the given freenet interface.
         *
-        * @param parent
-        *            The parent of the dialog
         * @param freenetInterface
         *            The freenet interface
         */
-       public UpdateChecker(JFrame parent, Freenet7Interface freenetInterface) {
-               this.parent = parent;
+       public UpdateChecker(Freenet7Interface freenetInterface) {
                this.freenetInterface = freenetInterface;
        }
 
@@ -94,14 +86,34 @@ public class UpdateChecker implements Runnable {
        // EVENT LISTENER MANAGEMENT
        //
 
+       /**
+        * Adds an update listener to the list of registered listeners.
+        *
+        * @param updateListener
+        *            The update listener to add
+        */
        public void addUpdateListener(UpdateListener updateListener) {
                updateListeners.add(updateListener);
        }
 
+       /**
+        * Removes the given listener from the list of registered listeners.
+        *
+        * @param updateListener
+        *            The update listener to remove
+        */
        public void removeUpdateListener(UpdateListener updateListener) {
                updateListeners.remove(updateListener);
        }
 
+       /**
+        * Notifies all listeners that a version was found.
+        *
+        * @param foundVersion
+        *            The version that was found
+        * @param versionTimestamp
+        *            The timestamp of the version
+        */
        protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
                for (UpdateListener updateListener : updateListeners) {
                        updateListener.foundUpdateData(foundVersion, versionTimestamp);
@@ -109,13 +121,32 @@ public class UpdateChecker implements Runnable {
        }
 
        //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the latest version that was found.
+        *
+        * @return The latest found version
+        */
+       public Version getLatestVersion() {
+               return lastVersion;
+       }
+
+       //
        // ACTIONS
        //
 
+       /**
+        * Starts the update checker.
+        */
        public void start() {
                new Thread(this).start();
        }
 
+       /**
+        * Stops the update checker.
+        */
        public void stop() {
                synchronized (syncObject) {
                        shouldStop = true;
@@ -127,12 +158,25 @@ public class UpdateChecker implements Runnable {
        // PRIVATE METHODS
        //
 
+       /**
+        * Returns whether the update checker should stop.
+        *
+        * @return <code>true</code> if the update checker should stop,
+        *         <code>false</code> otherwise
+        */
        private boolean shouldStop() {
                synchronized (syncObject) {
                        return shouldStop;
                }
        }
 
+       /**
+        * Creates the URI of the update file for the given edition.
+        *
+        * @param edition
+        *            The edition number
+        * @return The URI for the update file for the given edition
+        */
        private String constructUpdateKey(int edition) {
                return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
        }
@@ -141,6 +185,9 @@ public class UpdateChecker implements Runnable {
        // INTERFACE Runnable
        //
 
+       /**
+        * {@inheritDoc}
+        */
        public void run() {
                Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
                try {
@@ -149,10 +196,13 @@ public class UpdateChecker implements Runnable {
                        e1.printStackTrace();
                }
                Client client = new Client(connection);
+               boolean checkNow = false;
+               int currentEdition = lastUpdateEdition;
                while (!shouldStop()) {
-                       System.out.println("Trying " + constructUpdateKey(lastUpdateEdition));
+                       checkNow = false;
+                       System.out.println("Trying " + constructUpdateKey(currentEdition));
                        ClientGet clientGet = new ClientGet("get-update-key");
-                       clientGet.setUri(constructUpdateKey(lastUpdateEdition));
+                       clientGet.setUri(constructUpdateKey(currentEdition));
                        clientGet.setPersistence(Persistence.CONNECTION);
                        clientGet.setReturnType(ReturnType.direct);
                        clientGet.setVerbosity(Verbosity.ALL);
@@ -173,7 +223,9 @@ public class UpdateChecker implements Runnable {
                                                        }
                                                        if (editionNumber != -1) {
                                                                System.out.println("Found new edition " + editionNumber);
+                                                               currentEdition = editionNumber;
                                                                lastUpdateEdition = editionNumber;
+                                                               checkNow = true;
                                                                break;
                                                        }
                                                }
@@ -193,17 +245,19 @@ public class UpdateChecker implements Runnable {
                                                if (foundVersionString != null) {
                                                        Version foundVersion = Version.parse(foundVersionString);
                                                        if (foundVersion != null) {
-                                                               if (foundVersion.compareTo(Main.getVersion()) > 0) {
-                                                                       lastVersion = foundVersion;
-                                                                       String versionTimestampString = properties.getProperty("jSite.Date");
-                                                                       long versionTimestamp = -1;
-                                                                       try {
-                                                                               versionTimestamp = Long.parseLong(versionTimestampString);
-                                                                       } catch (NumberFormatException nfe1) {
-                                                                               /* ignore. */
-                                                                       }
-                                                                       fireUpdateFound(foundVersion, versionTimestamp);
+                                                               lastVersion = foundVersion;
+                                                               String versionTimestampString = properties.getProperty("jSite.Date");
+                                                               System.out.println(versionTimestampString);
+                                                               long versionTimestamp = -1;
+                                                               try {
+                                                                       versionTimestamp = Long.parseLong(versionTimestampString);
+                                                               } catch (NumberFormatException nfe1) {
+                                                                       /* ignore. */
                                                                }
+                                                               fireUpdateFound(foundVersion, versionTimestamp);
+                                                               stop = true;
+                                                               checkNow = true;
+                                                               ++currentEdition;
                                                        }
                                                }
                                        }
@@ -212,6 +266,16 @@ public class UpdateChecker implements Runnable {
                                System.out.println("Got IOException: " + e.getMessage());
                                e.printStackTrace();
                        }
+                       if (!checkNow && !shouldStop()) {
+                               synchronized (syncObject) {
+                                       try {
+                                               syncObject.wait(15 * 60 * 1000);
+                                       } catch (InterruptedException ie1) {
+                                               /* ignore. */
+                                       }
+                               }
+                       }
                }
        }
+
 }