Bookmark.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.Context;
  4
  5import java.lang.ref.WeakReference;
  6import java.util.ArrayList;
  7import java.util.List;
  8import java.util.Locale;
  9
 10import eu.siacs.conversations.utils.UIHelper;
 11import eu.siacs.conversations.xml.Element;
 12import rocks.xmpp.addr.Jid;
 13
 14public class Bookmark extends Element implements ListItem {
 15
 16	private Account account;
 17	private WeakReference<Conversation> conversation;
 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		final Conversation c = getConversation();
 54		if (c != null) {
 55			return c.getName();
 56		} else if (getBookmarkName() != null
 57				&& !getBookmarkName().trim().isEmpty()) {
 58			return getBookmarkName().trim();
 59		} else {
 60			Jid jid = this.getJid();
 61			String name = jid != null ? jid.getLocal() : getAttribute("jid");
 62			return name != null ? name : "";
 63		}
 64	}
 65
 66	@Override
 67	public Jid getJid() {
 68		return this.getAttributeAsJid("jid");
 69	}
 70
 71	@Override
 72	public List<Tag> getTags(Context context) {
 73		ArrayList<Tag> tags = new ArrayList<>();
 74		for (Element element : getChildren()) {
 75			if (element.getName().equals("group") && element.getContent() != null) {
 76				String group = element.getContent();
 77				tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
 78			}
 79		}
 80		return tags;
 81	}
 82
 83	public String getNick() {
 84		return this.findChildContent("nick");
 85	}
 86
 87	public void setNick(String nick) {
 88		Element element = this.findChild("nick");
 89		if (element == null) {
 90			element = this.addChild("nick");
 91		}
 92		element.setContent(nick);
 93	}
 94
 95	public boolean autojoin() {
 96		return this.getAttributeAsBoolean("autojoin");
 97	}
 98
 99	public String getPassword() {
100		return this.findChildContent("password");
101	}
102
103	public void setPassword(String password) {
104		Element element = this.findChild("password");
105		if (element != null) {
106			element.setContent(password);
107		}
108	}
109
110	@Override
111	public boolean match(Context context, String needle) {
112		if (needle == null) {
113			return true;
114		}
115		needle = needle.toLowerCase(Locale.US);
116		final Jid jid = getJid();
117		return (jid != null && jid.toString().contains(needle)) ||
118			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
119			matchInTag(context, needle);
120	}
121
122	private boolean matchInTag(Context context, String needle) {
123		needle = needle.toLowerCase(Locale.US);
124		for (Tag tag : getTags(context)) {
125			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
126				return true;
127			}
128		}
129		return false;
130	}
131
132	public Account getAccount() {
133		return this.account;
134	}
135
136	public synchronized Conversation getConversation() {
137		return this.conversation != null ? this.conversation.get() : null;
138	}
139
140	public synchronized void setConversation(Conversation conversation) {
141		if (this.conversation != null) {
142			this.conversation.clear();
143		}
144		this.conversation = new WeakReference<>(conversation);
145	}
146
147	public String getBookmarkName() {
148		return this.getAttribute("name");
149	}
150
151	public boolean setBookmarkName(String name) {
152		String before = getBookmarkName();
153		if (name != null && !name.equals(before)) {
154			this.setAttribute("name", name);
155			return true;
156		} else {
157			return false;
158		}
159	}
160}