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, String jid) {
20 this.account = account;
21 this.jid = jid;
22 }
23
24 public static Bookmark parse(Element element, Account account) {
25 Bookmark bookmark = new Bookmark(account,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 setNick(String nick) {
49 this.nick = nick;
50 }
51
52 @Override
53 public int compareTo(ListItem another) {
54 return this.getDisplayName().compareToIgnoreCase(another.getDisplayName());
55 }
56
57 @Override
58 public String getDisplayName() {
59 if (this.mJoinedConversation!=null && (this.mJoinedConversation.getMucOptions().getSubject() != null)) {
60 return this.mJoinedConversation.getMucOptions().getSubject();
61 } else if (name!=null) {
62 return name;
63 } else {
64 return this.jid.split("@")[0];
65 }
66 }
67
68 @Override
69 public String getJid() {
70 return this.jid.toLowerCase(Locale.US);
71 }
72
73 public String getNick() {
74 return this.nick;
75 }
76
77 public boolean autojoin() {
78 return autojoin;
79 }
80
81 public boolean match(String needle) {
82 return needle == null
83 || getJid().contains(needle.toLowerCase(Locale.US))
84 || getDisplayName().toLowerCase(Locale.US)
85 .contains(needle.toLowerCase(Locale.US));
86 }
87
88 public Account getAccount() {
89 return this.account;
90 }
91
92 @Override
93 public Bitmap getImage(int dpSize, Context context) {
94 if (this.mJoinedConversation==null) {
95 return UIHelper.getContactPicture(getDisplayName(), dpSize, context, false);
96 } else {
97 return UIHelper.getContactPicture(this.mJoinedConversation, dpSize, context, false);
98 }
99 }
100
101 public void setConversation(Conversation conversation) {
102 this.mJoinedConversation = conversation;
103 }
104
105 public String getName() {
106 return name;
107 }
108
109 public Element toElement() {
110 Element element = new Element("conference");
111 element.setAttribute("jid", this.getJid());
112 if (this.getName() != null) {
113 element.setAttribute("name", this.getName());
114 }
115 if (this.autojoin) {
116 element.setAttribute("autojoin", "true");
117 } else {
118 element.setAttribute("autojoin", "false");
119 }
120 if (this.nick != null) {
121 element.addChild("nick").setContent(this.nick);
122 }
123 return element;
124 }
125
126 public void unregisterConversation() {
127 if (this.mJoinedConversation != null) {
128 this.mJoinedConversation.deregisterWithBookmark();
129 }
130 }
131}