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 bookmark.extensions = extensions;
109 }
110 return bookmark;
111 }
112
113 public Element getExtensions() {
114 return extensions;
115 }
116
117 public void setAutojoin(boolean autojoin) {
118 if (autojoin) {
119 this.setAttribute("autojoin", "true");
120 } else {
121 this.setAttribute("autojoin", "false");
122 }
123 }
124
125 @Override
126 public int compareTo(final @NonNull ListItem another) {
127 return this.getDisplayName().compareToIgnoreCase(
128 another.getDisplayName());
129 }
130
131 @Override
132 public String getDisplayName() {
133 final Conversation c = getConversation();
134 final String name = getBookmarkName();
135 if (c != null) {
136 return c.getName().toString();
137 } else if (printableValue(name, false)) {
138 return name.trim();
139 } else {
140 Jid jid = this.getJid();
141 return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
142 }
143 }
144
145 public static boolean printableValue(@Nullable String value, boolean permitNone) {
146 return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
147 }
148
149 public static boolean printableValue(@Nullable String value) {
150 return printableValue(value, true);
151 }
152
153 @Override
154 public Jid getJid() {
155 return this.jid;
156 }
157
158 public Jid getFullJid() {
159 final String nick = getNick();
160 return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
161 }
162
163 @Override
164 public List<Tag> getTags(Context context) {
165 ArrayList<Tag> tags = new ArrayList<>();
166 for (Element element : getChildren()) {
167 if (element.getName().equals("group") && element.getContent() != null) {
168 String group = element.getContent();
169 tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
170 }
171 }
172 return tags;
173 }
174
175 public String getNick() {
176 return this.findChildContent("nick");
177 }
178
179 public void setNick(String nick) {
180 Element element = this.findChild("nick");
181 if (element == null) {
182 element = this.addChild("nick");
183 }
184 element.setContent(nick);
185 }
186
187 public boolean autojoin() {
188 return this.getAttributeAsBoolean("autojoin");
189 }
190
191 public String getPassword() {
192 return this.findChildContent("password");
193 }
194
195 public void setPassword(String password) {
196 Element element = this.findChild("password");
197 if (element != null) {
198 element.setContent(password);
199 }
200 }
201
202 @Override
203 public boolean match(Context context, String needle) {
204 if (needle == null) {
205 return true;
206 }
207 needle = needle.toLowerCase(Locale.US);
208 final Jid jid = getJid();
209 return (jid != null && jid.toString().contains(needle)) ||
210 getDisplayName().toLowerCase(Locale.US).contains(needle) ||
211 matchInTag(context, needle);
212 }
213
214 private boolean matchInTag(Context context, String needle) {
215 needle = needle.toLowerCase(Locale.US);
216 for (Tag tag : getTags(context)) {
217 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
218 return true;
219 }
220 }
221 return false;
222 }
223
224 public Account getAccount() {
225 return this.account;
226 }
227
228 public synchronized Conversation getConversation() {
229 return this.conversation != null ? this.conversation.get() : null;
230 }
231
232 public synchronized void setConversation(Conversation conversation) {
233 if (this.conversation != null) {
234 this.conversation.clear();
235 }
236 if (conversation == null) {
237 this.conversation = null;
238 } else {
239 this.conversation = new WeakReference<>(conversation);
240 conversation.getMucOptions().notifyOfBookmarkNick(getNick());
241 }
242 }
243
244 public String getBookmarkName() {
245 return this.getAttribute("name");
246 }
247
248 public boolean setBookmarkName(String name) {
249 String before = getBookmarkName();
250 if (name != null) {
251 this.setAttribute("name", name);
252 } else {
253 this.removeAttribute("name");
254 }
255 return StringUtils.changed(before, name);
256 }
257
258 @Override
259 public int getAvatarBackgroundColor() {
260 return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
261 }
262
263 @Override
264 public String getAvatarName() {
265 return getDisplayName();
266 }
267}