| 1 | package be.nikiroo.jvcard.tui.windows; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import jexer.TAction; |
| 10 | import jexer.TApplication; |
| 11 | import jexer.TKeypress; |
| 12 | import jexer.TWindow; |
| 13 | import be.nikiroo.jvcard.Card; |
| 14 | import be.nikiroo.jvcard.launcher.CardResult; |
| 15 | import be.nikiroo.jvcard.launcher.CardResult.MergeCallback; |
| 16 | import be.nikiroo.jvcard.launcher.Main; |
| 17 | import be.nikiroo.jvcard.parsers.Format; |
| 18 | |
| 19 | public class TuiFileListWindow extends TuiBrowserWindow { |
| 20 | private List<String> files; |
| 21 | private List<CardResult> cards; |
| 22 | |
| 23 | public TuiFileListWindow(TApplication app, int width, int height, |
| 24 | List<String> files) { |
| 25 | super(app, width, height, "Contacts", false); |
| 26 | |
| 27 | this.files = files; |
| 28 | |
| 29 | cards = new ArrayList<CardResult>(); |
| 30 | for (int i = 0; i < files.size(); i++) { |
| 31 | cards.add(null); |
| 32 | } |
| 33 | |
| 34 | List<String> headers = new ArrayList<String>(); |
| 35 | headers.add("File"); |
| 36 | List<List<String>> dataLines = new ArrayList<List<String>>(); |
| 37 | for (String file : files) { |
| 38 | List<String> listOfOneFile = new ArrayList<String>(1); |
| 39 | listOfOneFile.add(new File(file).getName()); |
| 40 | dataLines.add(listOfOneFile); |
| 41 | } |
| 42 | |
| 43 | addKeyBinding(TKeypress.kbQ, "Quit", new TAction() { |
| 44 | @Override |
| 45 | public void DO() { |
| 46 | close(); |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | setData(headers, dataLines); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onAction(int selectedLine, int selectedColumn) { |
| 55 | try { |
| 56 | @SuppressWarnings("unused") |
| 57 | TWindow w = new TuiContactListWindow(TuiFileListWindow.this, |
| 58 | getCard(selectedLine)); |
| 59 | } catch (IOException e) { |
| 60 | setMessage("Fail to get file: " + e.getMessage(), true); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private Card getCard(int index) throws IOException { |
| 65 | // TODO: check index? |
| 66 | if (cards.get(index) == null) { |
| 67 | String file = files.get(index); |
| 68 | CardResult cardResult = retrieveCardResult(file); |
| 69 | cards.set(index, cardResult); |
| 70 | } |
| 71 | |
| 72 | return cards.get(index).getCard(); |
| 73 | } |
| 74 | |
| 75 | private CardResult retrieveCardResult(String file) throws IOException { |
| 76 | CardResult cardResult = null; |
| 77 | final Card arr[] = new Card[4]; |
| 78 | try { |
| 79 | cardResult = Main.getCard(file, new MergeCallback() { |
| 80 | @Override |
| 81 | public Card merge(Card previous, Card local, Card server, |
| 82 | Card autoMerged) { |
| 83 | arr[0] = previous; |
| 84 | arr[1] = local; |
| 85 | arr[2] = server; |
| 86 | arr[3] = autoMerged; |
| 87 | |
| 88 | return null; |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | cardResult.getCard(); // throw IOE if sync issues |
| 93 | } catch (IOException e) { |
| 94 | // Check if merge issue or something else I/O related |
| 95 | if (arr[0] == null) |
| 96 | throw e; // other I/O problems |
| 97 | |
| 98 | // merge management: set all merge vars in |
| 99 | // merger, |
| 100 | // make sure it has cards but mergeTargetFile |
| 101 | // does not exist |
| 102 | // (create then delete if needed) |
| 103 | // TODO: i18n |
| 104 | setMessage("Merge error, please check/fix the merged contact", true); |
| 105 | |
| 106 | // TODO: i18n + filename with numbers in it to |
| 107 | // fix |
| 108 | File a = File.createTempFile("Merge result ", ".vcf"); |
| 109 | File p = File.createTempFile("Previous common version ", ".vcf"); |
| 110 | File l = File.createTempFile("Local ", ".vcf"); |
| 111 | File s = File.createTempFile("Remote ", ".vcf"); |
| 112 | arr[3].saveAs(a, Format.VCard21); |
| 113 | arr[0].saveAs(p, Format.VCard21); |
| 114 | arr[1].saveAs(l, Format.VCard21); |
| 115 | arr[2].saveAs(s, Format.VCard21); |
| 116 | List<String> mfiles = new LinkedList<String>(); |
| 117 | mfiles.add(a.getAbsolutePath()); |
| 118 | mfiles.add(p.getAbsolutePath()); |
| 119 | mfiles.add(l.getAbsolutePath()); |
| 120 | mfiles.add(s.getAbsolutePath()); |
| 121 | /* |
| 122 | * merger = new FileList(mfiles); merger.mergeRemoteState = |
| 123 | * arr[2].getContentState(false); merger.mergeSourceFile = |
| 124 | * files.get(index); merger.mergeTargetFile = a; |
| 125 | * |
| 126 | * obj = merger; |
| 127 | */ |
| 128 | } |
| 129 | |
| 130 | // TODO: |
| 131 | // invalidate(); |
| 132 | |
| 133 | if (cardResult.isSynchronised()) { |
| 134 | // TODO i18n |
| 135 | if (cardResult.isChanged()) |
| 136 | setMessage("card synchronised: changes from server", false); |
| 137 | else |
| 138 | setMessage("card synchronised: no changes", false); |
| 139 | } |
| 140 | |
| 141 | return cardResult; |
| 142 | } |
| 143 | |
| 144 | // TODO |
| 145 | private void setMessage(String message, boolean error) { |
| 146 | if (error) { |
| 147 | System.err.println(message); |
| 148 | } else { |
| 149 | System.out.println(message); |
| 150 | } |
| 151 | } |
| 152 | } |