Account.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4import java.util.List;
  5import java.util.Locale;
  6import java.util.concurrent.CopyOnWriteArrayList;
  7
  8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  9import net.java.otr4j.crypto.OtrCryptoException;
 10
 11import org.json.JSONException;
 12import org.json.JSONObject;
 13
 14import eu.siacs.conversations.R;
 15import eu.siacs.conversations.crypto.OtrEngine;
 16import eu.siacs.conversations.persistance.FileBackend;
 17import eu.siacs.conversations.services.XmppConnectionService;
 18import eu.siacs.conversations.utils.UIHelper;
 19import eu.siacs.conversations.xmpp.XmppConnection;
 20import android.content.ContentValues;
 21import android.content.Context;
 22import android.database.Cursor;
 23import android.graphics.Bitmap;
 24
 25public class Account extends AbstractEntity {
 26
 27	public static final String TABLENAME = "accounts";
 28
 29	public static final String USERNAME = "username";
 30	public static final String SERVER = "server";
 31	public static final String PASSWORD = "password";
 32	public static final String OPTIONS = "options";
 33	public static final String ROSTERVERSION = "rosterversion";
 34	public static final String KEYS = "keys";
 35	public static final String AVATAR = "avatar";
 36
 37	public static final int OPTION_USETLS = 0;
 38	public static final int OPTION_DISABLED = 1;
 39	public static final int OPTION_REGISTER = 2;
 40	public static final int OPTION_USECOMPRESSION = 3;
 41
 42	public static final int STATUS_CONNECTING = 0;
 43	public static final int STATUS_DISABLED = -2;
 44	public static final int STATUS_OFFLINE = -1;
 45	public static final int STATUS_ONLINE = 1;
 46	public static final int STATUS_NO_INTERNET = 2;
 47	public static final int STATUS_UNAUTHORIZED = 3;
 48	public static final int STATUS_SERVER_NOT_FOUND = 5;
 49
 50	public static final int STATUS_REGISTRATION_FAILED = 7;
 51	public static final int STATUS_REGISTRATION_CONFLICT = 8;
 52	public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
 53	public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
 54
 55	protected String username;
 56	protected String server;
 57	protected String password;
 58	protected int options = 0;
 59	protected String rosterVersion;
 60	protected String resource = "mobile";
 61	protected int status = -1;
 62	protected JSONObject keys = new JSONObject();
 63	protected String avatar;
 64
 65	protected boolean online = false;
 66
 67	transient OtrEngine otrEngine = null;
 68	transient XmppConnection xmppConnection = null;
 69	transient protected Presences presences = new Presences();
 70
 71	private String otrFingerprint;
 72
 73	private Roster roster = null;
 74
 75	private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
 76
 77	public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
 78	public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
 79
 80	public Account() {
 81		this.uuid = "0";
 82	}
 83
 84	public Account(String username, String server, String password) {
 85		this(java.util.UUID.randomUUID().toString(), username, server,
 86				password, 0, null, "", null);
 87	}
 88
 89	public Account(String uuid, String username, String server,
 90			String password, int options, String rosterVersion, String keys,
 91			String avatar) {
 92		this.uuid = uuid;
 93		this.username = username;
 94		this.server = server;
 95		this.password = password;
 96		this.options = options;
 97		this.rosterVersion = rosterVersion;
 98		try {
 99			this.keys = new JSONObject(keys);
100		} catch (JSONException e) {
101
102		}
103		this.avatar = avatar;
104	}
105
106	public boolean isOptionSet(int option) {
107		return ((options & (1 << option)) != 0);
108	}
109
110	public void setOption(int option, boolean value) {
111		if (value) {
112			this.options |= 1 << option;
113		} else {
114			this.options &= ~(1 << option);
115		}
116	}
117
118	public String getUsername() {
119		return username;
120	}
121
122	public void setUsername(String username) {
123		this.username = username;
124	}
125
126	public String getServer() {
127		return server;
128	}
129
130	public void setServer(String server) {
131		this.server = server;
132	}
133
134	public String getPassword() {
135		return password;
136	}
137
138	public void setPassword(String password) {
139		this.password = password;
140	}
141
142	public void setStatus(int status) {
143		this.status = status;
144	}
145
146	public int getStatus() {
147		if (isOptionSet(OPTION_DISABLED)) {
148			return STATUS_DISABLED;
149		} else {
150			return this.status;
151		}
152	}
153
154	public boolean errorStatus() {
155		int s = getStatus();
156		return (s == STATUS_REGISTRATION_FAILED
157				|| s == STATUS_REGISTRATION_CONFLICT
158				|| s == STATUS_REGISTRATION_NOT_SUPPORTED
159				|| s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED);
160	}
161
162	public boolean hasErrorStatus() {
163		return getStatus() > STATUS_NO_INTERNET
164				&& (getXmppConnection().getAttempt() >= 2);
165	}
166
167	public void setResource(String resource) {
168		this.resource = resource;
169	}
170
171	public String getResource() {
172		return this.resource;
173	}
174
175	public String getJid() {
176		return username.toLowerCase(Locale.getDefault()) + "@"
177				+ server.toLowerCase(Locale.getDefault());
178	}
179
180	public JSONObject getKeys() {
181		return keys;
182	}
183
184	public String getSSLFingerprint() {
185		if (keys.has("ssl_cert")) {
186			try {
187				return keys.getString("ssl_cert");
188			} catch (JSONException e) {
189				return null;
190			}
191		} else {
192			return null;
193		}
194	}
195
196	public void setSSLCertFingerprint(String fingerprint) {
197		this.setKey("ssl_cert", fingerprint);
198	}
199
200	public boolean setKey(String keyName, String keyValue) {
201		try {
202			this.keys.put(keyName, keyValue);
203			return true;
204		} catch (JSONException e) {
205			return false;
206		}
207	}
208
209	@Override
210	public ContentValues getContentValues() {
211		ContentValues values = new ContentValues();
212		values.put(UUID, uuid);
213		values.put(USERNAME, username);
214		values.put(SERVER, server);
215		values.put(PASSWORD, password);
216		values.put(OPTIONS, options);
217		values.put(KEYS, this.keys.toString());
218		values.put(ROSTERVERSION, rosterVersion);
219		values.put(AVATAR, avatar);
220		return values;
221	}
222
223	public static Account fromCursor(Cursor cursor) {
224		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
225				cursor.getString(cursor.getColumnIndex(USERNAME)),
226				cursor.getString(cursor.getColumnIndex(SERVER)),
227				cursor.getString(cursor.getColumnIndex(PASSWORD)),
228				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
229				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
230				cursor.getString(cursor.getColumnIndex(KEYS)),
231				cursor.getString(cursor.getColumnIndex(AVATAR)));
232	}
233
234	public OtrEngine getOtrEngine(XmppConnectionService context) {
235		if (otrEngine == null) {
236			otrEngine = new OtrEngine(context, this);
237		}
238		return this.otrEngine;
239	}
240
241	public XmppConnection getXmppConnection() {
242		return this.xmppConnection;
243	}
244
245	public void setXmppConnection(XmppConnection connection) {
246		this.xmppConnection = connection;
247	}
248
249	public String getFullJid() {
250		return this.getJid() + "/" + this.resource;
251	}
252
253	public String getOtrFingerprint() {
254		if (this.otrFingerprint == null) {
255			try {
256				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine
257						.getPublicKey();
258				if (pubkey == null) {
259					return null;
260				}
261				StringBuilder builder = new StringBuilder(
262						new OtrCryptoEngineImpl().getFingerprint(pubkey));
263				builder.insert(8, " ");
264				builder.insert(17, " ");
265				builder.insert(26, " ");
266				builder.insert(35, " ");
267				this.otrFingerprint = builder.toString();
268			} catch (OtrCryptoException e) {
269
270			}
271		}
272		return this.otrFingerprint;
273	}
274
275	public String getRosterVersion() {
276		if (this.rosterVersion == null) {
277			return "";
278		} else {
279			return this.rosterVersion;
280		}
281	}
282
283	public void setRosterVersion(String version) {
284		this.rosterVersion = version;
285	}
286
287	public String getOtrFingerprint(XmppConnectionService service) {
288		this.getOtrEngine(service);
289		return this.getOtrFingerprint();
290	}
291
292	public void updatePresence(String resource, int status) {
293		this.presences.updatePresence(resource, status);
294	}
295
296	public void removePresence(String resource) {
297		this.presences.removePresence(resource);
298	}
299
300	public void clearPresences() {
301		this.presences = new Presences();
302	}
303
304	public int countPresences() {
305		return this.presences.size();
306	}
307
308	public String getPgpSignature() {
309		if (keys.has("pgp_signature")) {
310			try {
311				return keys.getString("pgp_signature");
312			} catch (JSONException e) {
313				return null;
314			}
315		} else {
316			return null;
317		}
318	}
319
320	public Roster getRoster() {
321		if (this.roster == null) {
322			this.roster = new Roster(this);
323		}
324		return this.roster;
325	}
326
327	public void setBookmarks(List<Bookmark> bookmarks) {
328		this.bookmarks = bookmarks;
329	}
330
331	public List<Bookmark> getBookmarks() {
332		return this.bookmarks;
333	}
334
335	public boolean hasBookmarkFor(String conferenceJid) {
336		for (Bookmark bmark : this.bookmarks) {
337			if (bmark.getJid().equals(conferenceJid)) {
338				return true;
339			}
340		}
341		return false;
342	}
343
344	public Bitmap getImage(Context context, int size) {
345		if (this.avatar != null) {
346			Bitmap bm = FileBackend.getAvatar(this.avatar, size, context);
347			if (bm == null) {
348				return UIHelper.getContactPicture(getJid(), size, context,
349						false);
350			} else {
351				return bm;
352			}
353		} else {
354			return UIHelper.getContactPicture(getJid(), size, context, false);
355		}
356	}
357
358	public boolean setAvatar(String filename) {
359		if (this.avatar != null && this.avatar.equals(filename)) {
360			return false;
361		} else {
362			this.avatar = filename;
363			return true;
364		}
365	}
366
367	public String getAvatar() {
368		return this.avatar;
369	}
370
371	public int getReadableStatusId() {
372		switch (getStatus()) {
373
374		case Account.STATUS_DISABLED:
375			return R.string.account_status_disabled;
376		case Account.STATUS_ONLINE:
377			return R.string.account_status_online;
378		case Account.STATUS_CONNECTING:
379			return R.string.account_status_connecting;
380		case Account.STATUS_OFFLINE:
381			return R.string.account_status_offline;
382		case Account.STATUS_UNAUTHORIZED:
383			return R.string.account_status_unauthorized;
384		case Account.STATUS_SERVER_NOT_FOUND:
385			return R.string.account_status_not_found;
386		case Account.STATUS_NO_INTERNET:
387			return R.string.account_status_no_internet;
388		case Account.STATUS_REGISTRATION_FAILED:
389			return R.string.account_status_regis_fail;
390		case Account.STATUS_REGISTRATION_CONFLICT:
391			return R.string.account_status_regis_conflict;
392		case Account.STATUS_REGISTRATION_SUCCESSFULL:
393			return R.string.account_status_regis_success;
394		case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
395			return R.string.account_status_regis_not_sup;
396		default:
397			return R.string.account_status_unknown;
398		}
399	}
400}