Refresh data on "Back", allow configuration of View + border
[jvcard.git] / src / be / nikiroo / jvcard / Contact.java
1 package be.nikiroo.jvcard;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.UUID;
11
12 import be.nikiroo.jvcard.parsers.Format;
13 import be.nikiroo.jvcard.parsers.Parser;
14 import be.nikiroo.jvcard.resources.StringUtils;
15
16 /**
17 * A contact is the information that represent a contact person or organisation.
18 *
19 * @author niki
20 *
21 */
22 public class Contact extends BaseClass<Data> {
23 private int nextBKey = 1;
24 private Map<Integer, Data> binaries;
25
26 /**
27 * Create a new Contact from the given information. Note that the BKeys data
28 * will be reset.
29 *
30 * @param content
31 * the information about the contact
32 */
33 public Contact(List<Data> content) {
34 super(load(content));
35 updateBKeys(true);
36 }
37
38 /**
39 * Return the preferred Data field with the given name, or NULL if none.
40 *
41 * @param name
42 * the name to look for
43 * @return the Data field, or NULL
44 */
45 public Data getPreferredData(String name) {
46 Data first = null;
47 for (Data data : getData(name)) {
48 if (first == null)
49 first = data;
50
51 if (data.isPreferred())
52 return data;
53 }
54
55 return first;
56 }
57
58 /**
59 * Return the value of the preferred data field with this name, or NULL if
60 * none (you cannot differentiate a NULL value and no value).
61 *
62 * @param name
63 * the name to look for
64 * @return the value (which can be NULL), or NULL
65 */
66 public String getPreferredDataValue(String name) {
67 Data data = getPreferredData(name);
68 if (data != null && data.getValue() != null)
69 return data.getValue().trim();
70 return null;
71 }
72
73 /**
74 * Get the Data fields that share the given name.
75 *
76 * @param name
77 * the name to ook for
78 * @return a list of Data fields with this name
79 */
80 public List<Data> getData(String name) {
81 List<Data> found = new LinkedList<Data>();
82
83 for (Data data : this) {
84 if (data.getName().equals(name))
85 found.add(data);
86 }
87
88 return found;
89 }
90
91 /**
92 * Return a {@link String} representation of this contact.
93 *
94 * @param format
95 * the {@link Format} to use
96 * @param startingBKey
97 * the starting BKey or -1 for no BKeys
98 * @return the {@link String} representation
99 */
100 public String toString(Format format, int startingBKey) {
101 updateBKeys(false);
102
103 StringBuilder builder = new StringBuilder();
104 for (String line : Parser.toStrings(this, format, startingBKey)) {
105 builder.append(line);
106 builder.append("\r\n");
107 }
108
109 return builder.toString();
110 }
111
112 /**
113 * Return a {@link String} representation of this contact formated
114 * accordingly to the given format.
115 *
116 * The format is basically a list of field names separated by a pipe and
117 * optionally parametrised. The parameters allows you to:
118 * <ul>
119 * <li>@x: show only a present/not present info</li>
120 * <li>@n: limit the size to a fixed value 'n'</li>
121 * <li>@+: expand the size of this field as much as possible</li>
122 * </ul>
123 *
124 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
125 *
126 * @param format
127 * the format to use
128 *
129 * @return the {@link String} representation
130 */
131 public String toString(String format) {
132 return toString(format, "|", null, -1, true, false);
133 }
134
135 /**
136 * Return a {@link String} representation of this contact formated
137 * accordingly to the given format.
138 *
139 * The format is basically a list of field names separated by a pipe and
140 * optionally parametrised. The parameters allows you to:
141 * <ul>
142 * <li>@x: (the 'x' is the letter 'x') show only a present/not present info</li>
143 * <li>@n: limit the size to a fixed value 'n'</li>
144 * <li>@+: expand the size of this field as much as possible</li>
145 * </ul>
146 *
147 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
148 *
149 * @param format
150 * the format to use
151 * @param separator
152 * the separator {@link String} to use between fields
153 * @param padding
154 * the {@link String} to use for left and right padding
155 * @param width
156 * a fixed width or -1 for "as long as needed"
157 *
158 * @param unicode
159 * allow Uniode or only ASCII characters
160 *
161 * @return the {@link String} representation
162 */
163 public String toString(String format, String separator, String padding,
164 int width, boolean unicode, boolean removeAccents) {
165 StringBuilder builder = new StringBuilder();
166
167 for (String str : toStringArray(format, separator, padding, width,
168 unicode)) {
169 builder.append(str);
170 }
171
172 return builder.toString();
173 }
174
175 /**
176 * Return a {@link String} representation of this contact formated
177 * accordingly to the given format, part by part.
178 *
179 * The format is basically a list of field names separated by a pipe and
180 * optionally parametrised. The parameters allows you to:
181 * <ul>
182 * <li>@x: show only a present/not present info</li>
183 * <li>@n: limit the size to a fixed value 'n'</li>
184 * <li>@+: expand the size of this field as much as possible</li>
185 * </ul>
186 *
187 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
188 *
189 * @param format
190 * the format to use
191 * @param separator
192 * the separator {@link String} to use between fields
193 * @param padding
194 * the {@link String} to use for left and right padding
195 * @param width
196 * a fixed width or -1 for "as long as needed"
197 *
198 * @param unicode
199 * allow Uniode or only ASCII characters
200 *
201 * @return the {@link String} representation
202 */
203 public String[] toStringArray(String format, String separator,
204 String padding, int width, boolean unicode) {
205 if (width > -1) {
206 int numOfFields = format.split("\\|").length;
207 if (separator != null)
208 width -= (numOfFields - 1) * separator.length();
209 if (padding != null)
210 width -= (numOfFields) * (2 * padding.length());
211
212 if (width < 0)
213 width = 0;
214 }
215
216 List<String> str = new LinkedList<String>();
217
218 boolean first = true;
219 for (String s : toStringArray(format, width, unicode)) {
220 if (!first) {
221 str.add(separator);
222 }
223
224 if (padding != null)
225 str.add(padding + s + padding);
226 else
227 str.add(s);
228
229 first = false;
230 }
231
232 return str.toArray(new String[] {});
233 }
234
235 /**
236 * Return a {@link String} representation of this contact formated
237 * accordingly to the given format, part by part.
238 *
239 * The format is basically a list of field names separated by a pipe and
240 * optionally parametrised. The parameters allows you to:
241 * <ul>
242 * <li>@x: show only a present/not present info</li>
243 * <li>@n: limit the size to a fixed value 'n'</li>
244 * <li>@+: expand the size of this field as much as possible</li>
245 * </ul>
246 *
247 * Example: "N@10|FN@20|NICK@+|PHOTO@x"
248 *
249 * @param format
250 * the format to use
251 * @param width
252 * a fixed width or -1 for "as long as needed"
253 * @param unicode
254 * allow Uniode or only ASCII characters
255 *
256 * @return the {@link String} representation
257 */
258 public String[] toStringArray(String format, int width, boolean unicode) {
259 List<String> str = new LinkedList<String>();
260
261 String[] formatFields = format.split("\\|");
262 String[] values = new String[formatFields.length];
263 Boolean[] expandedFields = new Boolean[formatFields.length];
264 Boolean[] fixedsizeFields = new Boolean[formatFields.length];
265 int numOfFieldsToExpand = 0;
266 int totalSize = 0;
267
268 if (width == 0) {
269 for (int i = 0; i < formatFields.length; i++) {
270 str.add("");
271 }
272
273 return str.toArray(new String[] {});
274 }
275
276 for (int i = 0; i < formatFields.length; i++) {
277 String field = formatFields[i];
278
279 int size = -1;
280 boolean binary = false;
281 boolean expand = false;
282
283 if (field.contains("@")) {
284 String[] opts = field.split("@");
285 if (opts.length > 0)
286 field = opts[0];
287 for (int io = 1; io < opts.length; io++) {
288 String opt = opts[io];
289 if (opt.equals("x")) {
290 binary = true;
291 } else if (opt.equals("+")) {
292 expand = true;
293 numOfFieldsToExpand++;
294 } else {
295 try {
296 size = Integer.parseInt(opt);
297 } catch (Exception e) {
298 }
299 }
300 }
301 }
302
303 String value = getPreferredDataValue(field);
304 if (value == null) {
305 value = "";
306 } else {
307 value = StringUtils.sanitize(value, unicode);
308 }
309
310 if (size > -1) {
311 value = StringUtils.padString(value, size);
312 }
313
314 expandedFields[i] = expand;
315 fixedsizeFields[i] = (size > -1);
316
317 if (binary) {
318 if (value != null && !value.equals(""))
319 values[i] = "x";
320 else
321 values[i] = " ";
322 totalSize++;
323 } else {
324 values[i] = value;
325 totalSize += value.length();
326 }
327 }
328
329 if (width > -1 && totalSize > width) {
330 int toDo = totalSize - width;
331 for (int i = fixedsizeFields.length - 1; toDo > 0 && i >= 0; i--) {
332 if (!fixedsizeFields[i]) {
333 int valueLength = values[i].length();
334 if (valueLength > 0) {
335 if (valueLength >= toDo) {
336 values[i] = values[i].substring(0, valueLength
337 - toDo);
338 toDo = 0;
339 } else {
340 values[i] = "";
341 toDo -= valueLength;
342 }
343 }
344 }
345 }
346
347 totalSize = width + toDo;
348 }
349
350 if (width > -1 && numOfFieldsToExpand > 0) {
351 int availablePadding = width - totalSize;
352
353 if (availablePadding > 0) {
354 int padPerItem = availablePadding / numOfFieldsToExpand;
355 int remainder = availablePadding % numOfFieldsToExpand;
356
357 for (int i = 0; i < values.length; i++) {
358 if (expandedFields[i]) {
359 if (remainder > 0) {
360 values[i] = values[i]
361 + StringUtils.padString("", remainder);
362 remainder = 0;
363 }
364 if (padPerItem > 0) {
365 values[i] = values[i]
366 + StringUtils.padString("", padPerItem);
367 }
368 }
369 }
370
371 totalSize = width;
372 }
373 }
374
375 int currentSize = 0;
376 for (int i = 0; i < values.length; i++) {
377 currentSize += addToList(str, values[i], currentSize, width);
378 }
379
380 return str.toArray(new String[] {});
381 }
382
383 /**
384 * Update the information from this contact with the information in the
385 * given contact. Non present fields will be removed, new fields will be
386 * added, BKey'ed fields will be completed with the binary information known
387 * by this contact.
388 *
389 * @param vc
390 * the contact with the newer information and optional BKeys
391 */
392 public void updateFrom(Contact vc) {
393 updateBKeys(false);
394
395 List<Data> newDatas = new LinkedList<Data>(vc);
396 for (int i = 0; i < newDatas.size(); i++) {
397 Data data = newDatas.get(i);
398 int bkey = Parser.getBKey(data);
399 if (bkey >= 0) {
400 if (binaries.containsKey(bkey)) {
401 newDatas.set(i, binaries.get(bkey));
402 }
403 }
404 }
405
406 replaceListContent(newDatas);
407 this.nextBKey = vc.nextBKey;
408 }
409
410 @Override
411 public String getId() {
412 return "" + getPreferredDataValue("UID");
413 }
414
415 @Override
416 public String getState() {
417 return getId();
418 }
419
420 /**
421 * Return a {@link String} representation of this contact, in vCard 2.1,
422 * without BKeys.
423 *
424 * @return the {@link String} representation
425 */
426 @Override
427 public String toString() {
428 return toString(Format.VCard21, -1);
429 }
430
431 /**
432 * Mark all the binary fields with a BKey number.
433 *
434 * @param force
435 * force the marking, and reset all the numbers.
436 */
437 protected void updateBKeys(boolean force) {
438 if (force) {
439 binaries = new HashMap<Integer, Data>();
440 nextBKey = 1;
441 }
442
443 if (binaries == null) {
444 binaries = new HashMap<Integer, Data>();
445 }
446
447 for (Data data : this) {
448 if (data.isBinary() && (data.getB64Key() <= 0 || force)) {
449 binaries.put(nextBKey, data);
450 data.resetB64Key(nextBKey++);
451 }
452 }
453 }
454
455 /**
456 * Load the data from the given {@link File} under the given {@link Format}.
457 *
458 * @param file
459 * the {@link File} to load from
460 * @param format
461 * the {@link Format} to load as
462 *
463 * @return the list of elements
464 * @throws IOException
465 * in case of IO error
466 */
467 static private List<Data> load(List<Data> content) {
468 List<Data> datas = new ArrayList<Data>();
469
470 boolean fn = false;
471 boolean n = false;
472 boolean uid = false;
473 if (content != null) {
474 for (Data data : content) {
475 if (data.getName().equals("N")) {
476 n = true;
477 } else if (data.getName().equals("FN")) {
478 fn = true;
479 } else if (data.getName().equals("UID")) {
480 uid = true;
481 }
482
483 if (!data.getName().equals("VERSION")) {
484 datas.add(data);
485 }
486 }
487 }
488
489 // required fields:
490 if (!n) // required since vCard 3.0, supported in 2.1
491 datas.add(new Data(null, "N", "", null));
492 if (!fn) // not required anymore but still supported in 4.0
493 datas.add(new Data(null, "FN", "", null));
494 if (!uid) // supported by vCard, required by this program
495 datas.add(new Data(null, "UID", UUID.randomUUID().toString(), null));
496
497 return datas;
498 }
499
500 /**
501 * Add a {@link String} to the given {@link List}, but make sure it does not
502 * exceed the maximum size, and truncate it if needed to fit.
503 *
504 * @param list
505 * @param add
506 * @param currentSize
507 * @param maxSize
508 * @return
509 */
510 static private int addToList(List<String> list, String add,
511 int currentSize, int maxSize) {
512 if (add == null || add.length() == 0) {
513 if (add != null)
514 list.add(add);
515 return 0;
516 }
517
518 if (maxSize > -1) {
519 if (currentSize < maxSize) {
520 if (currentSize + add.length() >= maxSize) {
521 add = add.substring(0, maxSize - currentSize);
522 }
523 } else {
524 add = "";
525 }
526 }
527
528 list.add(add);
529 return add.length();
530 }
531 }