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