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