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