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.Collections;
 13import java.util.HashMap;
 14import java.util.List;
 15import java.util.Locale;
 16import java.util.Map;
 17
 18import eu.siacs.conversations.utils.StringUtils;
 19import eu.siacs.conversations.utils.UIHelper;
 20import eu.siacs.conversations.xml.Element;
 21import eu.siacs.conversations.xml.Namespace;
 22import eu.siacs.conversations.xmpp.InvalidJid;
 23import eu.siacs.conversations.xmpp.Jid;
 24
 25public class Bookmark extends Element implements ListItem {
 26
 27	private final Account account;
 28	private WeakReference<Conversation> conversation;
 29	private Jid jid;
 30	protected Element extensions = new Element("extensions", Namespace.BOOKMARKS2);
 31
 32	public Bookmark(final Account account, final Jid jid) {
 33		super("conference");
 34		this.jid = jid;
 35		this.setAttribute("jid", jid);
 36		this.account = account;
 37	}
 38
 39	private Bookmark(Account account) {
 40		super("conference");
 41		this.account = account;
 42	}
 43
 44	public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
 45		if (storage == null) {
 46			return Collections.emptyMap();
 47		}
 48		final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
 49		for (final Element item : storage.getChildren()) {
 50			if (item.getName().equals("conference")) {
 51				final Bookmark bookmark = Bookmark.parse(item, account);
 52				if (bookmark != null) {
 53					final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
 54					if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
 55						bookmark.setBookmarkName(old.getBookmarkName());
 56					}
 57				}
 58			}
 59		}
 60		return bookmarks;
 61	}
 62
 63	public static Map<Jid, Bookmark> parseFromPubSub(final Element pubSub, final Account account) {
 64		if (pubSub == null) {
 65			return Collections.emptyMap();
 66		}
 67		final Element items = pubSub.findChild("items");
 68		if (items != null && Namespace.BOOKMARKS2.equals(items.getAttribute("node"))) {
 69			final Map<Jid, Bookmark> bookmarks = new HashMap<>();
 70			for(Element item : items.getChildren()) {
 71				if (item.getName().equals("item")) {
 72					final Bookmark bookmark = Bookmark.parseFromItem(item, account);
 73					if (bookmark != null) {
 74						bookmarks.put(bookmark.jid, bookmark);
 75					}
 76				}
 77			}
 78			return bookmarks;
 79		}
 80		return Collections.emptyMap();
 81	}
 82
 83	public static Bookmark parse(Element element, Account account) {
 84		Bookmark bookmark = new Bookmark(account);
 85		bookmark.setAttributes(element.getAttributes());
 86		bookmark.setChildren(element.getChildren());
 87		bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
 88		if (bookmark.jid == null) {
 89			return null;
 90		}
 91		return bookmark;
 92	}
 93
 94	public static Bookmark parseFromItem(Element item, Account account) {
 95		final Element conference = item.findChild("conference", Namespace.BOOKMARKS2);
 96		if (conference == null) {
 97			return null;
 98		}
 99		final Bookmark bookmark = new Bookmark(account);
100		bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
101		// TODO verify that we only use bare jids and ignore full jids
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			bookmark.extensions = extensions;
112		}
113		return bookmark;
114	}
115
116	public Element getExtensions() {
117		return extensions;
118	}
119
120	public void setAutojoin(boolean autojoin) {
121		if (autojoin) {
122			this.setAttribute("autojoin", "true");
123		} else {
124			this.setAttribute("autojoin", "false");
125		}
126	}
127
128	@Override
129	public int compareTo(final @NonNull ListItem another) {
130		return this.getDisplayName().compareToIgnoreCase(
131				another.getDisplayName());
132	}
133
134	@Override
135	public String getDisplayName() {
136		final Conversation c = getConversation();
137		final String name = getBookmarkName();
138		if (c != null) {
139			return c.getName().toString();
140		} else if (printableValue(name, false)) {
141			return name.trim();
142		} else {
143			Jid jid = this.getJid();
144			return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
145		}
146	}
147
148	public static boolean printableValue(@Nullable String value, boolean permitNone) {
149		return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
150	}
151
152	public static boolean printableValue(@Nullable String value) {
153		return printableValue(value, true);
154	}
155
156	@Override
157	public Jid getJid() {
158		return this.jid;
159	}
160
161	public Jid getFullJid() {
162		final String nick = Strings.nullToEmpty(getNick()).trim();
163		if (jid == null || nick.isEmpty()) {
164			return jid;
165		}
166		try {
167			return jid.withResource(nick);
168		} catch (final IllegalArgumentException e) {
169			return jid;
170		}
171	}
172
173	@Override
174	public List<Tag> getTags(final Context context) {
175		final ImmutableList.Builder<Tag> tags = new ImmutableList.Builder<>();
176		for (final Element element : getChildren()) {
177			final String content = element.getContent();
178			if (Strings.isNullOrEmpty(content)) {
179				continue;
180			}
181			if (element.getName().equals("group")) {
182				tags.add(new Tag(content));
183			}
184		}
185		return tags.build();
186	}
187
188	public String getNick() {
189		return this.findChildContent("nick");
190	}
191
192	public void setNick(String nick) {
193		Element element = this.findChild("nick");
194		if (element == null) {
195			element = this.addChild("nick");
196		}
197		element.setContent(nick);
198	}
199
200	public boolean autojoin() {
201		return this.getAttributeAsBoolean("autojoin");
202	}
203
204	public String getPassword() {
205		return this.findChildContent("password");
206	}
207
208	public void setPassword(String password) {
209		Element element = this.findChild("password");
210		if (element != null) {
211			element.setContent(password);
212		}
213	}
214
215	@Override
216	public boolean match(Context context, String needle) {
217		if (needle == null) {
218			return true;
219		}
220		needle = needle.toLowerCase(Locale.US);
221		final Jid jid = getJid();
222		return (jid != null && jid.toString().contains(needle)) ||
223			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
224			matchInTag(context, needle);
225	}
226
227	private boolean matchInTag(Context context, String needle) {
228		needle = needle.toLowerCase(Locale.US);
229		for (Tag tag : getTags(context)) {
230			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
231				return true;
232			}
233		}
234		return false;
235	}
236
237	public Account getAccount() {
238		return this.account;
239	}
240
241	public synchronized Conversation getConversation() {
242		return this.conversation != null ? this.conversation.get() : null;
243	}
244
245	public synchronized void setConversation(Conversation conversation) {
246		if (this.conversation != null) {
247			this.conversation.clear();
248		}
249		if (conversation == null) {
250			this.conversation = null;
251		} else {
252			this.conversation = new WeakReference<>(conversation);
253			conversation.getMucOptions().notifyOfBookmarkNick(getNick());
254		}
255	}
256
257	public String getBookmarkName() {
258		return this.getAttribute("name");
259	}
260
261	public boolean setBookmarkName(String name) {
262		String before = getBookmarkName();
263		if (name != null) {
264			this.setAttribute("name", name);
265		} else {
266			this.removeAttribute("name");
267		}
268		return StringUtils.changed(before, name);
269	}
270
271	@Override
272	public int getAvatarBackgroundColor() {
273		return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
274	}
275
276	@Override
277	public String getAvatarName() {
278		return getDisplayName();
279	}
280}