Conversation.java

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