aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities/Roster.java
blob: 27d4deb02859103c164f67943fac33b6609cab62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package eu.siacs.conversations.entities;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import eu.siacs.conversations.xmpp.jid.Jid;

public class Roster {
	Account account;
	final ConcurrentHashMap<String, Contact> contacts = new ConcurrentHashMap<>();
	private String version = null;

	public Roster(Account account) {
		this.account = account;
	}

	public Contact getContactFromRoster(String jid) {
		if (jid == null) {
			return null;
		}
		String cleanJid = jid.split("/", 2)[0];
		Contact contact = contacts.get(cleanJid);
		if (contact != null && contact.showInRoster()) {
			return contact;
		} else {
			return null;
		}
	}

	public Contact getContact(final Jid jid) {
		final Jid bareJid = jid.toBareJid();
		if (contacts.containsKey(bareJid.toString())) {
			return contacts.get(bareJid.toString());
		} else {
			Contact contact = new Contact(bareJid);
			contact.setAccount(account);
			contacts.put(bareJid.toString(), contact);
			return contact;
		}
	}

	public void clearPresences() {
		for (Contact contact : getContacts()) {
			contact.clearPresences();
		}
	}

	public void markAllAsNotInRoster() {
		for (Contact contact : getContacts()) {
			contact.resetOption(Contact.Options.IN_ROSTER);
		}
	}

	public void clearSystemAccounts() {
		for (Contact contact : getContacts()) {
			contact.setPhotoUri(null);
			contact.setSystemName(null);
			contact.setSystemAccount(null);
		}
	}

	public List<Contact> getContacts() {
		return new ArrayList<>(this.contacts.values());
	}

	public void initContact(final Contact contact) {
		contact.setAccount(account);
		contact.setOption(Contact.Options.IN_ROSTER);
		contacts.put(contact.getJid().toBareJid().toString(), contact);
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return this.version;
	}

	public Account getAccount() {
		return this.account;
	}
}