New option: save/load contacts' photos
[jvcard.git] / src / be / nikiroo / jvcard / tui / panes / ContactList.java
1 package be.nikiroo.jvcard.tui.panes;
2
3 import java.io.IOException;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 import be.nikiroo.jvcard.Card;
8 import be.nikiroo.jvcard.Contact;
9 import be.nikiroo.jvcard.Data;
10 import be.nikiroo.jvcard.launcher.Main;
11 import be.nikiroo.jvcard.resources.Bundles;
12 import be.nikiroo.jvcard.resources.Trans;
13 import be.nikiroo.jvcard.tui.KeyAction;
14 import be.nikiroo.jvcard.tui.KeyAction.DataType;
15 import be.nikiroo.jvcard.tui.KeyAction.Mode;
16 import be.nikiroo.jvcard.tui.UiColors.Element;
17
18 import com.googlecode.lanterna.input.KeyType;
19
20 public class ContactList extends MainContentList {
21 private Card card;
22 private List<Contact> contacts;
23 private String filter;
24
25 private List<String> formats;
26 private int selectedFormat;
27 private String format;
28
29 public ContactList(Card card) {
30 formats = new LinkedList<String>();
31 for (String format : Bundles.getBundle("display")
32 .getString("CONTACT_LIST_FORMAT").split(",")) {
33 formats.add(format);
34 }
35
36 selectedFormat = -1;
37 switchFormat();
38
39 setCard(card);
40 }
41
42 /**
43 * Change the currently displayed contacts card, only allowing those that
44 * satisfy the current filter.
45 *
46 * @param card
47 * the new {@link Card}
48 * @param filter
49 * the text filter or NULL for all contacts
50 */
51 public void setCard(Card card) {
52 clearItems();
53 this.card = card;
54 this.contacts = new LinkedList<Contact>();
55
56 if (card != null) {
57 for (Contact c : card) {
58 if (filter == null
59 || c.toString(format, "|").toLowerCase()
60 .contains(filter.toLowerCase())) {
61 addItem("x");
62 contacts.add(c);
63 }
64 }
65 }
66
67 setSelectedIndex(0);
68 }
69
70 @Override
71 public void refreshData() {
72 int index = getSelectedIndex();
73 setCard(card);
74 if (index >= contacts.size())
75 index = contacts.size() - 1;
76 setSelectedIndex(index);
77
78 super.refreshData();
79 }
80
81 @Override
82 public String getExitWarning() {
83 if (card != null && card.isDirty()) {
84 return "Ignore unsaved changes? [Y/N]";
85 }
86
87 return null;
88 }
89
90 @Override
91 public List<KeyAction> getKeyBindings() {
92 List<KeyAction> actions = new LinkedList<KeyAction>();
93
94 // TODO ui
95 actions.add(new KeyAction(Mode.ASK_USER, 'a',
96 Trans.StringId.KEY_ACTION_ADD) {
97 @Override
98 public Object getObject() {
99 return card;
100 }
101
102 @Override
103 public String getQuestion() {
104 return Main.trans(Trans.StringId.ASK_USER_CONTACT_NAME);
105 }
106
107 @Override
108 public String callback(String answer) {
109 if (answer.length() > 0) {
110 List<Data> datas = new LinkedList<Data>();
111 datas.add(new Data(null, "FN", answer, null));
112 getCard().add(new Contact(datas));
113 addItem("x");
114 }
115
116 return null;
117 }
118 });
119 actions.add(new KeyAction(Mode.ASK_USER_KEY, 'd',
120 Trans.StringId.KEY_ACTION_DELETE_CONTACT) {
121 @Override
122 public Object getObject() {
123 return getSelectedContact();
124 }
125
126 @Override
127 public String getQuestion() {
128 Contact contact = getSelectedContact();
129 String contactName = "null";
130 if (contact != null)
131 contactName = "" + contact.getPreferredDataValue("FN");
132
133 return Main.trans(Trans.StringId.CONFIRM_USER_DELETE_CONTACT,
134 contactName);
135 }
136
137 @Override
138 public String callback(String answer) {
139 if (answer.equalsIgnoreCase("y")) {
140 Contact contact = getSelectedContact();
141 if (contact != null && contact.delete()) {
142 removeItem("x");
143 return null;
144 }
145
146 String contactName = "null";
147 if (contact != null)
148 contactName = "" + contact.getPreferredDataValue("FN");
149
150 return Main.trans(Trans.StringId.ERR_CANNOT_DELETE_CONTACT,
151 contactName);
152 }
153
154 return null;
155 }
156 });
157 actions.add(new KeyAction(Mode.ASK_USER_KEY, 's',
158 Trans.StringId.KEY_ACTION_SAVE_CARD) {
159 @Override
160 public Object getObject() {
161 return card;
162 }
163
164 @Override
165 public String getQuestion() {
166 return "Save changes? [Y/N]";
167 }
168
169 @Override
170 public String callback(String answer) {
171 if (answer.equalsIgnoreCase("y")) {
172 boolean ok = false;
173 try {
174 if (card != null && card.save())
175 ok = true;
176 } catch (IOException ioe) {
177 ioe.printStackTrace();
178 }
179
180 if (!ok) {
181 return "Cannot save to file";
182 }
183 }
184
185 return null;
186 }
187
188 });
189 actions.add(new KeyAction(Mode.CONTACT_DETAILS, KeyType.Enter,
190 Trans.StringId.KEY_ACTION_VIEW_CONTACT) {
191 @Override
192 public Object getObject() {
193 return getSelectedContact();
194 }
195 });
196 actions.add(new KeyAction(Mode.NONE, KeyType.Tab,
197 Trans.StringId.KEY_ACTION_SWITCH_FORMAT) {
198 @Override
199 public boolean onAction() {
200 switchFormat();
201 return false;
202 }
203 });
204 actions.add(new KeyAction(Mode.ASK_USER, 'w',
205 Trans.StringId.KEY_ACTION_SEARCH) {
206
207 @Override
208 public String getQuestion() {
209 return "Search:";
210 }
211
212 @Override
213 public String getDefaultAnswer() {
214 return filter;
215 }
216
217 @Override
218 public String callback(String answer) {
219 filter = answer;
220 setCard(card);
221 return null;
222 }
223 });
224
225 return actions;
226 }
227
228 @Override
229 public DataType getDataType() {
230 return DataType.CARD;
231 }
232
233 @Override
234 public String getTitle() {
235 if (card != null) {
236 if (filter != null)
237 return card.getName() + " [" + filter + "]";
238 return card.getName();
239 }
240
241 return null;
242 }
243
244 @Override
245 protected List<TextPart> getLabel(int index, int width, boolean selected,
246 boolean focused) {
247 List<TextPart> parts = new LinkedList<TextPart>();
248
249 Contact contact = null;
250 if (index > -1 && index < contacts.size())
251 contact = contacts.get(index);
252
253 if (contact == null)
254 return parts;
255
256 Element el = (focused && selected) ? Element.CONTACT_LINE_SELECTED
257 : Element.CONTACT_LINE;
258 Element elSep = (focused && selected) ? Element.CONTACT_LINE_SEPARATOR_SELECTED
259 : Element.CONTACT_LINE_SEPARATOR;
260 Element elDirty = (focused && selected) ? Element.CONTACT_LINE_DIRTY_SELECTED
261 : Element.CONTACT_LINE_DIRTY;
262
263 width -= 2; // dirty mark space
264
265 String[] array = contact.toStringArray(format, getSeparator(), " ",
266 width, Main.isUnicode());
267
268 if (contact.isDirty()) {
269 parts.add(new TextPart(" ", el));
270 parts.add(new TextPart("*", elDirty));
271 } else {
272 parts.add(new TextPart(" ", elSep));
273 }
274
275 boolean separator = false;
276 for (String str : array) {
277 parts.add(new TextPart(str, (separator ? elSep : el)));
278 separator = !separator;
279 }
280
281 return parts;
282 }
283
284 /**
285 * Return the currently selected {@link Contact}.
286 *
287 * @return the currently selected {@link Contact}
288 */
289 private Contact getSelectedContact() {
290 int index = getSelectedIndex();
291 if (index > -1 && index < contacts.size())
292 return contacts.get(index);
293 return null;
294 }
295
296 private void switchFormat() {
297 if (formats.size() == 0)
298 return;
299
300 selectedFormat++;
301 if (selectedFormat >= formats.size()) {
302 selectedFormat = 0;
303 }
304
305 format = formats.get(selectedFormat);
306
307 invalidate();
308 }
309 }