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		return this.getDisplayName().compareToIgnoreCase(
158				another.getDisplayName());
159	}
160
161	@Override
162	public String getDisplayName() {
163		final Conversation c = getConversation();
164		final String name = getBookmarkName();
165		if (c != null) {
166			return c.getName().toString();
167		} else if (printableValue(name, false)) {
168			return name.trim();
169		} else {
170			Jid jid = this.getJid();
171			return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
172		}
173	}
174
175	public static boolean printableValue(@Nullable String value, boolean permitNone) {
176		return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
177	}
178
179	public static boolean printableValue(@Nullable String value) {
180		return printableValue(value, true);
181	}
182
183	@Override
184	public Jid getJid() {
185		return this.jid;
186	}
187
188	public Jid getFullJid() {
189		final String nick = getNick();
190		return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
191	}
192
193	public List<Tag> getGroupTags() {
194		ArrayList<Tag> tags = new ArrayList<>();
195
196		for (Element element : getChildren()) {
197			if (element.getName().equals("group") && element.getContent() != null) {
198				String group = element.getContent();
199				tags.add(new Tag(group, UIHelper.getColorForName(group, true)));
200			}
201		}
202
203		return tags;
204	}
205
206	@Override
207	public List<Tag> getTags(Context context) {
208		ArrayList<Tag> tags = new ArrayList<>();
209		tags.add(new Tag("Channel", UIHelper.getColorForName("Channel",true)));
210		tags.addAll(getGroupTags());
211		return tags;
212	}
213
214	public String getNick() {
215		return this.findChildContent("nick");
216	}
217
218	public void setNick(String nick) {
219		Element element = this.findChild("nick");
220		if (element == null) {
221			element = this.addChild("nick");
222		}
223		element.setContent(nick);
224	}
225
226	public boolean autojoin() {
227		return this.getAttributeAsBoolean("autojoin");
228	}
229
230	public String getPassword() {
231		return this.findChildContent("password");
232	}
233
234	public void setPassword(String password) {
235		Element element = this.findChild("password");
236		if (element != null) {
237			element.setContent(password);
238		}
239	}
240
241	@Override
242	public boolean match(Context context, String needle) {
243		if (needle == null) {
244			return true;
245		}
246		needle = needle.toLowerCase(Locale.US);
247		String[] parts = needle.split("[,\\s]+");
248		if (parts.length > 1) {
249			for (String part : parts) {
250				if (!match(context, part)) {
251					return false;
252				}
253			}
254			return true;
255		} else {
256			final Jid jid = getJid();
257			return (jid != null && jid.toString().contains(parts[0])) ||
258				getDisplayName().toLowerCase(Locale.US).contains(parts[0]) ||
259				matchInTag(context, parts[0]);
260		}
261	}
262
263	private boolean matchInTag(Context context, String needle) {
264		needle = needle.toLowerCase(Locale.US);
265		for (Tag tag : getTags(context)) {
266			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
267				return true;
268			}
269		}
270		return false;
271	}
272
273	public Account getAccount() {
274		return this.account;
275	}
276
277	public synchronized Conversation getConversation() {
278		return this.conversation != null ? this.conversation.get() : null;
279	}
280
281	public synchronized void setConversation(Conversation conversation) {
282		if (this.conversation != null) {
283			this.conversation.clear();
284		}
285		if (conversation == null) {
286			this.conversation = null;
287		} else {
288			this.conversation = new WeakReference<>(conversation);
289			conversation.getMucOptions().notifyOfBookmarkNick(getNick());
290		}
291	}
292
293	public String getBookmarkName() {
294		return this.getAttribute("name");
295	}
296
297	public boolean setBookmarkName(String name) {
298		String before = getBookmarkName();
299		if (name != null) {
300			this.setAttribute("name", name);
301		} else {
302			this.removeAttribute("name");
303		}
304		return StringUtils.changed(before, name);
305	}
306
307	@Override
308	public int getAvatarBackgroundColor() {
309		return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
310	}
311
312	@Override
313	public String getAvatarName() {
314		return getDisplayName();
315	}
316}