do not cross reference bookmarks and conversations

This commit is contained in:
Daniel Gultsch 2018-02-10 19:06:31 +01:00
parent 2eb2513615
commit e2ac1db225
8 changed files with 38 additions and 50 deletions

View file

@ -610,18 +610,21 @@ public class Account extends AbstractEntity {
return this.bookmarks; return this.bookmarks;
} }
public void setBookmarks(final List<Bookmark> bookmarks) { public void setBookmarks(final CopyOnWriteArrayList<Bookmark> bookmarks) {
this.bookmarks = bookmarks; this.bookmarks = bookmarks;
} }
public boolean hasBookmarkFor(final Jid conferenceJid) { public boolean hasBookmarkFor(final Jid conferenceJid) {
for (final Bookmark bookmark : this.bookmarks) { return getBookmark(conferenceJid) != null;
final Jid jid = bookmark.getJid(); }
if (jid != null && jid.equals(conferenceJid.toBareJid())) {
return true; public Bookmark getBookmark(final Jid jid) {
for(final Bookmark bookmark : this.bookmarks) {
if (bookmark.getJid() != null && jid.toBareJid().equals(bookmark.getJid().toBareJid())) {
return bookmark;
} }
} }
return false; return null;
} }
public boolean setAvatar(final String filename) { public boolean setAvatar(final String filename) {

View file

@ -2,6 +2,7 @@ package eu.siacs.conversations.entities;
import android.content.Context; import android.content.Context;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -13,7 +14,7 @@ import eu.siacs.conversations.xmpp.jid.Jid;
public class Bookmark extends Element implements ListItem { public class Bookmark extends Element implements ListItem {
private Account account; private Account account;
private Conversation mJoinedConversation; private WeakReference<Conversation> conversation;
public Bookmark(final Account account, final Jid jid) { public Bookmark(final Account account, final Jid jid) {
super("conference"); super("conference");
@ -49,8 +50,9 @@ public class Bookmark extends Element implements ListItem {
@Override @Override
public String getDisplayName() { public String getDisplayName() {
if (this.mJoinedConversation != null) { final Conversation c = getConversation();
return this.mJoinedConversation.getName(); if (c != null) {
return c.getName();
} else if (getBookmarkName() != null } else if (getBookmarkName() != null
&& !getBookmarkName().trim().isEmpty()) { && !getBookmarkName().trim().isEmpty()) {
return getBookmarkName().trim(); return getBookmarkName().trim();
@ -141,12 +143,15 @@ public class Bookmark extends Element implements ListItem {
return this.account; return this.account;
} }
public Conversation getConversation() { public synchronized Conversation getConversation() {
return this.mJoinedConversation; return this.conversation != null ? this.conversation.get() : null;
} }
public void setConversation(Conversation conversation) { public synchronized void setConversation(Conversation conversation) {
this.mJoinedConversation = conversation; if (this.conversation != null) {
this.conversation.clear();
}
this.conversation = new WeakReference<>(conversation);
} }
public String getBookmarkName() { public String getBookmarkName() {
@ -162,11 +167,4 @@ public class Bookmark extends Element implements ListItem {
return false; return false;
} }
} }
public void unregisterConversation() {
if (this.mJoinedConversation != null) {
this.mJoinedConversation.deregisterWithBookmark();
}
this.mJoinedConversation = null;
}
} }

View file

@ -86,8 +86,6 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
private byte[] symmetricKey; private byte[] symmetricKey;
private Bookmark bookmark;
private boolean messagesLeftOnServer = true; private boolean messagesLeftOnServer = true;
private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE; private ChatState mOutgoingChatState = Config.DEFAULT_CHATSTATE;
private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE; private ChatState mIncomingChatState = Config.DEFAULT_CHATSTATE;
@ -494,6 +492,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
public String getName() { public String getName() {
if (getMode() == MODE_MULTI) { if (getMode() == MODE_MULTI) {
final String subject = getMucOptions().getSubject(); final String subject = getMucOptions().getSubject();
Bookmark bookmark = getBookmark();
final String bookmarkName = bookmark != null ? bookmark.getBookmarkName() : null; final String bookmarkName = bookmark != null ? bookmark.getBookmarkName() : null;
if (subject != null && !subject.trim().isEmpty()) { if (subject != null && !subject.trim().isEmpty()) {
return subject; return subject;
@ -790,20 +789,8 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
return this.symmetricKey; return this.symmetricKey;
} }
public void setBookmark(Bookmark bookmark) {
this.bookmark = bookmark;
this.bookmark.setConversation(this);
}
public void deregisterWithBookmark() {
if (this.bookmark != null) {
this.bookmark.setConversation(null);
}
this.bookmark = null;
}
public Bookmark getBookmark() { public Bookmark getBookmark() {
return this.bookmark; return this.account.getBookmark(this.contactJid);
} }
public Message findDuplicateMessage(Message message) { public Message findDuplicateMessage(Message message) {

View file

@ -54,7 +54,7 @@ public class MucOptions {
} }
public boolean isSelf(Jid counterpart) { public boolean isSelf(Jid counterpart) {
return counterpart.getResourcepart().equals(getActualNick()); return counterpart.equals(self.getFullJid());
} }
public void resetChatState() { public void resetChatState() {

View file

@ -69,15 +69,13 @@ public class PresenceParser extends AbstractParser implements
if (item != null && !from.isBareJid()) { if (item != null && !from.isBareJid()) {
mucOptions.setError(MucOptions.Error.NONE); mucOptions.setError(MucOptions.Error.NONE);
MucOptions.User user = parseItem(conversation, item, from); MucOptions.User user = parseItem(conversation, item, from);
if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) if (codes.contains(MucOptions.STATUS_CODE_SELF_PRESENCE) || (codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED) && jid.equals(item.getAttributeAsJid("jid")))) {
|| ((codes.isEmpty() || codes.contains(MucOptions.STATUS_CODE_ROOM_CREATED)) && jid.equals(item.getAttributeAsJid("jid")))) {
if (mucOptions.setOnline()) { if (mucOptions.setOnline()) {
mXmppConnectionService.getAvatarService().clear(mucOptions); mXmppConnectionService.getAvatarService().clear(mucOptions);
} }
mucOptions.setSelf(user); mucOptions.setSelf(user);
mXmppConnectionService.persistSelfNick(user); mXmppConnectionService.persistSelfNick(user);
invokeRenameListener(mucOptions, true); invokeRenameListener(mucOptions, true);
} }
boolean isNew = mucOptions.updateUser(user); boolean isNew = mucOptions.updateUser(user);

View file

@ -1424,15 +1424,15 @@ public class XmppConnectionService extends Service {
} }
Conversation conversation = find(bookmark); Conversation conversation = find(bookmark);
if (conversation != null) { if (conversation != null) {
conversation.setBookmark(bookmark); bookmark.setConversation(conversation);
} else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) { } else if (bookmark.autojoin() && bookmark.getJid() != null && autojoin) {
conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false); conversation = findOrCreateConversation(account, bookmark.getJid(), true, true, false);
conversation.setBookmark(bookmark); bookmark.setConversation(conversation);
} }
} }
} }
} }
account.setBookmarks(new ArrayList<>(bookmarks.values())); account.setBookmarks(new CopyOnWriteArrayList<>(bookmarks.values()));
} else { } else {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fetch bookmarks"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": could not fetch bookmarks");
} }
@ -2478,7 +2478,10 @@ public class XmppConnectionService extends Service {
if (account.getStatus() == Account.State.ONLINE || now) { if (account.getStatus() == Account.State.ONLINE || now) {
sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions())); sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
conversation.getMucOptions().setOffline(); conversation.getMucOptions().setOffline();
conversation.deregisterWithBookmark(); Bookmark bookmark = conversation.getBookmark();
if (bookmark != null) {
bookmark.setConversation(null);
}
Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": leaving muc " + conversation.getJid()); Log.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + ": leaving muc " + conversation.getJid());
} else { } else {
account.pendingConferenceLeaves.add(conversation); account.pendingConferenceLeaves.add(conversation);
@ -3948,7 +3951,7 @@ public class XmppConnectionService extends Service {
bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))); bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
account.getBookmarks().add(bookmark); account.getBookmarks().add(bookmark);
pushBookmarks(account); pushBookmarks(account);
conversation.setBookmark(bookmark); bookmark.setConversation(conversation);
} }
public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) { public boolean verifyFingerprints(Contact contact, List<XmppUri.Fingerprint> fingerprints) {

View file

@ -347,8 +347,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
if (mConversation == null) { if (mConversation == null) {
return true; return true;
} }
Account account = mConversation.getAccount(); if (mConversation.getBookmark() != null) {
if (account.hasBookmarkFor(mConversation.getJid().toBareJid())) {
menuItemSaveBookmark.setVisible(false); menuItemSaveBookmark.setVisible(false);
menuItemDeleteBookmark.setVisible(true); menuItemDeleteBookmark.setVisible(true);
} else { } else {
@ -515,8 +514,8 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
protected void deleteBookmark() { protected void deleteBookmark() {
Account account = mConversation.getAccount(); Account account = mConversation.getAccount();
Bookmark bookmark = mConversation.getBookmark(); Bookmark bookmark = mConversation.getBookmark();
mConversation.deregisterWithBookmark();
account.getBookmarks().remove(bookmark); account.getBookmarks().remove(bookmark);
bookmark.setConversation(null);
xmppConnectionService.pushBookmarks(account); xmppConnectionService.pushBookmarks(account);
updateView(); updateView();
} }

View file

@ -342,7 +342,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
return; return;
} }
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true, true, true); Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true, true, true);
conversation.setBookmark(bookmark); bookmark.setConversation(conversation);
if (!bookmark.autojoin() && getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))) { if (!bookmark.autojoin() && getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))) {
bookmark.setAutojoin(true); bookmark.setAutojoin(true);
xmppConnectionService.pushBookmarks(bookmark.getAccount()); xmppConnectionService.pushBookmarks(bookmark.getAccount());
@ -393,7 +393,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
bookmark.unregisterConversation(); bookmark.setConversation(null);
Account account = bookmark.getAccount(); Account account = bookmark.getAccount();
account.getBookmarks().remove(bookmark); account.getBookmarks().remove(bookmark);
xmppConnectionService.pushBookmarks(account); xmppConnectionService.pushBookmarks(account);
@ -498,7 +498,7 @@ public class StartConversationActivity extends XmppActivity implements OnRosterU
xmppConnectionService.pushBookmarks(account); xmppConnectionService.pushBookmarks(account);
final Conversation conversation = xmppConnectionService final Conversation conversation = xmppConnectionService
.findOrCreateConversation(account, conferenceJid, true, true, true); .findOrCreateConversation(account, conferenceJid, true, true, true);
conversation.setBookmark(bookmark); bookmark.setConversation(conversation);
dialog.dismiss(); dialog.dismiss();
mCurrentDialog = null; mCurrentDialog = null;
switchToConversation(conversation); switchToConversation(conversation);