Bookmark.java

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