diff options
Diffstat (limited to 'src/eu/siacs/conversations/entities')
-rw-r--r-- | src/eu/siacs/conversations/entities/Bookmark.java | 115 | ||||
-rw-r--r-- | src/eu/siacs/conversations/entities/Contact.java | 1 | ||||
-rw-r--r-- | src/eu/siacs/conversations/entities/Conversation.java | 116 | ||||
-rw-r--r-- | src/eu/siacs/conversations/entities/Message.java | 6 | ||||
-rw-r--r-- | src/eu/siacs/conversations/entities/MucOptions.java | 63 | ||||
-rw-r--r-- | src/eu/siacs/conversations/entities/Roster.java | 13 |
6 files changed, 194 insertions, 120 deletions
diff --git a/src/eu/siacs/conversations/entities/Bookmark.java b/src/eu/siacs/conversations/entities/Bookmark.java index 14f010e7..722fb6d9 100644 --- a/src/eu/siacs/conversations/entities/Bookmark.java +++ b/src/eu/siacs/conversations/entities/Bookmark.java @@ -7,46 +7,35 @@ import android.graphics.Bitmap; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.xml.Element; -public class Bookmark implements ListItem { +public class Bookmark extends Element implements ListItem { private Account account; - private String jid; - private String nick; - private String name; - private String password; - private boolean autojoin; - private boolean providePassword; private Conversation mJoinedConversation; public Bookmark(Account account, String jid) { + super("conference"); + this.setAttribute("jid", jid); + this.account = account; + } + + private Bookmark(Account account) { + super("conference"); this.account = account; - this.jid = jid; } public static Bookmark parse(Element element, Account account) { - Bookmark bookmark = new Bookmark(account, element.getAttribute("jid")); - bookmark.setName(element.getAttribute("name")); - String autojoin = element.getAttribute("autojoin"); - if (autojoin != null - && (autojoin.equals("true") || autojoin.equals("1"))) { - bookmark.setAutojoin(true); - } else { - bookmark.setAutojoin(false); - } - Element nick = element.findChild("nick"); - if (nick != null) { - bookmark.setNick(nick.getContent()); - } - Element password = element.findChild("password"); - if (password != null) { - bookmark.setPassword(password.getContent()); - bookmark.setProvidePassword(true); - } + Bookmark bookmark = new Bookmark(account); + bookmark.setAttributes(element.getAttributes()); + bookmark.setChildren(element.getChildren()); return bookmark; } public void setAutojoin(boolean autojoin) { - this.autojoin = autojoin; + if (autojoin) { + this.setAttribute("autojoin", "true"); + } else { + this.setAttribute("autojoin", "false"); + } } public void setName(String name) { @@ -54,15 +43,18 @@ public class Bookmark implements ListItem { } public void setNick(String nick) { - this.nick = nick; + Element element = this.findChild("nick"); + if (element == null) { + element = this.addChild("nick"); + } + element.setContent(nick); } public void setPassword(String password) { - this.password = password; - } - - private void setProvidePassword(boolean providePassword) { - this.providePassword = providePassword; + Element element = this.findChild("password"); + if (element != null) { + element.setContent(password); + } } @Override @@ -76,32 +68,45 @@ public class Bookmark implements ListItem { if (this.mJoinedConversation != null && (this.mJoinedConversation.getMucOptions().getSubject() != null)) { return this.mJoinedConversation.getMucOptions().getSubject(); - } else if (name != null) { - return name; + } else if (getName() != null) { + return getName(); } else { - return this.jid.split("@")[0]; + return this.getJid().split("@")[0]; } } @Override public String getJid() { - return this.jid.toLowerCase(Locale.US); + String jid = this.getAttribute("jid"); + if (jid != null) { + return jid.toLowerCase(Locale.US); + } else { + return null; + } } public String getNick() { - return this.nick; + Element nick = this.findChild("nick"); + if (nick != null) { + return nick.getContent(); + } else { + return null; + } } public boolean autojoin() { - return autojoin; + String autojoin = this.getAttribute("autojoin"); + return (autojoin != null && (autojoin.equalsIgnoreCase("true") || autojoin + .equalsIgnoreCase("1"))); } public String getPassword() { - return this.password; - } - - public boolean isProvidePassword() { - return this.providePassword; + Element password = this.findChild("password"); + if (password != null) { + return password.getContent(); + } else { + return null; + } } public boolean match(String needle) { @@ -131,27 +136,7 @@ public class Bookmark implements ListItem { } public String getName() { - return name; - } - - public Element toElement() { - Element element = new Element("conference"); - element.setAttribute("jid", this.getJid()); - if (this.getName() != null) { - element.setAttribute("name", this.getName()); - } - if (this.autojoin) { - element.setAttribute("autojoin", "true"); - } else { - element.setAttribute("autojoin", "false"); - } - if (this.nick != null) { - element.addChild("nick").setContent(this.nick); - } - if (this.password != null && isProvidePassword()) { - element.addChild("password").setContent(this.password); - } - return element; + return this.getAttribute("name"); } public void unregisterConversation() { diff --git a/src/eu/siacs/conversations/entities/Contact.java b/src/eu/siacs/conversations/entities/Contact.java index dfd6c059..b1ebe662 100644 --- a/src/eu/siacs/conversations/entities/Contact.java +++ b/src/eu/siacs/conversations/entities/Contact.java @@ -156,6 +156,7 @@ public class Contact implements ListItem { public void clearPresences() { this.presences.clearPresences(); + this.resetOption(Options.PENDING_SUBSCRIPTION_REQUEST); } public int getMostAvailableStatus() { diff --git a/src/eu/siacs/conversations/entities/Conversation.java b/src/eu/siacs/conversations/entities/Conversation.java index 8395449d..b4c99dc1 100644 --- a/src/eu/siacs/conversations/entities/Conversation.java +++ b/src/eu/siacs/conversations/entities/Conversation.java @@ -4,6 +4,9 @@ import java.security.interfaces.DSAPublicKey; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.json.JSONException; +import org.json.JSONObject; + import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.UIHelper; @@ -36,6 +39,11 @@ public class Conversation extends AbstractEntity { public static final String STATUS = "status"; public static final String CREATED = "created"; public static final String MODE = "mode"; + public static final String ATTRIBUTES = "attributes"; + + public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption"; + public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password"; + public static final String ATTRIBUTE_MUTED_TILL = "muted_till"; private String name; private String contactUuid; @@ -45,7 +53,7 @@ public class Conversation extends AbstractEntity { private long created; private int mode; - private long mutedTill = 0; + private JSONObject attributes = new JSONObject(); private String nextPresence; @@ -56,12 +64,11 @@ public class Conversation extends AbstractEntity { private transient String otrFingerprint = null; - private int nextMessageEncryption = -1; private String nextMessage; private transient MucOptions mucOptions = null; - private transient String latestMarkableMessageId; + //private transient String latestMarkableMessageId; private byte[] symmetricKey; @@ -73,13 +80,13 @@ public class Conversation extends AbstractEntity { int mode) { this(java.util.UUID.randomUUID().toString(), name, null, account .getUuid(), contactJid, System.currentTimeMillis(), - STATUS_AVAILABLE, mode); + STATUS_AVAILABLE, mode, ""); this.account = account; } public Conversation(String uuid, String name, String contactUuid, String accountUuid, String contactJid, long created, int status, - int mode) { + int mode, String attributes) { this.uuid = uuid; this.name = name; this.contactUuid = contactUuid; @@ -88,6 +95,14 @@ public class Conversation extends AbstractEntity { this.created = created; this.status = status; this.mode = mode; + try { + if (attributes == null) { + attributes = new String(); + } + this.attributes = new JSONObject(attributes); + } catch (JSONException e) { + this.attributes = new JSONObject(); + } } public List<Message> getMessages() { @@ -123,10 +138,20 @@ public class Conversation extends AbstractEntity { } } - public String popLatestMarkableMessageId() { - String id = this.latestMarkableMessageId; - this.latestMarkableMessageId = null; - return id; + public String getLatestMarkableMessageId() { + if (this.messages == null) { + return null; + } + for(int i = this.messages.size() - 1; i >= 0; --i) { + if (this.messages.get(i).getStatus() <= Message.STATUS_RECEIVED && this.messages.get(i).markable) { + if (this.messages.get(i).isRead()) { + return null; + } else { + return this.messages.get(i).getRemoteMsgId(); + } + } + } + return null; } public Message getLatestMessage() { @@ -198,6 +223,7 @@ public class Conversation extends AbstractEntity { values.put(CREATED, created); values.put(STATUS, status); values.put(MODE, mode); + values.put(ATTRIBUTES, attributes.toString()); return values; } @@ -209,7 +235,8 @@ public class Conversation extends AbstractEntity { cursor.getString(cursor.getColumnIndex(CONTACTJID)), cursor.getLong(cursor.getColumnIndex(CREATED)), cursor.getInt(cursor.getColumnIndex(STATUS)), - cursor.getInt(cursor.getColumnIndex(MODE))); + cursor.getInt(cursor.getColumnIndex(MODE)), + cursor.getString(cursor.getColumnIndex(ATTRIBUTES))); } public void setStatus(int status) { @@ -229,8 +256,8 @@ public class Conversation extends AbstractEntity { if (this.otrSession != null) { return this.otrSession; } else { - SessionID sessionId = new SessionID( - this.getContactJid().split("/",2)[0], presence, "xmpp"); + SessionID sessionId = new SessionID(this.getContactJid().split("/", + 2)[0], presence, "xmpp"); this.otrSession = new SessionImpl(sessionId, getAccount() .getOtrEngine(service)); try { @@ -345,7 +372,8 @@ public class Conversation extends AbstractEntity { } public int getNextEncryption(boolean force) { - if (this.nextMessageEncryption == -1) { + int next = this.getIntAttribute(ATTRIBUTE_NEXT_ENCRYPTION, -1); + if (next == -1) { int latest = this.getLatestEncryption(); if (latest == Message.ENCRYPTION_NONE) { if (force && getMode() == MODE_SINGLE) { @@ -363,16 +391,16 @@ public class Conversation extends AbstractEntity { return latest; } } - if (this.nextMessageEncryption == Message.ENCRYPTION_NONE && force + if (next == Message.ENCRYPTION_NONE && force && getMode() == MODE_SINGLE) { return Message.ENCRYPTION_OTR; } else { - return this.nextMessageEncryption; + return next; } } public void setNextEncryption(int encryption) { - this.nextMessageEncryption = encryption; + this.setAttribute(ATTRIBUTE_NEXT_ENCRYPTION, String.valueOf(encryption)); } public String getNextMessage() { @@ -387,12 +415,6 @@ public class Conversation extends AbstractEntity { this.nextMessage = message; } - public void setLatestMarkableMessageId(String id) { - if (id != null) { - this.latestMarkableMessageId = id; - } - } - public void setSymmetricKey(byte[] key) { this.symmetricKey = key; } @@ -433,11 +455,55 @@ public class Conversation extends AbstractEntity { return false; } - public void setMutedTill(long mutedTill) { - this.mutedTill = mutedTill; + public void setMutedTill(long value) { + this.setAttribute(ATTRIBUTE_MUTED_TILL, String.valueOf(value)); } public boolean isMuted() { - return SystemClock.elapsedRealtime() < this.mutedTill; + return SystemClock.elapsedRealtime() < this.getLongAttribute( + ATTRIBUTE_MUTED_TILL, 0); + } + + public boolean setAttribute(String key, String value) { + try { + this.attributes.put(key, value); + return true; + } catch (JSONException e) { + return false; + } + } + + public String getAttribute(String key) { + try { + return this.attributes.getString(key); + } catch (JSONException e) { + return null; + } + } + + public int getIntAttribute(String key, int defaultValue) { + String value = this.getAttribute(key); + if (value == null) { + return defaultValue; + } else { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + return defaultValue; + } + } + } + + public long getLongAttribute(String key, long defaultValue) { + String value = this.getAttribute(key); + if (value == null) { + return defaultValue; + } else { + try { + return Long.parseLong(value); + } catch (NumberFormatException e) { + return defaultValue; + } + } } } diff --git a/src/eu/siacs/conversations/entities/Message.java b/src/eu/siacs/conversations/entities/Message.java index 8f9885c5..49482bbc 100644 --- a/src/eu/siacs/conversations/entities/Message.java +++ b/src/eu/siacs/conversations/entities/Message.java @@ -57,9 +57,9 @@ public class Message extends AbstractEntity { protected boolean read = true; protected String remoteMsgId = null; - protected transient Conversation conversation = null; - - protected transient Downloadable downloadable = null; + protected Conversation conversation = null; + protected Downloadable downloadable = null; + public boolean markable = false; private Message() { diff --git a/src/eu/siacs/conversations/entities/MucOptions.java b/src/eu/siacs/conversations/entities/MucOptions.java index 676fb4f4..12ea4e93 100644 --- a/src/eu/siacs/conversations/entities/MucOptions.java +++ b/src/eu/siacs/conversations/entities/MucOptions.java @@ -15,6 +15,13 @@ public class MucOptions { public static final int ERROR_NICK_IN_USE = 1; public static final int ERROR_ROOM_NOT_FOUND = 2; public static final int ERROR_PASSWORD_REQUIRED = 3; + public static final int ERROR_BANNED = 4; + public static final int ERROR_MEMBERS_ONLY = 5; + + public static final int KICKED_FROM_ROOM = 9; + + public static final String STATUS_CODE_BANNED = "301"; + public static final String STATUS_CODE_KICKED = "307"; public interface OnRenameListener { public void onRename(boolean success); @@ -108,7 +115,6 @@ public class MucOptions { private String subject = null; private String joinnick; private String password = null; - private boolean passwordChanged = false; public MucOptions(Account account) { this.account = account; @@ -134,7 +140,7 @@ public class MucOptions { } public void processPacket(PresencePacket packet, PgpEngine pgp) { - String[] fromParts = packet.getFrom().split("/",2); + String[] fromParts = packet.getFrom().split("/", 2); if (fromParts.length >= 2) { String name = fromParts[1]; String type = packet.getAttribute("type"); @@ -158,10 +164,6 @@ public class MucOptions { } aboutToRename = false; } - if (conversation.getBookmark() != null - && conversation.getBookmark().isProvidePassword()) { - this.passwordChanged = false; - } } else { addUser(user); } @@ -179,11 +181,27 @@ public class MucOptions { x.getContent())); } } + } else if (type.equals("unavailable") && name.equals(this.joinnick)) { + Element x = packet.findChild("x", + "http://jabber.org/protocol/muc#user"); + if (x != null) { + Element status = x.findChild("status"); + if (status != null) { + String code = status.getAttribute("code"); + if (STATUS_CODE_KICKED.equals(code)) { + this.isOnline = false; + this.error = KICKED_FROM_ROOM; + } else if (STATUS_CODE_BANNED.equals(code)) { + this.isOnline = false; + this.error = ERROR_BANNED; + } + } + } } else if (type.equals("unavailable")) { - deleteUser(packet.getAttribute("from").split("/",2)[1]); + deleteUser(packet.getAttribute("from").split("/", 2)[1]); } else if (type.equals("error")) { Element error = packet.findChild("error"); - if (error.hasChild("conflict")) { + if (error != null && error.hasChild("conflict")) { if (aboutToRename) { if (renameListener != null) { renameListener.onRename(false); @@ -193,12 +211,13 @@ public class MucOptions { } else { this.error = ERROR_NICK_IN_USE; } - } else if (error.hasChild("not-authorized")) { - if (conversation.getBookmark() != null - && conversation.getBookmark().isProvidePassword()) { - this.passwordChanged = true; - } + } else if (error != null && error.hasChild("not-authorized")) { this.error = ERROR_PASSWORD_REQUIRED; + } else if (error != null && error.hasChild("forbidden")) { + this.error = ERROR_BANNED; + } else if (error != null + && error.hasChild("registration-required")) { + this.error = ERROR_MEMBERS_ONLY; } } } @@ -209,7 +228,7 @@ public class MucOptions { } public String getProposedNick() { - String[] mucParts = conversation.getContactJid().split("/",2); + String[] mucParts = conversation.getContactJid().split("/", 2); if (conversation.getBookmark() != null && conversation.getBookmark().getNick() != null) { return conversation.getBookmark().getNick(); @@ -309,7 +328,7 @@ public class MucOptions { } public String getJoinJid() { - return this.conversation.getContactJid().split("/",2)[0] + "/" + return this.conversation.getContactJid().split("/", 2)[0] + "/" + this.joinnick; } @@ -323,7 +342,9 @@ public class MucOptions { } public String getPassword() { - if (conversation.getBookmark() != null + this.password = conversation + .getAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD); + if (this.password == null && conversation.getBookmark() != null && conversation.getBookmark().getPassword() != null) { return conversation.getBookmark().getPassword(); } else { @@ -332,16 +353,12 @@ public class MucOptions { } public void setPassword(String password) { - if (conversation.getBookmark() != null - && conversation.getBookmark().isProvidePassword()) { + if (conversation.getBookmark() != null) { conversation.getBookmark().setPassword(password); } else { this.password = password; } + conversation + .setAttribute(Conversation.ATTRIBUTE_MUC_PASSWORD, password); } - - public boolean isPasswordChanged() { - return this.passwordChanged; - } - }
\ No newline at end of file diff --git a/src/eu/siacs/conversations/entities/Roster.java b/src/eu/siacs/conversations/entities/Roster.java index f11f0250..b6908793 100644 --- a/src/eu/siacs/conversations/entities/Roster.java +++ b/src/eu/siacs/conversations/entities/Roster.java @@ -14,13 +14,18 @@ public class Roster { this.account = account; } - public boolean hasContact(String jid) { - String cleanJid = jid.split("/",2)[0]; - return contacts.containsKey(cleanJid); + public Contact getContactAsShownInRoster(String jid) { + String cleanJid = jid.split("/", 2)[0]; + Contact contact = contacts.get(cleanJid); + if (contact != null && contact.showInRoster()) { + return contact; + } else { + return null; + } } public Contact getContact(String jid) { - String cleanJid = jid.split("/",2)[0].toLowerCase(Locale.getDefault()); + String cleanJid = jid.split("/", 2)[0].toLowerCase(Locale.getDefault()); if (contacts.containsKey(cleanJid)) { return contacts.get(cleanJid); } else { |