Commit | Line | Data |
---|---|---|
10dd1e38 NR |
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 TApplication app; | |
21 | private List<String> files; | |
22 | private List<CardResult> cards; | |
23 | ||
24 | public TuiFileListWindow(TApplication app, List<String> files) { | |
25 | super(app, "Contacts", false); | |
26 | ||
27 | this.app = app; | |
28 | this.files = files; | |
29 | ||
30 | cards = new ArrayList<CardResult>(); | |
31 | for (int i = 0; i < files.size(); i++) { | |
32 | cards.add(null); | |
33 | } | |
34 | ||
35 | List<String> headers = new ArrayList<String>(); | |
36 | headers.add("File"); | |
37 | List<List<String>> dataLines = new ArrayList<List<String>>(); | |
38 | for (String file : files) { | |
39 | List<String> listOfOneFile = new ArrayList<String>(1); | |
40 | listOfOneFile.add(new File(file).getName()); | |
41 | dataLines.add(listOfOneFile); | |
42 | } | |
43 | ||
44 | addKeyBinding(TKeypress.kbQ, new TAction() { | |
45 | @Override | |
46 | public void DO() { | |
47 | close(); | |
48 | } | |
49 | }); | |
50 | ||
51 | setData(headers, dataLines); | |
52 | } | |
53 | ||
54 | @Override | |
55 | public void onAction(int selectedLine, int selectedColumn) { | |
56 | try { | |
57 | @SuppressWarnings("unused") | |
58 | TWindow w = new TuiContactListWindow(app, 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 | } |