X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FCard.java;h=d2567a496237c159546fa40f46015cebc241223b;hb=e4444b0bc462544629d9e7e7ab62b96a4d9cab10;hp=53ae972a5803d38482333acae7e7fe565e45e34d;hpb=176a83279a5aafb7e44cc7c34bf78f0bc35225fe;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Card.java b/src/be/nikiroo/jvcard/Card.java index 53ae972..d2567a4 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,94 +17,66 @@ 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 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()} - */ - public Contact get(int index) { - return contacts.get(index); - } - - /** - * Add a new {@link Contact} in this {@link Card}. - * - * @param contact - * the new contact - */ - public void add(Contact contact) { - contact.setParent(this); - contact.setDirty(); - contacts.add(contact); - } - - /** - * Remove the given {@link Contact} from its this {@link Card} if it is in. - * - * @return TRUE in case of success + * @throws IOException + * in case of IO error + * @throws InvalidParameterException + * if format is NULL */ - public boolean remove(Contact contact) { - if (contacts.remove(contact)) { - setDirty(); - } + public Card(List contacts) throws IOException { + super(contacts); - return false; + lastModified = -1; } /** @@ -117,7 +84,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 * @@ -134,7 +101,9 @@ public class Card { writer.append(toString(format)); writer.close(); - if (file.equals(this.file)) { + if (this.file != null + && file.getCanonicalPath().equals(this.file.getCanonicalPath())) { + lastModified = file.lastModified(); setPristine(); } @@ -153,6 +122,25 @@ public class Card { 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(Parser.parseContact(file, format)); + lastModified = file.lastModified(); + setPristine(); + + return true; + } + /** * Return a {@link String} representation of this {@link Card} in the given * {@link Format}. @@ -163,16 +151,12 @@ public class Card { * @return the {@link String} */ public String toString(Format format) { - return Parser.toString(this, format); - } - - /** - * Check if this {@link Card} has unsaved changes. - * - * @return TRUE if it has - */ - public boolean isDirty() { - return dirty; + StringBuilder builder = new StringBuilder(); + for (String line : Parser.toStrings(this, format)) { + builder.append(line); + builder.append("\r\n"); + } + return builder.toString(); } /** @@ -185,57 +169,65 @@ public class Card { return name; } - @Override - public String toString() { - return toString(Format.VCard21); + /** + * Return the original {@link Format} of the {@link Card}. + * + * @return the {@link 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 serializedContent - * the data - * @param format - * the {@link Format} + * @return the input */ - protected void load(String serializedContent, Format format) { - // note: fixed size array - List lines = Arrays.asList(serializedContent.split("\n")); - load(lines, format); + public File getFile() { + return file; } /** - * Load the given data from the given {@link Format} in this {@link Card}. + * Return the date of the last modification for this {@link Card} (or -1 if + * unknown/new). * - * @param lines - * the data - * @param format - * the {@link Format} + * @return the last modified date */ - protected void load(List lines, Format format) { - this.contacts = Parser.parse(lines, format); - setDirty(); - - for (Contact contact : contacts) { - contact.setParent(this); - } + public long getLastModified() { + return lastModified; } /** - * Notify that this element has unsaved changes. + * Check if this {@link Card} is remote. + * + * @return TRUE if this {@link Card} is remote */ - void setDirty() { - dirty = true; + public boolean isRemote() { + return remote; } /** - * Notify this element and all its descendants that it is in pristine - * state (as opposed to dirty). + * Set the remote option on this {@link Card}. + * + * @param remote + * TRUE if this {@link Card} is remote */ - void setPristine() { - dirty = false; - for (Contact contact : contacts) { - contact.setPristine(); - } + 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).replace(' ', '_'); } }