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 boolean autojoin;
17 private Conversation mJoinedConversation;
18
19 public Bookmark(Account account) {
20 this.account = account;
21 }
22
23 public static Bookmark parse(Element element, Account account) {
24 Bookmark bookmark = new Bookmark(account);
25 bookmark.setJid(element.getAttribute("jid"));
26 bookmark.setName(element.getAttribute("name"));
27 String autojoin = element.getAttribute("autojoin");
28 if (autojoin!=null && (autojoin.equals("true")||autojoin.equals("1"))) {
29 bookmark.setAutojoin(true);
30 } else {
31 bookmark.setAutojoin(false);
32 }
33 Element nick = element.findChild("nick");
34 if (nick!=null) {
35 bookmark.setNick(nick.getContent());
36 }
37 return bookmark;
38 }
39
40 public void setAutojoin(boolean autojoin) {
41 this.autojoin = autojoin;
42 }
43
44 public void setName(String name) {
45 this.name = name;
46 }
47
48 public void setJid(String jid) {
49 this.jid = jid;
50 }
51
52 public void setNick(String nick) {
53 this.nick = nick;
54 }
55
56 @Override
57 public int compareTo(ListItem another) {
58 return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
59 }
60
61 @Override
62 public String getDisplayName() {
63 if (this.mJoinedConversation!=null) {
64 return this.mJoinedConversation.getName(true);
65 } else if (name!=null) {
66 return name;
67 } else {
68 return this.jid.split("@")[0];
69 }
70 }
71
72 @Override
73 public String getJid() {
74 return this.jid.toLowerCase(Locale.US);
75 }
76
77 public String getNick() {
78 return this.nick;
79 }
80
81 public boolean autojoin() {
82 return autojoin;
83 }
84
85 public boolean match(String needle) {
86 return needle == null
87 || getJid().contains(needle.toLowerCase(Locale.US))
88 || getDisplayName().toLowerCase(Locale.US)
89 .contains(needle.toLowerCase(Locale.US));
90 }
91
92 public Account getAccount() {
93 return this.account;
94 }
95
96 @Override
97 public Bitmap getImage(int dpSize, Context context) {
98 if (this.mJoinedConversation==null) {
99 return UIHelper.getContactPicture(getDisplayName(), dpSize, context, false);
100 } else {
101 return UIHelper.getContactPicture(this.mJoinedConversation, dpSize, context, false);
102 }
103 }
104
105 public void setConversation(Conversation conversation) {
106 this.mJoinedConversation = conversation;
107 }
108
109 public String getName() {
110 return name;
111 }
112}