Bookmark.java

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