1 package be
.nikiroo
.jvcard
.tui
.panes
;
4 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
6 import java
.util
.LinkedList
;
9 import be
.nikiroo
.jvcard
.Card
;
10 import be
.nikiroo
.jvcard
.launcher
.CardResult
;
11 import be
.nikiroo
.jvcard
.launcher
.CardResult
.MergeCallback
;
12 import be
.nikiroo
.jvcard
.launcher
.Main
;
13 import be
.nikiroo
.jvcard
.parsers
.Format
;
14 import be
.nikiroo
.jvcard
.resources
.ColorOption
;
15 import be
.nikiroo
.jvcard
.resources
.StringId
;
16 import be
.nikiroo
.jvcard
.tui
.KeyAction
;
17 import be
.nikiroo
.jvcard
.tui
.KeyAction
.DataType
;
18 import be
.nikiroo
.jvcard
.tui
.KeyAction
.Mode
;
19 import be
.nikiroo
.utils
.StringUtils
;
21 import com
.googlecode
.lanterna
.input
.KeyType
;
23 public class FileList
extends MainContentList
{
24 private List
<String
> files
;
25 private List
<CardResult
> cards
;
27 private FileList merger
;
28 private String mergeRemoteState
;
29 private String mergeSourceFile
;
30 private File mergeTargetFile
;
32 public FileList(List
<String
> files
) {
37 * Change the list of currently selected files.
42 public void setFiles(List
<String
> files
) {
45 cards
= new ArrayList
<CardResult
>();
47 for (String file
: files
) {
48 addItem(file
); // TODO
56 public DataType
getDataType() {
57 return DataType
.CARD_FILES
;
61 protected List
<TextPart
> getLabel(int index
, int width
, boolean selected
,
63 // TODO: from ini file?
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
;
71 List
<TextPart
> parts
= new LinkedList
<TextPart
>();
74 if (cards
.get(index
) != null) {
76 count
+= cards
.get(index
).getCard().size();
77 } catch (IOException e
) {
81 String name
= files
.get(index
).replaceAll("\\\\", "/");
82 int indexSl
= name
.lastIndexOf('/');
84 name
= name
.substring(indexSl
+ 1);
87 name
= StringUtils
.sanitize(name
, Main
.isUnicode());
89 count
= " " + StringUtils
.padString(count
, SIZE_COL_1
) + " ";
91 + StringUtils
.padString(name
, width
- SIZE_COL_1
92 - getSeparator().length()) + " ";
94 parts
.add(new TextPart(count
, el
));
95 parts
.add(new TextPart(getSeparator(), elSep
));
96 parts
.add(new TextPart(name
, el
));
102 public List
<KeyAction
> getKeyBindings() {
103 List
<KeyAction
> actions
= new LinkedList
<KeyAction
>();
106 actions
.add(new KeyAction(Mode
.CONTACT_LIST
, KeyType
.Enter
,
107 StringId
.KEY_ACTION_VIEW_CARD
) {
108 private Object obj
= null;
111 public Object
getObject() {
113 int index
= getSelectedIndex();
114 if (index
< 0 || index
>= cards
.size())
118 if (cards
.get(index
) != null) {
119 obj
= cards
.get(index
).getCard();
121 String file
= files
.get(index
);
123 CardResult card
= null;
124 final Card arr
[] = new Card
[4];
126 card
= Main
.getCard(file
, new MergeCallback() {
128 public Card
merge(Card previous
,
129 Card local
, Card server
,
140 obj
= card
.getCard(); // throw IOE if problem
141 } catch (IOException e
) {
145 // merge management: set all merge vars in
147 // make sure it has cards but mergeTargetFile
149 // (create then delete if needed)
152 "Merge error, please check/fix the merged contact",
155 // TODO: i18n + filename with numbers in it to
157 File a
= File
.createTempFile("Merge result ",
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
;
182 cards
.set(index
, card
);
186 if (card
.isSynchronised()) {
188 if (card
.isChanged())
190 "card synchronised: changes from server",
193 setMessage("card synchronised: no changes",
197 } catch (IOException ioe
) {
198 ioe
.printStackTrace();
200 setMessage("ERROR!", true);
213 public String
wakeup() throws IOException
{
214 String s
= super.wakeup();
218 if (merger
!= null) {
219 if (!merger
.mergeTargetFile
.exists()) {
220 throw new IOException("Merge cancelled");
223 // merge back to server if needed and not changed:
225 Main
.getCard(merger
.mergeSourceFile
, new MergeCallback() {
227 public Card
merge(Card previous
, Card local
, Card server
,
230 if (server
.getContentState(false).equals(
231 merger
.mergeRemoteState
)) {
232 return new Card(merger
.mergeTargetFile
,
235 } catch (IOException e
) {
242 } catch (Exception e
) {
244 throw new IOException("Server changed since merge, cancel", e
);