2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2016-04-22 19:25:06 +00:00
|
|
|
import android.content.Context;
|
2021-01-18 17:26:46 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2016-04-22 19:25:06 +00:00
|
|
|
|
2018-02-10 18:06:31 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
2014-11-16 16:21:21 +00:00
|
|
|
import java.util.ArrayList;
|
2019-09-27 22:32:29 +00:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2014-11-16 16:21:21 +00:00
|
|
|
import java.util.List;
|
2014-11-17 16:53:19 +00:00
|
|
|
import java.util.Locale;
|
2019-09-27 22:32:29 +00:00
|
|
|
import java.util.Map;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2018-06-24 14:33:15 +00:00
|
|
|
import eu.siacs.conversations.utils.StringUtils;
|
2014-11-16 22:58:30 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xml.Element;
|
2019-09-27 22:32:29 +00:00
|
|
|
import eu.siacs.conversations.xml.Namespace;
|
2018-04-28 14:26:40 +00:00
|
|
|
import eu.siacs.conversations.xmpp.InvalidJid;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public class Bookmark extends Element implements ListItem {
|
|
|
|
|
|
|
|
private Account account;
|
2018-02-10 18:06:31 +00:00
|
|
|
private WeakReference<Conversation> conversation;
|
2018-03-11 11:06:07 +00:00
|
|
|
private Jid jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Bookmark(final Account account, final Jid jid) {
|
2014-10-22 16:38:44 +00:00
|
|
|
super("conference");
|
2018-03-11 11:06:07 +00:00
|
|
|
this.jid = jid;
|
2020-05-15 16:44:55 +00:00
|
|
|
this.setAttribute("jid", jid);
|
2014-10-22 16:38:44 +00:00
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bookmark(Account account) {
|
|
|
|
super("conference");
|
|
|
|
this.account = account;
|
|
|
|
}
|
|
|
|
|
2019-09-28 00:23:15 +00:00
|
|
|
public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
|
2019-09-27 22:32:29 +00:00
|
|
|
if (storage == null) {
|
2019-09-28 00:23:15 +00:00
|
|
|
return Collections.emptyMap();
|
2019-09-27 22:32:29 +00:00
|
|
|
}
|
|
|
|
final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
|
|
|
|
for (final Element item : storage.getChildren()) {
|
|
|
|
if (item.getName().equals("conference")) {
|
|
|
|
final Bookmark bookmark = Bookmark.parse(item, account);
|
|
|
|
if (bookmark != null) {
|
2019-09-28 00:23:15 +00:00
|
|
|
final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
|
2019-09-27 22:32:29 +00:00
|
|
|
if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
|
|
|
|
bookmark.setBookmarkName(old.getBookmarkName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 00:23:15 +00:00
|
|
|
return bookmarks;
|
2019-09-27 22:32:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 00:23:15 +00:00
|
|
|
public static Map<Jid, Bookmark> parseFromPubsub(Element pubsub, Account account) {
|
2019-09-27 22:32:29 +00:00
|
|
|
if (pubsub == null) {
|
2019-09-28 00:23:15 +00:00
|
|
|
return Collections.emptyMap();
|
2019-09-27 22:32:29 +00:00
|
|
|
}
|
|
|
|
final Element items = pubsub.findChild("items");
|
2019-09-28 11:14:59 +00:00
|
|
|
if (items != null && Namespace.BOOKMARKS2.equals(items.getAttribute("node"))) {
|
2019-09-28 00:23:15 +00:00
|
|
|
final Map<Jid, Bookmark> bookmarks = new HashMap<>();
|
2019-09-27 22:32:29 +00:00
|
|
|
for(Element item : items.getChildren()) {
|
|
|
|
if (item.getName().equals("item")) {
|
|
|
|
final Bookmark bookmark = Bookmark.parseFromItem(item, account);
|
|
|
|
if (bookmark != null) {
|
2019-09-28 00:23:15 +00:00
|
|
|
bookmarks.put(bookmark.jid, bookmark);
|
2019-09-27 22:32:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bookmarks;
|
|
|
|
}
|
2019-09-28 00:23:15 +00:00
|
|
|
return Collections.emptyMap();
|
2019-09-27 22:32:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public static Bookmark parse(Element element, Account account) {
|
|
|
|
Bookmark bookmark = new Bookmark(account);
|
|
|
|
bookmark.setAttributes(element.getAttributes());
|
|
|
|
bookmark.setChildren(element.getChildren());
|
2018-04-28 14:26:40 +00:00
|
|
|
bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
|
2019-09-27 22:32:29 +00:00
|
|
|
if (bookmark.jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Bookmark parseFromItem(Element item, Account account) {
|
2019-09-28 11:14:59 +00:00
|
|
|
final Element conference = item.findChild("conference", Namespace.BOOKMARKS2);
|
2019-09-27 22:32:29 +00:00
|
|
|
if (conference == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
final Bookmark bookmark = new Bookmark(account);
|
|
|
|
bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
|
|
|
|
if (bookmark.jid == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
bookmark.setBookmarkName(conference.getAttribute("name"));
|
|
|
|
bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
|
|
|
|
bookmark.setNick(conference.findChildContent("nick"));
|
2014-10-22 16:38:44 +00:00
|
|
|
return bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAutojoin(boolean autojoin) {
|
|
|
|
if (autojoin) {
|
|
|
|
this.setAttribute("autojoin", "true");
|
|
|
|
} else {
|
|
|
|
this.setAttribute("autojoin", "false");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-11 11:13:56 +00:00
|
|
|
public int compareTo(final @NonNull ListItem another) {
|
2014-11-16 22:58:30 +00:00
|
|
|
return this.getDisplayName().compareToIgnoreCase(
|
|
|
|
another.getDisplayName());
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDisplayName() {
|
2018-02-10 18:06:31 +00:00
|
|
|
final Conversation c = getConversation();
|
2018-03-23 15:52:05 +00:00
|
|
|
final String name = getBookmarkName();
|
2018-02-10 18:06:31 +00:00
|
|
|
if (c != null) {
|
2018-03-11 11:13:56 +00:00
|
|
|
return c.getName().toString();
|
2018-03-23 15:52:05 +00:00
|
|
|
} else if (printableValue(name, false)) {
|
|
|
|
return name.trim();
|
2014-10-22 16:38:44 +00:00
|
|
|
} else {
|
2016-06-16 18:38:35 +00:00
|
|
|
Jid jid = this.getJid();
|
2018-03-23 15:52:05 +00:00
|
|
|
return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 15:52:05 +00:00
|
|
|
public static boolean printableValue(@Nullable String value, boolean permitNone) {
|
|
|
|
return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean printableValue(@Nullable String value) {
|
|
|
|
return printableValue(value, true);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@Override
|
2014-11-05 20:55:47 +00:00
|
|
|
public Jid getJid() {
|
2018-03-11 11:06:07 +00:00
|
|
|
return this.jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 16:33:24 +00:00
|
|
|
public Jid getFullJid() {
|
|
|
|
final String nick = getNick();
|
|
|
|
return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
|
|
|
|
}
|
|
|
|
|
2014-11-16 16:21:21 +00:00
|
|
|
@Override
|
2016-04-22 19:25:06 +00:00
|
|
|
public List<Tag> getTags(Context context) {
|
2016-06-16 18:38:35 +00:00
|
|
|
ArrayList<Tag> tags = new ArrayList<>();
|
2014-11-16 22:58:30 +00:00
|
|
|
for (Element element : getChildren()) {
|
|
|
|
if (element.getName().equals("group") && element.getContent() != null) {
|
|
|
|
String group = element.getContent();
|
2017-12-05 13:15:10 +00:00
|
|
|
tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
|
2014-11-16 22:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tags;
|
2014-11-16 16:21:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public String getNick() {
|
2015-05-14 12:42:21 +00:00
|
|
|
return this.findChildContent("nick");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setNick(String nick) {
|
|
|
|
Element element = this.findChild("nick");
|
|
|
|
if (element == null) {
|
|
|
|
element = this.addChild("nick");
|
|
|
|
}
|
|
|
|
element.setContent(nick);
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public boolean autojoin() {
|
2014-12-05 00:54:16 +00:00
|
|
|
return this.getAttributeAsBoolean("autojoin");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
2015-05-14 12:42:21 +00:00
|
|
|
return this.findChildContent("password");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public void setPassword(String password) {
|
|
|
|
Element element = this.findChild("password");
|
|
|
|
if (element != null) {
|
|
|
|
element.setContent(password);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-22 19:25:06 +00:00
|
|
|
@Override
|
|
|
|
public boolean match(Context context, String needle) {
|
2014-11-16 22:58:30 +00:00
|
|
|
if (needle == null) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-17 16:53:19 +00:00
|
|
|
needle = needle.toLowerCase(Locale.US);
|
2014-12-03 09:45:47 +00:00
|
|
|
final Jid jid = getJid();
|
|
|
|
return (jid != null && jid.toString().contains(needle)) ||
|
2014-11-17 16:53:19 +00:00
|
|
|
getDisplayName().toLowerCase(Locale.US).contains(needle) ||
|
2016-04-22 19:25:06 +00:00
|
|
|
matchInTag(context, needle);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:25:06 +00:00
|
|
|
private boolean matchInTag(Context context, String needle) {
|
2014-11-17 16:53:19 +00:00
|
|
|
needle = needle.toLowerCase(Locale.US);
|
2016-04-22 19:25:06 +00:00
|
|
|
for (Tag tag : getTags(context)) {
|
2014-11-17 16:53:19 +00:00
|
|
|
if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
|
2014-11-16 22:58:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 22:58:30 +00:00
|
|
|
public Account getAccount() {
|
|
|
|
return this.account;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 18:06:31 +00:00
|
|
|
public synchronized Conversation getConversation() {
|
|
|
|
return this.conversation != null ? this.conversation.get() : null;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-10 18:06:31 +00:00
|
|
|
public synchronized void setConversation(Conversation conversation) {
|
|
|
|
if (this.conversation != null) {
|
|
|
|
this.conversation.clear();
|
|
|
|
}
|
2018-11-10 16:33:24 +00:00
|
|
|
if (conversation == null) {
|
|
|
|
this.conversation = null;
|
|
|
|
} else {
|
|
|
|
this.conversation = new WeakReference<>(conversation);
|
|
|
|
conversation.getMucOptions().notifyOfBookmarkNick(getNick());
|
|
|
|
}
|
2014-11-16 22:58:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 20:52:35 +00:00
|
|
|
public String getBookmarkName() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.getAttribute("name");
|
|
|
|
}
|
|
|
|
|
2015-12-11 18:28:44 +00:00
|
|
|
public boolean setBookmarkName(String name) {
|
|
|
|
String before = getBookmarkName();
|
2018-06-24 14:33:15 +00:00
|
|
|
if (name != null) {
|
2015-12-11 18:28:44 +00:00
|
|
|
this.setAttribute("name", name);
|
|
|
|
} else {
|
2018-06-24 14:33:15 +00:00
|
|
|
this.removeAttribute("name");
|
2015-12-11 18:28:44 +00:00
|
|
|
}
|
2018-06-24 14:33:15 +00:00
|
|
|
return StringUtils.changed(before, name);
|
2015-12-11 18:28:44 +00:00
|
|
|
}
|
2019-01-25 09:07:02 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getAvatarBackgroundColor() {
|
|
|
|
return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
|
|
|
|
}
|
2020-08-31 11:05:10 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getAvatarName() {
|
|
|
|
return getDisplayName();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|