1package eu.siacs.conversations.entities;
 2
 3import java.util.ArrayList;
 4import java.util.HashMap;
 5import java.util.Iterator;
 6import java.util.List;
 7
 8import eu.siacs.conversations.android.AbstractPhoneContact;
 9import eu.siacs.conversations.xmpp.Jid;
10
11
12public class Roster {
13	private final Account account;
14	private final HashMap<Jid, Contact> contacts = new HashMap<>();
15	private String version = null;
16
17	public Roster(Account account) {
18		this.account = account;
19	}
20
21	public Contact getContactFromContactList(Jid jid) {
22		if (jid == null) {
23			return null;
24		}
25		synchronized (this.contacts) {
26			Contact contact = contacts.get(jid.asBareJid());
27			if (contact != null && contact.showInContactList()) {
28				return contact;
29			} else {
30				return null;
31			}
32		}
33	}
34
35	public Contact getContact(final Jid jid) {
36		synchronized (this.contacts) {
37			if (!contacts.containsKey(jid.asBareJid())) {
38				Contact contact = new Contact(jid.asBareJid());
39				contact.setAccount(account);
40				contacts.put(contact.getJid().asBareJid(), contact);
41				return contact;
42			}
43			return contacts.get(jid.asBareJid());
44		}
45	}
46
47	public void clearPresences() {
48		for (Contact contact : getContacts()) {
49			contact.clearPresences();
50		}
51	}
52
53	public void markAllAsNotInRoster() {
54		for (Contact contact : getContacts()) {
55			contact.resetOption(Contact.Options.IN_ROSTER);
56		}
57	}
58
59	public List<Contact> getWithSystemAccounts(Class<?extends AbstractPhoneContact> clazz) {
60		int option = Contact.getOption(clazz);
61		List<Contact> with = getContacts();
62		for(Iterator<Contact> iterator = with.iterator(); iterator.hasNext();) {
63			Contact contact = iterator.next();
64			if (!contact.getOption(option)) {
65				iterator.remove();
66			}
67		}
68		return with;
69	}
70
71	public List<Contact> getContacts() {
72		synchronized (this.contacts) {
73			return new ArrayList<>(this.contacts.values());
74		}
75	}
76
77	public void initContact(final Contact contact) {
78		if (contact == null) {
79			return;
80		}
81		contact.setAccount(account);
82		synchronized (this.contacts) {
83			contacts.put(contact.getJid().asBareJid(), contact);
84		}
85	}
86
87	public void setVersion(String version) {
88		this.version = version;
89	}
90
91	public String getVersion() {
92		return this.version;
93	}
94
95	public Account getAccount() {
96		return this.account;
97	}
98}