Conversations/src/main/java/eu/siacs/conversations/entities/Bookmark.java

172 lines
4.0 KiB
Java
Raw Normal View History

2014-10-22 12:38:44 -04:00
package eu.siacs.conversations.entities;
import android.content.Context;
2014-11-16 11:21:21 -05:00
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
2014-10-22 12:38:44 -04:00
2014-11-16 17:58:30 -05:00
import eu.siacs.conversations.utils.UIHelper;
2014-10-22 12:38:44 -04:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.jid.Jid;
2014-10-22 12:38:44 -04:00
public class Bookmark extends Element implements ListItem {
private Account account;
private Conversation mJoinedConversation;
public Bookmark(final Account account, final Jid jid) {
2014-10-22 12:38:44 -04:00
super("conference");
this.setAttribute("jid", jid.toString());
2014-10-22 12:38:44 -04:00
this.account = account;
}
private Bookmark(Account account) {
super("conference");
this.account = account;
}
public static Bookmark parse(Element element, Account account) {
Bookmark bookmark = new Bookmark(account);
bookmark.setAttributes(element.getAttributes());
bookmark.setChildren(element.getChildren());
return bookmark;
}
public void setAutojoin(boolean autojoin) {
if (autojoin) {
this.setAttribute("autojoin", "true");
} else {
this.setAttribute("autojoin", "false");
}
}
@Override
public int compareTo(final ListItem another) {
2014-11-16 17:58:30 -05:00
return this.getDisplayName().compareToIgnoreCase(
another.getDisplayName());
}
2014-10-22 12:38:44 -04:00
@Override
public String getDisplayName() {
2016-05-26 16:53:55 -04:00
if (this.mJoinedConversation != null) {
return this.mJoinedConversation.getName();
} else if (getBookmarkName() != null
&& !getBookmarkName().trim().isEmpty()) {
return getBookmarkName().trim();
2014-10-22 12:38:44 -04:00
} else {
2016-06-16 14:38:35 -04:00
Jid jid = this.getJid();
String name = jid != null ? jid.getLocalpart() : getAttribute("jid");
return name != null ? name : "";
2014-10-22 12:38:44 -04:00
}
}
@Override
public String getDisplayJid() {
Jid jid = getJid();
2016-04-27 10:43:02 -04:00
if (jid != null) {
return jid.toString();
} else {
2016-06-16 14:38:35 -04:00
return getAttribute("jid"); //fallback if jid wasn't parsable
}
}
2014-10-22 12:38:44 -04:00
@Override
public Jid getJid() {
return this.getAttributeAsJid("jid");
2014-10-22 12:38:44 -04:00
}
2014-11-16 11:21:21 -05:00
@Override
public List<Tag> getTags(Context context) {
2016-06-16 14:38:35 -04:00
ArrayList<Tag> tags = new ArrayList<>();
2014-11-16 17:58:30 -05:00
for (Element element : getChildren()) {
if (element.getName().equals("group") && element.getContent() != null) {
String group = element.getContent();
tags.add(new Tag(group, UIHelper.getColorForName(group)));
}
}
return tags;
2014-11-16 11:21:21 -05:00
}
2014-10-22 12:38:44 -04:00
public String getNick() {
return this.findChildContent("nick");
2014-10-22 12:38:44 -04:00
}
2014-11-16 17:58:30 -05:00
public void setNick(String nick) {
Element element = this.findChild("nick");
if (element == null) {
element = this.addChild("nick");
}
element.setContent(nick);
}
2014-10-22 12:38:44 -04:00
public boolean autojoin() {
2014-12-04 19:54:16 -05:00
return this.getAttributeAsBoolean("autojoin");
2014-10-22 12:38:44 -04:00
}
public String getPassword() {
return this.findChildContent("password");
2014-10-22 12:38:44 -04:00
}
2014-11-16 17:58:30 -05:00
public void setPassword(String password) {
Element element = this.findChild("password");
if (element != null) {
element.setContent(password);
}
}
@Override
public boolean match(Context context, String needle) {
2014-11-16 17:58:30 -05:00
if (needle == null) {
return true;
}
needle = needle.toLowerCase(Locale.US);
2014-12-03 04:45:47 -05:00
final Jid jid = getJid();
return (jid != null && jid.toString().contains(needle)) ||
getDisplayName().toLowerCase(Locale.US).contains(needle) ||
matchInTag(context, needle);
2014-10-22 12:38:44 -04:00
}
private boolean matchInTag(Context context, String needle) {
needle = needle.toLowerCase(Locale.US);
for (Tag tag : getTags(context)) {
if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
2014-11-16 17:58:30 -05:00
return true;
}
}
return false;
2014-10-22 12:38:44 -04:00
}
2014-11-16 17:58:30 -05:00
public Account getAccount() {
return this.account;
2014-10-22 12:38:44 -04:00
}
public Conversation getConversation() {
return this.mJoinedConversation;
}
2014-11-16 17:58:30 -05:00
public void setConversation(Conversation conversation) {
this.mJoinedConversation = conversation;
}
public String getBookmarkName() {
2014-10-22 12:38:44 -04:00
return this.getAttribute("name");
}
public boolean setBookmarkName(String name) {
String before = getBookmarkName();
if (name != null && !name.equals(before)) {
this.setAttribute("name", name);
return true;
} else {
return false;
}
}
2014-10-22 12:38:44 -04:00
public void unregisterConversation() {
if (this.mJoinedConversation != null) {
this.mJoinedConversation.deregisterWithBookmark();
}
}
}