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