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