Indexed colours, better image handling, lines cut at 74:
[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) {
46 if (file.exists()) {
47 lastModified = file.lastModified();
48 }
49 }
50
51 this.format = format;
52
53 if (file != null) {
54 this.file = file;
55 switch (format) {
56 case VCard21:
57 this.name = file.getName().replaceAll(".[vV][cC][fF]$", "");
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;
82 }
83
84 /**
85 * Save the {@link Card} to the given {@link File} with the given
86 * {@link Format}.
87 *
88 * @param file
89 * the output to save to
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 */
98 public boolean saveAs(File file, Format format) throws IOException {
99 if (file == null)
100 return false;
101
102 BufferedWriter writer = new BufferedWriter(new FileWriter(file));
103 writer.append(toString(format));
104 writer.close();
105
106 if (this.file != null
107 && file.getCanonicalPath().equals(this.file.getCanonicalPath())) {
108 setPristine();
109 }
110
111 return true;
112 }
113
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 */
122 public boolean save() throws IOException {
123 return saveAs(file, format);
124 }
125
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
138 this.replaceListContent(Parser.parse(file, format));
139 setPristine();
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 return Parser.toString(this, format);
154 }
155
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
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 /**
176 * Return the {@link File} which was used to open this {@link Card}.
177 *
178 * @return the input
179 */
180 public File getFile() {
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
213 @Override
214 public String toString() {
215 return toString(Format.VCard21);
216 }
217
218 @Override
219 public String getId() {
220 return "" + name;
221 }
222
223 @Override
224 public String getState() {
225 return "" + name + format;
226 }
227 }