1package eu.siacs.conversations.entities;
2
3import java.io.Serializable;
4import java.util.HashSet;
5import java.util.Hashtable;
6import java.util.Set;
7import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
10import eu.siacs.conversations.xml.Element;
11import android.content.ContentValues;
12import android.database.Cursor;
13
14public class Contact extends AbstractEntity implements Serializable {
15 private static final long serialVersionUID = -4570817093119419962L;
16
17 public static final String TABLENAME = "contacts";
18
19 public static final String SYSTEMNAME = "systemname";
20 public static final String SERVERNAME = "servername";
21 public static final String JID = "jid";
22 public static final String OPTIONS = "options";
23 public static final String SYSTEMACCOUNT = "systemaccount";
24 public static final String PHOTOURI = "photouri";
25 public static final String KEYS = "pgpkey";
26 public static final String ACCOUNT = "accountUuid";
27
28 protected String accountUuid;
29 protected String systemName;
30 protected String serverName;
31 protected String jid;
32 protected int subscription = 0;
33 protected String systemAccount;
34 protected String photoUri;
35 protected JSONObject keys = new JSONObject();
36 protected Presences presences = new Presences();
37
38 protected Account account;
39
40 protected boolean inRoster = true;
41
42 public Contact(String uuid, String account, String systemName,
43 String serverName, String jid, int subscription, String photoUri,
44 String systemAccount, String keys) {
45 this.uuid = uuid;
46 this.accountUuid = account;
47 this.systemName = systemName;
48 this.serverName = serverName;
49 this.jid = jid;
50 this.subscription = subscription;
51 this.photoUri = photoUri;
52 this.systemAccount = systemAccount;
53 if (keys == null) {
54 keys = "";
55 }
56 try {
57 this.keys = new JSONObject(keys);
58 } catch (JSONException e) {
59 this.keys = new JSONObject();
60 }
61 }
62
63 public Contact(String jid) {
64 this.jid = jid;
65 }
66
67 public String getDisplayName() {
68 if (this.systemName != null) {
69 return this.systemName;
70 } else if (this.serverName != null) {
71 return this.serverName;
72 } else {
73 return this.jid.split("@")[0];
74 }
75 }
76
77 public String getProfilePhoto() {
78 return this.photoUri;
79 }
80
81 public String getJid() {
82 return this.jid;
83 }
84
85 public boolean match(String needle) {
86 return (jid.toLowerCase().contains(needle.toLowerCase()) || (getDisplayName()
87 .toLowerCase().contains(needle.toLowerCase())));
88 }
89
90 @Override
91 public ContentValues getContentValues() {
92 ContentValues values = new ContentValues();
93 values.put(UUID, uuid);
94 values.put(ACCOUNT, accountUuid);
95 values.put(SYSTEMNAME, systemName);
96 values.put(SERVERNAME, serverName);
97 values.put(JID, jid);
98 values.put(OPTIONS, subscription);
99 values.put(SYSTEMACCOUNT, systemAccount);
100 values.put(PHOTOURI, photoUri);
101 values.put(KEYS, keys.toString());
102 return values;
103 }
104
105 public static Contact fromCursor(Cursor cursor) {
106 return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
107 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
108 cursor.getString(cursor.getColumnIndex(SYSTEMNAME)),
109 cursor.getString(cursor.getColumnIndex(SERVERNAME)),
110 cursor.getString(cursor.getColumnIndex(JID)),
111 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
112 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
113 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
114 cursor.getString(cursor.getColumnIndex(KEYS)));
115 }
116
117 public int getSubscription() {
118 return this.subscription;
119 }
120
121 public void setSystemAccount(String account) {
122 this.systemAccount = account;
123 }
124
125 public void setAccount(Account account) {
126 this.account = account;
127 this.accountUuid = account.getUuid();
128 }
129
130 public Account getAccount() {
131 return this.account;
132 }
133
134 public void setUuid(String uuid) {
135 this.uuid = uuid;
136 }
137
138 public boolean couldBeMuc() {
139 String[] split = this.getJid().split("@");
140 if (split.length != 2) {
141 return false;
142 } else {
143 String[] domainParts = split[1].split("\\.");
144 if (domainParts.length < 3) {
145 return false;
146 } else {
147 return (domainParts[0].equals("conf")
148 || domainParts[0].equals("conference")
149 || domainParts[0].equals("muc")
150 || domainParts[0].equals("sala") || domainParts[0]
151 .equals("salas"));
152 }
153 }
154 }
155
156 public Hashtable<String, Integer> getPresences() {
157 return this.presences.getPresences();
158 }
159
160 public void updatePresence(String resource, int status) {
161 this.presences.updatePresence(resource, status);
162 }
163
164 public void removePresence(String resource) {
165 this.presences.removePresence(resource);
166 }
167
168 public void clearPresences() {
169 this.presences.clearPresences();
170 }
171
172 public int getMostAvailableStatus() {
173 return this.presences.getMostAvailableStatus();
174 }
175
176 public void setPresences(Presences pres) {
177 this.presences = pres;
178 }
179
180 public void setPhotoUri(String uri) {
181 this.photoUri = uri;
182 }
183
184 public void setServerName(String serverName) {
185 this.serverName = serverName;
186 }
187
188 public void setSystemName(String systemName) {
189 this.systemName = systemName;
190 }
191
192 public String getSystemAccount() {
193 return systemAccount;
194 }
195
196 public Set<String> getOtrFingerprints() {
197 Set<String> set = new HashSet<String>();
198 try {
199 if (this.keys.has("otr_fingerprints")) {
200 JSONArray fingerprints = this.keys
201 .getJSONArray("otr_fingerprints");
202 for (int i = 0; i < fingerprints.length(); ++i) {
203 set.add(fingerprints.getString(i));
204 }
205 }
206 } catch (JSONException e) {
207 // TODO Auto-generated catch block
208 e.printStackTrace();
209 }
210 return set;
211 }
212
213 public void addOtrFingerprint(String print) {
214 try {
215 JSONArray fingerprints;
216 if (!this.keys.has("otr_fingerprints")) {
217 fingerprints = new JSONArray();
218
219 } else {
220 fingerprints = this.keys.getJSONArray("otr_fingerprints");
221 }
222 fingerprints.put(print);
223 this.keys.put("otr_fingerprints", fingerprints);
224 } catch (JSONException e) {
225
226 }
227 }
228
229 public void setPgpKeyId(long keyId) {
230 try {
231 this.keys.put("pgp_keyid", keyId);
232 } catch (JSONException e) {
233
234 }
235 }
236
237 public long getPgpKeyId() {
238 if (this.keys.has("pgp_keyid")) {
239 try {
240 return this.keys.getLong("pgp_keyid");
241 } catch (JSONException e) {
242 return 0;
243 }
244 } else {
245 return 0;
246 }
247 }
248
249 public void setOption(int option) {
250 this.subscription |= 1 << option;
251 }
252
253 public void resetOption(int option) {
254 this.subscription &= ~(1 << option);
255 }
256
257 public boolean getOption(int option) {
258 return ((this.subscription & (1 << option)) != 0);
259 }
260
261 public void parseSubscriptionFromElement(Element item) {
262 String ask = item.getAttribute("ask");
263 String subscription = item.getAttribute("subscription");
264
265 if (subscription != null) {
266 if (subscription.equals("to")) {
267 this.resetOption(Contact.Options.FROM);
268 this.setOption(Contact.Options.TO);
269 } else if (subscription.equals("from")) {
270 this.resetOption(Contact.Options.TO);
271 this.setOption(Contact.Options.FROM);
272 } else if (subscription.equals("both")) {
273 this.setOption(Contact.Options.TO);
274 this.setOption(Contact.Options.FROM);
275 }
276 }
277
278 if ((ask != null) && (ask.equals("subscribe"))) {
279 this.setOption(Contact.Options.ASKING);
280 } else {
281 this.resetOption(Contact.Options.ASKING);
282 }
283 }
284
285 public Element asElement() {
286 Element item = new Element("item");
287 item.setAttribute("jid", this.jid);
288 if (this.serverName != null) {
289 item.setAttribute("name", this.serverName);
290 }
291 return item;
292 }
293
294 public class Options {
295 public static final int TO = 0;
296 public static final int FROM = 1;
297 public static final int ASKING = 2;
298 public static final int PREEMPTIVE_GRANT = 4;
299 public static final int IN_ROSTER = 8;
300 }
301}