right-click = go back
[fanfix.git] / src / be / nikiroo / fanfix / Main.java
index b49cf0fb13e38338c293166027882e3df3876e67..7be305a0977abc17c0fd227f10d1f18c97dca70a 100644 (file)
@@ -19,6 +19,7 @@ import be.nikiroo.fanfix.library.CacheLibrary;
 import be.nikiroo.fanfix.library.LocalLibrary;
 import be.nikiroo.fanfix.library.RemoteLibrary;
 import be.nikiroo.fanfix.library.RemoteLibraryServer;
+import be.nikiroo.fanfix.library.WebLibraryServer;
 import be.nikiroo.fanfix.output.BasicOutput;
 import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.fanfix.reader.BasicReader;
@@ -28,7 +29,7 @@ import be.nikiroo.fanfix.supported.BasicSupport;
 import be.nikiroo.fanfix.supported.SupportType;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Version;
-import be.nikiroo.utils.serial.server.ServerObject;
+import be.nikiroo.utils.VersionCheck;
 
 /**
  * Main program entry point.
@@ -78,7 +79,7 @@ public class Main {
         * <li>--version: get the version of the program</li>
         * <li>--server: start the server mode (see config file for parameters)</li>
         * <li>--stop-server: stop the running server on this port if any</li>
-        * <li>--remote [key] [host] [port]: use the given remote library</li>
+        * <li>--remote [key] [host] [port]: use the given remote library</li>
         * </ul>
         * 
         * @param args
@@ -126,6 +127,9 @@ public class Main {
 
                int exitCode = 0;
                for (int i = 0; exitCode == 0 && i < args.length; i++) {
+                       if (args[i] == null)
+                               continue;
+
                        // Action (--) handling:
                        if (!noMoreActions && args[i].startsWith("--")) {
                                if (args[i].equals("--")) {
@@ -394,8 +398,10 @@ public class Main {
                if (exitCode == 0) {
                        switch (action) {
                        case IMPORT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                try {
                                        exitCode = imprt(BasicReader.getUrl(urlString), pg);
@@ -406,8 +412,10 @@ public class Main {
                                
                                break;
                        case EXPORT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                OutputType exportType = OutputType.valueOfNullOkUC(sourceString, null);
                                if (exportType == null) {
@@ -420,8 +428,10 @@ public class Main {
                                
                                break;
                        case CONVERT:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                OutputType convertType = OutputType.valueOfAllOkUC(sourceString, null);
                                if (convertType == null) {
@@ -596,8 +606,10 @@ public class Main {
                                exitCode = 0;
                                break;
                        case VERSION:
-                               if (updates != null)
-                                       updates.ok(); // we consider it read
+                               if (updates != null) {
+                                       // we consider it read
+                                       Instance.getInstance().setVersionChecked(); 
+                               }
                                
                                System.out
                                                .println(String.format("Fanfix version %s"
@@ -614,15 +626,8 @@ public class Main {
                                }
                                break;
                        case SERVER:
-                               key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY);
-                               port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT);
-                               if (port == null) {
-                                       System.err.println("No port configured in the config file");
-                                       exitCode = 15;
-                                       break;
-                               }
                                try {
-                                       startServer(key, port);
+                                       startServer();
                                } catch (IOException e) {
                                        Instance.getInstance().getTraceHandler().error(e);
                                }
@@ -687,10 +692,13 @@ public class Main {
        }
 
        /**
-        * Will check if updates are available.
+        * Will check if updates are available, synchronously.
         * <p>
         * For this, it will simply forward the call to
         * {@link Main#checkUpdates(String)} with a value of "nikiroo/fanfix".
+        * <p>
+        * You may want to override it so you call the forward method with the right
+        * parameters (or also if you want it to be asynchronous).
         * 
         * @return the newer version information or NULL if nothing new
         */
@@ -710,12 +718,17 @@ public class Main {
         * @return the newer version information or NULL if nothing new
         */
        protected VersionCheck checkUpdates(String githubProject) {
-               VersionCheck updates = VersionCheck.check(githubProject);
-               if (updates.isNewVersionAvailable()) {
-                       notifyUpdates(updates);
-                       return updates;
+               try {
+                       VersionCheck updates = VersionCheck.check(githubProject,
+                                       Instance.getInstance().getTrans().getLocale());
+                       if (updates.isNewVersionAvailable()) {
+                               notifyUpdates(updates);
+                               return updates;
+                       }
+               } catch (IOException e) {
+                       // Maybe no internet. Do not report any update.
                }
-               
+
                return null;
        }
 
@@ -724,9 +737,9 @@ public class Main {
         * <p>
         * Will only be called when a version is available.
         * <p>
-        * Note that you can call {@link VersionCheck#ok()} on it if the user has
-        * read the information (by default, it is marked read only on certain other
-        * actions).
+        * Note that you can call {@link Instance#setVersionChecked()} on it if the
+        * user has read the information (by default, it is marked read only on
+        * certain other actions).
         * 
         * @param updates
         *            the new version information
@@ -1017,20 +1030,29 @@ public class Main {
        /**
         * Start a Fanfix server.
         * 
-        * @param key
-        *            the key taht will be needed to contact the Fanfix server
-        * @param port
-        *            the port on which to run
-        * 
         * @throws IOException
         *             in case of I/O errors
         * @throws SSLException
         *             when the key was not accepted
         */
-       private void startServer(String key, int port) throws IOException {
-               ServerObject server = new RemoteLibraryServer(key, port);
-               server.setTraceHandler(Instance.getInstance().getTraceHandler());
-               server.run();
+       private void startServer() throws IOException {
+               String mode = Instance.getInstance().getConfig()
+                               .getString(Config.SERVER_MODE, "fanfix");
+               if (mode.equals("fanfix")) {
+                       RemoteLibraryServer server = new RemoteLibraryServer();
+                       server.setTraceHandler(Instance.getInstance().getTraceHandler());
+                       server.run();
+               } else if (mode.equals("http")) {
+                       WebLibraryServer server = new WebLibraryServer(false);
+                       server.setTraceHandler(Instance.getInstance().getTraceHandler());
+                       server.run();
+               } else if (mode.equals("https")) {
+                       WebLibraryServer server = new WebLibraryServer(true);
+                       server.setTraceHandler(Instance.getInstance().getTraceHandler());
+                       server.run();
+               } else {
+                       throw new IOException("Unknown server mode: " + mode);
+               }
        }
 
        /**