Bookmark.java

  1package eu.siacs.conversations.entities;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5import java.util.Locale;
  6
  7import eu.siacs.conversations.utils.UIHelper;
  8import eu.siacs.conversations.xml.Element;
  9import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 10import eu.siacs.conversations.xmpp.jid.Jid;
 11
 12public class Bookmark extends Element implements ListItem {
 13
 14	private Account account;
 15	private Conversation mJoinedConversation;
 16
 17	public Bookmark(final Account account, final Jid jid) {
 18		super("conference");
 19		this.setAttribute("jid", jid.toString());
 20		this.account = account;
 21	}
 22
 23	private Bookmark(Account account) {
 24		super("conference");
 25		this.account = account;
 26	}
 27
 28	public static Bookmark parse(Element element, Account account) {
 29		Bookmark bookmark = new Bookmark(account);
 30		bookmark.setAttributes(element.getAttributes());
 31		bookmark.setChildren(element.getChildren());
 32		return bookmark;
 33	}
 34
 35	public void setAutojoin(boolean autojoin) {
 36		if (autojoin) {
 37			this.setAttribute("autojoin", "true");
 38		} else {
 39			this.setAttribute("autojoin", "false");
 40		}
 41	}
 42
 43	@Override
 44	public int compareTo(final ListItem another) {
 45		return this.getDisplayName().compareToIgnoreCase(
 46				another.getDisplayName());
 47	}
 48
 49	@Override
 50	public String getDisplayName() {
 51		if (this.mJoinedConversation != null
 52				&& (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
 53			return this.mJoinedConversation.getMucOptions().getSubject();
 54		} else if (getName() != null) {
 55			return getName();
 56		} else {
 57			return this.getJid().getLocalpart();
 58		}
 59	}
 60
 61	@Override
 62	public Jid getJid() {
 63		return this.getAttributeAsJid("jid");
 64	}
 65
 66	@Override
 67	public List<Tag> getTags() {
 68		ArrayList<Tag> tags = new ArrayList<Tag>();
 69		for (Element element : getChildren()) {
 70			if (element.getName().equals("group") && element.getContent() != null) {
 71				String group = element.getContent();
 72				tags.add(new Tag(group, UIHelper.getColorForName(group)));
 73			}
 74		}
 75		return tags;
 76	}
 77
 78	public String getNick() {
 79		Element nick = this.findChild("nick");
 80		if (nick != null) {
 81			return nick.getContent();
 82		} else {
 83			return null;
 84		}
 85	}
 86
 87	public void setNick(String nick) {
 88		Element element = this.findChild("nick");
 89		if (element == null) {
 90			element = this.addChild("nick");
 91		}
 92		element.setContent(nick);
 93	}
 94
 95	public boolean autojoin() {
 96		return this.getAttributeAsBoolean("autojoin");
 97	}
 98
 99	public String getPassword() {
100		Element password = this.findChild("password");
101		if (password != null) {
102			return password.getContent();
103		} else {
104			return null;
105		}
106	}
107
108	public void setPassword(String password) {
109		Element element = this.findChild("password");
110		if (element != null) {
111			element.setContent(password);
112		}
113	}
114
115	public boolean match(String needle) {
116		if (needle == null) {
117			return true;
118		}
119		needle = needle.toLowerCase(Locale.US);
120		final Jid jid = getJid();
121		return (jid != null && jid.toString().contains(needle)) ||
122			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
123			matchInTag(needle);
124	}
125
126	private boolean matchInTag(String needle) {
127		needle = needle.toLowerCase(Locale.US);
128		for (Tag tag : getTags()) {
129			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
130				return true;
131			}
132		}
133		return false;
134	}
135
136	public Account getAccount() {
137		return this.account;
138	}
139
140	public Conversation getConversation() {
141		return this.mJoinedConversation;
142	}
143
144	public void setConversation(Conversation conversation) {
145		this.mJoinedConversation = conversation;
146	}
147
148	public String getName() {
149		return this.getAttribute("name");
150	}
151
152	public void setName(String name) {
153		this.name = name;
154	}
155
156	public void unregisterConversation() {
157		if (this.mJoinedConversation != null) {
158			this.mJoinedConversation.deregisterWithBookmark();
159		}
160	}
161}