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.List;
10import java.util.Locale;
11
12import eu.siacs.conversations.utils.StringUtils;
13import eu.siacs.conversations.utils.UIHelper;
14import eu.siacs.conversations.xml.Element;
15import eu.siacs.conversations.xmpp.InvalidJid;
16import rocks.xmpp.addr.Jid;
17
18public class Bookmark extends Element implements ListItem {
19
20 private Account account;
21 private WeakReference<Conversation> conversation;
22 private Jid jid;
23
24 public Bookmark(final Account account, final Jid jid) {
25 super("conference");
26 this.jid = jid;
27 this.setAttribute("jid", jid.toString());
28 this.account = account;
29 }
30
31 private Bookmark(Account account) {
32 super("conference");
33 this.account = account;
34 }
35
36 public static Bookmark parse(Element element, Account account) {
37 Bookmark bookmark = new Bookmark(account);
38 bookmark.setAttributes(element.getAttributes());
39 bookmark.setChildren(element.getChildren());
40 bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
41 return bookmark;
42 }
43
44 public void setAutojoin(boolean autojoin) {
45 if (autojoin) {
46 this.setAttribute("autojoin", "true");
47 } else {
48 this.setAttribute("autojoin", "false");
49 }
50 }
51
52 @Override
53 public int compareTo(final @NonNull ListItem another) {
54 return this.getDisplayName().compareToIgnoreCase(
55 another.getDisplayName());
56 }
57
58 @Override
59 public String getDisplayName() {
60 final Conversation c = getConversation();
61 final String name = getBookmarkName();
62 if (c != null) {
63 return c.getName().toString();
64 } else if (printableValue(name, false)) {
65 return name.trim();
66 } else {
67 Jid jid = this.getJid();
68 return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
69 }
70 }
71
72 public static boolean printableValue(@Nullable String value, boolean permitNone) {
73 return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
74 }
75
76 public static boolean printableValue(@Nullable String value) {
77 return printableValue(value, true);
78 }
79
80 @Override
81 public Jid getJid() {
82 return this.jid;
83 }
84
85 public Jid getFullJid() {
86 final String nick = getNick();
87 return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
88 }
89
90 @Override
91 public List<Tag> getTags(Context context) {
92 ArrayList<Tag> tags = new ArrayList<>();
93 for (Element element : getChildren()) {
94 if (element.getName().equals("group") && element.getContent() != null) {
95 String group = element.getContent();
96 tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
97 }
98 }
99 return tags;
100 }
101
102 public String getNick() {
103 return this.findChildContent("nick");
104 }
105
106 public void setNick(String nick) {
107 Element element = this.findChild("nick");
108 if (element == null) {
109 element = this.addChild("nick");
110 }
111 element.setContent(nick);
112 }
113
114 public boolean autojoin() {
115 return this.getAttributeAsBoolean("autojoin");
116 }
117
118 public String getPassword() {
119 return this.findChildContent("password");
120 }
121
122 public void setPassword(String password) {
123 Element element = this.findChild("password");
124 if (element != null) {
125 element.setContent(password);
126 }
127 }
128
129 @Override
130 public boolean match(Context context, String needle) {
131 if (needle == null) {
132 return true;
133 }
134 needle = needle.toLowerCase(Locale.US);
135 final Jid jid = getJid();
136 return (jid != null && jid.toString().contains(needle)) ||
137 getDisplayName().toLowerCase(Locale.US).contains(needle) ||
138 matchInTag(context, needle);
139 }
140
141 private boolean matchInTag(Context context, String needle) {
142 needle = needle.toLowerCase(Locale.US);
143 for (Tag tag : getTags(context)) {
144 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151 public Account getAccount() {
152 return this.account;
153 }
154
155 public synchronized Conversation getConversation() {
156 return this.conversation != null ? this.conversation.get() : null;
157 }
158
159 public synchronized void setConversation(Conversation conversation) {
160 if (this.conversation != null) {
161 this.conversation.clear();
162 }
163 if (conversation == null) {
164 this.conversation = null;
165 } else {
166 this.conversation = new WeakReference<>(conversation);
167 conversation.getMucOptions().notifyOfBookmarkNick(getNick());
168 }
169 }
170
171 public String getBookmarkName() {
172 return this.getAttribute("name");
173 }
174
175 public boolean setBookmarkName(String name) {
176 String before = getBookmarkName();
177 if (name != null) {
178 this.setAttribute("name", name);
179 } else {
180 this.removeAttribute("name");
181 }
182 return StringUtils.changed(before, name);
183 }
184
185 @Override
186 public int getAvatarBackgroundColor() {
187 return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
188 }
189}