52eec8c6e4af0d5ac3ae67dbb94f7d63b93f0f37
[jvcard.git] / src / be / nikiroo / jvcard / Card.java
1 package be.nikiroo.jvcard;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.security.InvalidParameterException;
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 */
20 public class Card extends BaseClass<Contact> {
21 private File file;
22 private String name;
23 private Format format;
24 private long lastModified;
25 private boolean remote;
26
27 /**
28 * Create a new {@link Card} from the given {@link File} and {@link Format}.
29 *
30 * @param file
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)
34 * @param format
35 * the {@link Format} to use to parse it
36 *
37 * @throws IOException
38 * in case of IO error
39 * @throws InvalidParameterException
40 * if format is NULL
41 */
42 public Card(File file, Format format) throws IOException {
43 this(Parser.parse(file, format));
44
45 if (file != null && file.exists()) {
46 lastModified = file.lastModified();
47 }
48
49 this.format = format;
50
51 if (file != null) {
52 this.file = file;
53 switch (format) {
54 case VCard21:
55 this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
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;
80 }
81
82 /**
83 * Save the {@link Card} to the given {@link File} with the given
84 * {@link Format}.
85 *
86 * @param file
87 * the output to save to
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 */
96 public boolean saveAs(File file, Format format) throws IOException {
97 if (file == null)
98 return false;
99
100 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
101 writer.append(toString(format));
102 writer.close();
103
104 if (this.file != null
105 && file.getCanonicalPath().equals(this.file.getCanonicalPath())) {
106 setPristine();
107 }
108
109 return true;
110 }
111
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 */
120 public boolean save() throws IOException {
121 return saveAs(file, format);
122 }
123
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
136 this.replaceListContent(Parser.parse(file, format));
137 lastModified = file.lastModified();
138 setPristine();
139
140 return true;
141 }
142
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 */
152 public String toString(Format format) {
153 StringBuilder builder = new StringBuilder();
154 for (String line : Parser.toStrings(this, format)) {
155 builder.append(line);
156 builder.append("\r\n");
157 }
158 return builder.toString();
159 }
160
161 /**
162 * Return the name of this card (the name of the {@link File} which it was
163 * opened from).
164 *
165 * @return the name
166 */
167 public String getName() {
168 return name;
169 }
170
171 /**
172 * Return the original {@link Format} of the {@link Card}.
173 *
174 * @return the {@link Format}
175 */
176 public Format getFormat() {
177 return format;
178 }
179
180 /**
181 * Return the {@link File} which was used to open this {@link Card}.
182 *
183 * @return the input
184 */
185 public File getFile() {
186 return file;
187 }
188
189 /**
190 * Return the date of the last modification for this {@link Card} (or -1 if
191 * unknown/new).
192 *
193 * @return the last modified date
194 */
195 public long getLastModified() {
196 return lastModified;
197 }
198
199 /**
200 * Check if this {@link Card} is remote.
201 *
202 * @return TRUE if this {@link Card} is remote
203 */
204 public boolean isRemote() {
205 return remote;
206 }
207
208 /**
209 * Set the remote option on this {@link Card}.
210 *
211 * @param remote
212 * TRUE if this {@link Card} is remote
213 */
214 public void setRemote(boolean remote) {
215 this.remote = remote;
216 }
217
218 @Override
219 public String toString() {
220 return toString(Format.VCard21);
221 }
222
223 @Override
224 public String getId() {
225 return "" + name;
226 }
227
228 @Override
229 public String getState() {
230 return "" + name + format;
231 }
232 }