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