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(Element pubsub, 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		if (bookmark.jid == null) {
103			return null;
104		}
105		bookmark.setBookmarkName(conference.getAttribute("name"));
106		bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
107		bookmark.setNick(conference.findChildContent("nick"));
108		bookmark.setPassword(conference.findChildContent("password"));
109		final Element extensions = conference.findChild("extensions", Namespace.BOOKMARKS2);
110		if (extensions != null) {
111			for (final Element ext : extensions.getChildren()) {
112				if (ext.getName().equals("group") && ext.getNamespace().equals("jabber:iq:roster")) {
113					bookmark.addGroup(ext.getContent());
114				}
115			}
116			bookmark.extensions = extensions;
117		}
118		return bookmark;
119	}
120
121	public Element getExtensions() {
122		return extensions;
123	}
124
125	public void addGroup(final String group) {
126		addChild("group", "jabber:iq:roster").setContent(group);
127		extensions.addChild("group", "jabber:iq:roster").setContent(group);
128	}
129
130	public void setGroups(List<String> groups) {
131		final List<Element> children = new ArrayList<>(getChildren());
132		for (final Element el : children) {
133			if (el.getName().equals("group")) {
134				removeChild(el);
135			}
136		}
137
138		final List<Element> extChildren = new ArrayList<>(extensions.getChildren());
139		for (final Element el : extChildren) {
140			if (el.getName().equals("group")) {
141				extensions.removeChild(el);
142			}
143		}
144
145		for (final String group : groups) {
146			addGroup(group);
147		}
148	}
149
150	public void setAutojoin(boolean autojoin) {
151		if (autojoin) {
152			this.setAttribute("autojoin", "true");
153		} else {
154			this.setAttribute("autojoin", "false");
155		}
156	}
157
158	@Override
159	public int compareTo(final @NonNull ListItem another) {
160		if (getJid().isDomainJid() && !another.getJid().isDomainJid()) {
161			return -1;
162		} else if (!getJid().isDomainJid() && another.getJid().isDomainJid()) {
163			return 1;
164		}
165
166		return this.getDisplayName().compareToIgnoreCase(
167				another.getDisplayName());
168	}
169
170	@Override
171	public String getDisplayName() {
172		final Conversation c = getConversation();
173		final String name = getBookmarkName();
174		if (c != null) {
175			return c.getName().toString();
176		} else if (printableValue(name, false)) {
177			return name.trim();
178		} else {
179			Jid jid = this.getJid();
180			return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
181		}
182	}
183
184	public static boolean printableValue(@Nullable String value, boolean permitNone) {
185		return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
186	}
187
188	public static boolean printableValue(@Nullable String value) {
189		return printableValue(value, true);
190	}
191
192	@Override
193	public Jid getJid() {
194		return this.jid;
195	}
196
197	public Jid getFullJid() {
198		return getFullJid(getNick(), true);
199	}
200
201	private Jid getFullJid(final String nick, boolean tryFix) {
202		try {
203			return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
204		} catch (final IllegalArgumentException e) {
205			try {
206				return tryFix ? getFullJid(gnu.inet.encoding.Punycode.encode(nick), false) : null;
207			} catch (final Exception e2) {
208				return null;
209			}
210		}
211	}
212
213	public List<Tag> getGroupTags() {
214		ArrayList<Tag> tags = new ArrayList<>();
215
216		for (Element element : getChildren()) {
217			if (element.getName().equals("group") && element.getContent() != null) {
218				String group = element.getContent();
219				tags.add(new Tag(group));
220			}
221		}
222
223		return tags;
224	}
225
226	@Override
227	public List<Tag> getTags(Context context) {
228		ArrayList<Tag> tags = new ArrayList<>();
229		tags.add(new Tag("Channel"));
230		tags.addAll(getGroupTags());
231		return tags;
232	}
233
234	public String getNick() {
235		return this.findChildContent("nick");
236	}
237
238	public void setNick(String nick) {
239		Element element = this.findChild("nick");
240		if (element == null) {
241			element = this.addChild("nick");
242		}
243		element.setContent(nick);
244	}
245
246	public boolean autojoin() {
247		return this.getAttributeAsBoolean("autojoin");
248	}
249
250	public String getPassword() {
251		return this.findChildContent("password");
252	}
253
254	public void setPassword(String password) {
255		Element element = this.findChild("password");
256		if (element != null) {
257			element.setContent(password);
258		}
259	}
260
261	@Override
262	public boolean match(Context context, String needle) {
263		if (needle == null) {
264			return true;
265		}
266		needle = needle.toLowerCase(Locale.US);
267		String[] parts = needle.split("[,\\s]+");
268		if (parts.length > 1) {
269			for (String part : parts) {
270				if (!match(context, part)) {
271					return false;
272				}
273			}
274			return true;
275		} else if (parts.length > 0) {
276			final Jid jid = getJid();
277			return (jid != null && jid.toString().contains(parts[0])) ||
278				getDisplayName().toLowerCase(Locale.US).contains(parts[0]) ||
279				matchInTag(context, parts[0]);
280		} else {
281			final Jid jid = getJid();
282			return (jid != null && jid.toString().contains(needle)) ||
283				getDisplayName().toLowerCase(Locale.US).contains(needle);
284		}
285	}
286
287	private boolean matchInTag(Context context, String needle) {
288		needle = needle.toLowerCase(Locale.US);
289		for (Tag tag : getTags(context)) {
290			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
291				return true;
292			}
293		}
294		return false;
295	}
296
297	public Account getAccount() {
298		return this.account;
299	}
300
301	public synchronized Conversation getConversation() {
302		return this.conversation != null ? this.conversation.get() : null;
303	}
304
305	public synchronized void setConversation(Conversation conversation) {
306		if (this.conversation != null) {
307			this.conversation.clear();
308		}
309		if (conversation == null) {
310			this.conversation = null;
311		} else {
312			this.conversation = new WeakReference<>(conversation);
313			conversation.getMucOptions().notifyOfBookmarkNick(getNick());
314		}
315	}
316
317	public String getBookmarkName() {
318		return this.getAttribute("name");
319	}
320
321	public boolean setBookmarkName(String name) {
322		String before = getBookmarkName();
323		if (name != null) {
324			this.setAttribute("name", name);
325		} else {
326			this.removeAttribute("name");
327		}
328		return StringUtils.changed(before, name);
329	}
330
331	@Override
332	public int getAvatarBackgroundColor() {
333		return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
334	}
335
336	@Override
337	public String getAvatarName() {
338		return getDisplayName();
339	}
340}