Bookmark.java

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