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 eu.siacs.conversations.xmpp.jid.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.getLocalpart() : getAttribute("jid");
 62			return name != null ? name : "";
 63		}
 64	}
 65
 66	@Override
 67	public String getDisplayJid() {
 68		Jid jid = getJid();
 69		if (jid != null) {
 70			return jid.toString();
 71		} else {
 72			return getAttribute("jid"); //fallback if jid wasn't parsable
 73		}
 74	}
 75
 76	@Override
 77	public Jid getJid() {
 78		return this.getAttributeAsJid("jid");
 79	}
 80
 81	@Override
 82	public List<Tag> getTags(Context context) {
 83		ArrayList<Tag> tags = new ArrayList<>();
 84		for (Element element : getChildren()) {
 85			if (element.getName().equals("group") && element.getContent() != null) {
 86				String group = element.getContent();
 87				tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
 88			}
 89		}
 90		return tags;
 91	}
 92
 93	public String getNick() {
 94		return this.findChildContent("nick");
 95	}
 96
 97	public void setNick(String nick) {
 98		Element element = this.findChild("nick");
 99		if (element == null) {
100			element = this.addChild("nick");
101		}
102		element.setContent(nick);
103	}
104
105	public boolean autojoin() {
106		return this.getAttributeAsBoolean("autojoin");
107	}
108
109	public String getPassword() {
110		return this.findChildContent("password");
111	}
112
113	public void setPassword(String password) {
114		Element element = this.findChild("password");
115		if (element != null) {
116			element.setContent(password);
117		}
118	}
119
120	@Override
121	public boolean match(Context context, String needle) {
122		if (needle == null) {
123			return true;
124		}
125		needle = needle.toLowerCase(Locale.US);
126		final Jid jid = getJid();
127		return (jid != null && jid.toString().contains(needle)) ||
128			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
129			matchInTag(context, needle);
130	}
131
132	private boolean matchInTag(Context context, String needle) {
133		needle = needle.toLowerCase(Locale.US);
134		for (Tag tag : getTags(context)) {
135			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
136				return true;
137			}
138		}
139		return false;
140	}
141
142	public Account getAccount() {
143		return this.account;
144	}
145
146	public synchronized Conversation getConversation() {
147		return this.conversation != null ? this.conversation.get() : null;
148	}
149
150	public synchronized void setConversation(Conversation conversation) {
151		if (this.conversation != null) {
152			this.conversation.clear();
153		}
154		this.conversation = new WeakReference<>(conversation);
155	}
156
157	public String getBookmarkName() {
158		return this.getAttribute("name");
159	}
160
161	public boolean setBookmarkName(String name) {
162		String before = getBookmarkName();
163		if (name != null && !name.equals(before)) {
164			this.setAttribute("name", name);
165			return true;
166		} else {
167			return false;
168		}
169	}
170}