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