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