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