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 String autojoin = this.getAttribute("autojoin");
106 return (autojoin != null && (autojoin.equalsIgnoreCase("true") || autojoin
107 .equalsIgnoreCase("1")));
108 }
109
110 public String getPassword() {
111 Element password = this.findChild("password");
112 if (password != null) {
113 return password.getContent();
114 } else {
115 return null;
116 }
117 }
118
119 public void setPassword(String password) {
120 Element element = this.findChild("password");
121 if (element != null) {
122 element.setContent(password);
123 }
124 }
125
126 public boolean match(String needle) {
127 if (needle == null) {
128 return true;
129 }
130 needle = needle.toLowerCase(Locale.US);
131 final Jid jid = getJid();
132 return (jid != null && jid.toString().contains(needle)) ||
133 getDisplayName().toLowerCase(Locale.US).contains(needle) ||
134 matchInTag(needle);
135 }
136
137 private boolean matchInTag(String needle) {
138 needle = needle.toLowerCase(Locale.US);
139 for (Tag tag : getTags()) {
140 if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147 public Account getAccount() {
148 return this.account;
149 }
150
151 public Conversation getConversation() {
152 return this.mJoinedConversation;
153 }
154
155 public void setConversation(Conversation conversation) {
156 this.mJoinedConversation = conversation;
157 }
158
159 public String getName() {
160 return this.getAttribute("name");
161 }
162
163 public void setName(String name) {
164 this.name = name;
165 }
166
167 public void unregisterConversation() {
168 if (this.mJoinedConversation != null) {
169 this.mJoinedConversation.deregisterWithBookmark();
170 }
171 }
172}