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