Add some comparisons methods, fix the existing checks
[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.FileWriter;
8import java.io.IOException;
296a0b75 9import java.io.InputStreamReader;
78e4af97 10import java.security.InvalidParameterException;
a3b510ab
NR
11import java.util.LinkedList;
12import java.util.List;
13
14import be.nikiroo.jvcard.parsers.Format;
15import 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 */
26d2bd05 24public class Card extends BaseClass<Contact> {
a3b510ab 25 private File file;
0b0b2b0f 26 private String name;
bcb54330 27 private Format format;
b9f192ed
NR
28 private long lastModified;
29 private boolean remote;
a3b510ab 30
78e4af97
NR
31 /**
32 * Create a new {@link Card} from the given {@link File} and {@link Format}.
33 *
34 * @param file
b9f192ed
NR
35 * the input {@link File} containing the {@link Card} data or
36 * NULL for an empty card (usually a {@link File} name or a
37 * network path)
78e4af97
NR
38 * @param format
39 * the {@link Format} to use to parse it
40 *
41 * @throws IOException
42 * in case of IO error
78e4af97
NR
43 * @throws InvalidParameterException
44 * if format is NULL
45 */
a3b510ab 46 public Card(File file, Format format) throws IOException {
b9f192ed
NR
47 this(load(file, format));
48
49 if (file != null) {
50 if (file.exists()) {
51 lastModified = file.lastModified();
52 }
53 }
26d2bd05 54
bcb54330 55 this.format = format;
b9f192ed
NR
56
57 if (file != null) {
58 this.file = file;
59 switch (format) {
60 case VCard21:
e253bd50 61 this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
b9f192ed
NR
62 break;
63 case Abook:
64 default:
65 this.name = file.getName();
66 break;
67 }
68 }
69 }
70
71 /**
72 * Create a new {@link Card} from the given {@link Contact}s.
73 *
74 * @param contacts
75 * the input contacts
76 *
77 * @throws IOException
78 * in case of IO error
79 * @throws InvalidParameterException
80 * if format is NULL
81 */
82 public Card(List<Contact> contacts) throws IOException {
83 super(contacts);
84
85 lastModified = -1;
176a8327 86 }
bcb54330 87
78e4af97
NR
88 /**
89 * Save the {@link Card} to the given {@link File} with the given
90 * {@link Format}.
91 *
b9f192ed
NR
92 * @param output
93 * the output to save to
78e4af97
NR
94 * @param format
95 * the {@link Format} to use
96 *
97 * @return TRUE if it was saved
98 *
99 * @throws IOException
100 * in case of IO errors
101 */
b9f192ed
NR
102 public boolean saveAs(File output, Format format) throws IOException {
103 if (output == null)
a3b510ab
NR
104 return false;
105
b9f192ed 106 BufferedWriter writer = new BufferedWriter(new FileWriter(output));
a3b510ab
NR
107 writer.append(toString(format));
108 writer.close();
109
b9f192ed 110 if (output.getCanonicalPath().equals(this.file.getCanonicalPath())) {
bcb54330 111 setPristine();
a3b510ab
NR
112 }
113
114 return true;
115 }
116
78e4af97
NR
117 /**
118 * Save the {@link Card} to the original {@link File} it was open from.
119 *
120 * @return TRUE if it was saved
121 *
122 * @throws IOException
123 * in case of IO errors
124 */
bcb54330 125 public boolean save() throws IOException {
a3b510ab
NR
126 return saveAs(file, format);
127 }
128
b9f192ed
NR
129 /**
130 * Reload the data from the input.
131 *
132 * @return TRUE if it was done
133 *
134 * @throws IOException
135 * in case of IO error
136 */
137 public boolean reload() throws IOException {
138 if (file == null)
139 return false;
140
141 this.replaceListContent(load(file, format));
142 setPristine();
143 return true;
144 }
145
78e4af97
NR
146 /**
147 * Return a {@link String} representation of this {@link Card} in the given
148 * {@link Format}.
149 *
150 * @param format
151 * the {@link Format} to use
152 *
153 * @return the {@link String}
154 */
a3b510ab
NR
155 public String toString(Format format) {
156 return Parser.toString(this, format);
157 }
158
78e4af97
NR
159 /**
160 * Return the name of this card (the name of the {@link File} which it was
161 * opened from).
162 *
163 * @return the name
164 */
165 public String getName() {
166 return name;
167 }
168
b9f192ed
NR
169 /**
170 * Return the original {@link Format} of the {@link Card}.
171 *
172 * @return the {@link Format}
173 */
174 public Format getFormat() {
175 return format;
176 }
177
178 /**
e253bd50 179 * Return the {@link File} which was used to open this {@link Card}.
b9f192ed
NR
180 *
181 * @return the input
182 */
e253bd50 183 public File getFile() {
b9f192ed
NR
184 return file;
185 }
186
187 /**
188 * Return the date of the last modification for this {@link Card} (or -1 if
189 * unknown/new).
190 *
191 * @return the last modified date
192 */
193 public long getLastModified() {
194 return lastModified;
195 }
196
197 /**
198 * Check if this {@link Card} is remote.
199 *
200 * @return TRUE if this {@link Card} is remote
201 */
202 public boolean isRemote() {
203 return remote;
204 }
205
206 /**
207 * Set the remote option on this {@link Card}.
208 *
209 * @param remote
210 * TRUE if this {@link Card} is remote
211 */
212 public void setRemote(boolean remote) {
213 this.remote = remote;
214 }
215
78e4af97 216 @Override
a3b510ab
NR
217 public String toString() {
218 return toString(Format.VCard21);
219 }
220
e253bd50
NR
221 @Override
222 public String getId() {
223 return "" + name;
224 }
225
226 @Override
227 public String getState() {
228 return "" + name + format;
229 }
230
78e4af97 231 /**
26d2bd05 232 * Load the data from the given {@link File} under the given {@link Format}.
78e4af97 233 *
26d2bd05 234 * @param file
b9f192ed 235 * the input to load from
78e4af97 236 * @param format
26d2bd05 237 * the {@link Format} to load as
78e4af97 238 *
26d2bd05 239 * @return the list of elements
b9f192ed 240 *
26d2bd05
NR
241 * @throws IOException
242 * in case of IO error
78e4af97 243 */
b9f192ed 244 private static List<Contact> load(File file, Format format)
26d2bd05 245 throws IOException {
b9f192ed
NR
246 List<String> lines = null;
247
248 if (file != null && file.exists()) {
249 BufferedReader buffer = new BufferedReader(new InputStreamReader(
250 new FileInputStream(file), "UTF-8"));
251 lines = new LinkedList<String>();
252 for (String line = buffer.readLine(); line != null; line = buffer
253 .readLine()) {
254 lines.add(line);
255 }
256 buffer.close();
a3b510ab 257 }
b9f192ed
NR
258
259 if (lines == null)
260 return new LinkedList<Contact>();
bcb54330 261
26d2bd05 262 return Parser.parse(lines, format);
bcb54330 263 }
a3b510ab 264}