New SAVE_TO option
authorNiki Roo <niki@nikiroo.be>
Sat, 28 Apr 2018 10:53:19 +0000 (12:53 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 28 Apr 2018 10:53:19 +0000 (12:53 +0200)
src/be/nikiroo/jvcard/Data.java
src/be/nikiroo/jvcard/launcher/Main.java
src/be/nikiroo/jvcard/parsers/AbookParser.java
src/be/nikiroo/jvcard/resources/StringId.java
src/be/nikiroo/jvcard/resources/resources.properties
src/be/nikiroo/jvcard/resources/resources_en.properties
src/be/nikiroo/jvcard/resources/resources_fr.properties

index 4d3da7e4c7d37f0aa3bd1754065cfbcbdc0e4872..fa9e7c25223d5563ce12693708b24f36b90bcbe9 100644 (file)
@@ -9,7 +9,7 @@ import java.util.List;
  * basically a key/value pair with optional types and an optional group name.
  * 
  * @author niki
- *
+ * 
  */
 public class Data extends BaseClass<TypeInfo> {
        public enum DataPart {
@@ -293,4 +293,17 @@ public class Data extends BaseClass<TypeInfo> {
        public String getState() {
                return ("" + name + value + group).replace(' ', '_');
        }
+
+       @Override
+       public String toString() {
+               String out = name + ": " + value;
+               if (group != null && !group.isEmpty()) {
+                       out += " (" + group + ")";
+               }
+               if (b64 >= 0) {
+                       out += " [" + b64 + "]";
+               }
+
+               return out;
+       }
 }
index a073e92921a09c0a41b12dc5af2b718de268f7d0..0860fec1bd6f3a858cf6cb1e6fafebef7b7a9514 100644 (file)
@@ -51,7 +51,7 @@ public class Main {
        static private boolean forceComputedFn;
 
        enum Mode {
-               CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, SAVE_CONFIG, HELP
+               CONTACT_MANAGER, I18N, SERVER, LOAD_PHOTO, SAVE_PHOTO, SAVE_CONFIG, HELP, SAVE_TO,
        }
 
        /**
@@ -110,6 +110,7 @@ public class Main {
                int port = -1;
                Mode mode = Mode.CONTACT_MANAGER;
                String format = null;
+               String output = null;
                for (int index = 0; index < args.length; index++) {
                        String arg = args[index];
                        if (!noMoreParams && arg.equals("--")) {
@@ -223,6 +224,20 @@ public class Main {
                                }
 
                                format = args[index];
+                       } else if (!noMoreParams && (arg.equals("--save-to"))) {
+                               if (mode != Mode.CONTACT_MANAGER) {
+                                       SERR(StringId.CLI_SERR_MODES);
+                                       return;
+                               }
+                               mode = Mode.SAVE_TO;
+
+                               index++;
+                               if (index >= args.length) {
+                                       SERR(StringId.CLI_ERR_NOFILES);
+                                       return;
+                               }
+
+                               output = args[index];
                        } else {
                                filesTried = true;
                                files.addAll(open(arg));
@@ -421,6 +436,30 @@ public class Main {
                        }
                        break;
                }
+               case SAVE_TO: {
+                       try {
+                               Card total = new Card(null, getCardFormat(output));
+
+                               for (String file : files) {
+                                       try {
+                                               Card card = getCard(file, null).getCard();
+                                               card.unlink();
+                                               while (card.size() > 0) {
+                                                       total.add(card.remove(0));
+                                               }
+                                       } catch (IOException e) {
+                                               System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN,
+                                                               file));
+                                       }
+                               }
+
+                               total.saveAs(new File(output), getCardFormat(output));
+                       } catch (IOException e) {
+                               System.err.println(trans(StringId.CLI_ERR_CANNOT_OPEN, output));
+                       }
+
+                       break;
+               }
                case HELP: {
                        System.out.println(APPLICATION_TITLE + " "
                                        + Version.getCurrentVersion());
@@ -444,6 +483,8 @@ public class Main {
                                        + trans(StringId.CLI_HELP_MODE_LOAD_PHOTO));
                        System.out.println("\t--save-photo DIR FORMAT ... : "
                                        + trans(StringId.CLI_HELP_MODE_SAVE_PHOTO));
+                       System.out.println("\t--save-to output(.vcf) ... : "
+                                       + trans(StringId.CLI_HELP_MODE_SAVE_TO));
                        System.out.println();
 
                        System.out.println(trans(StringId.CLI_HELP_OPTIONS));
@@ -461,6 +502,7 @@ public class Main {
                        System.out.println(trans(StringId.CLI_HELP_FOOTER));
                        System.out.println();
 
+                       break;
                }
                }
        }
@@ -488,20 +530,8 @@ public class Main {
         */
        static public CardResult getCard(String input, MergeCallback callback)
                        throws IOException {
-               boolean remote = false;
-               Format format = Format.Abook;
-               String ext = input;
-               if (ext.contains(".")) {
-                       String tab[] = ext.split("\\.");
-                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
-                               format = Format.VCard21;
-                       }
-               }
-
-               if (input.contains("://")) {
-                       format = Format.VCard21;
-                       remote = true;
-               }
+               boolean remote = isFileRemote(input);
+               Format format = getCardFormat(input);
 
                CardResult card = null;
                try {
@@ -522,8 +552,10 @@ public class Main {
                        try {
                                for (Contact contact : card.getCard()) {
                                        Data name = contact.getPreferredData("FN");
+                                       Data n = contact.getPreferredData("N");
+                                       boolean hasN = n != null && n.getValue().length() > 0;
                                        if (name == null || name.getValue().length() == 0
-                                                       || forceComputedFn) {
+                                                       || (forceComputedFn && hasN)) {
                                                name.setValue(contact.toString(defaultFn, "").trim());
                                        }
                                }
@@ -536,6 +568,27 @@ public class Main {
                return card;
        }
 
+       static private boolean isFileRemote(String input) {
+               return input.contains("://");
+       }
+
+       static Format getCardFormat(String input) {
+               if (isFileRemote(input)) {
+                       return Format.VCard21;
+               }
+
+               Format format = Format.Abook;
+               String ext = input;
+               if (ext.contains(".")) {
+                       String tab[] = ext.split("\\.");
+                       if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
+                               format = Format.VCard21;
+                       }
+               }
+
+               return format;
+       }
+
        /**
         * Open the given path and add all its files if it is a directory or just
         * this one if not to the returned list.
index fc4df3613f2af8e34947712cdcb0860217b9d168..17080225b34cc5d9fa9cbc7c1a58fd69b5da8d6c 100644 (file)
@@ -110,10 +110,13 @@ public class AbookParser {
                        builder.append(notes);
                }
 
+               // abook format = one line per contact
+               String out = builder.toString().replace("\n", "\\n");
+
                // note: save as pine means normal LN, nor CRLN
-               builder.append('\n');
+               out = out + "\n";
 
-               return Arrays.asList(new String[] { builder.toString() });
+               return Arrays.asList(new String[] { out });
        }
 
        /**
@@ -153,7 +156,6 @@ public class AbookParser {
                        int startingBKey) throws IOException {
                for (String s : toStrings(contact, startingBKey)) {
                        writer.append(s);
-                       writer.append('\n');
                }
        }
 
@@ -171,7 +173,6 @@ public class AbookParser {
        public static void write(Appendable writer, Card card) throws IOException {
                for (String s : toStrings(card)) {
                        writer.append(s);
-                       writer.append('\n');
                }
        }
 }
index 1b11e50593eee8b0740538d3b51df629aacb44ff..6412f500bc4a5141e1b3ae1d6bccf78971e9ffd6 100644 (file)
@@ -72,6 +72,8 @@ public enum StringId {
        CLI_HELP_MODE_LOAD_PHOTO, //
        @Meta(description = "The Help message line for --save-photo usage")
        CLI_HELP_MODE_SAVE_PHOTO, //
+       @Meta(description = "The Help message line for --save-to usage")
+       CLI_HELP_MODE_SAVE_TO, //
        @Meta(description = "The Help message line for config save usage")
        CLI_HELP_MODE_SAVE_CONFIG, //
        @Meta(description = "The Help message line before the list of options")
index b1783d331bffef6329b368c4aac138d7d030dacc..56fe16a1304c1e79a459f748804cdcf878c69556 100644 (file)
@@ -95,6 +95,9 @@ CLI_HELP_MODE_LOAD_PHOTO = load the contact photos (following FORMAT) from DIR
 # The Help message line for --save-photo usage
 CLI_HELP_MODE_SAVE_PHOTO = save the contact photos (following FORMAT) into DIR
 # (WHAT: CLI --help)
+# The Help message line for --save-to usage
+CLI_HELP_MODE_SAVE_TO = save all the given files into the first card (.vcf means VCard, the rest is abook format)
+# (WHAT: CLI --help)
 # The Help message line for config save usage
 CLI_HELP_MODE_SAVE_CONFIG = save the configuration files into DIR
 # (WHAT: CLI --help)
index c2897564388ac9a84d5750aff716789642dd9205..4d85912b1fa290602856f9fcfe5094ae53978df1 100644 (file)
@@ -95,6 +95,9 @@ CLI_HELP_MODE_LOAD_PHOTO = load the contact photos (following FORMAT) from DIR
 # The Help message line for --save-photo usage
 CLI_HELP_MODE_SAVE_PHOTO = save the contact photos (following FORMAT) into DIR
 # (WHAT: CLI --help)
+# The Help message line for --save-to usage
+CLI_HELP_MODE_SAVE_TO = save all the given files into the first card (.vcf means VCard, the rest is abook format)
+# (WHAT: CLI --help)
 # The Help message line for config save usage
 CLI_HELP_MODE_SAVE_CONFIG = save the configuration files into DIR
 # (WHAT: CLI --help)
index e14a1fdca694c0b109495f8cdb99c7c394c7b7f2..1f24a0123a241a09625c8442f76bf5773c0c4c97 100644 (file)
@@ -95,6 +95,9 @@ CLI_HELP_MODE_LOAD_PHOTO = charge les photos des contacts (en suivant FORMAT) de
 # The Help message line for --save-photo usage
 CLI_HELP_MODE_SAVE_PHOTO = sauve les photos des contacts (en suivant FORMAT) dans DIR
 # (WHAT: CLI --help)
+# The Help message line for --save-photo usage
+CLI_HELP_MODE_SAVE_TO = sauve les fichiers dans la première carte (.vcf donne une VCard, le reste est au format abook)
+# (WHAT: CLI --help)
 # The Help message line for config save usage
 CLI_HELP_MODE_SAVE_CONFIG = exporte les fichiers de configuration dans DIR
 # (WHAT: CLI --help)