X-Git-Url: https://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2FCard.java;h=e82ce7cc933ebb864a2b7b405875d09865d12cf9;hb=bcb54330afff6a443ab43ee3d38cc7f863c701b7;hp=a9018ca066d1cc590fcb92b76ce884919150a1ea;hpb=a3b510ab4bf89a7a2a05f3851ffe0f030b8a78f4;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/Card.java b/src/be/nikiroo/jvcard/Card.java index a9018ca..e82ce7c 100644 --- a/src/be/nikiroo/jvcard/Card.java +++ b/src/be/nikiroo/jvcard/Card.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -24,9 +25,16 @@ public class Card { private List contacts; private File file; private boolean dirty; + private String name; + private Format format; public Card(File file, Format format) throws IOException { this.file = file; + this.format = format; + + if (file != null) { + name = file.getName(); + } BufferedReader buffer = new BufferedReader(new FileReader(file)); List lines = new LinkedList(); @@ -34,14 +42,42 @@ public class Card { .readLine()) { lines.add(line); } + buffer.close(); load(lines, format); + dirty = false; // initial load, so no change yet, so no need to call + // setPristine() } - public List getContacts() { + /** + * Return the full list of {@link Contact}s. Please use responsibly (this is + * the original list, do not modify the list itself). + * + * @return the list of {@link Contact}s + */ + public List getContactsList() { return contacts; } + /** + * Return the list of {@link Contact}s. Note that this list is a copy. + * + * @return the list of {@link Contact}s + */ + public List getContacts() { + ArrayList list = new ArrayList(size()); + list.addAll(contacts); + return list; + } + + public int size() { + return contacts.size(); + } + + public Contact get(int index) { + return contacts.get(index); + } + public boolean saveAs(File file, Format format) throws IOException { if (file == null) return false; @@ -51,13 +87,13 @@ public class Card { writer.close(); if (file.equals(this.file)) { - dirty = false; + setPristine(); } return true; } - public boolean save(Format format, boolean bKeys) throws IOException { + public boolean save() throws IOException { return saveAs(file, format); } @@ -88,10 +124,30 @@ public class Card { return dirty; } + /** + * Return the name of this card. + * + * @return the name + */ + public String getName() { + return name; + } + /** * Notify that this element has unsaved changes. */ void setDirty() { dirty = true; } + + /** + * 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(); + } + } }