Commit | Line | Data |
---|---|---|
fae07ea7 NR |
1 | package be.nikiroo.jvcard.tui.panes; |
2 | ||
5ad0e17e | 3 | import java.io.File; |
9c8baf0c | 4 | import java.io.IOException; |
bcb54330 | 5 | import java.util.ArrayList; |
9c8baf0c | 6 | import java.util.LinkedList; |
fae07ea7 NR |
7 | import java.util.List; |
8 | ||
9c8baf0c | 9 | import be.nikiroo.jvcard.Card; |
5ad0e17e NR |
10 | import be.nikiroo.jvcard.launcher.CardResult; |
11 | import be.nikiroo.jvcard.launcher.CardResult.MergeCallback; | |
7da41ecd | 12 | import be.nikiroo.jvcard.launcher.Main; |
5ad0e17e | 13 | import be.nikiroo.jvcard.parsers.Format; |
7da41ecd | 14 | import be.nikiroo.jvcard.resources.StringUtils; |
e119a1c1 NR |
15 | import be.nikiroo.jvcard.resources.enums.ColorOption; |
16 | import be.nikiroo.jvcard.resources.enums.StringId; | |
fae07ea7 | 17 | import be.nikiroo.jvcard.tui.KeyAction; |
fae07ea7 NR |
18 | import be.nikiroo.jvcard.tui.KeyAction.DataType; |
19 | import be.nikiroo.jvcard.tui.KeyAction.Mode; | |
20 | ||
9c8baf0c | 21 | import com.googlecode.lanterna.input.KeyType; |
fae07ea7 NR |
22 | |
23 | public class FileList extends MainContentList { | |
7f82bf68 | 24 | private List<String> files; |
5ad0e17e NR |
25 | private List<CardResult> cards; |
26 | ||
27 | private FileList merger; | |
28 | private String mergeRemoteState; | |
29 | private String mergeSourceFile; | |
30 | private File mergeTargetFile; | |
fae07ea7 | 31 | |
7f82bf68 | 32 | public FileList(List<String> files) { |
fae07ea7 NR |
33 | setFiles(files); |
34 | } | |
35 | ||
36 | /** | |
37 | * Change the list of currently selected files. | |
38 | * | |
39 | * @param files | |
40 | * the new files | |
41 | */ | |
7f82bf68 | 42 | public void setFiles(List<String> files) { |
fae07ea7 NR |
43 | clearItems(); |
44 | this.files = files; | |
5ad0e17e | 45 | cards = new ArrayList<CardResult>(); |
fae07ea7 | 46 | |
7f82bf68 NR |
47 | for (String file : files) { |
48 | addItem(file); // TODO | |
bcb54330 | 49 | cards.add(null); |
fae07ea7 NR |
50 | } |
51 | ||
52 | setSelectedIndex(0); | |
53 | } | |
54 | ||
55 | @Override | |
56 | public DataType getDataType() { | |
57 | return DataType.CARD_FILES; | |
58 | } | |
59 | ||
60 | @Override | |
bcb54330 NR |
61 | protected List<TextPart> getLabel(int index, int width, boolean selected, |
62 | boolean focused) { | |
63 | // TODO: from ini file? | |
64 | int SIZE_COL_1 = 3; | |
65 | ||
e119a1c1 NR |
66 | ColorOption el = (focused && selected) ? ColorOption.CONTACT_LINE_SELECTED |
67 | : ColorOption.CONTACT_LINE; | |
68 | ColorOption elSep = (focused && selected) ? ColorOption.CONTACT_LINE_SEPARATOR_SELECTED | |
69 | : ColorOption.CONTACT_LINE_SEPARATOR; | |
bcb54330 NR |
70 | |
71 | List<TextPart> parts = new LinkedList<TextPart>(); | |
72 | ||
73 | String count = ""; | |
5ad0e17e NR |
74 | if (cards.get(index) != null) { |
75 | try { | |
76 | count += cards.get(index).getCard().size(); | |
77 | } catch (IOException e) { | |
78 | } | |
79 | } | |
bcb54330 | 80 | |
7f82bf68 NR |
81 | String name = files.get(index).replaceAll("\\\\", "/"); |
82 | int indexSl = name.lastIndexOf('/'); | |
83 | if (indexSl >= 0) { | |
84 | name = name.substring(indexSl + 1); | |
85 | } | |
bcb54330 | 86 | |
7da41ecd | 87 | name = StringUtils.sanitize(name, Main.isUnicode()); |
296a0b75 | 88 | |
bcb54330 NR |
89 | count = " " + StringUtils.padString(count, SIZE_COL_1) + " "; |
90 | name = " " | |
91 | + StringUtils.padString(name, width - SIZE_COL_1 | |
92 | - getSeparator().length()) + " "; | |
93 | ||
94 | parts.add(new TextPart(count, el)); | |
95 | parts.add(new TextPart(getSeparator(), elSep)); | |
96 | parts.add(new TextPart(name, el)); | |
97 | ||
98 | return parts; | |
99 | }; | |
fae07ea7 NR |
100 | |
101 | @Override | |
102 | public List<KeyAction> getKeyBindings() { | |
9c8baf0c NR |
103 | List<KeyAction> actions = new LinkedList<KeyAction>(); |
104 | ||
105 | // TODO del, save... | |
106 | actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter, | |
e119a1c1 | 107 | StringId.KEY_ACTION_VIEW_CARD) { |
5ad0e17e NR |
108 | private Object obj = null; |
109 | ||
9c8baf0c NR |
110 | @Override |
111 | public Object getObject() { | |
5ad0e17e NR |
112 | if (obj == null) { |
113 | int index = getSelectedIndex(); | |
114 | if (index < 0 || index >= cards.size()) | |
115 | return null; | |
116 | ||
117 | try { | |
118 | if (cards.get(index) != null) { | |
119 | obj = cards.get(index).getCard(); | |
120 | } else { | |
121 | String file = files.get(index); | |
122 | ||
123 | CardResult card = null; | |
124 | final Card arr[] = new Card[4]; | |
125 | try { | |
126 | card = Main.getCard(file, new MergeCallback() { | |
127 | @Override | |
128 | public Card merge(Card previous, | |
129 | Card local, Card server, | |
130 | Card autoMerged) { | |
131 | arr[0] = previous; | |
132 | arr[1] = local; | |
133 | arr[2] = server; | |
134 | arr[3] = autoMerged; | |
135 | ||
136 | return null; | |
137 | } | |
138 | }); | |
139 | ||
140 | obj = card.getCard(); // throw IOE if problem | |
141 | } catch (IOException e) { | |
142 | if (arr[0] == null) | |
143 | throw e; | |
144 | ||
145 | // merge management: set all merge vars in | |
146 | // merger, | |
147 | // make sure it has cards but mergeTargetFile | |
148 | // does not exist | |
149 | // (create then delete if needed) | |
150 | // TODO: i18n | |
151 | setMessage( | |
152 | "Merge error, please check/fix the merged contact", | |
153 | true); | |
154 | ||
155 | // TODO: i18n + filename with numbers in it to | |
156 | // fix | |
157 | File a = File.createTempFile("Merge result ", | |
158 | ".vcf"); | |
159 | File p = File.createTempFile( | |
160 | "Previous common version ", ".vcf"); | |
161 | File l = File.createTempFile("Local ", ".vcf"); | |
162 | File s = File.createTempFile("Remote ", ".vcf"); | |
163 | arr[3].saveAs(a, Format.VCard21); | |
164 | arr[0].saveAs(p, Format.VCard21); | |
165 | arr[1].saveAs(l, Format.VCard21); | |
166 | arr[2].saveAs(s, Format.VCard21); | |
167 | List<String> mfiles = new LinkedList<String>(); | |
168 | mfiles.add(a.getAbsolutePath()); | |
169 | mfiles.add(p.getAbsolutePath()); | |
170 | mfiles.add(l.getAbsolutePath()); | |
171 | mfiles.add(s.getAbsolutePath()); | |
172 | merger = new FileList(mfiles); | |
173 | merger.mergeRemoteState = arr[2] | |
174 | .getContentState(false); | |
175 | merger.mergeSourceFile = files.get(index); | |
176 | merger.mergeTargetFile = a; | |
177 | ||
178 | obj = merger; | |
179 | return obj; | |
180 | } | |
181 | ||
182 | cards.set(index, card); | |
183 | ||
184 | invalidate(); | |
185 | ||
186 | if (card.isSynchronised()) { | |
187 | // TODO i18n | |
188 | if (card.isChanged()) | |
189 | setMessage( | |
190 | "card synchronised: changes from server", | |
191 | false); | |
192 | else | |
193 | setMessage("card synchronised: no changes", | |
194 | false); | |
195 | } | |
196 | } | |
197 | } catch (IOException ioe) { | |
198 | ioe.printStackTrace(); | |
199 | // TODO | |
200 | setMessage("ERROR!", true); | |
201 | } | |
202 | } | |
bcb54330 | 203 | |
5ad0e17e NR |
204 | return obj; |
205 | } | |
bcb54330 | 206 | |
5ad0e17e | 207 | }); |
7f82bf68 | 208 | |
5ad0e17e NR |
209 | return actions; |
210 | } | |
bcb54330 | 211 | |
5ad0e17e NR |
212 | @Override |
213 | public String wakeup() throws IOException { | |
3634193b NR |
214 | String s = super.wakeup(); |
215 | if (s != null) | |
216 | return s; | |
217 | ||
5ad0e17e NR |
218 | if (merger != null) { |
219 | if (!merger.mergeTargetFile.exists()) { | |
220 | throw new IOException("Merge cancelled"); | |
221 | } | |
bcb54330 | 222 | |
5ad0e17e NR |
223 | // merge back to server if needed and not changed: |
224 | try { | |
225 | Main.getCard(merger.mergeSourceFile, new MergeCallback() { | |
226 | @Override | |
227 | public Card merge(Card previous, Card local, Card server, | |
228 | Card autoMerged) { | |
229 | try { | |
230 | if (server.getContentState(false).equals( | |
231 | merger.mergeRemoteState)) { | |
232 | return new Card(merger.mergeTargetFile, | |
233 | Format.VCard21); | |
234 | } | |
235 | } catch (IOException e) { | |
236 | e.printStackTrace(); | |
237 | } | |
238 | ||
239 | return null; | |
240 | } | |
241 | }).getCard(); | |
242 | } catch (Exception e) { | |
243 | e.printStackTrace(); | |
244 | throw new IOException("Server changed since merge, cancel", e); | |
9c8baf0c | 245 | } |
9c8baf0c | 246 | |
5ad0e17e NR |
247 | merger = null; |
248 | ||
249 | // TODO i18n | |
250 | return "merged."; | |
251 | } | |
252 | ||
253 | return null; | |
fae07ea7 | 254 | } |
fae07ea7 | 255 | } |