From 7671a2499e6f0d6c8e0765b36c18c1e89bc457c5 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 2 Apr 2016 14:51:08 +0200 Subject: [PATCH] Fix PREF handling (was not correct relative to the RFC!) --- src/be/nikiroo/jvcard/Contact.java | 19 +++++++++++-------- src/be/nikiroo/jvcard/Data.java | 17 +++++++++++------ .../jvcard/tui/panes/ContactDetails.java | 3 ++- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/be/nikiroo/jvcard/Contact.java b/src/be/nikiroo/jvcard/Contact.java index 9facbcd..d75d338 100644 --- a/src/be/nikiroo/jvcard/Contact.java +++ b/src/be/nikiroo/jvcard/Contact.java @@ -36,23 +36,26 @@ public class Contact extends BaseClass { } /** - * Return the preferred Data field with the given name, or NULL if none. + * Return the preferred Data field with the given name, the first one if + * none is preferred, or NULL if none at all. * * @param name * the name to look for - * @return the Data field, or NULL + * + * @return the {@link Data} field, or NULL */ public Data getPreferredData(String name) { - Data first = null; + Data pref = null; + int ipref = Integer.MAX_VALUE; for (Data data : getData(name)) { - if (first == null) - first = data; + if (pref == null) + pref = data; - if (data.isPreferred()) - return data; + if (data.getPreferred() < ipref) + pref = data; } - return first; + return pref; } /** diff --git a/src/be/nikiroo/jvcard/Data.java b/src/be/nikiroo/jvcard/Data.java index ba5fde5..4d3da7e 100644 --- a/src/be/nikiroo/jvcard/Data.java +++ b/src/be/nikiroo/jvcard/Data.java @@ -186,18 +186,23 @@ public class Data extends BaseClass { } /** - * Check if this {@link Data} has the "preferred" flag. + * Return the preferred value of this {@link Data}, or + * {@link Integer#MAX_VALUE} if none. * - * @return TRUE if it has + * @return the preferred value */ - public boolean isPreferred() { + public int getPreferred() { for (TypeInfo type : this) { - if (type.getName().equals("TYPE") && type.getValue().equals("pref")) { - return true; + if (type.getName().equals("PRE")) { + try { + return Integer.parseInt(type.getValue()); + } catch (NumberFormatException e) { + e.printStackTrace(); + } } } - return false; + return Integer.MAX_VALUE; } /** diff --git a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java index be2c201..2ea8a97 100644 --- a/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java +++ b/src/be/nikiroo/jvcard/tui/panes/ContactDetails.java @@ -131,8 +131,9 @@ public class ContactDetails extends MainContent { String field = info.substring(index + 1); if (all) { + Data pref = contact.getPreferredData(field); for (Data data : contact.getData(field)) { - if (data.isPreferred()) { + if (data == pref) { infoPanel.addComponent(UiColors.createLabel(el, StringUtils.padString(label, labelSize) + data.toString())); -- 2.27.0