2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2014-11-16 16:21:21 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-11-17 16:53:19 +00:00
|
|
|
import java.util.Locale;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2014-11-05 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public class Bookmark extends Element implements ListItem {
|
|
|
|
|
|
|
|
private Account account;
|
|
|
|
private Conversation mJoinedConversation;
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Bookmark(final Account account, final Jid jid) {
|
2014-10-22 16:38:44 +00:00
|
|
|
super("conference");
|
2014-11-05 20:55:47 +00:00
|
|
|
this.setAttribute("jid", jid.toString());
|
2014-10-22 16:38:44 +00: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
|
2014-11-05 20:55:47 +00:00
|
|
|
public int compareTo(final ListItem another) {
|
2014-11-16 22:58:30 +00:00
|
|
|
return this.getDisplayName().compareToIgnoreCase(
|
|
|
|
another.getDisplayName());
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
|
|
|
if (this.mJoinedConversation != null
|
|
|
|
&& (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
|
|
|
|
return this.mJoinedConversation.getMucOptions().getSubject();
|
|
|
|
} else if (getName() != null) {
|
|
|
|
return getName();
|
|
|
|
} else {
|
2014-11-05 20:55:47 +00:00
|
|
|
return this.getJid().getLocalpart();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-05 20:55:47 +00:00
|
|
|
public Jid getJid() {
|
2014-12-15 16:29:17 +00:00
|
|
|
return this.getAttributeAsJid("jid");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 16:21:21 +00:00
|
|
|
@Override
|
|
|
|
public List<Tag> getTags() {
|
2014-11-16 22:58:30 +00:00
|
|
|
ArrayList<Tag> tags = new ArrayList<Tag>();
|
|
|
|
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 16:21:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public String getNick() {
|
2015-05-14 12:42:21 +00:00
|
|
|
return this.findChildContent("nick");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setNick(String nick) {
|
|
|
|
Element element = this.findChild("nick");
|
|
|
|
if (element == null) {
|
|
|
|
element = this.addChild("nick");
|
|
|
|
}
|
|
|
|
element.setContent(nick);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public boolean autojoin() {
|
2014-12-05 00:54:16 +00:00
|
|
|
return this.getAttributeAsBoolean("autojoin");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
2015-05-14 12:42:21 +00:00
|
|
|
return this.findChildContent("password");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setPassword(String password) {
|
|
|
|
Element element = this.findChild("password");
|
|
|
|
if (element != null) {
|
|
|
|
element.setContent(password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public boolean match(String needle) {
|
2014-11-16 22:58:30 +00:00
|
|
|
if (needle == null) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-17 16:53:19 +00:00
|
|
|
needle = needle.toLowerCase(Locale.US);
|
2014-12-03 09:45:47 +00:00
|
|
|
final Jid jid = getJid();
|
|
|
|
return (jid != null && jid.toString().contains(needle)) ||
|
2014-11-17 16:53:19 +00:00
|
|
|
getDisplayName().toLowerCase(Locale.US).contains(needle) ||
|
|
|
|
matchInTag(needle);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
private boolean matchInTag(String needle) {
|
2014-11-17 16:53:19 +00:00
|
|
|
needle = needle.toLowerCase(Locale.US);
|
2014-11-16 22:58:30 +00:00
|
|
|
for (Tag tag : getTags()) {
|
2014-11-17 16:53:19 +00:00
|
|
|
if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
|
2014-11-16 22:58:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public Account getAccount() {
|
|
|
|
return this.account;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Conversation getConversation() {
|
|
|
|
return this.mJoinedConversation;
|
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setConversation(Conversation conversation) {
|
|
|
|
this.mJoinedConversation = conversation;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public String getName() {
|
|
|
|
return this.getAttribute("name");
|
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setName(String name) {
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public void unregisterConversation() {
|
|
|
|
if (this.mJoinedConversation != null) {
|
|
|
|
this.mJoinedConversation.deregisterWithBookmark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|