jvcard remote support (initial commit, not ready for use yet)
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / FileList.java
CommitLineData
fae07ea7
NR
1package be.nikiroo.jvcard.tui.panes;
2
3import java.io.File;
9c8baf0c 4import java.io.IOException;
bcb54330 5import java.util.ArrayList;
9c8baf0c 6import java.util.LinkedList;
fae07ea7
NR
7import java.util.List;
8
9c8baf0c
NR
9import be.nikiroo.jvcard.Card;
10import be.nikiroo.jvcard.i18n.Trans;
11import be.nikiroo.jvcard.parsers.Format;
a046fa49 12import be.nikiroo.jvcard.remote.Sync;
fae07ea7 13import be.nikiroo.jvcard.tui.KeyAction;
fae07ea7
NR
14import be.nikiroo.jvcard.tui.KeyAction.DataType;
15import be.nikiroo.jvcard.tui.KeyAction.Mode;
bcb54330
NR
16import be.nikiroo.jvcard.tui.StringUtils;
17import be.nikiroo.jvcard.tui.UiColors;
18import be.nikiroo.jvcard.tui.UiColors.Element;
fae07ea7 19
9c8baf0c 20import com.googlecode.lanterna.input.KeyType;
fae07ea7
NR
21
22public class FileList extends MainContentList {
7f82bf68 23 private List<String> files;
bcb54330 24 private List<Card> cards;
fae07ea7 25
7f82bf68 26 public FileList(List<String> files) {
fae07ea7
NR
27 setFiles(files);
28 }
29
30 /**
31 * Change the list of currently selected files.
32 *
33 * @param files
34 * the new files
35 */
7f82bf68 36 public void setFiles(List<String> files) {
fae07ea7
NR
37 clearItems();
38 this.files = files;
bcb54330 39 cards = new ArrayList<Card>();
fae07ea7 40
7f82bf68
NR
41 for (String file : files) {
42 addItem(file); // TODO
bcb54330 43 cards.add(null);
fae07ea7
NR
44 }
45
46 setSelectedIndex(0);
47 }
48
49 @Override
50 public DataType getDataType() {
51 return DataType.CARD_FILES;
52 }
53
54 @Override
bcb54330
NR
55 protected List<TextPart> getLabel(int index, int width, boolean selected,
56 boolean focused) {
57 // TODO: from ini file?
58 int SIZE_COL_1 = 3;
59
60 Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
61 : Element.CONTACT_LINE;
62 Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
63 : Element.CONTACT_LINE_SEPARATOR;
64
65 List<TextPart> parts = new LinkedList<TextPart>();
66
67 String count = "";
68 if (cards.get(index) != null)
69 count += cards.get(index).size();
70
7f82bf68
NR
71 String name = files.get(index).replaceAll("\\\\", "/");
72 int indexSl = name.lastIndexOf('/');
73 if (indexSl >= 0) {
74 name = name.substring(indexSl + 1);
75 }
bcb54330 76
296a0b75
NR
77 name = StringUtils.sanitize(name, UiColors.getInstance().isUnicode());
78
bcb54330
NR
79 count = " " + StringUtils.padString(count, SIZE_COL_1) + " ";
80 name = " "
81 + StringUtils.padString(name, width - SIZE_COL_1
82 - getSeparator().length()) + " ";
83
84 parts.add(new TextPart(count, el));
85 parts.add(new TextPart(getSeparator(), elSep));
86 parts.add(new TextPart(name, el));
87
88 return parts;
89 };
fae07ea7
NR
90
91 @Override
92 public List<KeyAction> getKeyBindings() {
9c8baf0c
NR
93 List<KeyAction> actions = new LinkedList<KeyAction>();
94
95 // TODO del, save...
96 actions.add(new KeyAction(Mode.CONTACT_LIST, KeyType.Enter,
97 Trans.StringId.KEY_ACTION_VIEW_CARD) {
98 @Override
99 public Object getObject() {
bcb54330
NR
100 int index = getSelectedIndex();
101
102 if (index < 0 || index >= cards.size())
103 return null;
104
105 if (cards.get(index) != null)
106 return cards.get(index);
107
7f82bf68
NR
108 String file = files.get(index);
109
9c8baf0c 110 try {
7f82bf68 111 Card card = FileList.getCard(file);
bcb54330
NR
112 cards.set(index, card);
113
114 invalidate();
115
116 return card;
9c8baf0c
NR
117 } catch (IOException ioe) {
118 ioe.printStackTrace();
119 return null;
120 }
121 }
122 });
123
124 return actions;
fae07ea7 125 }
7f82bf68
NR
126
127 static private Card getCard(String input) throws IOException {
a046fa49 128 boolean remote = false;
7f82bf68
NR
129 Format format = Format.Abook;
130 String ext = input;
131 if (ext.contains(".")) {
132 String tab[] = ext.split("\\.");
133 if (tab.length > 1 && tab[tab.length - 1].equalsIgnoreCase("vcf")) {
134 format = Format.VCard21;
135 }
136 }
137
a046fa49
NR
138 if (input.contains("://")) {
139 format = Format.VCard21;
140 remote = true;
141 }
142
7f82bf68
NR
143 Card card = null;
144 try {
a046fa49
NR
145 if (remote) {
146 Sync sync = new Sync(input);
147 card = new Card(sync.getCache(), format);
148 card.setRemote(true);
149 sync.sync(card, false);
150 } else {
151 card = new Card(new File(input), format);
152 }
7f82bf68
NR
153 } catch (IOException ioe) {
154 ioe.printStackTrace();
155 throw ioe;
156 }
157
158 return card;
159 }
fae07ea7 160}