Conversation.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4import java.util.ArrayList;
  5import java.util.List;
  6
  7import net.java.otr4j.OtrException;
  8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  9import net.java.otr4j.crypto.OtrCryptoException;
 10import net.java.otr4j.session.SessionID;
 11import net.java.otr4j.session.SessionImpl;
 12import net.java.otr4j.session.SessionStatus;
 13import android.content.ContentValues;
 14import android.content.Context;
 15import android.database.Cursor;
 16import android.net.Uri;
 17
 18public class Conversation extends AbstractEntity {
 19
 20	private static final long serialVersionUID = -6727528868973996739L;
 21
 22	public static final String TABLENAME = "conversations";
 23
 24	public static final int STATUS_AVAILABLE = 0;
 25	public static final int STATUS_ARCHIVED = 1;
 26	public static final int STATUS_DELETED = 2;
 27
 28	public static final int MODE_MULTI = 1;
 29	public static final int MODE_SINGLE = 0;
 30
 31	public static final String NAME = "name";
 32	public static final String ACCOUNT = "accountUuid";
 33	public static final String CONTACT = "contactUuid";
 34	public static final String CONTACTJID = "contactJid";
 35	public static final String STATUS = "status";
 36	public static final String CREATED = "created";
 37	public static final String MODE = "mode";
 38
 39	private String name;
 40	private String contactUuid;
 41	private String accountUuid;
 42	private String contactJid;
 43	private int status;
 44	private long created;
 45	private int mode;
 46
 47	private String nextPresence;
 48
 49	private transient List<Message> messages = null;
 50	private transient Account account = null;
 51
 52	private transient SessionImpl otrSession;
 53
 54	private transient String otrFingerprint = null;
 55
 56	private int nextMessageEncryption = -1;
 57	private String nextMessage;
 58
 59	private transient MucOptions mucOptions = null;
 60
 61	private transient String latestMarkableMessageId;
 62
 63	private byte[] symmetricKey;
 64
 65	private boolean otrSessionNeedsStarting = false;
 66
 67	private Bookmark bookmark;
 68
 69	public Conversation(String name, Account account, String contactJid,
 70			int mode) {
 71		this(java.util.UUID.randomUUID().toString(), name, null, account
 72				.getUuid(), contactJid, System.currentTimeMillis(),
 73				STATUS_AVAILABLE, mode);
 74		this.account = account;
 75	}
 76
 77	public Conversation(String uuid, String name, String contactUuid,
 78			String accountUuid, String contactJid, long created, int status,
 79			int mode) {
 80		this.uuid = uuid;
 81		this.name = name;
 82		this.contactUuid = contactUuid;
 83		this.accountUuid = accountUuid;
 84		this.contactJid = contactJid;
 85		this.created = created;
 86		this.status = status;
 87		this.mode = mode;
 88	}
 89
 90	public List<Message> getMessages() {
 91		if (messages == null)
 92			this.messages = new ArrayList<Message>(); // prevent null pointer
 93
 94		// populate with Conversation (this)
 95
 96		for (Message msg : messages) {
 97			msg.setConversation(this);
 98		}
 99
100		return messages;
101	}
102
103	public boolean isRead() {
104		if ((this.messages == null) || (this.messages.size() == 0))
105			return true;
106		return this.messages.get(this.messages.size() - 1).isRead();
107	}
108
109	public void markRead() {
110		if (this.messages == null) {
111			return;
112		}
113		for (int i = this.messages.size() - 1; i >= 0; --i) {
114			if (messages.get(i).isRead()) {
115				break;
116			}
117			this.messages.get(i).markRead();
118		}
119	}
120	
121	public String popLatestMarkableMessageId() {
122		String id = this.latestMarkableMessageId;
123		this.latestMarkableMessageId = null;
124		return id;
125	}
126
127	public Message getLatestMessage() {
128		if ((this.messages == null) || (this.messages.size() == 0)) {
129			Message message = new Message(this, "", Message.ENCRYPTION_NONE);
130			message.setTime(getCreated());
131			return message;
132		} else {
133			Message message = this.messages.get(this.messages.size() - 1);
134			message.setConversation(this);
135			return message;
136		}
137	}
138
139	public void setMessages(List<Message> msgs) {
140		this.messages = msgs;
141	}
142
143	public String getName(boolean useSubject) {
144		if ((getMode() == MODE_MULTI) && (getMucOptions().getSubject() != null)
145				&& useSubject) {
146			return getMucOptions().getSubject();
147		} else {
148			return this.getContact().getDisplayName();
149		}
150	}
151
152	public String getProfilePhotoString() {
153		return this.getContact().getProfilePhoto();
154	}
155
156	public String getAccountUuid() {
157		return this.accountUuid;
158	}
159
160	public Account getAccount() {
161		return this.account;
162	}
163
164	public Contact getContact() {
165		return this.account.getRoster().getContact(this.contactJid);
166	}
167
168	public void setAccount(Account account) {
169		this.account = account;
170	}
171
172	public String getContactJid() {
173		return this.contactJid;
174	}
175
176	public Uri getProfilePhotoUri() {
177		if (this.getProfilePhotoString() != null) {
178			return Uri.parse(this.getProfilePhotoString());
179		}
180		return null;
181	}
182
183	public int getStatus() {
184		return this.status;
185	}
186
187	public long getCreated() {
188		return this.created;
189	}
190
191	public ContentValues getContentValues() {
192		ContentValues values = new ContentValues();
193		values.put(UUID, uuid);
194		values.put(NAME, name);
195		values.put(CONTACT, contactUuid);
196		values.put(ACCOUNT, accountUuid);
197		values.put(CONTACTJID, contactJid);
198		values.put(CREATED, created);
199		values.put(STATUS, status);
200		values.put(MODE, mode);
201		return values;
202	}
203
204	public static Conversation fromCursor(Cursor cursor) {
205		return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)),
206				cursor.getString(cursor.getColumnIndex(NAME)),
207				cursor.getString(cursor.getColumnIndex(CONTACT)),
208				cursor.getString(cursor.getColumnIndex(ACCOUNT)),
209				cursor.getString(cursor.getColumnIndex(CONTACTJID)),
210				cursor.getLong(cursor.getColumnIndex(CREATED)),
211				cursor.getInt(cursor.getColumnIndex(STATUS)),
212				cursor.getInt(cursor.getColumnIndex(MODE)));
213	}
214
215	public void setStatus(int status) {
216		this.status = status;
217	}
218
219	public int getMode() {
220		return this.mode;
221	}
222
223	public void setMode(int mode) {
224		this.mode = mode;
225	}
226
227	public SessionImpl startOtrSession(Context context, String presence,
228			boolean sendStart) {
229		if (this.otrSession != null) {
230			return this.otrSession;
231		} else {
232			SessionID sessionId = new SessionID(this.getContactJid(), presence,
233					"xmpp");
234			this.otrSession = new SessionImpl(sessionId, getAccount()
235					.getOtrEngine(context));
236			try {
237				if (sendStart) {
238					this.otrSession.startSession();
239					this.otrSessionNeedsStarting = false;
240					return this.otrSession;
241				} else {
242					this.otrSessionNeedsStarting  = true;
243				}
244				return this.otrSession;
245			} catch (OtrException e) {
246				return null;
247			}
248		}
249
250	}
251
252	public SessionImpl getOtrSession() {
253		return this.otrSession;
254	}
255
256	public void resetOtrSession() {
257		this.otrFingerprint = null;
258		this.otrSessionNeedsStarting = false;
259		this.otrSession = null;
260	}
261
262	public void startOtrIfNeeded() {
263		if (this.otrSession != null && this.otrSessionNeedsStarting) {
264			try {
265				this.otrSession.startSession();
266			} catch (OtrException e) {
267				this.resetOtrSession();
268			}
269		}
270	}
271	
272	public void endOtrIfNeeded() {
273		if (this.otrSession != null) {
274			if (this.otrSession.getSessionStatus() == SessionStatus.ENCRYPTED) {
275				try {
276					this.otrSession.endSession();
277					this.resetOtrSession();
278				} catch (OtrException e) {
279					this.resetOtrSession();
280				}
281			} else {
282				this.resetOtrSession();
283			}
284		}
285	}
286
287	public boolean hasValidOtrSession() {
288		return this.otrSession != null;
289	}
290
291	public String getOtrFingerprint() {
292		if (this.otrFingerprint == null) {
293			try {
294				DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession()
295						.getRemotePublicKey();
296				StringBuilder builder = new StringBuilder(
297						new OtrCryptoEngineImpl().getFingerprint(remotePubKey));
298				builder.insert(8, " ");
299				builder.insert(17, " ");
300				builder.insert(26, " ");
301				builder.insert(35, " ");
302				this.otrFingerprint = builder.toString();
303			} catch (OtrCryptoException e) {
304
305			}
306		}
307		return this.otrFingerprint;
308	}
309
310	public synchronized MucOptions getMucOptions() {
311		if (this.mucOptions == null) {
312			this.mucOptions = new MucOptions(this.getAccount());
313		}
314		this.mucOptions.setConversation(this);
315		return this.mucOptions;
316	}
317
318	public void resetMucOptions() {
319		this.mucOptions = null;
320	}
321
322	public void setContactJid(String jid) {
323		this.contactJid = jid;
324	}
325
326	public void setNextPresence(String presence) {
327		this.nextPresence = presence;
328	}
329
330	public String getNextPresence() {
331		return this.nextPresence;
332	}
333
334	public int getLatestEncryption() {
335		int latestEncryption = this.getLatestMessage().getEncryption();
336		if ((latestEncryption == Message.ENCRYPTION_DECRYPTED)
337				|| (latestEncryption == Message.ENCRYPTION_DECRYPTION_FAILED)) {
338			return Message.ENCRYPTION_PGP;
339		} else {
340			return latestEncryption;
341		}
342	}
343
344	public int getNextEncryption() {
345		if (this.nextMessageEncryption == -1) {
346			return this.getLatestEncryption();
347		}
348		return this.nextMessageEncryption;
349	}
350
351	public void setNextEncryption(int encryption) {
352		this.nextMessageEncryption = encryption;
353	}
354
355	public String getNextMessage() {
356		if (this.nextMessage == null) {
357			return "";
358		} else {
359			return this.nextMessage;
360		}
361	}
362
363	public void setNextMessage(String message) {
364		this.nextMessage = message;
365	}
366
367	public void setLatestMarkableMessageId(String id) {
368		if (id != null) {
369			this.latestMarkableMessageId = id;
370		}
371	}
372
373	public void setSymmetricKey(byte[] key) {
374		this.symmetricKey = key;
375	}
376	
377	public byte[] getSymmetricKey() {
378		return this.symmetricKey;
379	}
380
381	public void setBookmark(Bookmark bookmark) {
382		this.bookmark = bookmark;
383	}
384}