jvcard remote support (initial commit, not ready for use yet)
[jvcard.git] / src / be / nikiroo / jvcard / tui / Main.java
index 6f12a2b7dba4c161daa1f6a029ea89c070ee9522..bf6ab4ba2c589a026d0285cde03fe8e397dc9f2f 100644 (file)
@@ -3,12 +3,16 @@ package be.nikiroo.jvcard.tui;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Field;
+import java.net.Socket;
 import java.nio.charset.Charset;
 import java.util.LinkedList;
 import java.util.List;
 
+import be.nikiroo.jvcard.Card;
 import be.nikiroo.jvcard.i18n.Trans;
 import be.nikiroo.jvcard.i18n.Trans.StringId;
+import be.nikiroo.jvcard.remote.Command.Verb;
+import be.nikiroo.jvcard.remote.SimpleSocket;
 import be.nikiroo.jvcard.resources.Bundles;
 import be.nikiroo.jvcard.tui.panes.FileList;
 
@@ -24,6 +28,11 @@ import com.googlecode.lanterna.input.KeyStroke;
  *
  */
 public class Main {
+       // TODO: move Main to be.nikiroo.jvcard, use introspection to load the other
+       // main classes, allow the 3 programs to run from this new Main
+       // also requires StringUtils/... in a new package
+       private int TODO;
+
        public static final String APPLICATION_TITLE = "jVcard";
        public static final String APPLICATION_VERSION = "1.0-beta2-dev";
 
@@ -93,7 +102,8 @@ public class Main {
                                                                + "\t--noutf: force non-utf8 mode if you need it\n"
                                                                + "\t--config DIRECTORY: force the given directory as a CONFIG_DIR\n"
                                                                + "everyhing else is either a file to open or a directory to open\n"
-                                                               + "(we will only open 1st level files in given directories)\n");
+                                                               + "(we will only open 1st level files in given directories)\n"
+                                                               + "('jvcard://hostname:8888/file' links -- or without 'file' -- are also ok)\n");
                                return;
                        } else if (!noMoreParams && arg.equals("--tui")) {
                                textMode = true;
@@ -131,6 +141,8 @@ public class Main {
                        files.addAll(open("."));
                }
 
+               // TODO error case when no file
+
                Window win = new MainWindow(new FileList(files));
 
                try {
@@ -153,18 +165,61 @@ public class Main {
        static private List<String> open(String path) {
                List<String> files = new LinkedList<String>();
 
-               File file = new File(path);
-               if (file.exists()) {
-                       if (file.isDirectory()) {
-                               for (File subfile : file.listFiles()) {
-                                       if (!subfile.isDirectory())
-                                               files.add(subfile.getAbsolutePath());
-                               }
+               if (path != null && path.startsWith("jvcard://")) {
+                       if (path.endsWith("/")) {
+                               files.addAll(list(path));
                        } else {
-                               files.add(file.getAbsolutePath());
+                               files.add(path);
                        }
                } else {
-                       System.err.println("File or directory not found: \"" + path + "\"");
+                       File file = new File(path);
+                       if (file.exists()) {
+                               if (file.isDirectory()) {
+                                       for (File subfile : file.listFiles()) {
+                                               if (!subfile.isDirectory())
+                                                       files.add(subfile.getAbsolutePath());
+                                       }
+                               } else {
+                                       files.add(file.getAbsolutePath());
+                               }
+                       } else {
+                               System.err.println("File or directory not found: \"" + path
+                                               + "\"");
+                       }
+               }
+
+               return files;
+       }
+
+       /**
+        * List all the available {@link Card}s on the given network location (which
+        * is expected to be a jVCard remote server, obviously).
+        * 
+        * @param path
+        *            the jVCard remote server path (e.g.:
+        *            <tt>jvcard://localhost:4444/</tt>)
+        * 
+        * @return the list of {@link Card}s
+        */
+       static private List<String> list(String path) {
+               List<String> files = new LinkedList<String>();
+
+               try {
+                       String host = path.split("\\:")[1].substring(2);
+                       int port = Integer.parseInt(path.split("\\:")[2].replaceAll("/$",
+                                       ""));
+                       SimpleSocket s = new SimpleSocket(new Socket(host, port),
+                                       "sync client");
+                       s.open(true);
+
+                       s.sendCommand(Verb.LIST);
+                       for (String p : s.receiveBlock()) {
+                               files.add(path
+                                               + p.substring(StringUtils.fromTime(0).length() + 1));
+                       }
+                       s.close();
+               } catch (Exception e) {
+                       e.printStackTrace();
                }
 
                return files;