Roster.java

 1package eu.siacs.conversations.entities;
 2
 3import java.util.ArrayList;
 4import java.util.List;
 5import java.util.concurrent.ConcurrentHashMap;
 6
 7import eu.siacs.conversations.xmpp.jid.Jid;
 8
 9public class Roster {
10	Account account;
11	final ConcurrentHashMap<String, Contact> contacts = new ConcurrentHashMap<>();
12	private String version = null;
13
14	public Roster(Account account) {
15		this.account = account;
16	}
17
18	public Contact getContactFromRoster(String jid) {
19		if (jid == null) {
20			return null;
21		}
22		String cleanJid = jid.split("/", 2)[0];
23		Contact contact = contacts.get(cleanJid);
24		if (contact != null && contact.showInRoster()) {
25			return contact;
26		} else {
27			return null;
28		}
29	}
30
31	public Contact getContact(final Jid jid) {
32		final Jid bareJid = jid.toBareJid();
33		if (contacts.containsKey(bareJid.toString())) {
34			return contacts.get(bareJid.toString());
35		} else {
36			Contact contact = new Contact(bareJid);
37			contact.setAccount(account);
38			contacts.put(bareJid.toString(), contact);
39			return contact;
40		}
41	}
42
43	public void clearPresences() {
44		for (Contact contact : getContacts()) {
45			contact.clearPresences();
46		}
47	}
48
49	public void markAllAsNotInRoster() {
50		for (Contact contact : getContacts()) {
51			contact.resetOption(Contact.Options.IN_ROSTER);
52		}
53	}
54
55	public void clearSystemAccounts() {
56		for (Contact contact : getContacts()) {
57			contact.setPhotoUri(null);
58			contact.setSystemName(null);
59			contact.setSystemAccount(null);
60		}
61	}
62
63	public List<Contact> getContacts() {
64		return new ArrayList<>(this.contacts.values());
65	}
66
67	public void initContact(final Contact contact) {
68		contact.setAccount(account);
69		contact.setOption(Contact.Options.IN_ROSTER);
70		contacts.put(contact.getJid().toBareJid().toString(), contact);
71	}
72
73	public void setVersion(String version) {
74		this.version = version;
75	}
76
77	public String getVersion() {
78		return this.version;
79	}
80
81	public Account getAccount() {
82		return this.account;
83	}
84}