Bookmark.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.Context;
  4import androidx.annotation.NonNull;
  5import androidx.annotation.Nullable;
  6import com.google.common.base.Strings;
  7import com.google.common.collect.ImmutableList;
  8import eu.siacs.conversations.utils.StringUtils;
  9import eu.siacs.conversations.utils.UIHelper;
 10import eu.siacs.conversations.xml.Element;
 11import eu.siacs.conversations.xmpp.Jid;
 12import im.conversations.android.xmpp.model.bookmark.Storage;
 13import im.conversations.android.xmpp.model.bookmark2.Extensions;
 14import java.lang.ref.WeakReference;
 15import java.util.Collections;
 16import java.util.HashMap;
 17import java.util.List;
 18import java.util.Locale;
 19import java.util.Map;
 20
 21public class Bookmark extends Element implements ListItem {
 22
 23    private final Account account;
 24    private WeakReference<Conversation> conversation;
 25    private Jid jid;
 26    protected Extensions extensions = new Extensions();
 27
 28    public Bookmark(final Account account, final Jid jid) {
 29        super("conference");
 30        this.jid = jid;
 31        this.setAttribute("jid", jid);
 32        this.account = account;
 33    }
 34
 35    public Bookmark(Account account) {
 36        super("conference");
 37        this.account = account;
 38    }
 39
 40    public static Map<Jid, Bookmark> parseFromStorage(
 41            final Storage storage, final Account account) {
 42        // TODO refactor to use extensions. get rid of the 'old' handling
 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
 53                            && old.getBookmarkName() != null
 54                            && bookmark.getBookmarkName() == null) {
 55                        bookmark.setBookmarkName(old.getBookmarkName());
 56                    }
 57                }
 58            }
 59        }
 60        return bookmarks;
 61    }
 62
 63    public static Bookmark parse(Element element, Account account) {
 64        Bookmark bookmark = new Bookmark(account);
 65        bookmark.setAttributes(element.getAttributes());
 66        bookmark.setChildren(element.getChildren());
 67        bookmark.jid = Jid.Invalid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
 68        if (bookmark.jid == null) {
 69            return null;
 70        }
 71        return bookmark;
 72    }
 73
 74    public Extensions getExtensions() {
 75        return extensions;
 76    }
 77
 78    public void setAutojoin(boolean autojoin) {
 79        if (autojoin) {
 80            this.setAttribute("autojoin", "true");
 81        } else {
 82            this.setAttribute("autojoin", "false");
 83        }
 84    }
 85
 86    @Override
 87    public int compareTo(final @NonNull ListItem another) {
 88        return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
 89    }
 90
 91    @Override
 92    public String getDisplayName() {
 93        final Conversation c = getConversation();
 94        final String name = getBookmarkName();
 95        if (c != null) {
 96            return c.getName().toString();
 97        } else if (printableValue(name, false)) {
 98            return name.trim();
 99        } else {
100            Jid jid = this.getJid();
101            return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
102        }
103    }
104
105    public static boolean printableValue(@Nullable String value, boolean permitNone) {
106        return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
107    }
108
109    public static boolean printableValue(@Nullable String value) {
110        return printableValue(value, true);
111    }
112
113    @Override
114    public Jid getJid() {
115        return this.jid;
116    }
117
118    public Jid getFullJid() {
119        final String nick = Strings.nullToEmpty(getNick()).trim();
120        if (jid == null || nick.isEmpty()) {
121            return jid;
122        }
123        try {
124            return jid.withResource(nick);
125        } catch (final IllegalArgumentException e) {
126            return jid;
127        }
128    }
129
130    @Override
131    public List<Tag> getTags(final Context context) {
132        final ImmutableList.Builder<Tag> tags = new ImmutableList.Builder<>();
133        for (final Element element : getChildren()) {
134            final String content = element.getContent();
135            if (Strings.isNullOrEmpty(content)) {
136                continue;
137            }
138            if (element.getName().equals("group")) {
139                tags.add(new Tag(content));
140            }
141        }
142        return tags.build();
143    }
144
145    public String getNick() {
146        return Strings.emptyToNull(this.findChildContent("nick"));
147    }
148
149    public void setNick(String nick) {
150        Element element = this.findChild("nick");
151        if (element == null) {
152            element = this.addChild("nick");
153        }
154        element.setContent(nick);
155    }
156
157    public boolean autojoin() {
158        return this.getAttributeAsBoolean("autojoin");
159    }
160
161    public String getPassword() {
162        return this.findChildContent("password");
163    }
164
165    public void setPassword(String password) {
166        Element element = this.findChild("password");
167        if (element != null) {
168            element.setContent(password);
169        }
170    }
171
172    @Override
173    public boolean match(Context context, String needle) {
174        if (needle == null) {
175            return true;
176        }
177        needle = needle.toLowerCase(Locale.US);
178        final Jid jid = getJid();
179        return (jid != null && jid.toString().contains(needle))
180                || getDisplayName().toLowerCase(Locale.US).contains(needle)
181                || matchInTag(context, needle);
182    }
183
184    private boolean matchInTag(Context context, String needle) {
185        needle = needle.toLowerCase(Locale.US);
186        for (Tag tag : getTags(context)) {
187            if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
188                return true;
189            }
190        }
191        return false;
192    }
193
194    public Account getAccount() {
195        return this.account;
196    }
197
198    public synchronized Conversation getConversation() {
199        return this.conversation != null ? this.conversation.get() : null;
200    }
201
202    public synchronized void setConversation(Conversation conversation) {
203        if (this.conversation != null) {
204            this.conversation.clear();
205        }
206        if (conversation == null) {
207            this.conversation = null;
208        } else {
209            this.conversation = new WeakReference<>(conversation);
210        }
211    }
212
213    public String getBookmarkName() {
214        return this.getAttribute("name");
215    }
216
217    public boolean setBookmarkName(String name) {
218        String before = getBookmarkName();
219        if (name != null) {
220            this.setAttribute("name", name);
221        } else {
222            this.removeAttribute("name");
223        }
224        return StringUtils.changed(before, name);
225    }
226
227    @Override
228    public int getAvatarBackgroundColor() {
229        return UIHelper.getColorForName(
230                jid != null ? jid.asBareJid().toString() : getDisplayName());
231    }
232
233    @Override
234    public String getAvatarName() {
235        return getDisplayName();
236    }
237
238    public void setExtensions(Extensions extensions) {
239        this.extensions = extensions;
240    }
241}