Bookmark.java

  1package eu.siacs.conversations.entities;
  2
  3import android.content.Context;
  4import android.support.annotation.NonNull;
  5import android.support.annotation.Nullable;
  6import android.util.Log;
  7
  8import java.lang.ref.WeakReference;
  9import java.util.ArrayList;
 10import java.util.Collection;
 11import java.util.Collections;
 12import java.util.HashMap;
 13import java.util.List;
 14import java.util.Locale;
 15import java.util.Map;
 16import java.util.concurrent.CopyOnWriteArrayList;
 17
 18import eu.siacs.conversations.Config;
 19import eu.siacs.conversations.utils.StringUtils;
 20import eu.siacs.conversations.utils.UIHelper;
 21import eu.siacs.conversations.xml.Element;
 22import eu.siacs.conversations.xml.Namespace;
 23import eu.siacs.conversations.xmpp.InvalidJid;
 24import rocks.xmpp.addr.Jid;
 25
 26public class Bookmark extends Element implements ListItem {
 27
 28	private Account account;
 29	private WeakReference<Conversation> conversation;
 30	private Jid jid;
 31
 32	public Bookmark(final Account account, final Jid jid) {
 33		super("conference");
 34		this.jid = jid;
 35		this.setAttribute("jid", jid.toString());
 36		this.account = account;
 37	}
 38
 39	private Bookmark(Account account) {
 40		super("conference");
 41		this.account = account;
 42	}
 43
 44	public static Collection<Bookmark> parseFromStorage(Element storage, Account account) {
 45		if (storage == null) {
 46			return Collections.emptyList();
 47		}
 48		final HashMap<Jid, Bookmark> bookmarks = new HashMap<>();
 49		for (final Element item : storage.getChildren()) {
 50			if (item.getName().equals("conference")) {
 51				final Bookmark bookmark = Bookmark.parse(item, account);
 52				if (bookmark != null) {
 53					final Bookmark old = bookmarks.put(bookmark.getJid(), bookmark);
 54					if (old != null && old.getBookmarkName() != null && bookmark.getBookmarkName() == null) {
 55						bookmark.setBookmarkName(old.getBookmarkName());
 56					}
 57				}
 58			}
 59		}
 60		return bookmarks.values();
 61	}
 62
 63	public static Collection<Bookmark> parseFromPubsub(Element pubsub, Account account) {
 64		if (pubsub == null) {
 65			return Collections.emptyList();
 66		}
 67		final Element items = pubsub.findChild("items");
 68		if (items != null && Namespace.BOOKMARK.equals(items.getAttribute("node"))) {
 69			final List<Bookmark> bookmarks = new ArrayList<>();
 70			for(Element item : items.getChildren()) {
 71				if (item.getName().equals("item")) {
 72					final Bookmark bookmark = Bookmark.parseFromItem(item, account);
 73					if (bookmark != null) {
 74						bookmarks.add(bookmark);
 75					}
 76				}
 77			}
 78			return bookmarks;
 79		}
 80		return Collections.emptyList();
 81	}
 82
 83	public static Bookmark parse(Element element, Account account) {
 84		Bookmark bookmark = new Bookmark(account);
 85		bookmark.setAttributes(element.getAttributes());
 86		bookmark.setChildren(element.getChildren());
 87		bookmark.jid = InvalidJid.getNullForInvalid(bookmark.getAttributeAsJid("jid"));
 88		if (bookmark.jid == null) {
 89			return null;
 90		}
 91		return bookmark;
 92	}
 93
 94	public static Bookmark parseFromItem(Element item, Account account) {
 95		final Element conference = item.findChild("conference", Namespace.BOOKMARK);
 96		if (conference == null) {
 97			return null;
 98		}
 99		final Bookmark bookmark = new Bookmark(account);
100		bookmark.jid = InvalidJid.getNullForInvalid(item.getAttributeAsJid("id"));
101		if (bookmark.jid == null) {
102			return null;
103		}
104		bookmark.setBookmarkName(conference.getAttribute("name"));
105		bookmark.setAutojoin(conference.getAttributeAsBoolean("autojoin"));
106		bookmark.setNick(conference.findChildContent("nick"));
107		return bookmark;
108	}
109
110	public void setAutojoin(boolean autojoin) {
111		if (autojoin) {
112			this.setAttribute("autojoin", "true");
113		} else {
114			this.setAttribute("autojoin", "false");
115		}
116	}
117
118	@Override
119	public int compareTo(final @NonNull ListItem another) {
120		return this.getDisplayName().compareToIgnoreCase(
121				another.getDisplayName());
122	}
123
124	@Override
125	public String getDisplayName() {
126		final Conversation c = getConversation();
127		final String name = getBookmarkName();
128		if (c != null) {
129			return c.getName().toString();
130		} else if (printableValue(name, false)) {
131			return name.trim();
132		} else {
133			Jid jid = this.getJid();
134			return jid != null && jid.getLocal() != null ? jid.getLocal() : "";
135		}
136	}
137
138	public static boolean printableValue(@Nullable String value, boolean permitNone) {
139		return value != null && !value.trim().isEmpty() && (permitNone || !"None".equals(value));
140	}
141
142	public static boolean printableValue(@Nullable String value) {
143		return printableValue(value, true);
144	}
145
146	@Override
147	public Jid getJid() {
148		return this.jid;
149	}
150
151	public Jid getFullJid() {
152		final String nick = getNick();
153		return jid == null || nick == null || nick.trim().isEmpty() ? jid : jid.withResource(nick);
154	}
155
156	@Override
157	public List<Tag> getTags(Context context) {
158		ArrayList<Tag> tags = new ArrayList<>();
159		for (Element element : getChildren()) {
160			if (element.getName().equals("group") && element.getContent() != null) {
161				String group = element.getContent();
162				tags.add(new Tag(group, UIHelper.getColorForName(group,true)));
163			}
164		}
165		return tags;
166	}
167
168	public String getNick() {
169		return this.findChildContent("nick");
170	}
171
172	public void setNick(String nick) {
173		Element element = this.findChild("nick");
174		if (element == null) {
175			element = this.addChild("nick");
176		}
177		element.setContent(nick);
178	}
179
180	public boolean autojoin() {
181		return this.getAttributeAsBoolean("autojoin");
182	}
183
184	public String getPassword() {
185		return this.findChildContent("password");
186	}
187
188	public void setPassword(String password) {
189		Element element = this.findChild("password");
190		if (element != null) {
191			element.setContent(password);
192		}
193	}
194
195	@Override
196	public boolean match(Context context, String needle) {
197		if (needle == null) {
198			return true;
199		}
200		needle = needle.toLowerCase(Locale.US);
201		final Jid jid = getJid();
202		return (jid != null && jid.toString().contains(needle)) ||
203			getDisplayName().toLowerCase(Locale.US).contains(needle) ||
204			matchInTag(context, needle);
205	}
206
207	private boolean matchInTag(Context context, String needle) {
208		needle = needle.toLowerCase(Locale.US);
209		for (Tag tag : getTags(context)) {
210			if (tag.getName().toLowerCase(Locale.US).contains(needle)) {
211				return true;
212			}
213		}
214		return false;
215	}
216
217	public Account getAccount() {
218		return this.account;
219	}
220
221	public synchronized Conversation getConversation() {
222		return this.conversation != null ? this.conversation.get() : null;
223	}
224
225	public synchronized void setConversation(Conversation conversation) {
226		if (this.conversation != null) {
227			this.conversation.clear();
228		}
229		if (conversation == null) {
230			this.conversation = null;
231		} else {
232			this.conversation = new WeakReference<>(conversation);
233			conversation.getMucOptions().notifyOfBookmarkNick(getNick());
234		}
235	}
236
237	public String getBookmarkName() {
238		return this.getAttribute("name");
239	}
240
241	public boolean setBookmarkName(String name) {
242		String before = getBookmarkName();
243		if (name != null) {
244			this.setAttribute("name", name);
245		} else {
246			this.removeAttribute("name");
247		}
248		return StringUtils.changed(before, name);
249	}
250
251	@Override
252	public int getAvatarBackgroundColor() {
253		return UIHelper.getColorForName(jid != null ? jid.asBareJid().toString() : getDisplayName());
254	}
255}