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