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