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