Fix --noutf, fix onAction being called to many times, lot of small fixes
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
CommitLineData
a3b510ab
NR
1package be.nikiroo.jvcard;
2
3import java.io.BufferedReader;
4import java.io.BufferedWriter;
5import java.io.File;
296a0b75 6import java.io.FileInputStream;
a3b510ab
NR
7import java.io.FileReader;
8import java.io.FileWriter;
9import java.io.IOException;
296a0b75 10import java.io.InputStreamReader;
bcb54330 11import java.util.ArrayList;
a3b510ab
NR
12import java.util.Arrays;
13import java.util.LinkedList;
14import java.util.List;
15
16import be.nikiroo.jvcard.parsers.Format;
17import 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 */
26public class Card {
27 private List<Contact> contacts;
28 private File file;
29 private boolean dirty;
0b0b2b0f 30 private String name;
bcb54330 31 private Format format;
a3b510ab
NR
32
33 public Card(File file, Format format) throws IOException {
34 this.file = file;
bcb54330 35 this.format = format;
a3b510ab 36
0b0b2b0f
NR
37 if (file != null) {
38 name = file.getName();
39 }
40
296a0b75
NR
41 BufferedReader buffer = new BufferedReader(new InputStreamReader(
42 new FileInputStream(file), "UTF-8"));
a3b510ab
NR
43 List<String> lines = new LinkedList<String>();
44 for (String line = buffer.readLine(); line != null; line = buffer
45 .readLine()) {
46 lines.add(line);
47 }
bcb54330 48 buffer.close();
a3b510ab
NR
49
50 load(lines, format);
bcb54330
NR
51 dirty = false; // initial load, so no change yet, so no need to call
52 // setPristine()
a3b510ab
NR
53 }
54
bcb54330
NR
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() {
a3b510ab
NR
62 return contacts;
63 }
64
bcb54330
NR
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
a3b510ab
NR
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)) {
bcb54330 93 setPristine();
a3b510ab
NR
94 }
95
96 return true;
97 }
98
bcb54330 99 public boolean save() throws IOException {
a3b510ab
NR
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
0b0b2b0f
NR
130 /**
131 * Return the name of this card.
132 *
133 * @return the name
134 */
135 public String getName() {
136 return name;
137 }
138
a3b510ab
NR
139 /**
140 * Notify that this element has unsaved changes.
141 */
142 void setDirty() {
143 dirty = true;
144 }
bcb54330
NR
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 }
a3b510ab 156}