1package de.gultsch.chat.entities;
2
3import java.io.Serializable;
4import java.util.HashSet;
5import java.util.Hashtable;
6import java.util.Set;
7
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11
12import de.gultsch.chat.xml.Element;
13
14import android.content.ContentValues;
15import android.database.Cursor;
16
17public class Contact extends AbstractEntity implements Serializable {
18 private static final long serialVersionUID = -4570817093119419962L;
19
20 public static final String TABLENAME = "contacts";
21
22 public static final String DISPLAYNAME = "name";
23 public static final String JID = "jid";
24 public static final String SUBSCRIPTION = "subscription";
25 public static final String SYSTEMACCOUNT = "systemaccount";
26 public static final String PHOTOURI = "photouri";
27 public static final String KEYS = "pgpkey";
28 public static final String PRESENCES = "presences";
29 public static final String ACCOUNT = "accountUuid";
30
31 protected String accountUuid;
32 protected String displayName;
33 protected String jid;
34 protected int subscription = 0;
35 protected String systemAccount;
36 protected String photoUri;
37 protected JSONObject keys = new JSONObject();
38 protected Presences presences = new Presences();
39
40 protected Account account;
41
42 public Contact(Account account, String displayName, String jid,
43 String photoUri) {
44 if (account == null) {
45 this.accountUuid = null;
46 } else {
47 this.accountUuid = account.getUuid();
48 }
49 this.account = account;
50 this.displayName = displayName;
51 this.jid = jid;
52 this.photoUri = photoUri;
53 this.uuid = java.util.UUID.randomUUID().toString();
54 }
55
56 public Contact(String uuid, String account, String displayName, String jid,
57 int subscription, String photoUri, String systemAccount,
58 String keys, String presences) {
59 this.uuid = uuid;
60 this.accountUuid = account;
61 this.displayName = displayName;
62 this.jid = jid;
63 this.subscription = subscription;
64 this.photoUri = photoUri;
65 this.systemAccount = systemAccount;
66 if (keys == null) {
67 keys = "";
68 }
69 try {
70 this.keys = new JSONObject(keys);
71 } catch (JSONException e) {
72 this.keys = new JSONObject();
73 }
74 this.presences = Presences.fromJsonString(presences);
75 }
76
77 public String getDisplayName() {
78 return this.displayName;
79 }
80
81 public String getProfilePhoto() {
82 return this.photoUri;
83 }
84
85 public String getJid() {
86 return this.jid;
87 }
88
89 public boolean match(String needle) {
90 return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
91 .toLowerCase().contains(needle.toLowerCase())));
92 }
93
94 @Override
95 public ContentValues getContentValues() {
96 ContentValues values = new ContentValues();
97 values.put(UUID, uuid);
98 values.put(ACCOUNT, accountUuid);
99 values.put(DISPLAYNAME, displayName);
100 values.put(JID, jid);
101 values.put(SUBSCRIPTION, subscription);
102 values.put(SYSTEMACCOUNT, systemAccount);
103 values.put(PHOTOURI, photoUri);
104 values.put(KEYS, keys.toString());
105 values.put(PRESENCES, presences.toJsonString());
106 return values;
107 }
108
109 public static Contact fromCursor(Cursor cursor) {
110 return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
111 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
112 cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
113 cursor.getString(cursor.getColumnIndex(JID)),
114 cursor.getInt(cursor.getColumnIndex(SUBSCRIPTION)),
115 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
116 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
117 cursor.getString(cursor.getColumnIndex(KEYS)),
118 cursor.getString(cursor.getColumnIndex(PRESENCES)));
119 }
120
121 public int getSubscription() {
122 return this.subscription;
123 }
124
125 public void setSystemAccount(String account) {
126 this.systemAccount = account;
127 }
128
129 public void setAccount(Account account) {
130 this.account = account;
131 this.accountUuid = account.getUuid();
132 }
133
134 public Account getAccount() {
135 return this.account;
136 }
137
138 public void setUuid(String uuid) {
139 this.uuid = uuid;
140 }
141
142 public boolean couldBeMuc() {
143 String[] split = this.getJid().split("@");
144 if (split.length != 2) {
145 return false;
146 } else {
147 String[] domainParts = split[1].split("\\.");
148 if (domainParts.length < 3) {
149 return false;
150 } else {
151 return (domainParts[0].equals("conf")
152 || domainParts[0].equals("conference") || domainParts[0]
153 .equals("muc"));
154 }
155 }
156 }
157
158 public Hashtable<String, Integer> getPresences() {
159 return this.presences.getPresences();
160 }
161
162 public void updatePresence(String resource, int status) {
163 this.presences.updatePresence(resource, status);
164 }
165
166 public void removePresence(String resource) {
167 this.presences.removePresence(resource);
168 }
169
170 public int getMostAvailableStatus() {
171 return this.presences.getMostAvailableStatus();
172 }
173
174 public void setPresences(Presences pres) {
175 this.presences = pres;
176 }
177
178 public void setPhotoUri(String uri) {
179 this.photoUri = uri;
180 }
181
182 public void setDisplayName(String name) {
183 this.displayName = name;
184 }
185
186 public String getSystemAccount() {
187 return systemAccount;
188 }
189
190 public Set<String> getOtrFingerprints() {
191 Set<String> set = new HashSet<String>();
192 try {
193 if (this.keys.has("otr_fingerprints")) {
194 JSONArray fingerprints = this.keys.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 setSubscriptionOption(int option) {
223 this.subscription |= 1 << option;
224 }
225
226 public void resetSubscriptionOption(int option) {
227 this.subscription &= ~(1 << option);
228 }
229
230 public boolean getSubscriptionOption(int option) {
231 return ((this.subscription & (1 << option)) != 0);
232 }
233
234 public void parseSubscriptionFromElement(Element item) {
235 String ask = item.getAttribute("ask");
236 String subscription = item.getAttribute("subscription");
237
238 if (subscription!=null) {
239 if (subscription.equals("to")) {
240 this.resetSubscriptionOption(Contact.Subscription.FROM);
241 this.setSubscriptionOption(Contact.Subscription.TO);
242 } else if (subscription.equals("from")) {
243 this.resetSubscriptionOption(Contact.Subscription.TO);
244 this.setSubscriptionOption(Contact.Subscription.FROM);
245 } else if (subscription.equals("both")) {
246 this.setSubscriptionOption(Contact.Subscription.TO);
247 this.setSubscriptionOption(Contact.Subscription.FROM);
248 }
249 }
250
251 if ((ask!=null)&&(ask.equals("subscribe"))) {
252 this.setSubscriptionOption(Contact.Subscription.ASKING);
253 } else {
254 this.resetSubscriptionOption(Contact.Subscription.ASKING);
255 }
256 }
257
258
259 public class Subscription {
260 public static final int TO = 0;
261 public static final int FROM = 1;
262 public static final int ASKING = 2;
263 public static final int PREEMPTIVE_GRANT = 4;
264 }
265}