package be.nikiroo.jvcard.launcher;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
-import javax.imageio.ImageIO;
-
import be.nikiroo.jvcard.Card;
import be.nikiroo.jvcard.Contact;
import be.nikiroo.jvcard.Data;
import be.nikiroo.jvcard.resources.RemoteBundle;
import be.nikiroo.jvcard.resources.StringId;
import be.nikiroo.jvcard.resources.TransBundle;
+import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.Image;
import be.nikiroo.utils.ImageUtils;
import be.nikiroo.utils.StringUtils;
import be.nikiroo.utils.Version;
*
*/
public class Main {
+ /** The name of the program */
static public final String APPLICATION_TITLE = "jVcard";
static private final int ERR_NO_FILE = 1;
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,
}
/**
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("--")) {
}
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));
}
String b64;
- InputStream in = null;
+ Image img = new Image(IOUtils.toByteArray(f));
try {
- in = new FileInputStream(f);
- b64 = ImageUtils.toBase64(in);
+ b64 = img.toBase64();
} finally {
- if (in != null) {
- in.close();
- }
+ img.close();
}
// remove previous photos:
String filename = contact.toString(format, "");
File f = new File(dir, filename + ".png");
System.out.println("Saving " + f);
+ Image img = new Image(photo.getValue());
try {
- ImageIO.write(
- ImageUtils.fromBase64(photo.getValue()),
- "png", f);
+ ImageUtils.getInstance().saveAsImage(img, f,
+ "png");
} catch (IOException e) {
System.err.println(trans(
StringId.CLI_ERR_CANNOT_SAVE_PHOTO,
}
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());
+ 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));
System.out.println(trans(StringId.CLI_HELP_FOOTER));
System.out.println();
+ break;
}
}
}
*/
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 {
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());
}
}
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.