| 1 | package be.nikiroo.jvcard; |
| 2 | |
| 3 | import java.io.BufferedReader; |
| 4 | import java.io.BufferedWriter; |
| 5 | import java.io.File; |
| 6 | import java.io.FileInputStream; |
| 7 | import java.io.FileReader; |
| 8 | import java.io.FileWriter; |
| 9 | import java.io.IOException; |
| 10 | import java.io.InputStreamReader; |
| 11 | import java.util.ArrayList; |
| 12 | import java.util.Arrays; |
| 13 | import java.util.LinkedList; |
| 14 | import java.util.List; |
| 15 | |
| 16 | import be.nikiroo.jvcard.parsers.Format; |
| 17 | import be.nikiroo.jvcard.parsers.Parser; |
| 18 | |
| 19 | /** |
| 20 | * A card is a contact information card. It contains data about one or more |
| 21 | * contacts. |
| 22 | * |
| 23 | * @author niki |
| 24 | * |
| 25 | */ |
| 26 | public class Card { |
| 27 | private List<Contact> contacts; |
| 28 | private File file; |
| 29 | private boolean dirty; |
| 30 | private String name; |
| 31 | private Format format; |
| 32 | |
| 33 | public Card(File file, Format format) throws IOException { |
| 34 | this.file = file; |
| 35 | this.format = format; |
| 36 | |
| 37 | if (file != null) { |
| 38 | name = file.getName(); |
| 39 | } |
| 40 | |
| 41 | BufferedReader buffer = new BufferedReader(new InputStreamReader( |
| 42 | new FileInputStream(file), "UTF-8")); |
| 43 | List<String> lines = new LinkedList<String>(); |
| 44 | for (String line = buffer.readLine(); line != null; line = buffer |
| 45 | .readLine()) { |
| 46 | lines.add(line); |
| 47 | } |
| 48 | buffer.close(); |
| 49 | |
| 50 | load(lines, format); |
| 51 | dirty = false; // initial load, so no change yet, so no need to call |
| 52 | // setPristine() |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Return the full list of {@link Contact}s. Please use responsibly (this is |
| 57 | * the original list, do not modify the list itself). |
| 58 | * |
| 59 | * @return the list of {@link Contact}s |
| 60 | */ |
| 61 | public List<Contact> getContactsList() { |
| 62 | return contacts; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Return the list of {@link Contact}s. Note that this list is a copy. |
| 67 | * |
| 68 | * @return the list of {@link Contact}s |
| 69 | */ |
| 70 | public List<Contact> getContacts() { |
| 71 | ArrayList<Contact> list = new ArrayList<Contact>(size()); |
| 72 | list.addAll(contacts); |
| 73 | return list; |
| 74 | } |
| 75 | |
| 76 | public int size() { |
| 77 | return contacts.size(); |
| 78 | } |
| 79 | |
| 80 | public Contact get(int index) { |
| 81 | return contacts.get(index); |
| 82 | } |
| 83 | |
| 84 | public boolean saveAs(File file, Format format) throws IOException { |
| 85 | if (file == null) |
| 86 | return false; |
| 87 | |
| 88 | BufferedWriter writer = new BufferedWriter(new FileWriter(file)); |
| 89 | writer.append(toString(format)); |
| 90 | writer.close(); |
| 91 | |
| 92 | if (file.equals(this.file)) { |
| 93 | setPristine(); |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | public boolean save() throws IOException { |
| 100 | return saveAs(file, format); |
| 101 | } |
| 102 | |
| 103 | public String toString(Format format) { |
| 104 | return Parser.toString(this, format); |
| 105 | } |
| 106 | |
| 107 | public String toString() { |
| 108 | return toString(Format.VCard21); |
| 109 | } |
| 110 | |
| 111 | protected void load(String serializedContent, Format format) { |
| 112 | // note: fixed size array |
| 113 | List<String> lines = Arrays.asList(serializedContent.split("\n")); |
| 114 | load(lines, format); |
| 115 | } |
| 116 | |
| 117 | protected void load(List<String> lines, Format format) { |
| 118 | this.contacts = Parser.parse(lines, format); |
| 119 | setDirty(); |
| 120 | |
| 121 | for (Contact contact : contacts) { |
| 122 | contact.setParent(this); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public boolean isDirty() { |
| 127 | return dirty; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Return the name of this card. |
| 132 | * |
| 133 | * @return the name |
| 134 | */ |
| 135 | public String getName() { |
| 136 | return name; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Notify that this element has unsaved changes. |
| 141 | */ |
| 142 | void setDirty() { |
| 143 | dirty = true; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Notify this element <i>and all its descendants</i> that it is in pristine |
| 148 | * state (as opposed to dirty). |
| 149 | */ |
| 150 | void setPristine() { |
| 151 | dirty = false; |
| 152 | for (Contact contact : contacts) { |
| 153 | contact.setPristine(); |
| 154 | } |
| 155 | } |
| 156 | } |