Fix --noutf, fix onAction being called to many times, lot of small fixes
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
index 65ab6fa5225b91fdacdd3430fdc52abdbb317395..f3e07733528f50d4ce121b67eecdba8243205ac4 100644 (file)
@@ -3,9 +3,12 @@ package be.nikiroo.jvcard;
 import java.io.BufferedReader;
 import java.io.BufferedWriter;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
@@ -25,29 +28,59 @@ public class Card {
        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));
+               BufferedReader buffer = new BufferedReader(new InputStreamReader(
+                               new FileInputStream(file), "UTF-8"));
                List<String> lines = new LinkedList<String>();
                for (String line = buffer.readLine(); line != null; line = buffer
                                .readLine()) {
                        lines.add(line);
                }
+               buffer.close();
 
                load(lines, format);
-               dirty = false; // initial load, so no change yet
+               dirty = false; // initial load, so no change yet, so no need to call
+                                               // setPristine()
        }
 
-       public List<Contact> 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<Contact> 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<Contact> getContacts() {
+               ArrayList<Contact> list = new ArrayList<Contact>(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;
@@ -57,13 +90,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);
        }
 
@@ -109,4 +142,15 @@ public class Card {
        void setDirty() {
                dirty = true;
        }
+
+       /**
+        * Notify this element <i>and all its descendants</i> that it is in pristine
+        * state (as opposed to dirty).
+        */
+       void setPristine() {
+               dirty = false;
+               for (Contact contact : contacts) {
+                       contact.setPristine();
+               }
+       }
 }