VCard format: correctly co/decode escaped values
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactDetails.java
index 8aeac19f27002419035c58ba6218cf36a692bc82..1d6beb9998bf01b461fb4590746edf1bb4d13868 100644 (file)
@@ -1,16 +1,21 @@
 package be.nikiroo.jvcard.tui.panes;
 
 import java.awt.Image;
-import java.util.Base64;
+import java.io.ByteArrayInputStream;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
 
-import javax.swing.ImageIcon;
+import javax.imageio.ImageIO;
+import javax.xml.bind.DatatypeConverter;
 
 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.Bundles;
+import be.nikiroo.jvcard.resources.StringUtils;
+import be.nikiroo.jvcard.resources.Trans;
 import be.nikiroo.jvcard.tui.ImageTextControl;
 import be.nikiroo.jvcard.tui.KeyAction;
 import be.nikiroo.jvcard.tui.KeyAction.DataType;
@@ -19,6 +24,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;
@@ -33,8 +39,11 @@ public class ContactDetails extends MainContent {
        private boolean fullscreenImage;
        private Panel infoPanel;
        private Label note;
+       ResourceBundle map;
 
        public ContactDetails(Contact contact) {
+               map = Bundles.getBundle("display");
+
                BorderLayout blayout = new BorderLayout();
                setLayoutManager(blayout);
 
@@ -56,25 +65,24 @@ public class ContactDetails extends MainContent {
 
                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);
        }
 
        /**
         * Change the enclosed {@link Contact} from this {@link ContactDetails}.
+        * Also re-set the image.
         * 
         * @param contact
         *            the new {@link Contact}
         */
        public void setContact(Contact contact) {
-               if (this.contact == contact)
-                       return;
-
                this.contact = contact;
+               image = null;
 
-               if (contact == null) {
-                       image = null;
-               } else {
+               if (contact != null) {
                        infoPanel.removeAllComponents();
 
                        String name = contact.getPreferredDataValue("FN");
@@ -83,45 +91,128 @@ public class ContactDetails extends MainContent {
                                name = contact.getPreferredDataValue("N");
                        }
 
-                       // 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")));
+
+                       // List of infos:
+                       int labelSize = -1;
+                       try {
+                               labelSize = Integer.parseInt(map
+                                               .getString("CONTACT_DETAILS_LABEL_WIDTH"));
+
+                       } catch (NumberFormatException e) {
+                               e.printStackTrace();
+                               labelSize = -1;
+                       } catch (MissingResourceException e) {
+                               labelSize = -1;
+                       }
+
+                       String infoFormat = "";
+                       try {
+                               infoFormat = map.getString("CONTACT_DETAILS_INFO");
+                       } catch (MissingResourceException e) {
+                               e.printStackTrace();
+                       }
+
+                       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("=")) {
+                                       UiColors.Element el = hl ? UiColors.Element.VIEW_CONTACT_HIGHLIGHT
+                                                       : UiColors.Element.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) {
+                                               for (Data data : contact.getData(field)) {
+                                                       if (data.isPreferred()) {
+                                                               infoPanel.addComponent(el
+                                                                               .createLabel(StringUtils.padString(
+                                                                                               label, labelSize)
+                                                                                               + data.toString()));
+                                                       } else {
+                                                               infoPanel
+                                                                               .addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
+                                                                                               .createLabel(StringUtils
+                                                                                                               .padString(label,
+                                                                                                                               labelSize)
+                                                                                                               + data.toString()));
+                                                       }
+                                               }
+                                       } else {
+                                               String val = contact.getPreferredDataValue(field);
+                                               if (val == null)
+                                                       val = "";
+                                               infoPanel.addComponent(el.createLabel(StringUtils
+                                                               .padString(label, labelSize) + val));
+                                       }
+                               } else {
+                                       String label = info;
+                                       infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
+                                                       .createLabel(StringUtils
+                                                                       .padString(label, labelSize)));
+                               }
+                       }
+                       // end of list
+
                        infoPanel.addComponent(UiColors.Element.VIEW_CONTACT_NORMAL
                                        .createLabel(""));
 
                        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;
-                               TypeInfo type = null;
                                for (int index = 0; index < photo.size(); index++) {
                                        TypeInfo info = photo.get(index);
                                        if (info.getName() != null) {
                                                if (info.getName().equalsIgnoreCase("ENCODING"))
                                                        encoding = info;
-                                               if (info.getName().equalsIgnoreCase("TYPE"))
-                                                       type = info;
+                                               // We don't check for the "TYPE" anymore, we just defer
+                                               // it to ImageIcon
                                        }
                                }
 
                                if (encoding != null && encoding.getValue() != null
                                                && encoding.getValue().equalsIgnoreCase("b")) {
 
-                                       image = new ImageIcon(Base64.getDecoder().decode(
-                                                       photo.getValue())).getImage();
+                                       try {
+                                               image = ImageIO.read(new ByteArrayInputStream(
+                                                               DatatypeConverter.parseBase64Binary(photo
+                                                                               .getValue())));
+                                               image.toString();
+                                       } catch (Exception e) {
+                                               System.err.println("Cannot parse image for contact: "
+                                                               + contact.getPreferredDataValue("UID"));
+                                       }
                                }
                        }
                }
@@ -138,7 +229,6 @@ 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) {
                        @Override
@@ -170,6 +260,14 @@ public class ContactDetails extends MainContent {
                                return false;
                        }
                });
+               // TODO: add "normal" edit
+               actions.add(new KeyAction(Mode.CONTACT_DETAILS_RAW, 'r',
+                               Trans.StringId.KEY_ACTION_EDIT_CONTACT_RAW) {
+                       @Override
+                       public Object getObject() {
+                               return contact;
+                       }
+               });
 
                return actions;
        }
@@ -223,7 +321,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;