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 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}