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