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.utils.UIHelper;
  8import eu.siacs.conversations.xml.Element;
  9import eu.siacs.conversations.xmpp.jid.Jid;
 10
 11public class Bookmark extends Element implements ListItem {
 12
 13	private Account account;
 14	private Conversation mJoinedConversation;
 15
 16	public Bookmark(final Account account, final Jid jid) {
 17		super("conference");
 18		this.setAttribute("jid", jid.toString());
 19		this.account = account;
 20	}
 21
 22	private Bookmark(Account account) {
 23		super("conference");
 24		this.account = account;
 25	}
 26
 27	public static Bookmark parse(Element element, Account account) {
 28		Bookmark bookmark = new Bookmark(account);
 29		bookmark.setAttributes(element.getAttributes());
 30		bookmark.setChildren(element.getChildren());
 31		return bookmark;
 32	}
 33
 34	public void setAutojoin(boolean autojoin) {
 35		if (autojoin) {
 36			this.setAttribute("autojoin", "true");
 37		} else {
 38			this.setAttribute("autojoin", "false");
 39		}
 40	}
 41
 42	@Override
 43	public int compareTo(final ListItem another) {
 44		return this.getDisplayName().compareToIgnoreCase(
 45				another.getDisplayName());
 46	}
 47
 48	@Override
 49	public String getDisplayName() {
 50		if (this.mJoinedConversation != null
 51				&& (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
 52			return this.mJoinedConversation.getMucOptions().getSubject();
 53		} else if (getName() != null) {
 54			return getName();
 55		} else {
 56			return this.getJid().getLocalpart();
 57		}
 58	}
 59
 60	@Override
 61	public Jid getJid() {
 62		return this.getAttributeAsJid("jid");
 63	}
 64
 65	@Override
 66	public List<Tag> getTags() {
 67		ArrayList<Tag> tags = new ArrayList<Tag>();
 68		for (Element element : getChildren()) {
 69			if (element.getName().equals("group") && element.getContent() != null) {
 70				String group = element.getContent();
 71				tags.add(new Tag(group, UIHelper.getColorForName(group)));
 72			}
 73		}
 74		return tags;
 75	}
 76
 77	public String getNick() {
 78		Element nick = this.findChild("nick");
 79		if (nick != null) {
 80			return nick.getContent();
 81		} else {
 82			return null;
 83		}
 84	}
 85
 86	public void setNick(String nick) {
 87		Element element = this.findChild("nick");
 88		if (element == null) {
 89			element = this.addChild("nick");
 90		}
 91		element.setContent(nick);
 92	}
 93
 94	public boolean autojoin() {
 95		return this.getAttributeAsBoolean("autojoin");
 96	}
 97
 98	public String getPassword() {
 99		Element password = this.findChild("password");
100		if (password != null) {
101			return password.getContent();
102		} else {
103			return null;
104		}
105	}
106
107	public void setPassword(String password) {
108		Element element = this.findChild("password");
109		if (element != null) {
110			element.setContent(password);
111		}
112	}
113
114	public boolean match(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(needle);
123	}
124
125	private boolean matchInTag(String needle) {
126		needle = needle.toLowerCase(Locale.US);
127		for (Tag tag : getTags()) {
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 Conversation getConversation() {
140		return this.mJoinedConversation;
141	}
142
143	public void setConversation(Conversation conversation) {
144		this.mJoinedConversation = conversation;
145	}
146
147	public String getName() {
148		return this.getAttribute("name");
149	}
150
151	public void setName(String name) {
152		this.name = name;
153	}
154
155	public void unregisterConversation() {
156		if (this.mJoinedConversation != null) {
157			this.mJoinedConversation.deregisterWithBookmark();
158		}
159	}
160}