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

138 lines
3.0 KiB
Java
Raw Normal View History

2014-07-14 11:47:42 +02:00
package eu.siacs.conversations.entities;
import java.util.Locale;
import eu.siacs.conversations.xml.Element;
public class Bookmark extends Element implements ListItem {
2014-08-31 16:28:21 +02:00
2014-07-14 11:47:42 +02:00
private Account account;
private Conversation mJoinedConversation;
2014-08-31 16:28:21 +02:00
2014-07-15 14:32:19 +02:00
public Bookmark(Account account, String jid) {
super("conference");
this.setAttribute("jid", jid);
this.account = account;
}
private Bookmark(Account account) {
super("conference");
2014-07-14 11:47:42 +02:00
this.account = account;
}
public static Bookmark parse(Element element, Account account) {
Bookmark bookmark = new Bookmark(account);
bookmark.setAttributes(element.getAttributes());
bookmark.setChildren(element.getChildren());
2014-07-14 11:47:42 +02:00
return bookmark;
}
public void setAutojoin(boolean autojoin) {
if (autojoin) {
this.setAttribute("autojoin", "true");
} else {
this.setAttribute("autojoin", "false");
}
2014-07-14 11:47:42 +02:00
}
2014-08-31 16:28:21 +02:00
public void setName(String name) {
this.name = name;
2014-07-14 11:47:42 +02:00
}
2014-08-31 16:28:21 +02:00
2014-07-14 11:47:42 +02:00
public void setNick(String nick) {
Element element = this.findChild("nick");
if (element == null) {
element = this.addChild("nick");
}
element.setContent(nick);
2014-07-14 11:47:42 +02:00
}
2014-09-07 14:06:23 +02:00
public void setPassword(String password) {
Element element = this.findChild("password");
if (element != null) {
element.setContent(password);
}
2014-09-07 14:06:23 +02:00
}
2014-07-14 11:47:42 +02:00
@Override
public int compareTo(ListItem another) {
2014-08-31 16:28:21 +02:00
return this.getDisplayName().compareToIgnoreCase(
another.getDisplayName());
2014-07-14 11:47:42 +02:00
}
@Override
public String getDisplayName() {
2014-08-31 16:28:21 +02:00
if (this.mJoinedConversation != null
&& (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
2014-07-15 14:32:19 +02:00
return this.mJoinedConversation.getMucOptions().getSubject();
} else if (getName() != null) {
return getName();
2014-07-14 11:47:42 +02:00
} else {
return this.getJid().split("@")[0];
2014-07-14 11:47:42 +02:00
}
}
@Override
public String getJid() {
String jid = this.getAttribute("jid");
if (jid != null) {
return jid.toLowerCase(Locale.US);
} else {
return null;
}
2014-07-14 11:47:42 +02:00
}
2014-08-31 16:28:21 +02:00
2014-07-14 11:47:42 +02:00
public String getNick() {
Element nick = this.findChild("nick");
if (nick != null) {
return nick.getContent();
} else {
return null;
}
2014-07-14 11:47:42 +02:00
}
2014-08-31 16:28:21 +02:00
2014-07-14 11:47:42 +02:00
public boolean autojoin() {
String autojoin = this.getAttribute("autojoin");
return (autojoin != null && (autojoin.equalsIgnoreCase("true") || autojoin
.equalsIgnoreCase("1")));
2014-07-14 11:47:42 +02:00
}
2014-09-07 14:06:23 +02:00
public String getPassword() {
Element password = this.findChild("password");
if (password != null) {
return password.getContent();
} else {
return null;
}
2014-09-07 14:06:23 +02:00
}
2014-07-14 11:47:42 +02:00
public boolean match(String needle) {
return needle == null
|| getJid().contains(needle.toLowerCase(Locale.US))
2014-08-31 16:28:21 +02:00
|| getDisplayName().toLowerCase(Locale.US).contains(
needle.toLowerCase(Locale.US));
2014-07-14 11:47:42 +02:00
}
public Account getAccount() {
return this.account;
}
public void setConversation(Conversation conversation) {
this.mJoinedConversation = conversation;
}
public Conversation getConversation() {
return this.mJoinedConversation;
}
public String getName() {
return this.getAttribute("name");
2014-07-15 14:32:19 +02:00
}
public void unregisterConversation() {
if (this.mJoinedConversation != null) {
this.mJoinedConversation.deregisterWithBookmark();
}
}
2014-07-14 11:47:42 +02:00
}