Bookmark.java

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