1package eu.siacs.conversations.entities;
2
3import java.util.HashSet;
4import java.util.Hashtable;
5import java.util.Set;
6
7import org.json.JSONArray;
8import org.json.JSONException;
9import org.json.JSONObject;
10
11import eu.siacs.conversations.xml.Element;
12import android.content.ContentValues;
13import android.database.Cursor;
14
15public class Contact {
16 public static final String TABLENAME = "contacts";
17
18 public static final String SYSTEMNAME = "systemname";
19 public static final String SERVERNAME = "servername";
20 public static final String JID = "jid";
21 public static final String OPTIONS = "options";
22 public static final String SYSTEMACCOUNT = "systemaccount";
23 public static final String PHOTOURI = "photouri";
24 public static final String KEYS = "pgpkey";
25 public static final String ACCOUNT = "accountUuid";
26
27 protected String accountUuid;
28 protected String systemName;
29 protected String serverName;
30 protected String jid;
31 protected int subscription = 0;
32 protected String systemAccount;
33 protected String photoUri;
34 protected JSONObject keys = new JSONObject();
35 protected Presences presences = new Presences();
36
37 protected Account account;
38
39 protected boolean inRoster = true;
40
41 public Lastseen lastseen = new Lastseen();
42
43 public Contact(String account, String systemName, String serverName,
44 String jid, int subscription, String photoUri,
45 String systemAccount, String keys) {
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 public ContentValues getContentValues() {
91 ContentValues values = new ContentValues();
92 values.put(ACCOUNT, accountUuid);
93 values.put(SYSTEMNAME, systemName);
94 values.put(SERVERNAME, serverName);
95 values.put(JID, jid);
96 values.put(OPTIONS, subscription);
97 values.put(SYSTEMACCOUNT, systemAccount);
98 values.put(PHOTOURI, photoUri);
99 values.put(KEYS, keys.toString());
100 return values;
101 }
102
103 public static Contact fromCursor(Cursor cursor) {
104 return new Contact(cursor.getString(cursor.getColumnIndex(ACCOUNT)),
105 cursor.getString(cursor.getColumnIndex(SYSTEMNAME)),
106 cursor.getString(cursor.getColumnIndex(SERVERNAME)),
107 cursor.getString(cursor.getColumnIndex(JID)),
108 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
109 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
110 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
111 cursor.getString(cursor.getColumnIndex(KEYS)));
112 }
113
114 public int getSubscription() {
115 return this.subscription;
116 }
117
118 public void setSystemAccount(String account) {
119 this.systemAccount = account;
120 }
121
122 public void setAccount(Account account) {
123 this.account = account;
124 this.accountUuid = account.getUuid();
125 }
126
127 public Account getAccount() {
128 return this.account;
129 }
130
131 public boolean couldBeMuc() {
132 String[] split = this.getJid().split("@");
133 if (split.length != 2) {
134 return false;
135 } else {
136 String[] domainParts = split[1].split("\\.");
137 if (domainParts.length < 3) {
138 return false;
139 } else {
140 return (domainParts[0].equals("conf")
141 || domainParts[0].equals("conference")
142 || domainParts[0].equals("muc")
143 || domainParts[0].equals("sala") || domainParts[0]
144 .equals("salas"));
145 }
146 }
147 }
148
149 public Hashtable<String, Integer> getPresences() {
150 return this.presences.getPresences();
151 }
152
153 public void updatePresence(String resource, int status) {
154 this.presences.updatePresence(resource, status);
155 }
156
157 public void removePresence(String resource) {
158 this.presences.removePresence(resource);
159 }
160
161 public void clearPresences() {
162 this.presences.clearPresences();
163 }
164
165 public int getMostAvailableStatus() {
166 return this.presences.getMostAvailableStatus();
167 }
168
169 public void setPresences(Presences pres) {
170 this.presences = pres;
171 }
172
173 public void setPhotoUri(String uri) {
174 this.photoUri = uri;
175 }
176
177 public void setServerName(String serverName) {
178 this.serverName = serverName;
179 }
180
181 public void setSystemName(String systemName) {
182 this.systemName = systemName;
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 } else if (subscription.equals("both")) {
272 this.setOption(Contact.Options.TO);
273 this.setOption(Contact.Options.FROM);
274 } else if (subscription.equals("none")) {
275 this.resetOption(Contact.Options.FROM);
276 this.resetOption(Contact.Options.TO);
277 }
278 }
279
280 // do NOT override asking if pending push request
281 if (!this.getOption(Contact.Options.DIRTY_PUSH)) {
282 if ((ask != null) && (ask.equals("subscribe"))) {
283 this.setOption(Contact.Options.ASKING);
284 } else {
285 this.resetOption(Contact.Options.ASKING);
286 }
287 }
288 }
289
290 public Element asElement() {
291 Element item = new Element("item");
292 item.setAttribute("jid", this.jid);
293 if (this.serverName != null) {
294 item.setAttribute("name", this.serverName);
295 }
296 return item;
297 }
298
299 public class Options {
300 public static final int TO = 0;
301 public static final int FROM = 1;
302 public static final int ASKING = 2;
303 public static final int PREEMPTIVE_GRANT = 3;
304 public static final int IN_ROSTER = 4;
305 public static final int PENDING_SUBSCRIPTION_REQUEST = 5;
306 public static final int DIRTY_PUSH = 6;
307 public static final int DIRTY_DELETE = 7;
308 }
309
310 public class Lastseen {
311 public long time = 0;
312 public String presence = null;
313 }
314}