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 | |
a3b510ab | 18 | */ |
26d2bd05 | 19 | public class Card extends BaseClass<Contact> { |
a3b510ab | 20 | private File file; |
0b0b2b0f | 21 | private String name; |
bcb54330 | 22 | private Format format; |
b9f192ed | 23 | private long lastModified; |
a3b510ab | 24 | |
78e4af97 NR |
25 | /** |
26 | * Create a new {@link Card} from the given {@link File} and {@link Format}. | |
27 | * | |
28 | * @param file | |
b9f192ed NR |
29 | * the input {@link File} containing the {@link Card} data or |
30 | * NULL for an empty card (usually a {@link File} name or a | |
31 | * network path) | |
78e4af97 NR |
32 | * @param format |
33 | * the {@link Format} to use to parse it | |
34 | * | |
35 | * @throws IOException | |
36 | * in case of IO error | |
78e4af97 NR |
37 | * @throws InvalidParameterException |
38 | * if format is NULL | |
39 | */ | |
a3b510ab | 40 | public Card(File file, Format format) throws IOException { |
0b6140e4 | 41 | this(Parser.parseContact(file, format)); |
b9f192ed | 42 | |
cf77cb35 NR |
43 | if (file != null && file.exists()) { |
44 | lastModified = file.lastModified(); | |
b9f192ed | 45 | } |
26d2bd05 | 46 | |
bcb54330 | 47 | this.format = format; |
b9f192ed NR |
48 | |
49 | if (file != null) { | |
50 | this.file = file; | |
51 | switch (format) { | |
52 | case VCard21: | |
e253bd50 | 53 | this.name = file.getName().replaceAll(".[vV][cC][fF]$", ""); |
b9f192ed NR |
54 | break; |
55 | case Abook: | |
56 | default: | |
57 | this.name = file.getName(); | |
58 | break; | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | /** | |
64 | * Create a new {@link Card} from the given {@link Contact}s. | |
65 | * | |
66 | * @param contacts | |
67 | * the input contacts | |
68 | * | |
69 | * @throws IOException | |
70 | * in case of IO error | |
71 | * @throws InvalidParameterException | |
72 | * if format is NULL | |
73 | */ | |
5ad0e17e | 74 | public Card(List<Contact> contacts) { |
b9f192ed NR |
75 | super(contacts); |
76 | ||
77 | lastModified = -1; | |
176a8327 | 78 | } |
bcb54330 | 79 | |
78e4af97 NR |
80 | /** |
81 | * Save the {@link Card} to the given {@link File} with the given | |
82 | * {@link Format}. | |
83 | * | |
1c03abaf | 84 | * @param file |
b9f192ed | 85 | * the output to save to |
78e4af97 NR |
86 | * @param format |
87 | * the {@link Format} to use | |
88 | * | |
89 | * @return TRUE if it was saved | |
90 | * | |
91 | * @throws IOException | |
92 | * in case of IO errors | |
93 | */ | |
1c03abaf NR |
94 | public boolean saveAs(File file, Format format) throws IOException { |
95 | if (file == null) | |
a3b510ab NR |
96 | return false; |
97 | ||
1c03abaf | 98 | BufferedWriter writer = new BufferedWriter(new FileWriter(file)); |
f04a32e9 NR |
99 | try { |
100 | Parser.write(writer, format, this); | |
101 | } finally { | |
102 | writer.close(); | |
103 | } | |
a3b510ab | 104 | |
1c03abaf NR |
105 | if (this.file != null |
106 | && file.getCanonicalPath().equals(this.file.getCanonicalPath())) { | |
6a77f2ed | 107 | lastModified = file.lastModified(); |
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 | ||
0b6140e4 | 138 | this.replaceListContent(Parser.parseContact(file, format)); |
cf77cb35 | 139 | lastModified = file.lastModified(); |
b9f192ed | 140 | setPristine(); |
cf77cb35 | 141 | |
b9f192ed NR |
142 | return true; |
143 | } | |
144 | ||
78e4af97 NR |
145 | /** |
146 | * Return the name of this card (the name of the {@link File} which it was | |
147 | * opened from). | |
148 | * | |
149 | * @return the name | |
150 | */ | |
151 | public String getName() { | |
152 | return name; | |
153 | } | |
154 | ||
b9f192ed NR |
155 | /** |
156 | * Return the original {@link Format} of the {@link Card}. | |
157 | * | |
158 | * @return the {@link Format} | |
159 | */ | |
160 | public Format getFormat() { | |
161 | return format; | |
162 | } | |
163 | ||
164 | /** | |
e253bd50 | 165 | * Return the {@link File} which was used to open this {@link Card}. |
b9f192ed NR |
166 | * |
167 | * @return the input | |
168 | */ | |
e253bd50 | 169 | public File getFile() { |
b9f192ed NR |
170 | return file; |
171 | } | |
172 | ||
59597d59 NR |
173 | /** |
174 | * Break the link between this {@link Card} and he {@link File} which was | |
175 | * used to open it if any. | |
176 | */ | |
177 | public void unlink() { | |
178 | file = null; | |
179 | lastModified = -1; | |
180 | } | |
181 | ||
b9f192ed NR |
182 | /** |
183 | * Return the date of the last modification for this {@link Card} (or -1 if | |
184 | * unknown/new). | |
185 | * | |
186 | * @return the last modified date | |
187 | */ | |
188 | public long getLastModified() { | |
189 | return lastModified; | |
190 | } | |
191 | ||
78e4af97 | 192 | @Override |
a3b510ab | 193 | public String toString() { |
59597d59 | 194 | return "[Card: " + name + "]"; |
a3b510ab NR |
195 | } |
196 | ||
e253bd50 NR |
197 | @Override |
198 | public String getId() { | |
199 | return "" + name; | |
200 | } | |
201 | ||
202 | @Override | |
203 | public String getState() { | |
e4444b0b | 204 | return ("" + name + format).replace(' ', '_'); |
e253bd50 | 205 | } |
a3b510ab | 206 | } |