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 = getNick();
163 return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
164 }
165
166 @Override
167 public List<Tag> getTags(final Context context) {
168 final ImmutableList.Builder<Tag> tags = new ImmutableList.Builder<>();
169 for (final Element element : getChildren()) {
170 final String content = element.getContent();
171 if (Strings.isNullOrEmpty(content)) {
172 continue;
173 }
174 if (element.getName().equals("group")) {
175 tags.add(new Tag(content));
176 }
177 }
178 return tags.build();
179 }
180
181 public String getNick() {
182 return this.findChildContent("nick");
183 }
184
185 public void setNick(String nick) {
186 Element element = this.findChild("nick");
187 if (element == null) {
188 element = this.addChild("nick");
189 }
190 element.setContent(nick);
191 }
192
193 public boolean autojoin() {
194 return this.getAttributeAsBoolean("autojoin");
195 }
196
197 public String getPassword() {
198 return this.findChildContent("password");
199 }
200
201 public void setPassword(String password) {
202 Element element = this.findChild("password");
203 if (element != null) {
204 element.setContent(password);
205 }
206 }
207
208 @Override
209 public boolean match(Context context, String needle) {
210 if (needle == null) {
211 return true;
212 }
213 needle = needle.toLowerCase(Locale.US);
214 final Jid jid = getJid();
215 return (jid != null && jid.toString().contains(needle)) ||
216 getDisplayName().toLowerCase(Locale.US).contains(needle) ||
217 matchInTag(context, needle);
218 }
219
220 private boolean matchInTag(Context context, String needle) {
221 needle = needle.toLowerCase(Locale.US);
222 for (Tag tag : getTags(context)) {
223 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
224 return true;
225 }
226 }
227 return false;
228 }
229
230 public Account getAccount() {
231 return this.account;
232 }
233
234 public synchronized Conversation getConversation() {
235 return this.conversation != null ? this.conversation.get() : null;
236 }
237
238 public synchronized void setConversation(Conversation conversation) {
239 if (this.conversation != null) {
240 this.conversation.clear();
241 }
242 if (conversation == null) {
243 this.conversation = null;
244 } else {
245 this.conversation = new WeakReference<>(conversation);
246 conversation.getMucOptions().notifyOfBookmarkNick(getNick());
247 }
248 }
249
250 public String getBookmarkName() {
251 return this.getAttribute("name");
252 }
253
254 public boolean setBookmarkName(String name) {
255 String before = getBookmarkName();
256 if (name != null) {
257 this.setAttribute("name", name);
258 } else {
259 this.removeAttribute("name");
260 }
261 return StringUtils.changed(before, name);
262 }
263
264 @Override
265 public int getAvatarBackgroundColor() {
266 return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
267 }
268
269 @Override
270 public String getAvatarName() {
271 return getDisplayName();
272 }
273}