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