X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FCard.java;h=986f81d17c0932cba2081d7586b0ce115ad25547;hb=e253bd50bb05519f4a16fed4fb95d5b3340128ea;hp=65ab6fa5225b91fdacdd3430fdc52abdbb317395;hpb=0b0b2b0ff1f5e21f7b0feb955b4b54855fb3d508;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Card.java b/src/be/nikiroo/jvcard/Card.java index 65ab6fa..986f81d 100644 --- a/src/be/nikiroo/jvcard/Card.java +++ b/src/be/nikiroo/jvcard/Card.java @@ -3,10 +3,11 @@ package be.nikiroo.jvcard; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileReader; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; -import java.util.Arrays; +import java.io.InputStreamReader; +import java.security.InvalidParameterException; import java.util.LinkedList; import java.util.List; @@ -20,93 +21,244 @@ import be.nikiroo.jvcard.parsers.Parser; * @author niki * */ -public class Card { - private List contacts; +public class Card extends BaseClass { private File file; - private boolean dirty; private String name; + private Format format; + private long lastModified; + private boolean remote; + /** + * Create a new {@link Card} from the given {@link File} and {@link Format}. + * + * @param file + * the input {@link File} containing the {@link Card} data or + * NULL for an empty card (usually a {@link File} name or a + * network path) + * @param format + * the {@link Format} to use to parse it + * + * @throws IOException + * in case of IO error + * @throws InvalidParameterException + * if format is NULL + */ public Card(File file, Format format) throws IOException { - this.file = file; + this(load(file, format)); if (file != null) { - name = file.getName(); + if (file.exists()) { + lastModified = file.lastModified(); + } } - BufferedReader buffer = new BufferedReader(new FileReader(file)); - List lines = new LinkedList(); - for (String line = buffer.readLine(); line != null; line = buffer - .readLine()) { - lines.add(line); - } + this.format = format; - load(lines, format); - dirty = false; // initial load, so no change yet + if (file != null) { + this.file = file; + switch (format) { + case VCard21: + this.name = file.getName().replaceAll(".[vV][cC][fF]$", ""); + break; + case Abook: + default: + this.name = file.getName(); + break; + } + } } - public List getContacts() { - return contacts; + /** + * Create a new {@link Card} from the given {@link Contact}s. + * + * @param contacts + * the input contacts + * + * @throws IOException + * in case of IO error + * @throws InvalidParameterException + * if format is NULL + */ + public Card(List contacts) throws IOException { + super(contacts); + + lastModified = -1; } - public boolean saveAs(File file, Format format) throws IOException { - if (file == null) + /** + * Save the {@link Card} to the given {@link File} with the given + * {@link Format}. + * + * @param output + * the output to save to + * @param format + * the {@link Format} to use + * + * @return TRUE if it was saved + * + * @throws IOException + * in case of IO errors + */ + public boolean saveAs(File output, Format format) throws IOException { + if (output == null) return false; - BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + BufferedWriter writer = new BufferedWriter(new FileWriter(output)); writer.append(toString(format)); writer.close(); - if (file.equals(this.file)) { - dirty = false; + if (output.getCanonicalPath().equals(this.file.getCanonicalPath())) { + setPristine(); } return true; } - public boolean save(Format format, boolean bKeys) throws IOException { + /** + * Save the {@link Card} to the original {@link File} it was open from. + * + * @return TRUE if it was saved + * + * @throws IOException + * in case of IO errors + */ + public boolean save() throws IOException { return saveAs(file, format); } + /** + * Reload the data from the input. + * + * @return TRUE if it was done + * + * @throws IOException + * in case of IO error + */ + public boolean reload() throws IOException { + if (file == null) + return false; + + this.replaceListContent(load(file, format)); + setPristine(); + return true; + } + + /** + * Return a {@link String} representation of this {@link Card} in the given + * {@link Format}. + * + * @param format + * the {@link Format} to use + * + * @return the {@link String} + */ public String toString(Format format) { return Parser.toString(this, format); } - public String toString() { - return toString(Format.VCard21); + /** + * Return the name of this card (the name of the {@link File} which it was + * opened from). + * + * @return the name + */ + public String getName() { + return name; } - protected void load(String serializedContent, Format format) { - // note: fixed size array - List lines = Arrays.asList(serializedContent.split("\n")); - load(lines, format); + /** + * Return the original {@link Format} of the {@link Card}. + * + * @return the {@link Format} + */ + public Format getFormat() { + return format; } - protected void load(List lines, Format format) { - this.contacts = Parser.parse(lines, format); - setDirty(); + /** + * Return the {@link File} which was used to open this {@link Card}. + * + * @return the input + */ + public File getFile() { + return file; + } - for (Contact contact : contacts) { - contact.setParent(this); - } + /** + * Return the date of the last modification for this {@link Card} (or -1 if + * unknown/new). + * + * @return the last modified date + */ + public long getLastModified() { + return lastModified; } - public boolean isDirty() { - return dirty; + /** + * Check if this {@link Card} is remote. + * + * @return TRUE if this {@link Card} is remote + */ + public boolean isRemote() { + return remote; } /** - * Return the name of this card. + * Set the remote option on this {@link Card}. * - * @return the name + * @param remote + * TRUE if this {@link Card} is remote */ - public String getName() { - return name; + public void setRemote(boolean remote) { + this.remote = remote; + } + + @Override + public String toString() { + return toString(Format.VCard21); + } + + @Override + public String getId() { + return "" + name; + } + + @Override + public String getState() { + return "" + name + format; } /** - * Notify that this element has unsaved changes. + * Load the data from the given {@link File} under the given {@link Format}. + * + * @param file + * the input to load from + * @param format + * the {@link Format} to load as + * + * @return the list of elements + * + * @throws IOException + * in case of IO error */ - void setDirty() { - dirty = true; + private static List load(File file, Format format) + throws IOException { + List lines = null; + + if (file != null && file.exists()) { + BufferedReader buffer = new BufferedReader(new InputStreamReader( + new FileInputStream(file), "UTF-8")); + lines = new LinkedList(); + for (String line = buffer.readLine(); line != null; line = buffer + .readLine()) { + lines.add(line); + } + buffer.close(); + } + + if (lines == null) + return new LinkedList(); + + return Parser.parse(lines, format); } }