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.ArrayList;
16import java.util.Collections;
17import java.util.HashMap;
18import java.util.List;
19import java.util.Locale;
20import java.util.Map;
21
22public class Bookmark extends Element implements ListItem {
23
24 private final Account account;
25 private WeakReference<Conversation> conversation;
26 private Jid jid;
27 protected Extensions extensions = new Extensions();
28
29 public Bookmark(final Account account, final Jid jid) {
30 super("conference");
31 this.jid = jid;
32 this.setAttribute("jid", jid);
33 this.account = account;
34 }
35
36 public Bookmark(Account account) {
37 super("conference");
38 this.account = account;
39 }
40
41 public static Map<Jid, Bookmark> parseFromStorage(
42 final Storage storage, final Account account) {
43 // TODO refactor to use extensions. get rid of the 'old' handling
44 if (storage == null) {
45 return Collections.emptyMap();
46 }
47 final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
48 for (final Element item : storage.getChildren()) {
49 if (item.getName().equals("conference")) {
50 final Bookmark bookmark = Bookmark.parse(item, account);
51 if (bookmark != null) {
52 final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
53 if (old != null
54 && old.getBookmarkName() != null
55 && bookmark.getBookmarkName() == null) {
56 bookmark.setBookmarkName(old.getBookmarkName());
57 }
58 }
59 }
60 }
61 return bookmarks;
62 }
63
64 public static Bookmark parse(Element element, Account account) {
65 Bookmark bookmark = new Bookmark(account);
66 bookmark.setAttributes(element.getAttributes());
67 bookmark.setChildren(element.getChildren());
68 bookmark.jid = Jid.Invalid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
69 if (bookmark.jid == null) {
70 return null;
71 }
72 return bookmark;
73 }
74
75 public Extensions getExtensions() {
76 return extensions;
77 }
78
79 public void addGroup(final String group) {
80 addChild("group", "jabber:iq:roster").setContent(group);
81 extensions.addChild("group", "jabber:iq:roster").setContent(group);
82 }
83
84 public void setGroups(List<String> groups) {
85 final List<Element> children = ImmutableList.copyOf(getChildren());
86 for (final Element el : children) {
87 if (el.getName().equals("group")) {
88 removeChild(el);
89 }
90 }
91
92 final List<Element> extChildren = ImmutableList.copyOf(extensions.getChildren());
93 for (final Element el : extChildren) {
94 if (el.getName().equals("group")) {
95 extensions.removeChild(el);
96 }
97 }
98
99 for (final String group : groups) {
100 addGroup(group);
101 }
102 }
103
104 public void setAutojoin(boolean autojoin) {
105 if (autojoin) {
106 this.setAttribute("autojoin", "true");
107 } else {
108 this.setAttribute("autojoin", "false");
109 }
110 }
111
112 @Override
113 public int compareTo(final @NonNull ListItem another) {
114 if (getJid().isDomainJid() && !another.getJid().isDomainJid()) {
115 return -1;
116 } else if (!getJid().isDomainJid() && another.getJid().isDomainJid()) {
117 return 1;
118 }
119
120 final var anotherName =
121 another.getDisplayName() == null ? "" : another.getDisplayName();
122 final var displayNameScore = getDisplayName().compareToIgnoreCase(anotherName);
123
124 if (displayNameScore == 0) {
125 return getJid().compareTo(another.getJid());
126 }
127
128 return displayNameScore;
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 return getFullJid(getNick(), true);
160 }
161
162 private Jid getFullJid(final String nick, boolean tryFix) {
163 try {
164 return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
165 } catch (final IllegalArgumentException e) {
166 try {
167 return tryFix ? getFullJid(gnu.inet.encoding.Punycode.encode(nick), false) : null;
168 } catch (final Exception e2) {
169 return null;
170 }
171 }
172 }
173
174 public List<Tag> getGroupTags() {
175 ArrayList<Tag> tags = new ArrayList<>();
176
177 for (Element element : getChildren()) {
178 if (element.getName().equals("group") && element.getContent() != null) {
179 String group = element.getContent();
180 tags.add(new Tag(group));
181 }
182 }
183
184 return tags;
185 }
186
187 @Override
188 public List<Tag> getTags(Context context) {
189 ArrayList<Tag> tags = new ArrayList<>();
190 tags.add(new Tag("Channel"));
191 tags.addAll(getGroupTags());
192 return tags;
193 }
194
195 public String getNick() {
196 return Strings.emptyToNull(this.findChildContent("nick"));
197 }
198
199 public void setNick(String nick) {
200 Element element = this.findChild("nick");
201 if (element == null) {
202 element = this.addChild("nick");
203 }
204 element.setContent(nick);
205 }
206
207 public boolean autojoin() {
208 return this.getAttributeAsBoolean("autojoin");
209 }
210
211 public String getPassword() {
212 return this.findChildContent("password");
213 }
214
215 public void setPassword(String password) {
216 Element element = this.findChild("password");
217 if (element != null) {
218 element.setContent(password);
219 }
220 }
221
222 @Override
223 public boolean match(Context context, String needle) {
224 if (needle == null) {
225 return true;
226 }
227 needle = needle.toLowerCase(Locale.US);
228 String[] parts = needle.split("[,\\s]+");
229 if (parts.length > 1) {
230 for (String part : parts) {
231 if (!match(context, part)) {
232 return false;
233 }
234 }
235 return true;
236 } else if (parts.length > 0) {
237 final Jid jid = getJid();
238 return (jid != null && jid.toString().contains(parts[0])) ||
239 getDisplayName().toLowerCase(Locale.US).contains(parts[0]) ||
240 matchInTag(context, parts[0]);
241 } else {
242 final Jid jid = getJid();
243 return (jid != null && jid.toString().contains(needle)) ||
244 getDisplayName().toLowerCase(Locale.US).contains(needle);
245 }
246 }
247
248 private boolean matchInTag(Context context, String needle) {
249 needle = needle.toLowerCase(Locale.US);
250 for (Tag tag : getTags(context)) {
251 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
252 return true;
253 }
254 }
255 return false;
256 }
257
258 public Account getAccount() {
259 return this.account;
260 }
261
262 public synchronized Conversation getConversation() {
263 return this.conversation != null ? this.conversation.get() : null;
264 }
265
266 public synchronized void setConversation(Conversation conversation) {
267 if (this.conversation != null) {
268 this.conversation.clear();
269 }
270 if (conversation == null) {
271 this.conversation = null;
272 } else {
273 this.conversation = new WeakReference<>(conversation);
274 }
275 }
276
277 public String getBookmarkName() {
278 return this.getAttribute("name");
279 }
280
281 public boolean setBookmarkName(String name) {
282 String before = getBookmarkName();
283 if (name != null) {
284 this.setAttribute("name", name);
285 } else {
286 this.removeAttribute("name");
287 }
288 return StringUtils.changed(before, name);
289 }
290
291 @Override
292 public int getAvatarBackgroundColor() {
293 return UIHelper.getColorForName(
294 jid != null ? jid.asBareJid().toString() : getDisplayName());
295 }
296
297 @Override
298 public String getAvatarName() {
299 return getDisplayName();
300 }
301
302 public void setExtensions(Extensions extensions) {
303 this.extensions = extensions;
304 }
305}