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