1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.entities;
 31
 32import android.database.Cursor;
 33
 34import java.util.Collection;
 35import java.util.Set;
 36
 37import eu.siacs.conversations.ui.adapter.MessageAdapter;
 38import eu.siacs.conversations.xmpp.Jid;
 39
 40public class IndividualMessage extends Message {
 41
 42
 43	private IndividualMessage(Conversational conversation) {
 44		super(conversation);
 45	}
 46
 47	private IndividualMessage(Conversational conversation, String uuid, String conversationUUid, Jid counterpart, Jid trueCounterpart, String body, long timeSent, int encryption, int status, int type, boolean carbon, String remoteMsgId, String relativeFilePath, String serverMsgId, String fingerprint, boolean read, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable, boolean deleted, String bodyLanguage, String occupantId, Collection<Reaction> reactions) {
 48		super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable, deleted, bodyLanguage, occupantId, reactions, timeSent, null, null, null);
 49	}
 50
 51	@Override
 52	public Message next() {
 53		return null;
 54	}
 55
 56	@Override
 57	public Message prev() {
 58		return null;
 59	}
 60
 61	@Override
 62	public boolean isValidInSession() {
 63		return true;
 64	}
 65
 66	public static Message createDateSeparator(Message message) {
 67		final Message separator = new IndividualMessage(message.getConversation());
 68		separator.setType(Message.TYPE_STATUS);
 69		separator.body = MessageAdapter.DATE_SEPARATOR_BODY;
 70		separator.setTime(message.getTimeSent());
 71		return separator;
 72	}
 73
 74	public static Message fromCursor(Cursor cursor, Conversational conversation) {
 75		Jid jid;
 76		try {
 77			String value = cursor.getString(cursor.getColumnIndexOrThrow(COUNTERPART));
 78			if (value != null) {
 79				jid = Jid.of(value);
 80			} else {
 81				jid = null;
 82			}
 83		} catch (IllegalArgumentException e) {
 84			jid = null;
 85		} catch (IllegalStateException e) {
 86			return null; // message too long?
 87		}
 88		Jid trueCounterpart;
 89		try {
 90			String value = cursor.getString(cursor.getColumnIndexOrThrow(TRUE_COUNTERPART));
 91			if (value != null) {
 92				trueCounterpart = Jid.of(value);
 93			} else {
 94				trueCounterpart = null;
 95			}
 96		} catch (IllegalArgumentException e) {
 97			trueCounterpart = null;
 98		}
 99		return new IndividualMessage(conversation,
100				cursor.getString(cursor.getColumnIndexOrThrow(UUID)),
101				cursor.getString(cursor.getColumnIndexOrThrow(CONVERSATION)),
102				jid,
103				trueCounterpart,
104				cursor.getString(cursor.getColumnIndexOrThrow(BODY)),
105				cursor.getLong(cursor.getColumnIndexOrThrow(TIME_SENT)),
106				cursor.getInt(cursor.getColumnIndexOrThrow(ENCRYPTION)),
107				cursor.getInt(cursor.getColumnIndexOrThrow(STATUS)),
108				cursor.getInt(cursor.getColumnIndexOrThrow(TYPE)),
109				cursor.getInt(cursor.getColumnIndexOrThrow(CARBON)) > 0,
110				cursor.getString(cursor.getColumnIndexOrThrow(REMOTE_MSG_ID)),
111				cursor.getString(cursor.getColumnIndexOrThrow(RELATIVE_FILE_PATH)),
112				cursor.getString(cursor.getColumnIndexOrThrow(SERVER_MSG_ID)),
113				cursor.getString(cursor.getColumnIndexOrThrow(FINGERPRINT)),
114				cursor.getInt(cursor.getColumnIndexOrThrow(READ)) > 0,
115				cursor.getString(cursor.getColumnIndexOrThrow(EDITED)),
116				cursor.getInt(cursor.getColumnIndexOrThrow(OOB)) > 0,
117				cursor.getString(cursor.getColumnIndexOrThrow(ERROR_MESSAGE)),
118				ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndexOrThrow(READ_BY_MARKERS))),
119				cursor.getInt(cursor.getColumnIndexOrThrow(MARKABLE)) > 0,
120				cursor.getInt(cursor.getColumnIndexOrThrow(DELETED)) > 0,
121				cursor.getString(cursor.getColumnIndexOrThrow(BODY_LANGUAGE)),
122				cursor.getString(cursor.getColumnIndexOrThrow(OCCUPANT_ID)),
123				Reaction.fromString(cursor.getString(cursor.getColumnIndexOrThrow(REACTIONS)))
124		);
125	}
126}