X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FCard.java;h=0a405bee47d744e6545b3360579343fdc5466806;hb=59597d59aa262e31c2e1b7f66b4cb299f88ebd1b;hp=4c4e5d174e419bb47c9037e2f4477a08438a3b73;hpb=78e4af97505df331618f9c13dd5d98440d364764;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Card.java b/src/be/nikiroo/jvcard/Card.java index 4c4e5d1..0a405be 100644 --- a/src/be/nikiroo/jvcard/Card.java +++ b/src/be/nikiroo/jvcard/Card.java @@ -1,15 +1,10 @@ package be.nikiroo.jvcard; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStreamReader; import java.security.InvalidParameterException; -import java.util.Arrays; -import java.util.LinkedList; import java.util.List; import be.nikiroo.jvcard.parsers.Format; @@ -22,69 +17,65 @@ 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; /** * Create a new {@link Card} from the given {@link File} and {@link Format}. * * @param file - * the file containing the {@link Card} data, must not be NULL + * 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 NullPointerException - * if file is NULL * @throws InvalidParameterException * if format is NULL */ public Card(File file, Format format) throws IOException { - this.file = file; - this.format = format; - this.name = file.getName(); - - BufferedReader buffer = new BufferedReader(new InputStreamReader( - new FileInputStream(file), "UTF-8")); - List lines = new LinkedList(); - for (String line = buffer.readLine(); line != null; line = buffer - .readLine()) { - lines.add(line); + this(Parser.parseContact(file, format)); + + if (file != null && file.exists()) { + lastModified = file.lastModified(); } - buffer.close(); - load(lines, format); - dirty = false; // initial load, so no change yet, so no need to call - // setPristine() - } + this.format = format; - /** - * Return the number of {@link Contact} present in this {@link Card}. - * - * @return the number of {@link Contact}s - */ - public int size() { - return contacts.size(); + 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; + } + } } /** - * Return the {@link Contact} at index index. - * - * @param index - * the index of the {@link Contact} to find + * Create a new {@link Card} from the given {@link Contact}s. * - * @return the {@link Contact} + * @param contacts + * the input contacts * - * @throws IndexOutOfBoundsException - * if the index is < 0 or >= {@link Card#size()} + * @throws IOException + * in case of IO error + * @throws InvalidParameterException + * if format is NULL */ - public Contact get(int index) { - return contacts.get(index); + public Card(List contacts) { + super(contacts); + + lastModified = -1; } /** @@ -92,7 +83,7 @@ public class Card { * {@link Format}. * * @param file - * the {@link File} to save to + * the output to save to * @param format * the {@link Format} to use * @@ -106,10 +97,12 @@ public class Card { return false; BufferedWriter writer = new BufferedWriter(new FileWriter(file)); - writer.append(toString(format)); + Parser.write(writer, format, this); writer.close(); - if (file.equals(this.file)) { + if (this.file != null + && file.getCanonicalPath().equals(this.file.getCanonicalPath())) { + lastModified = file.lastModified(); setPristine(); } @@ -129,25 +122,22 @@ public class Card { } /** - * Return a {@link String} representation of this {@link Card} in the given - * {@link Format}. + * Reload the data from the input. * - * @param format - * the {@link Format} to use + * @return TRUE if it was done * - * @return the {@link String} + * @throws IOException + * in case of IO error */ - public String toString(Format format) { - return Parser.toString(this, format); - } + public boolean reload() throws IOException { + if (file == null) + return false; - /** - * Check if this {@link Card} has unsaved changes. - * - * @return TRUE if it has - */ - public boolean isDirty() { - return dirty; + this.replaceListContent(Parser.parseContact(file, format)); + lastModified = file.lastModified(); + setPristine(); + + return true; } /** @@ -160,67 +150,55 @@ public class Card { return name; } - @Override - public String toString() { - return toString(Format.VCard21); - } - /** - * Load the given data from the given {@link Format} in this {@link Card}. + * Return the original {@link Format} of the {@link Card}. * - * @param serializedContent - * the data - * @param format - * the {@link Format} + * @return the {@link Format} */ - protected void load(String serializedContent, Format format) { - // note: fixed size array - List lines = Arrays.asList(serializedContent.split("\n")); - load(lines, format); + public Format getFormat() { + return format; } /** - * Load the given data from the given {@link Format} in this {@link Card}. + * Return the {@link File} which was used to open this {@link Card}. * - * @param lines - * the data - * @param format - * the {@link Format} + * @return the input */ - protected void load(List lines, Format format) { - this.contacts = Parser.parse(lines, format); - setDirty(); - - for (Contact contact : contacts) { - contact.setParent(this); - } + public File getFile() { + return file; } /** - * Return the full list of {@link Contact}s. Please use responsibly (this is - * the original list). - * - * @return the list of {@link Contact}s + * Break the link between this {@link Card} and he {@link File} which was + * used to open it if any. */ - List getContactsList() { - return contacts; + public void unlink() { + file = null; + lastModified = -1; } /** - * Notify that this element has unsaved changes. + * Return the date of the last modification for this {@link Card} (or -1 if + * unknown/new). + * + * @return the last modified date */ - void setDirty() { - dirty = true; + public long getLastModified() { + return lastModified; } - /** - * Notify this element and all its descendants that it is in pristine - * state (as opposed to dirty). - */ - void setPristine() { - dirty = false; - for (Contact contact : contacts) { - contact.setPristine(); - } + @Override + public String toString() { + return "[Card: " + name + "]"; + } + + @Override + public String getId() { + return "" + name; + } + + @Override + public String getState() { + return ("" + name + format).replace(' ', '_'); } }