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