1package eu.siacs.conversations.entities;
2
3import java.util.HashSet;
4import java.util.Locale;
5import java.util.Set;
6
7import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
10
11import eu.siacs.conversations.persistance.FileBackend;
12import eu.siacs.conversations.utils.UIHelper;
13import eu.siacs.conversations.xml.Element;
14import android.content.ContentValues;
15import android.content.Context;
16import android.database.Cursor;
17import android.graphics.Bitmap;
18
19public class Contact implements ListItem {
20 public static final String TABLENAME = "contacts";
21
22 public static final String SYSTEMNAME = "systemname";
23 public static final String SERVERNAME = "servername";
24 public static final String JID = "jid";
25 public static final String OPTIONS = "options";
26 public static final String SYSTEMACCOUNT = "systemaccount";
27 public static final String PHOTOURI = "photouri";
28 public static final String KEYS = "pgpkey";
29 public static final String ACCOUNT = "accountUuid";
30 public static final String AVATAR = "avatar";
31
32 protected String accountUuid;
33 protected String systemName;
34 protected String serverName;
35 protected String presenceName;
36 protected String jid;
37 protected int subscription = 0;
38 protected String systemAccount;
39 protected String photoUri;
40 protected String avatar;
41 protected JSONObject keys = new JSONObject();
42 protected Presences presences = new Presences();
43
44 protected Account account;
45
46 protected boolean inRoster = true;
47
48 public Lastseen lastseen = new Lastseen();
49
50 public Contact(String account, String systemName, String serverName,
51 String jid, int subscription, String photoUri,
52 String systemAccount, String keys, String avatar) {
53 this.accountUuid = account;
54 this.systemName = systemName;
55 this.serverName = serverName;
56 this.jid = jid;
57 this.subscription = subscription;
58 this.photoUri = photoUri;
59 this.systemAccount = systemAccount;
60 if (keys == null) {
61 keys = "";
62 }
63 try {
64 this.keys = new JSONObject(keys);
65 } catch (JSONException e) {
66 this.keys = new JSONObject();
67 }
68 this.avatar = avatar;
69 }
70
71 public Contact(String jid) {
72 this.jid = jid;
73 }
74
75 public String getDisplayName() {
76 if (this.systemName != null) {
77 return this.systemName;
78 } else if (this.serverName != null) {
79 return this.serverName;
80 } else if (this.presenceName != null) {
81 return this.presenceName;
82 } else {
83 return this.jid.split("@")[0];
84 }
85 }
86
87 public String getProfilePhoto() {
88 return this.photoUri;
89 }
90
91 public String getJid() {
92 return this.jid.toLowerCase(Locale.getDefault());
93 }
94
95 public boolean match(String needle) {
96 return needle == null
97 || jid.contains(needle.toLowerCase())
98 || getDisplayName().toLowerCase()
99 .contains(needle.toLowerCase());
100 }
101
102 public ContentValues getContentValues() {
103 ContentValues values = new ContentValues();
104 values.put(ACCOUNT, accountUuid);
105 values.put(SYSTEMNAME, systemName);
106 values.put(SERVERNAME, serverName);
107 values.put(JID, jid);
108 values.put(OPTIONS, subscription);
109 values.put(SYSTEMACCOUNT, systemAccount);
110 values.put(PHOTOURI, photoUri);
111 values.put(KEYS, keys.toString());
112 values.put(AVATAR, avatar);
113 return values;
114 }
115
116 public static Contact fromCursor(Cursor cursor) {
117 return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)),
118 cursor.getString(cursor.getColumnIndex(SYSTEMNAME)),
119 cursor.getString(cursor.getColumnIndex(SERVERNAME)),
120 cursor.getString(cursor.getColumnIndex(JID)),
121 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
122 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
123 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
124 cursor.getString(cursor.getColumnIndex(KEYS)),
125 cursor.getString(cursor.getColumnIndex(AVATAR)));
126 }
127
128 public int getSubscription() {
129 return this.subscription;
130 }
131
132 public void setSystemAccount(String account) {
133 this.systemAccount = account;
134 }
135
136 public void setAccount(Account account) {
137 this.account = account;
138 this.accountUuid = account.getUuid();
139 }
140
141 public Account getAccount() {
142 return this.account;
143 }
144
145 public Presences getPresences() {
146 return this.presences;
147 }
148
149 public void updatePresence(String resource, int status) {
150 this.presences.updatePresence(resource, status);
151 }
152
153 public void removePresence(String resource) {
154 this.presences.removePresence(resource);
155 }
156
157 public void clearPresences() {
158 this.presences.clearPresences();
159 }
160
161 public int getMostAvailableStatus() {
162 return this.presences.getMostAvailableStatus();
163 }
164
165 public void setPresences(Presences pres) {
166 this.presences = pres;
167 }
168
169 public void setPhotoUri(String uri) {
170 this.photoUri = uri;
171 }
172
173 public void setServerName(String serverName) {
174 this.serverName = serverName;
175 }
176
177 public void setSystemName(String systemName) {
178 this.systemName = systemName;
179 }
180
181 public void setPresenceName(String presenceName) {
182 this.presenceName = presenceName;
183 }
184
185 public String getSystemAccount() {
186 return systemAccount;
187 }
188
189 public Set<String> getOtrFingerprints() {
190 Set<String> set = new HashSet<String>();
191 try {
192 if (this.keys.has("otr_fingerprints")) {
193 JSONArray fingerprints = this.keys
194 .getJSONArray("otr_fingerprints");
195 for (int i = 0; i < fingerprints.length(); ++i) {
196 set.add(fingerprints.getString(i));
197 }
198 }
199 } catch (JSONException e) {
200 // TODO Auto-generated catch block
201 e.printStackTrace();
202 }
203 return set;
204 }
205
206 public void addOtrFingerprint(String print) {
207 try {
208 JSONArray fingerprints;
209 if (!this.keys.has("otr_fingerprints")) {
210 fingerprints = new JSONArray();
211
212 } else {
213 fingerprints = this.keys.getJSONArray("otr_fingerprints");
214 }
215 fingerprints.put(print);
216 this.keys.put("otr_fingerprints", fingerprints);
217 } catch (JSONException e) {
218
219 }
220 }
221
222 public void setPgpKeyId(long keyId) {
223 try {
224 this.keys.put("pgp_keyid", keyId);
225 } catch (JSONException e) {
226
227 }
228 }
229
230 public long getPgpKeyId() {
231 if (this.keys.has("pgp_keyid")) {
232 try {
233 return this.keys.getLong("pgp_keyid");
234 } catch (JSONException e) {
235 return 0;
236 }
237 } else {
238 return 0;
239 }
240 }
241
242 public void setOption(int option) {
243 this.subscription |= 1 << option;
244 }
245
246 public void resetOption(int option) {
247 this.subscription &= ~(1 << option);
248 }
249
250 public boolean getOption(int option) {
251 return ((this.subscription & (1 << option)) != 0);
252 }
253
254 public boolean showInRoster() {
255 return (this.getOption(Contact.Options.IN_ROSTER) && (!this
256 .getOption(Contact.Options.DIRTY_DELETE)))
257 || (this.getOption(Contact.Options.DIRTY_PUSH));
258 }
259
260 public void parseSubscriptionFromElement(Element item) {
261 String ask = item.getAttribute("ask");
262 String subscription = item.getAttribute("subscription");
263
264 if (subscription != null) {
265 if (subscription.equals("to")) {
266 this.resetOption(Contact.Options.FROM);
267 this.setOption(Contact.Options.TO);
268 } else if (subscription.equals("from")) {
269 this.resetOption(Contact.Options.TO);
270 this.setOption(Contact.Options.FROM);
271 this.resetOption(Contact.Options.PREEMPTIVE_GRANT);
272 } else if (subscription.equals("both")) {
273 this.setOption(Contact.Options.TO);
274 this.setOption(Contact.Options.FROM);
275 this.resetOption(Contact.Options.PREEMPTIVE_GRANT);
276 } else if (subscription.equals("none")) {
277 this.resetOption(Contact.Options.FROM);
278 this.resetOption(Contact.Options.TO);
279 }
280 }
281
282 // do NOT override asking if pending push request
283 if (!this.getOption(Contact.Options.DIRTY_PUSH)) {
284 if ((ask != null) && (ask.equals("subscribe"))) {
285 this.setOption(Contact.Options.ASKING);
286 } else {
287 this.resetOption(Contact.Options.ASKING);
288 }
289 }
290 }
291
292 public Element asElement() {
293 Element item = new Element("item");
294 item.setAttribute("jid", this.jid);
295 if (this.serverName != null) {
296 item.setAttribute("name", this.serverName);
297 }
298 return item;
299 }
300
301 public class Options {
302 public static final int TO = 0;
303 public static final int FROM = 1;
304 public static final int ASKING = 2;
305 public static final int PREEMPTIVE_GRANT = 3;
306 public static final int IN_ROSTER = 4;
307 public static final int PENDING_SUBSCRIPTION_REQUEST = 5;
308 public static final int DIRTY_PUSH = 6;
309 public static final int DIRTY_DELETE = 7;
310 }
311
312 public class Lastseen {
313 public long time = 0;
314 public String presence = null;
315 }
316
317 @Override
318 public int compareTo(ListItem another) {
319 return this.getDisplayName().compareToIgnoreCase(
320 another.getDisplayName());
321 }
322
323 public String getServer() {
324 String[] split = getJid().split("@");
325 if (split.length >= 2) {
326 return split[1];
327 } else {
328 return null;
329 }
330 }
331
332 @Override
333 public Bitmap getImage(int size, Context context) {
334 if (this.avatar != null) {
335 Bitmap bm = FileBackend.getAvatar(avatar, size, context);
336 if (bm == null) {
337 return UIHelper.getContactPicture(this, size, context, false);
338 } else {
339 return bm;
340 }
341 } else {
342 return UIHelper.getContactPicture(this, size, context, false);
343 }
344 }
345
346 public boolean setAvatar(String filename) {
347 if (this.avatar != null && this.avatar.equals(filename)) {
348 return false;
349 } else {
350 this.avatar = filename;
351 return true;
352 }
353 }
354
355 public boolean deleteOtrFingerprint(String fingerprint) {
356 boolean success = false;
357 try {
358 if (this.keys.has("otr_fingerprints")) {
359 JSONArray newPrints = new JSONArray();
360 JSONArray oldPrints = this.keys
361 .getJSONArray("otr_fingerprints");
362 for (int i = 0; i < oldPrints.length(); ++i) {
363 if (!oldPrints.getString(i).equals(fingerprint)) {
364 newPrints.put(oldPrints.getString(i));
365 } else {
366 success = true;
367 }
368 }
369 this.keys.put("otr_fingerprints", newPrints);
370 }
371 return success;
372 } catch (JSONException e) {
373 return false;
374 }
375 }
376}