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