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