1package eu.siacs.conversations.entities;
2
3import android.content.Context;
4import android.support.annotation.NonNull;
5import android.support.annotation.Nullable;
6
7import java.lang.ref.WeakReference;
8import java.util.ArrayList;
9import java.util.Collections;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Locale;
13import java.util.Map;
14
15import eu.siacs.conversations.utils.StringUtils;
16import eu.siacs.conversations.utils.UIHelper;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xml.Namespace;
19import eu.siacs.conversations.xmpp.InvalidJid;
20import eu.siacs.conversations.xmpp.Jid;
21
22public class Bookmark extends Element implements ListItem {
23
24 private Account account;
25 private WeakReference<Conversation> conversation;
26 private Jid jid;
27
28 public Bookmark(final Account account, final Jid jid) {
29 super("conference");
30 this.jid = jid;
31 this.setAttribute("jid", jid.toString());
32 this.account = account;
33 }
34
35 private Bookmark(Account account) {
36 super("conference");
37 this.account = account;
38 }
39
40 public static Map<Jid, Bookmark> parseFromStorage(Element storage, Account account) {
41 if (storage == null) {
42 return Collections.emptyMap();
43 }
44 final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
45 for (final Element item : storage.getChildren()) {
46 if (item.getName().equals("conference")) {
47 final Bookmark bookmark = Bookmark.parse(item, account);
48 if (bookmark != null) {
49 final Bookmark old = bookmarks.put(bookmark.jid, bookmark);
50 if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
51 bookmark.setBookmarkName(old.getBookmarkName());
52 }
53 }
54 }
55 }
56 return bookmarks;
57 }
58
59 public static Map<Jid, Bookmark> parseFromPubsub(Element pubsub, Account account) {
60 if (pubsub == null) {
61 return Collections.emptyMap();
62 }
63 final Element items = pubsub.findChild("items");
64 if (items != null && Namespace.BOOKMARKS2.equals(items.getAttribute("node"))) {
65 final Map<Jid, Bookmark> bookmarks = new HashMap<>();
66 for(Element item : items.getChildren()) {
67 if (item.getName().equals("item")) {
68 final Bookmark bookmark = Bookmark.parseFromItem(item, account);
69 if (bookmark != null) {
70 bookmarks.put(bookmark.jid, bookmark);
71 }
72 }
73 }
74 return bookmarks;
75 }
76 return Collections.emptyMap();
77 }
78
79 public static Bookmark parse(Element element, Account account) {
80 Bookmark bookmark = new Bookmark(account);
81 bookmark.setAttributes(element.getAttributes());
82 bookmark.setChildren(element.getChildren());
83 bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
84 if (bookmark.jid == null) {
85 return null;
86 }
87 return bookmark;
88 }
89
90 public static Bookmark parseFromItem(Element item, Account account) {
91 final Element conference = item.findChild("conference", Namespace.BOOKMARKS2);
92 if (conference == null) {
93 return null;
94 }
95 final Bookmark bookmark = new Bookmark(account);
96 bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
97 if (bookmark.jid == null) {
98 return null;
99 }
100 bookmark.setBookmarkName(conference.getAttribute("name"));
101 bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
102 bookmark.setNick(conference.findChildContent("nick"));
103 return bookmark;
104 }
105
106 public void setAutojoin(boolean autojoin) {
107 if (autojoin) {
108 this.setAttribute("autojoin", "true");
109 } else {
110 this.setAttribute("autojoin", "false");
111 }
112 }
113
114 @Override
115 public int compareTo(final @NonNull ListItem another) {
116 return this.getDisplayName().compareToIgnoreCase(
117 another.getDisplayName());
118 }
119
120 @Override
121 public String getDisplayName() {
122 final Conversation c = getConversation();
123 final String name = getBookmarkName();
124 if (c != null) {
125 return c.getName().toString();
126 } else if (printableValue(name, false)) {
127 return name.trim();
128 } else {
129 Jid jid = this.getJid();
130 return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
131 }
132 }
133
134 public static boolean printableValue(@Nullable String value, boolean permitNone) {
135 return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
136 }
137
138 public static boolean printableValue(@Nullable String value) {
139 return printableValue(value, true);
140 }
141
142 @Override
143 public Jid getJid() {
144 return this.jid;
145 }
146
147 public Jid getFullJid() {
148 final String nick = getNick();
149 return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
150 }
151
152 @Override
153 public List<Tag> getTags(Context context) {
154 ArrayList<Tag> tags = new ArrayList<>();
155 for (Element element : getChildren()) {
156 if (element.getName().equals("group") && element.getContent() != null) {
157 String group = element.getContent();
158 tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
159 }
160 }
161 return tags;
162 }
163
164 public String getNick() {
165 return this.findChildContent("nick");
166 }
167
168 public void setNick(String nick) {
169 Element element = this.findChild("nick");
170 if (element == null) {
171 element = this.addChild("nick");
172 }
173 element.setContent(nick);
174 }
175
176 public boolean autojoin() {
177 return this.getAttributeAsBoolean("autojoin");
178 }
179
180 public String getPassword() {
181 return this.findChildContent("password");
182 }
183
184 public void setPassword(String password) {
185 Element element = this.findChild("password");
186 if (element != null) {
187 element.setContent(password);
188 }
189 }
190
191 @Override
192 public boolean match(Context context, String needle) {
193 if (needle == null) {
194 return true;
195 }
196 needle = needle.toLowerCase(Locale.US);
197 final Jid jid = getJid();
198 return (jid != null && jid.toString().contains(needle)) ||
199 getDisplayName().toLowerCase(Locale.US).contains(needle) ||
200 matchInTag(context, needle);
201 }
202
203 private boolean matchInTag(Context context, String needle) {
204 needle = needle.toLowerCase(Locale.US);
205 for (Tag tag : getTags(context)) {
206 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
207 return true;
208 }
209 }
210 return false;
211 }
212
213 public Account getAccount() {
214 return this.account;
215 }
216
217 public synchronized Conversation getConversation() {
218 return this.conversation != null ? this.conversation.get() : null;
219 }
220
221 public synchronized void setConversation(Conversation conversation) {
222 if (this.conversation != null) {
223 this.conversation.clear();
224 }
225 if (conversation == null) {
226 this.conversation = null;
227 } else {
228 this.conversation = new WeakReference<>(conversation);
229 conversation.getMucOptions().notifyOfBookmarkNick(getNick());
230 }
231 }
232
233 public String getBookmarkName() {
234 return this.getAttribute("name");
235 }
236
237 public boolean setBookmarkName(String name) {
238 String before = getBookmarkName();
239 if (name != null) {
240 this.setAttribute("name", name);
241 } else {
242 this.removeAttribute("name");
243 }
244 return StringUtils.changed(before, name);
245 }
246
247 @Override
248 public int getAvatarBackgroundColor() {
249 return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
250 }
251}