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.Conference;
14import im.conversations.android.xmpp.model.bookmark2.Extensions;
15import java.lang.ref.WeakReference;
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 private 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 static Bookmark parseFromItem(
76 final String id, final Conference conference, final Account account) {
77 if (id == null || conference == null) {
78 return null;
79 }
80 final Bookmark bookmark = new Bookmark(account);
81 bookmark.jid = Jid.Invalid.getNullForInvalid(Jid.ofOrInvalid(id));
82 // TODO verify that we only use bare jids and ignore full jids
83 if (bookmark.jid == null) {
84 return null;
85 }
86
87 // TODO use proper API
88
89 bookmark.setBookmarkName(conference.getAttribute("name"));
90 bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
91 bookmark.setNick(conference.findChildContent("nick"));
92 bookmark.setPassword(conference.findChildContent("password"));
93 final var extensions = conference.getExtensions();
94 if (extensions != null) {
95 bookmark.extensions = conference.getExtensions();
96 }
97 return bookmark;
98 }
99
100 public Extensions getExtensions() {
101 return extensions;
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 return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
115 }
116
117 @Override
118 public String getDisplayName() {
119 final Conversation c = getConversation();
120 final String name = getBookmarkName();
121 if (c != null) {
122 return c.getName().toString();
123 } else if (printableValue(name, false)) {
124 return name.trim();
125 } else {
126 Jid jid = this.getJid();
127 return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
128 }
129 }
130
131 public static boolean printableValue(@Nullable String value, boolean permitNone) {
132 return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
133 }
134
135 public static boolean printableValue(@Nullable String value) {
136 return printableValue(value, true);
137 }
138
139 @Override
140 public Jid getJid() {
141 return this.jid;
142 }
143
144 public Jid getFullJid() {
145 final String nick = Strings.nullToEmpty(getNick()).trim();
146 if (jid == null || nick.isEmpty()) {
147 return jid;
148 }
149 try {
150 return jid.withResource(nick);
151 } catch (final IllegalArgumentException e) {
152 return jid;
153 }
154 }
155
156 @Override
157 public List<Tag> getTags(final Context context) {
158 final ImmutableList.Builder<Tag> tags = new ImmutableList.Builder<>();
159 for (final Element element : getChildren()) {
160 final String content = element.getContent();
161 if (Strings.isNullOrEmpty(content)) {
162 continue;
163 }
164 if (element.getName().equals("group")) {
165 tags.add(new Tag(content));
166 }
167 }
168 return tags.build();
169 }
170
171 public String getNick() {
172 return Strings.emptyToNull(this.findChildContent("nick"));
173 }
174
175 public void setNick(String nick) {
176 Element element = this.findChild("nick");
177 if (element == null) {
178 element = this.addChild("nick");
179 }
180 element.setContent(nick);
181 }
182
183 public boolean autojoin() {
184 return this.getAttributeAsBoolean("autojoin");
185 }
186
187 public String getPassword() {
188 return this.findChildContent("password");
189 }
190
191 public void setPassword(String password) {
192 Element element = this.findChild("password");
193 if (element != null) {
194 element.setContent(password);
195 }
196 }
197
198 @Override
199 public boolean match(Context context, String needle) {
200 if (needle == null) {
201 return true;
202 }
203 needle = needle.toLowerCase(Locale.US);
204 final Jid jid = getJid();
205 return (jid != null && jid.toString().contains(needle))
206 || getDisplayName().toLowerCase(Locale.US).contains(needle)
207 || matchInTag(context, needle);
208 }
209
210 private boolean matchInTag(Context context, String needle) {
211 needle = needle.toLowerCase(Locale.US);
212 for (Tag tag : getTags(context)) {
213 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
214 return true;
215 }
216 }
217 return false;
218 }
219
220 public Account getAccount() {
221 return this.account;
222 }
223
224 public synchronized Conversation getConversation() {
225 return this.conversation != null ? this.conversation.get() : null;
226 }
227
228 public synchronized void setConversation(Conversation conversation) {
229 if (this.conversation != null) {
230 this.conversation.clear();
231 }
232 if (conversation == null) {
233 this.conversation = null;
234 } else {
235 this.conversation = new WeakReference<>(conversation);
236 }
237 }
238
239 public String getBookmarkName() {
240 return this.getAttribute("name");
241 }
242
243 public boolean setBookmarkName(String name) {
244 String before = getBookmarkName();
245 if (name != null) {
246 this.setAttribute("name", name);
247 } else {
248 this.removeAttribute("name");
249 }
250 return StringUtils.changed(before, name);
251 }
252
253 @Override
254 public int getAvatarBackgroundColor() {
255 return UIHelper.getColorForName(
256 jid != null ? jid.asBareJid().toString() : getDisplayName());
257 }
258
259 @Override
260 public String getAvatarName() {
261 return getDisplayName();
262 }
263}