1package eu.siacs.conversations.entities;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Locale;
6
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 public void setName(String name) {
43 this.name = name;
44 }
45
46 public void setNick(String nick) {
47 Element element = this.findChild("nick");
48 if (element == null) {
49 element = this.addChild("nick");
50 }
51 element.setContent(nick);
52 }
53
54 public void setPassword(String password) {
55 Element element = this.findChild("password");
56 if (element != null) {
57 element.setContent(password);
58 }
59 }
60
61 @Override
62 public int compareTo(final ListItem another) {
63 return this.getDisplayName().compareToIgnoreCase(
64 another.getDisplayName());
65 }
66
67 @Override
68 public String getDisplayName() {
69 if (this.mJoinedConversation != null
70 && (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
71 return this.mJoinedConversation.getMucOptions().getSubject();
72 } else if (getName() != null) {
73 return getName();
74 } else {
75 return this.getJid().getLocalpart();
76 }
77 }
78
79 @Override
80 public Jid getJid() {
81 final String jid = this.getAttribute("jid");
82 if (jid != null) {
83 try {
84 return Jid.fromString(jid);
85 } catch (final InvalidJidException e) {
86 return null;
87 }
88 } else {
89 return null;
90 }
91 }
92
93 @Override
94 public List<Tag> getTags() {
95 return new ArrayList<Tag>();
96 }
97
98 public String getNick() {
99 Element nick = this.findChild("nick");
100 if (nick != null) {
101 return nick.getContent();
102 } else {
103 return null;
104 }
105 }
106
107 public boolean autojoin() {
108 String autojoin = this.getAttribute("autojoin");
109 return (autojoin != null && (autojoin.equalsIgnoreCase("true") || autojoin
110 .equalsIgnoreCase("1")));
111 }
112
113 public String getPassword() {
114 Element password = this.findChild("password");
115 if (password != null) {
116 return password.getContent();
117 } else {
118 return null;
119 }
120 }
121
122 public boolean match(String needle) {
123 return needle == null
124 || getJid().toString().toLowerCase(Locale.US).contains(needle.toLowerCase(Locale.US))
125 || getDisplayName().toLowerCase(Locale.US).contains(
126 needle.toLowerCase(Locale.US));
127 }
128
129 public Account getAccount() {
130 return this.account;
131 }
132
133 public void setConversation(Conversation conversation) {
134 this.mJoinedConversation = conversation;
135 }
136
137 public Conversation getConversation() {
138 return this.mJoinedConversation;
139 }
140
141 public String getName() {
142 return this.getAttribute("name");
143 }
144
145 public void unregisterConversation() {
146 if (this.mJoinedConversation != null) {
147 this.mJoinedConversation.deregisterWithBookmark();
148 }
149 }
150}