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				&& (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
 55			return this.mJoinedConversation.getMucOptions().getSubject();
 56		} else if (getBookmarkName() != null) {
 57			return getBookmarkName();
 58		} else {
 59			return this.getJid().getLocalpart();
 60		}
 61	}
 62
 63	@Override
 64	public String getDisplayJid() {
 65		Jid jid = getJid();
 66		if (jid != null) {
 67			return jid.toString();
 68		} else {
 69			return null;
 70		}
 71	}
 72
 73	@Override
 74	public Jid getJid() {
 75		return this.getAttributeAsJid("jid");
 76	}
 77
 78	@Override
 79	public List<Tag> getTags(Context context) {
 80		ArrayList<Tag> tags = new ArrayList<Tag>();
 81		for (Element element : getChildren()) {
 82			if (element.getName().equals("group") && element.getContent() != null) {
 83				String group = element.getContent();
 84				tags.add(new Tag(group, UIHelper.getColorForName(group)));
 85			}
 86		}
 87		return tags;
 88	}
 89
 90	public String getNick() {
 91		return this.findChildContent("nick");
 92	}
 93
 94	public void setNick(String nick) {
 95		Element element = this.findChild("nick");
 96		if (element == null) {
 97			element = this.addChild("nick");
 98		}
 99		element.setContent(nick);
100	}
101
102	public boolean autojoin() {
103		return this.getAttributeAsBoolean("autojoin");
104	}
105
106	public String getPassword() {
107		return this.findChildContent("password");
108	}
109
110	public void setPassword(String password) {
111		Element element = this.findChild("password");
112		if (element != null) {
113			element.setContent(password);
114		}
115	}
116
117	@Override
118	public boolean match(Context context, String needle) {
119		if (needle == null) {
120			return true;
121		}
122		needle = needle.toLowerCase(Locale.US);
123		final Jid jid = getJid();
124		return (jid != null && jid.toString().contains(needle)) ||
125			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
126			matchInTag(context, needle);
127	}
128
129	private boolean matchInTag(Context context, String needle) {
130		needle = needle.toLowerCase(Locale.US);
131		for (Tag tag : getTags(context)) {
132			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
133				return true;
134			}
135		}
136		return false;
137	}
138
139	public Account getAccount() {
140		return this.account;
141	}
142
143	public Conversation getConversation() {
144		return this.mJoinedConversation;
145	}
146
147	public void setConversation(Conversation conversation) {
148		this.mJoinedConversation = conversation;
149	}
150
151	public String getBookmarkName() {
152		return this.getAttribute("name");
153	}
154
155	public boolean setBookmarkName(String name) {
156		String before = getBookmarkName();
157		if (name != null && !name.equals(before)) {
158			this.setAttribute("name", name);
159			return true;
160		} else {
161			return false;
162		}
163	}
164
165	public void unregisterConversation() {
166		if (this.mJoinedConversation != null) {
167			this.mJoinedConversation.deregisterWithBookmark();
168		}
169	}
170}