Bookmark.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.Context;
  4
  5import androidx.annotation.NonNull;
  6import androidx.annotation.Nullable;
  7
  8import java.lang.ref.WeakReference;
  9import java.util.ArrayList;
 10import java.util.Collections;
 11import java.util.HashMap;
 12import java.util.List;
 13import java.util.Locale;
 14import java.util.Map;
 15
 16import eu.siacs.conversations.utils.StringUtils;
 17import eu.siacs.conversations.utils.UIHelper;
 18import eu.siacs.conversations.xml.Element;
 19import eu.siacs.conversations.xml.Namespace;
 20import eu.siacs.conversations.xmpp.InvalidJid;
 21import eu.siacs.conversations.xmpp.Jid;
 22
 23public class Bookmark extends Element implements ListItem {
 24
 25	private final Account account;
 26	private WeakReference<Conversation> conversation;
 27	private Jid jid;
 28	protected Element extensions = new Element("extensions", Namespace.BOOKMARKS2);
 29
 30	public Bookmark(final Account account, final Jid jid) {
 31		super("conference");
 32		this.jid = jid;
 33		this.setAttribute("jid", jid);
 34		this.account = account;
 35	}
 36
 37	private Bookmark(Account account) {
 38		super("conference");
 39		this.account = account;
 40	}
 41
 42	public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
 43		if (storage == null) {
 44			return Collections.emptyMap();
 45		}
 46		final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
 47		for (final Element item : storage.getChildren()) {
 48			if (item.getName().equals("conference")) {
 49				final Bookmark bookmark = Bookmark.parse(item, account);
 50				if (bookmark != null) {
 51					final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
 52					if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
 53						bookmark.setBookmarkName(old.getBookmarkName());
 54					}
 55				}
 56			}
 57		}
 58		return bookmarks;
 59	}
 60
 61	public static Map<Jid, Bookmark> parseFromPubsub(Element pubsub, Account account) {
 62		if (pubsub == null) {
 63			return Collections.emptyMap();
 64		}
 65		final Element items = pubsub.findChild("items");
 66		if (items != null && Namespace.BOOKMARKS2.equals(items.getAttribute("node"))) {
 67			final Map<Jid, Bookmark> bookmarks = new HashMap<>();
 68			for(Element item : items.getChildren()) {
 69				if (item.getName().equals("item")) {
 70					final Bookmark bookmark = Bookmark.parseFromItem(item, account);
 71					if (bookmark != null) {
 72						bookmarks.put(bookmark.jid, bookmark);
 73					}
 74				}
 75			}
 76			return bookmarks;
 77		}
 78		return Collections.emptyMap();
 79	}
 80
 81	public static Bookmark parse(Element element, Account account) {
 82		Bookmark bookmark = new Bookmark(account);
 83		bookmark.setAttributes(element.getAttributes());
 84		bookmark.setChildren(element.getChildren());
 85		bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
 86		if (bookmark.jid == null) {
 87			return null;
 88		}
 89		return bookmark;
 90	}
 91
 92	public static Bookmark parseFromItem(Element item, Account account) {
 93		final Element conference = item.findChild("conference", Namespace.BOOKMARKS2);
 94		if (conference == null) {
 95			return null;
 96		}
 97		final Bookmark bookmark = new Bookmark(account);
 98		bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
 99		if (bookmark.jid == null) {
100			return null;
101		}
102		bookmark.setBookmarkName(conference.getAttribute("name"));
103		bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
104		bookmark.setNick(conference.findChildContent("nick"));
105		bookmark.setPassword(conference.findChildContent("password"));
106		final Element extensions = conference.findChild("extensions", Namespace.BOOKMARKS2);
107		if (extensions != null) {
108			for (final Element ext : extensions.getChildren()) {
109				if (ext.getName().equals("group") && ext.getNamespace().equals("jabber:iq:roster")) {
110					bookmark.addGroup(ext.getContent());
111				}
112			}
113			bookmark.extensions = extensions;
114		}
115		return bookmark;
116	}
117
118	public Element getExtensions() {
119		return extensions;
120	}
121
122	public void addGroup(final String group) {
123		addChild("group", "jabber:iq:roster").setContent(group);
124		extensions.addChild("group", "jabber:iq:roster").setContent(group);
125	}
126
127	public void setGroups(List<String> groups) {
128		final List<Element> children = new ArrayList<>(getChildren());
129		for (final Element el : children) {
130			if (el.getName().equals("group")) {
131				removeChild(el);
132			}
133		}
134
135		final List<Element> extChildren = new ArrayList<>(extensions.getChildren());
136		for (final Element el : extChildren) {
137			if (el.getName().equals("group")) {
138				extensions.removeChild(el);
139			}
140		}
141
142		for (final String group : groups) {
143			addGroup(group);
144		}
145	}
146
147	public void setAutojoin(boolean autojoin) {
148		if (autojoin) {
149			this.setAttribute("autojoin", "true");
150		} else {
151			this.setAttribute("autojoin", "false");
152		}
153	}
154
155	@Override
156	public int compareTo(final @NonNull ListItem another) {
157		if (getJid().isDomainJid() && !another.getJid().isDomainJid()) {
158			return -1;
159		} else if (!getJid().isDomainJid() && another.getJid().isDomainJid()) {
160			return 1;
161		}
162
163		return this.getDisplayName().compareToIgnoreCase(
164				another.getDisplayName());
165	}
166
167	@Override
168	public String getDisplayName() {
169		final Conversation c = getConversation();
170		final String name = getBookmarkName();
171		if (c != null) {
172			return c.getName().toString();
173		} else if (printableValue(name, false)) {
174			return name.trim();
175		} else {
176			Jid jid = this.getJid();
177			return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
178		}
179	}
180
181	public static boolean printableValue(@Nullable String value, boolean permitNone) {
182		return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
183	}
184
185	public static boolean printableValue(@Nullable String value) {
186		return printableValue(value, true);
187	}
188
189	@Override
190	public Jid getJid() {
191		return this.jid;
192	}
193
194	public Jid getFullJid() {
195		final String nick = getNick();
196		return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
197	}
198
199	public List<Tag> getGroupTags() {
200		ArrayList<Tag> tags = new ArrayList<>();
201
202		for (Element element : getChildren()) {
203			if (element.getName().equals("group") && element.getContent() != null) {
204				String group = element.getContent();
205				tags.add(new Tag(group, UIHelper.getColorForName(group, true)));
206			}
207		}
208
209		return tags;
210	}
211
212	@Override
213	public List<Tag> getTags(Context context) {
214		ArrayList<Tag> tags = new ArrayList<>();
215		tags.add(new Tag("Channel", UIHelper.getColorForName("Channel",true)));
216		tags.addAll(getGroupTags());
217		return tags;
218	}
219
220	public String getNick() {
221		return this.findChildContent("nick");
222	}
223
224	public void setNick(String nick) {
225		Element element = this.findChild("nick");
226		if (element == null) {
227			element = this.addChild("nick");
228		}
229		element.setContent(nick);
230	}
231
232	public boolean autojoin() {
233		return this.getAttributeAsBoolean("autojoin");
234	}
235
236	public String getPassword() {
237		return this.findChildContent("password");
238	}
239
240	public void setPassword(String password) {
241		Element element = this.findChild("password");
242		if (element != null) {
243			element.setContent(password);
244		}
245	}
246
247	@Override
248	public boolean match(Context context, String needle) {
249		if (needle == null) {
250			return true;
251		}
252		needle = needle.toLowerCase(Locale.US);
253		String[] parts = needle.split("[,\\s]+");
254		if (parts.length > 1) {
255			for (String part : parts) {
256				if (!match(context, part)) {
257					return false;
258				}
259			}
260			return true;
261		} else if (parts.length > 0) {
262			final Jid jid = getJid();
263			return (jid != null && jid.toString().contains(parts[0])) ||
264				getDisplayName().toLowerCase(Locale.US).contains(parts[0]) ||
265				matchInTag(context, parts[0]);
266		} else {
267			final Jid jid = getJid();
268			return (jid != null && jid.toString().contains(needle)) ||
269				getDisplayName().toLowerCase(Locale.US).contains(needle);
270		}
271	}
272
273	private boolean matchInTag(Context context, String needle) {
274		needle = needle.toLowerCase(Locale.US);
275		for (Tag tag : getTags(context)) {
276			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
277				return true;
278			}
279		}
280		return false;
281	}
282
283	public Account getAccount() {
284		return this.account;
285	}
286
287	public synchronized Conversation getConversation() {
288		return this.conversation != null ? this.conversation.get() : null;
289	}
290
291	public synchronized void setConversation(Conversation conversation) {
292		if (this.conversation != null) {
293			this.conversation.clear();
294		}
295		if (conversation == null) {
296			this.conversation = null;
297		} else {
298			this.conversation = new WeakReference<>(conversation);
299			conversation.getMucOptions().notifyOfBookmarkNick(getNick());
300		}
301	}
302
303	public String getBookmarkName() {
304		return this.getAttribute("name");
305	}
306
307	public boolean setBookmarkName(String name) {
308		String before = getBookmarkName();
309		if (name != null) {
310			this.setAttribute("name", name);
311		} else {
312			this.removeAttribute("name");
313		}
314		return StringUtils.changed(before, name);
315	}
316
317	@Override
318	public int getAvatarBackgroundColor() {
319		return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
320	}
321
322	@Override
323	public String getAvatarName() {
324		return getDisplayName();
325	}
326}