Bookmark.java

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