Remote support ~complete (need more tests at least)
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
index f3e07733528f50d4ce121b67eecdba8243205ac4..547535804b8f37fa80fb5dc3ca22f89afc22857e 100644 (file)
@@ -1,16 +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.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.security.InvalidParameterException;
 import java.util.List;
 
 import be.nikiroo.jvcard.parsers.Format;
@@ -23,64 +17,81 @@ import be.nikiroo.jvcard.parsers.Parser;
  * @author niki
  * 
  */
-public class Card {
-       private List<Contact> contacts;
+public class Card extends BaseClass<Contact> {
        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 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.format = format;
+               this(Parser.parseContact(file, format));
 
-               if (file != null) {
-                       name = file.getName();
+               if (file != null && file.exists()) {
+                       lastModified = file.lastModified();
                }
 
-               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();
+               this.format = format;
 
-               load(lines, format);
-               dirty = false; // initial load, so no change yet, so no need to call
-                                               // setPristine()
+               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 full list of {@link Contact}s. Please use responsibly (this is
-        * the original list, do not modify the list itself).
+        * Create a new {@link Card} from the given {@link Contact}s.
         * 
-        * @return the list of {@link Contact}s
+        * @param contacts
+        *            the input contacts
+        * 
+        * @throws IOException
+        *             in case of IO error
+        * @throws InvalidParameterException
+        *             if format is NULL
         */
-       public List<Contact> getContactsList() {
-               return contacts;
+       public Card(List<Contact> contacts) {
+               super(contacts);
+
+               lastModified = -1;
        }
 
        /**
-        * Return the list of {@link Contact}s. Note that this list is a copy.
+        * Save the {@link Card} to the given {@link File} with the given
+        * {@link Format}.
         * 
-        * @return the list of {@link Contact}s
+        * @param file
+        *            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 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;
@@ -89,46 +100,67 @@ 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();
                }
 
                return true;
        }
 
+       /**
+        * 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);
        }
 
-       public String toString(Format format) {
-               return Parser.toString(this, 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;
 
-       public String toString() {
-               return toString(Format.VCard21);
-       }
+               this.replaceListContent(Parser.parseContact(file, format));
+               lastModified = file.lastModified();
+               setPristine();
 
-       protected void load(String serializedContent, Format format) {
-               // note: fixed size array
-               List<String> lines = Arrays.asList(serializedContent.split("\n"));
-               load(lines, format);
+               return true;
        }
 
-       protected void load(List<String> lines, Format format) {
-               this.contacts = Parser.parse(lines, format);
-               setDirty();
-
-               for (Contact contact : contacts) {
-                       contact.setParent(this);
+       /**
+        * 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) {
+               StringBuilder builder = new StringBuilder();
+               for (String line : Parser.toStrings(this, format)) {
+                       builder.append(line);
+                       builder.append("\r\n");
                }
-       }
-
-       public boolean isDirty() {
-               return dirty;
+               return builder.toString();
        }
 
        /**
-        * Return the name of this card.
+        * Return the name of this card (the name of the {@link File} which it was
+        * opened from).
         * 
         * @return the name
         */
@@ -137,20 +169,45 @@ public class Card {
        }
 
        /**
-        * Notify that this element has unsaved changes.
+        * Return the original {@link Format} of the {@link Card}.
+        * 
+        * @return the {@link Format}
+        */
+       public Format getFormat() {
+               return format;
+       }
+
+       /**
+        * Return the {@link File} which was used to open this {@link Card}.
+        * 
+        * @return the input
         */
-       void setDirty() {
-               dirty = true;
+       public File getFile() {
+               return file;
        }
 
        /**
-        * Notify this element <i>and all its descendants</i> that it is in pristine
-        * state (as opposed to dirty).
+        * Return the date of the last modification for this {@link Card} (or -1 if
+        * unknown/new).
+        * 
+        * @return the last modified date
         */
-       void setPristine() {
-               dirty = false;
-               for (Contact contact : contacts) {
-                       contact.setPristine();
-               }
+       public long getLastModified() {
+               return lastModified;
+       }
+
+       @Override
+       public String toString() {
+               return toString(Format.VCard21);
+       }
+
+       @Override
+       public String getId() {
+               return "" + name;
+       }
+
+       @Override
+       public String getState() {
+               return ("" + name + format).replace(' ', '_');
        }
 }