Fix PREF handling (was not correct relative to the RFC!)
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
index 06c88fc9ec02a66f71011f6842ecb54d6e1e0603..2ea8a9724b80cf6ac777cb37fc75ea08ae7f328c 100644 (file)
@@ -4,13 +4,14 @@ import java.awt.Image;
 import java.util.LinkedList;
 import java.util.List;
 
-import javax.xml.bind.DatatypeConverter;
-import javax.swing.ImageIcon;
-
 import be.nikiroo.jvcard.Contact;
 import be.nikiroo.jvcard.Data;
 import be.nikiroo.jvcard.TypeInfo;
-import be.nikiroo.jvcard.i18n.Trans;
+import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.jvcard.resources.bundles.DisplayBundle;
+import be.nikiroo.jvcard.resources.enums.DisplayOption;
+import be.nikiroo.jvcard.resources.enums.ColorOption;
+import be.nikiroo.jvcard.resources.enums.StringId;
 import be.nikiroo.jvcard.tui.ImageTextControl;
 import be.nikiroo.jvcard.tui.KeyAction;
 import be.nikiroo.jvcard.tui.KeyAction.DataType;
@@ -19,6 +20,7 @@ import be.nikiroo.jvcard.tui.UiColors;
 
 import com.googlecode.lanterna.TerminalSize;
 import com.googlecode.lanterna.gui2.BorderLayout;
+import com.googlecode.lanterna.gui2.Borders;
 import com.googlecode.lanterna.gui2.Direction;
 import com.googlecode.lanterna.gui2.Label;
 import com.googlecode.lanterna.gui2.LinearLayout;
