import java.util.List;
import be.nikiroo.jvcard.Card;
-import be.nikiroo.jvcard.i18n.Trans;
+import be.nikiroo.jvcard.launcher.CardResult;
+import be.nikiroo.jvcard.launcher.CardResult.MergeCallback;
+import be.nikiroo.jvcard.launcher.Main;
import be.nikiroo.jvcard.parsers.Format;
-import be.nikiroo.jvcard.remote.Sync;
+import be.nikiroo.jvcard.resources.ColorOption;
+import be.nikiroo.jvcard.resources.StringId;
import be.nikiroo.jvcard.tui.KeyAction;
import be.nikiroo.jvcard.tui.KeyAction.DataType;
import be.nikiroo.jvcard.tui.KeyAction.Mode;
-import be.nikiroo.jvcard.tui.StringUtils;
-import be.nikiroo.jvcard.tui.UiColors;
-import be.nikiroo.jvcard.tui.UiColors.Element;
+import be.nikiroo.utils.StringUtils;
import com.googlecode.lanterna.input.KeyType;
public class FileList extends MainContentList {
private List<String> files;
- private List<Card> cards;
+ private List<CardResult> cards;
+
+ private FileList merger;
+ private String mergeRemoteState;
+ private String mergeSourceFile;
+ private File mergeTargetFile;
public FileList(List<String> files) {
setFiles(files);
public void setFiles(List<String> files) {
clearItems();
this.files = files;
- cards = new ArrayList<Card>();
+ cards = new ArrayList<CardResult>();
for (String file : files) {
addItem(file); // TODO
// TODO: from ini file?
int SIZE_COL_1 = 3;
- Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
- : Element.CONTACT_LINE;
- Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
- : Element.CONTACT_LINE_SEPARATOR;
+ ColorOption el = (focused && selected) ? ColorOption.CONTACT_LINE_SELECTED
+ : ColorOption.CONTACT_LINE;
+ ColorOption elSep = (focused && selected) ? ColorOption.CONTACT_LINE_SEPARATOR_SELECTED
+ : ColorOption.CONTACT_LINE_SEPARATOR;
List<TextPart> parts = new LinkedList<TextPart>();
String count = "";
- if (cards.get(index) != null)
- count += cards.get(index).size();
+ if (cards.get(index) != null) {
+ try {
+ count += cards.get(index).getCard().size();
+ } catch (IOException e) {
+ }
+ }
String name = files.get(index).replaceAll("\\\\", "/");
int indexSl = name.lastIndexOf('/');
name = name.substring(indexSl + 1);
}
- name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
+ name = StringUtils.sanitize(name, Main.isUnicode());
count = " " + StringUtils.padString(count, SIZE_COL_1) + " ";
name = " "
parts.add(new TextPart(name, el));
return parts;
- };
+ }
@Override
public List<KeyAction> getKeyBindings() {
// TODO del, save...
actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter,
- Trans.StringId.KEY_ACTION_VIEW_CARD) {
+ StringId.KEY_ACTION_VIEW_CARD) {
+ private Object obj = null;
+
@Override
public Object getObject() {
- int index = getSelectedIndex();
-
- if (index < 0 || index >= cards.size())
- return null;
-
- if (cards.get(index) != null)
- return cards.get(index);
-
- String file = files.get(index);
-
- try {
- Card card = FileList.getCard(file);
- cards.set(index, card);
-
- invalidate();
-
- return card;
- } catch (IOException ioe) {
- ioe.printStackTrace();
- return null;
+ if (obj == null) {
+ int index = getSelectedIndex();
+ if (index < 0 || index >= cards.size())
+ return null;
+
+ try {
+ if (cards.get(index) != null) {
+ obj = cards.get(index).getCard();
+ } else {
+ String file = files.get(index);
+
+ CardResult card = null;
+ final Card arr[] = new Card[4];
+ try {
+ card = Main.getCard(file, new MergeCallback() {
+ @Override
+ public Card merge(Card previous,
+ Card local, Card server,
+ Card autoMerged) {
+ arr[0] = previous;
+ arr[1] = local;
+ arr[2] = server;
+ arr[3] = autoMerged;
+
+ return null;
+ }
+ });
+
+ obj = card.getCard(); // throw IOE if problem
+ } catch (IOException e) {
+ if (arr[0] == null)
+ throw e;
+
+ // merge management: set all merge vars in
+ // merger,
+ // make sure it has cards but mergeTargetFile
+ // does not exist
+ // (create then delete if needed)
+ // TODO: i18n
+ setMessage(
+ "Merge error, please check/fix the merged contact",
+ true);
+
+ // TODO: i18n + filename with numbers in it to
+ // fix
+ File a = File.createTempFile("Merge result ",
+ ".vcf");
+ File p = File.createTempFile(
+ "Previous common version ", ".vcf");
+ File l = File.createTempFile("Local ", ".vcf");
+ File s = File.createTempFile("Remote ", ".vcf");
+ arr[3].saveAs(a, Format.VCard21);
+ arr[0].saveAs(p, Format.VCard21);
+ arr[1].saveAs(l, Format.VCard21);
+ arr[2].saveAs(s, Format.VCard21);
+ List<String> mfiles = new LinkedList<String>();
+ mfiles.add(a.getAbsolutePath());
+ mfiles.add(p.getAbsolutePath());
+ mfiles.add(l.getAbsolutePath());
+ mfiles.add(s.getAbsolutePath());
+ merger = new FileList(mfiles);
+ merger.mergeRemoteState = arr[2]
+ .getContentState(false);
+ merger.mergeSourceFile = files.get(index);
+ merger.mergeTargetFile = a;
+
+ obj = merger;
+ return obj;
+ }
+
+ cards.set(index, card);
+
+ invalidate();
+
+ if (card.isSynchronised()) {
+ // TODO i18n
+ if (card.isChanged())
+ setMessage(
+ "card synchronised: changes from server",
+ false);
+ else
+ setMessage("card synchronised: no changes",
+ false);
+ }
+ }
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ // TODO
+ setMessage("ERROR!", true);
+ }
}
+
+ return obj;
}
+
});
return actions;
}
- static private Card getCard(String input) throws IOException {
- boolean remote = false;
- Format format = Format.Abook;
- String ext = input;
- if (ext.contains(".")) {
- String tab[] = ext.split("\\.");
- if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
- format = Format.VCard21;
+ @Override
+ public String wakeup() throws IOException {
+ String s = super.wakeup();
+ if (s != null)
+ return s;
+
+ if (merger != null) {
+ if (!merger.mergeTargetFile.exists()) {
+ throw new IOException("Merge cancelled");
}
- }
- if (input.contains("://")) {
- format = Format.VCard21;
- remote = true;
- }
-
- Card card = null;
- try {
- if (remote) {
- Sync sync = new Sync(input);
- card = new Card(sync.getCache(), format);
- card.setRemote(true);
- sync.sync(card, false);
- } else {
- card = new Card(new File(input), format);
+ // merge back to server if needed and not changed:
+ try {
+ Main.getCard(merger.mergeSourceFile, new MergeCallback() {
+ @Override
+ public Card merge(Card previous, Card local, Card server,
+ Card autoMerged) {
+ try {
+ if (server.getContentState(false).equals(
+ merger.mergeRemoteState)) {
+ return new Card(merger.mergeTargetFile,
+ Format.VCard21);
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+ }).getCard();
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException("Server changed since merge, cancel", e);
}
- } catch (IOException ioe) {
- ioe.printStackTrace();
- throw ioe;
+
+ merger = null;
+
+ // TODO i18n
+ return "merged.";
}
- return card;
+ return null;
}
}