diff --git a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java index 4dc904ce1..cd91adb82 100644 --- a/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java +++ b/src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlMessage.java @@ -135,7 +135,7 @@ public class XmppAxolotlMessage { break; } } - Element payloadElement = axolotlMessage.findChild(PAYLOAD); + Element payloadElement = axolotlMessage.findChild(PAYLOAD); //TODO make sure we only have _one_ paypload if (payloadElement != null) { ciphertext = Base64.decode(payloadElement.getContent().trim(), Base64.DEFAULT); } diff --git a/src/main/java/eu/siacs/conversations/entities/IndividualMessage.java b/src/main/java/eu/siacs/conversations/entities/IndividualMessage.java index 42496e4eb..9a3ae6236 100644 --- a/src/main/java/eu/siacs/conversations/entities/IndividualMessage.java +++ b/src/main/java/eu/siacs/conversations/entities/IndividualMessage.java @@ -43,8 +43,8 @@ public class IndividualMessage extends Message { super(conversation); } - 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 readByMarkers, boolean markable, boolean deleted) { - super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable, deleted); + 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 readByMarkers, boolean markable, boolean deleted, String bodyLanguage) { + super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable, deleted, bodyLanguage); } @Override @@ -116,6 +116,8 @@ public class IndividualMessage extends Message { cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)), ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))), cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0, - cursor.getInt(cursor.getColumnIndex(DELETED)) > 0); + cursor.getInt(cursor.getColumnIndex(DELETED)) > 0, + cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE)) + ); } } diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 02fa07818..c18693976 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -62,6 +62,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable public static final String COUNTERPART = "counterpart"; public static final String TRUE_COUNTERPART = "trueCounterpart"; public static final String BODY = "body"; + public static final String BODY_LANGUAGE = "bodyLanguage"; public static final String TIME_SENT = "timeSent"; public static final String ENCRYPTION = "encryption"; public static final String STATUS = "status"; @@ -100,6 +101,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable protected String relativeFilePath; protected boolean read = true; protected String remoteMsgId = null; + private String bodyLanguage = null; protected String serverMsgId = null; private final Conversational conversation; protected Transferable transferable = null; @@ -145,7 +147,8 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable null, null, false, - false); + false, + null); } protected Message(final Conversational conversation, final String uuid, final String conversationUUid, final Jid counterpart, @@ -154,7 +157,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable final String remoteMsgId, final String relativeFilePath, final String serverMsgId, final String fingerprint, final boolean read, final String edited, final boolean oob, final String errorMessage, final Set readByMarkers, - final boolean markable, final boolean deleted) { + final boolean markable, final boolean deleted, final String bodyLanguage) { this.conversation = conversation; this.uuid = uuid; this.conversationUuid = conversationUUid; @@ -177,6 +180,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable this.readByMarkers = readByMarkers == null ? new HashSet<>() : readByMarkers; this.markable = markable; this.deleted = deleted; + this.bodyLanguage = bodyLanguage; } public static Message fromCursor(Cursor cursor, Conversation conversation) { @@ -201,7 +205,9 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)), ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))), cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0, - cursor.getInt(cursor.getColumnIndex(DELETED)) > 0); + cursor.getInt(cursor.getColumnIndex(DELETED)) > 0, + cursor.getString(cursor.getColumnIndex(BODY_LANGUAGE)) + ); } private static Jid fromString(String value) { @@ -266,6 +272,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable values.put(READ_BY_MARKERS, ReadByMarker.toJson(readByMarkers).toString()); values.put(MARKABLE, markable ? 1 : 0); values.put(DELETED, deleted ? 1 : 0); + values.put(BODY_LANGUAGE, bodyLanguage); return values; } @@ -430,6 +437,14 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable this.edits.add(new Edited(edited, serverMsgId)); } + public String getBodyLanguage() { + return this.bodyLanguage; + } + + public void setBodyLanguage(String language) { + this.bodyLanguage = language; + } + public boolean edited() { return this.edits.size() > 0; } diff --git a/src/main/java/eu/siacs/conversations/parser/MessageParser.java b/src/main/java/eu/siacs/conversations/parser/MessageParser.java index 33179b851..552f38ebb 100644 --- a/src/main/java/eu/siacs/conversations/parser/MessageParser.java +++ b/src/main/java/eu/siacs/conversations/parser/MessageParser.java @@ -33,6 +33,7 @@ import eu.siacs.conversations.services.MessageArchiveService; import eu.siacs.conversations.services.QuickConversationsService; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.xml.LocalizedContent; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.InvalidJid; @@ -328,7 +329,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece if (timestamp == null) { timestamp = AbstractParser.parseTimestamp(original, AbstractParser.parseTimestamp(packet)); } - final String body = packet.getBody(); + final LocalizedContent body = packet.getBody(); final Element mucUserElement = packet.findChild("x", "http://jabber.org/protocol/muc#user"); final String pgpEncrypted = packet.findChildContent("x", "jabber:x:encrypted"); final Element replaceElement = packet.findChild("replace", "urn:xmpp:message-correct:0"); @@ -337,7 +338,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece final URL xP1S3url = xP1S3 == null ? null : P1S3UrlStreamHandler.of(xP1S3); final String oobUrl = oob != null ? oob.findChildContent("url") : null; final String replacementId = replaceElement == null ? null : replaceElement.getAttribute("id"); - final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); + final Element axolotlEncrypted = packet.findChild(XmppAxolotlMessage.CONTAINERTAG, AxolotlService.PEP_PREFIX); //TODO make sure we only have _one_ axolotl element! int status; final Jid counterpart; final Jid to = packet.getTo(); @@ -409,10 +410,13 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece if (mXmppConnectionService.markMessage(conversation, remoteMsgId, status, serverMsgId)) { return; } else if (remoteMsgId == null || Config.IGNORE_ID_REWRITE_IN_MUC) { - Message message = conversation.findSentMessageWithBody(packet.getBody()); - if (message != null) { - mXmppConnectionService.markMessage(message, status); - return; + LocalizedContent localizedBody = packet.getBody(); + if (localizedBody != null) { + Message message = conversation.findSentMessageWithBody(localizedBody.content); + if (message != null) { + mXmppConnectionService.markMessage(message, status); + return; + } } } } else { @@ -491,7 +495,10 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece message.setEncryption(Message.ENCRYPTION_DECRYPTED); } } else { - message = new Message(conversation, body, Message.ENCRYPTION_NONE, status); + message = new Message(conversation, body.content, Message.ENCRYPTION_NONE, status); + if (body.count > 1) { + message.setBodyLanguage(body.language); + } } message.setCounterpart(counterpart); @@ -499,7 +506,7 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece message.setServerMsgId(serverMsgId); message.setCarbon(isCarbon); message.setTime(timestamp); - if (body != null && body.equals(oobUrl)) { + if (body != null && body.content != null && body.content.equals(oobUrl)) { message.setOob(true); if (CryptoHelper.isPgpEncryptedUrl(oobUrl)) { message.setEncryption(Message.ENCRYPTION_DECRYPTED); @@ -702,11 +709,11 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece } if (isTypeGroupChat) { - if (packet.hasChild("subject")) { + if (packet.hasChild("subject")) { //TODO usually we would want to check for lack of body; however some servers do set a body :( if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) { conversation.setHasMessagesLeftOnServer(conversation.countMessages() > 0); - String subject = packet.findInternationalizedChildContent("subject"); - if (conversation.getMucOptions().setSubject(subject)) { + final LocalizedContent subject = packet.findInternationalizedChildContentInDefaultNamespace("subject"); + if (subject != null && conversation.getMucOptions().setSubject(subject.content)) { mXmppConnectionService.updateConversation(conversation); } mXmppConnectionService.updateConversationUi(); diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index b9d5d5bbe..bc4fde0ae 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -62,7 +62,7 @@ import rocks.xmpp.addr.Jid; public class DatabaseBackend extends SQLiteOpenHelper { private static final String DATABASE_NAME = "history"; - private static final int DATABASE_VERSION = 44; + private static final int DATABASE_VERSION = 45; private static DatabaseBackend instance = null; private static String CREATE_CONTATCS_STATEMENT = "create table " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " @@ -239,6 +239,7 @@ public class DatabaseBackend extends SQLiteOpenHelper { + Message.READ_BY_MARKERS + " TEXT," + Message.MARKABLE + " NUMBER DEFAULT 0," + Message.DELETED + " NUMBER DEFAULT 0," + + Message.BODY_LANGUAGE + " TEXT," + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY(" + Message.CONVERSATION + ") REFERENCES " + Conversation.TABLENAME + "(" + Conversation.UUID @@ -540,6 +541,10 @@ public class DatabaseBackend extends SQLiteOpenHelper { db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX); db.execSQL(CREATE_MESSAGE_TYPE_INDEX); } + + if (oldVersion < 45 && newVersion >= 45) { + db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE); + } } private void canonicalizeJids(SQLiteDatabase db) { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 6c4ffd9fd..490bd0f4f 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -36,6 +36,7 @@ import com.google.common.base.Strings; import java.net.URL; import java.util.List; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -283,30 +284,32 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie viewHolder.indicator.setVisibility(View.VISIBLE); } - String formatedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent()); - if (message.getStatus() <= Message.STATUS_RECEIVED) { + final String formattedTime = UIHelper.readableTimeDifferenceFull(getContext(), message.getMergedTimeSent()); + final String bodyLanguage = message.getBodyLanguage(); + final String bodyLanguageInfo = bodyLanguage == null ? "" : String.format(" \u00B7 %s", bodyLanguage.toUpperCase(Locale.US)); + if (message.getStatus() <= Message.STATUS_RECEIVED) { ; if ((filesize != null) && (info != null)) { - viewHolder.time.setText(formatedTime + " \u00B7 " + filesize + " \u00B7 " + info); + viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + " \u00B7 " + info + bodyLanguageInfo); } else if ((filesize == null) && (info != null)) { - viewHolder.time.setText(formatedTime + " \u00B7 " + info); + viewHolder.time.setText(formattedTime + " \u00B7 " + info + bodyLanguageInfo); } else if ((filesize != null) && (info == null)) { - viewHolder.time.setText(formatedTime + " \u00B7 " + filesize); + viewHolder.time.setText(formattedTime + " \u00B7 " + filesize + bodyLanguageInfo); } else { - viewHolder.time.setText(formatedTime); + viewHolder.time.setText(formattedTime+bodyLanguageInfo); } } else { if ((filesize != null) && (info != null)) { - viewHolder.time.setText(filesize + " \u00B7 " + info); + viewHolder.time.setText(filesize + " \u00B7 " + info + bodyLanguageInfo); } else if ((filesize == null) && (info != null)) { if (error) { - viewHolder.time.setText(info + " \u00B7 " + formatedTime); + viewHolder.time.setText(info + " \u00B7 " + formattedTime + bodyLanguageInfo); } else { viewHolder.time.setText(info); } } else if ((filesize != null) && (info == null)) { - viewHolder.time.setText(filesize + " \u00B7 " + formatedTime); + viewHolder.time.setText(filesize + " \u00B7 " + formattedTime + bodyLanguageInfo); } else { - viewHolder.time.setText(formatedTime); + viewHolder.time.setText(formattedTime+bodyLanguageInfo); } } } diff --git a/src/main/java/eu/siacs/conversations/xml/Element.java b/src/main/java/eu/siacs/conversations/xml/Element.java index c708ca2b1..9f27844df 100644 --- a/src/main/java/eu/siacs/conversations/xml/Element.java +++ b/src/main/java/eu/siacs/conversations/xml/Element.java @@ -1,15 +1,11 @@ package eu.siacs.conversations.xml; -import android.support.annotation.NonNull; -import android.util.Log; - import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Locale; -import eu.siacs.conversations.Config; import eu.siacs.conversations.utils.XmlHelper; import eu.siacs.conversations.xmpp.InvalidJid; import eu.siacs.conversations.xmpp.stanzas.MessagePacket; @@ -71,31 +67,8 @@ public class Element { return element == null ? null : element.getContent(); } - public String findInternationalizedChildContent(String name) { - return findInternationalizedChildContent(name, Locale.getDefault().getLanguage()); - } - - private String findInternationalizedChildContent(String name, @NonNull String language) { - final HashMap contents = new HashMap<>(); - for(Element child : this.children) { - if (name.equals(child.getName())) { - String lang = child.getAttribute("xml:lang"); - String content = child.getContent(); - if (content != null) { - if (language.equals(lang)) { - return content; - } else { - contents.put(lang, content); - } - } - } - } - final String value = contents.get(null); - if (value != null) { - return value; - } - final String[] values = contents.values().toArray(new String[0]); - return values.length == 0 ? null : values[0]; + public LocalizedContent findInternationalizedChildContentInDefaultNamespace(String name) { + return LocalizedContent.get(this, name); } public Element findChild(String name, String xmlns) { diff --git a/src/main/java/eu/siacs/conversations/xml/LocalizedContent.java b/src/main/java/eu/siacs/conversations/xml/LocalizedContent.java new file mode 100644 index 000000000..ee70df859 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/xml/LocalizedContent.java @@ -0,0 +1,57 @@ +package eu.siacs.conversations.xml; + +import com.google.common.collect.Iterables; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class LocalizedContent { + + public static final String STREAM_LANGUAGE = "en"; + + public final String content; + public final String language; + public final int count; + + private LocalizedContent(String content, String language, int count) { + this.content = content; + this.language = language; + this.count = count; + } + + public static LocalizedContent get(final Element element, String name) { + final HashMap contents = new HashMap<>(); + for(Element child : element.children) { + if (name.equals(child.getName())) { + final String namespace = child.getNamespace(); + final String lang = child.getAttribute("xml:lang"); + final String content = child.getContent(); + if (content != null && (namespace == null || "jabber:client".equals(namespace))) { + if (contents.put(lang, content) != null) { + //anything that has multiple contents for the same language is invalid + return null; + } + } + } + } + if (contents.size() == 0) { + return null; + } + final String userLanguage = Locale.getDefault().getLanguage(); + final String localized = contents.get(userLanguage); + if (localized != null) { + return new LocalizedContent(localized, userLanguage, contents.size()); + } + final String defaultLanguageContent = contents.get(null); + if (defaultLanguageContent != null) { + return new LocalizedContent(defaultLanguageContent, STREAM_LANGUAGE, contents.size()); + } + final String streamLanguageContent = contents.get(STREAM_LANGUAGE); + if (streamLanguageContent != null) { + return new LocalizedContent(streamLanguageContent, STREAM_LANGUAGE, contents.size()); + } + final Map.Entry first = Iterables.get(contents.entrySet(), 0); + return new LocalizedContent(first.getValue(), first.getKey(), contents.size()); + } +} diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index fe384ebf2..45e4cb887 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -80,6 +80,7 @@ import eu.siacs.conversations.utils.SSLSocketHelper; import eu.siacs.conversations.utils.SocksSocketFactory; import eu.siacs.conversations.utils.XmlHelper; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xml.LocalizedContent; import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xml.Tag; import eu.siacs.conversations.xml.TagWriter; @@ -1334,7 +1335,7 @@ public class XmppConnection implements Runnable { final Tag stream = Tag.start("stream:stream"); stream.setAttribute("to", account.getServer()); stream.setAttribute("version", "1.0"); - stream.setAttribute("xml:lang", "en"); + stream.setAttribute("xml:lang", LocalizedContent.STREAM_LANGUAGE); stream.setAttribute("xmlns", "jabber:client"); stream.setAttribute("xmlns:stream", "http://etherx.jabber.org/streams"); tagWriter.writeTag(stream); diff --git a/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java b/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java index ac75a5e59..86068bf77 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java +++ b/src/main/java/eu/siacs/conversations/xmpp/stanzas/MessagePacket.java @@ -4,6 +4,7 @@ import android.util.Pair; import eu.siacs.conversations.parser.AbstractParser; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xml.LocalizedContent; public class MessagePacket extends AbstractAcknowledgeableStanza { public static final int TYPE_CHAT = 0; @@ -16,8 +17,8 @@ public class MessagePacket extends AbstractAcknowledgeableStanza { super("message"); } - public String getBody() { - return findChildContent("body"); + public LocalizedContent getBody() { + return findInternationalizedChildContentInDefaultNamespace("body"); } public void setBody(String text) {