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