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.xmpp.jid.Jid;
9
10public class Roster {
11 final Account account;
12 final HashMap<Jid, Contact> contacts = new HashMap<>();
13 private String version = null;
14
15 public Roster(Account account) {
16 this.account = account;
17 }
18
19 public Contact getContactFromRoster(Jid jid) {
20 if (jid == null) {
21 return null;
22 }
23 synchronized (this.contacts) {
24 Contact contact = contacts.get(jid.toBareJid());
25 if (contact != null && contact.showInRoster()) {
26 return contact;
27 } else {
28 return null;
29 }
30 }
31 }
32
33 public Contact getContact(final Jid jid) {
34 synchronized (this.contacts) {
35 if (!contacts.containsKey(jid.toBareJid())) {
36 Contact contact = new Contact(jid.toBareJid());
37 contact.setAccount(account);
38 contacts.put(contact.getJid().toBareJid(), contact);
39 return contact;
40 }
41 return contacts.get(jid.toBareJid());
42 }
43 }
44
45 public void clearPresences() {
46 for (Contact contact : getContacts()) {
47 contact.clearPresences();
48 }
49 }
50
51 public void markAllAsNotInRoster() {
52 for (Contact contact : getContacts()) {
53 contact.resetOption(Contact.Options.IN_ROSTER);
54 }
55 }
56
57 public List<Contact> getWithSystemAccounts() {
58 List<Contact> with = getContacts();
59 for(Iterator<Contact> iterator = with.iterator(); iterator.hasNext();) {
60 Contact contact = iterator.next();
61 if (contact.getSystemAccount() == null) {
62 iterator.remove();
63 }
64 }
65 return with;
66 }
67
68 public List<Contact> getContacts() {
69 synchronized (this.contacts) {
70 return new ArrayList<>(this.contacts.values());
71 }
72 }
73
74 public void initContact(final Contact contact) {
75 if (contact == null) {
76 return;
77 }
78 contact.setAccount(account);
79 contact.setOption(Contact.Options.IN_ROSTER);
80 synchronized (this.contacts) {
81 contacts.put(contact.getJid().toBareJid(), contact);
82 }
83 }
84
85 public void setVersion(String version) {
86 this.version = version;
87 }
88
89 public String getVersion() {
90 return this.version;
91 }
92
93 public Account getAccount() {
94 return this.account;
95 }
96}