@@ -34,7 +36,20 @@ public class ContactDetails extends MainContent {
        private Panel infoPanel;
        private Label note;
 
+       // from .properties file:
+       private int labelSize = -1;
+       private String infoFormat = "";
+
+       //
+
        public ContactDetails(Contact contact) {
+               // Get the .properties info:
+               DisplayBundle map = new DisplayBundle();
+               labelSize = map.getInteger(DisplayOption.CONTACT_DETAILS_LABEL_WIDTH,
+                               -1);
+               infoFormat = map.getString(DisplayOption.CONTACT_DETAILS_INFO);
+               //
+
                BorderLayout blayout = new BorderLayout();
                setLayoutManager(blayout);
 
@@ -49,15 +64,17 @@ public class ContactDetails extends MainContent {
                Panel notePanel = new Panel();
                notePanel.setLayoutManager(new LinearLayout(Direction.HORIZONTAL));
 
-               notePanel.addComponent(UiColors.Element.VIEW_CONTACT_NOTES_TITLE
-                               .createLabel("Notes:"));
-               note = UiColors.Element.VIEW_CONTACT_NORMAL.createLabel("");
+               notePanel.addComponent(UiColors.createLabel(
+                               ColorOption.VIEW_CONTACT_NOTES_TITLE, "Notes:"));
+               note = UiColors.createLabel(ColorOption.VIEW_CONTACT_NORMAL, "");
                notePanel.addComponent(note);
 
                setContact(contact);
 
-               addComponent(top, BorderLayout.Location.TOP);
-               addComponent(notePanel, BorderLayout.Location.CENTER);
+               addComponent(top.withBorder(Borders.doubleLineBevel()),
+                               BorderLayout.Location.TOP);
+               addComponent(notePanel.withBorder(Borders.singleLineBevel()),
+                               BorderLayout.Location.CENTER);
        }
 
        /**
@@ -75,36 +92,86 @@ public class ContactDetails extends MainContent {
                        infoPanel.removeAllComponents();
 
                        String name = contact.getPreferredDataValue("FN");
-                       if (name == null || name.length() == 0) {
-                               // TODO format it ourself
-                               name = contact.getPreferredDataValue("N");
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NAME, name));
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NORMAL, ""));
+
+                       // List of infos:
+                       String[] infos = infoFormat.split("\\|");
+                       for (String info : infos) {
+                               // # - "=FIELD" will take the preferred value for this field
+                               // # - "+FIELD" will take the preferred value for this field and
+                               // highlight it
+                               // # - "#FIELD" will take all the values with this field's name
+                               // # - "*FIELD" will take all the values with this field's name,
+                               // highlighting the preferred one
+                               // #
+
+                               boolean hl = false;
+                               boolean all = false;
+                               if (info.contains("+") || info.contains("#"))
+                                       hl = true;
+                               if (info.contains("*") || info.contains("#"))
+                                       all = true;
+
+                               if (all || hl || info.contains("=")) {
+                                       ColorOption el = hl ? ColorOption.VIEW_CONTACT_HIGHLIGHT
+                                                       : ColorOption.VIEW_CONTACT_NORMAL;
+
+                                       int index = info.indexOf('=');
+                                       if (index < 0)
+                                               index = info.indexOf('+');
+                                       if (index < 0)
+                                               index = info.indexOf('#');
+                                       if (index < 0)
+                                               index = info.indexOf('*');
+
+                                       String label = info.substring(0, index);
+                                       String field = info.substring(index + 1);
+
+                                       if (all) {
+                                               Data pref = contact.getPreferredData(field);
+                                               for (Data data : contact.getData(field)) {
+                                                       if (data == pref) {
+                                                               infoPanel.addComponent(UiColors.createLabel(el,
+                                                                               StringUtils.padString(label, labelSize)
+                                                                                               + data.toString()));
+                                                       } else {
+                                                               infoPanel.addComponent(UiColors.createLabel(
+                                                                               ColorOption.VIEW_CONTACT_NORMAL,
+                                                                               StringUtils.padString(label, labelSize)
+                                                                                               + data.toString()));
+                                                       }
+                                               }
+                                       } else {
+                                               String val = contact.getPreferredDataValue(field);
+                                               if (val == null)
+                                                       val = "";
+                                               infoPanel.addComponent(UiColors.createLabel(el,
+                                                               StringUtils.padString(label, labelSize) + val));
+                                       }
+                               } else {
+                                       String label = info;
+                                       infoPanel.addComponent(UiColors.createLabel(
+                                                       ColorOption.VIEW_CONTACT_NORMAL,
+                                                       StringUtils.padString(label, labelSize)));
+                               }
                        }
+                       // end of list
 
-                       // TODO: i18n + do it properly
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NAME
-                                       .createLabel(name));
-
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel(""));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel("Phone:    "
-                                                       + contact.getPreferredDataValue("TEL")));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel("eMail:    "
-                                                       + contact.getPreferredDataValue("EMAIL")));
-                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
-                                       .createLabel(""));
+                       infoPanel.addComponent(UiColors.createLabel(
+                                       ColorOption.VIEW_CONTACT_NORMAL, ""));
 
                        String notes = contact.getPreferredDataValue("NOTE");
                        if (notes == null)
                                notes = "";
-                       note.setText(notes.replaceAll("\\\\n", "\n"));
+                       note.setText(notes);
 
                        Data photo = contact.getPreferredData("PHOTO");
                        if (photo != null) {
                                TypeInfo encoding = null;
-                               for (int index = 0; index < photo.size(); index++) {
-                                       TypeInfo info = photo.get(index);
+                               for (TypeInfo info : photo) {
                                        if (info.getName() != null) {
                                                if (info.getName().equalsIgnoreCase("ENCODING"))
                                                        encoding = info;
@@ -116,8 +183,12 @@ public class ContactDetails extends MainContent {
                                if (encoding != null && encoding.getValue() != null
                                                && encoding.getValue().equalsIgnoreCase("b")) {
 
-                                       image = new ImageIcon(DatatypeConverter.parseBase64Binary(
-                                                       photo.getValue())).getImage();
+                                       try {
+                                               image = StringUtils.toImage(photo.getValue());
+                                       } catch (Exception e) {
+                                               System.err.println("Cannot parse image for contact: "
+                                                               + contact.getPreferredDataValue("UID"));
+                                       }
                                }
                        }
                }
@@ -134,9 +205,8 @@ public class ContactDetails extends MainContent {
        public List<KeyAction> getKeyBindings() {
                List<KeyAction> actions = new LinkedList<KeyAction>();
 
-               // TODO
                actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
-                               Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
+                               StringId.KEY_ACTION_SWITCH_FORMAT) {
                        @Override
                        public boolean onAction() {
                                if (txtImage != null) {
@@ -146,8 +216,7 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
-               actions.add(new KeyAction(Mode.NONE, 'i',
-                               Trans.StringId.KEY_ACTION_INVERT) {
+               actions.add(new KeyAction(Mode.NONE, 'i', StringId.KEY_ACTION_INVERT) {
                        @Override
                        public boolean onAction() {
                                if (txtImage != null) {
@@ -158,7 +227,7 @@ public class ContactDetails extends MainContent {
                        }
                });
                actions.add(new KeyAction(Mode.NONE, 'f',
-                               Trans.StringId.KEY_ACTION_FULLSCREEN) {
+                               StringId.KEY_ACTION_FULLSCREEN) {
                        @Override
                        public boolean onAction() {
                                fullscreenImage = !fullscreenImage;
@@ -166,9 +235,9 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
-               // TODO: add "normal" edit and remove this one into RAW edit
-               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'e',
-                               Trans.StringId.KEY_ACTION_EDIT_CONTACT) {
+               // TODO: add "normal" edit
+               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r',
+                               StringId.KEY_ACTION_EDIT_CONTACT_RAW) {
                        @Override
                        public Object getObject() {
                                return contact;
@@ -227,7 +296,7 @@ public class ContactDetails extends MainContent {
                        } else {
                                // TODO: configure size?
                                int w = getSize().getColumns() - 40;
-                               int h = getSize().getRows() - 5;
+                               int h = getSize().getRows() - 9;
                                if (w <= 0 || h <= 0)
                                        return null;