2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public class Roster {
|
|
|
|
Account account;
|
2014-11-05 20:55:47 +00:00
|
|
|
final ConcurrentHashMap<String, Contact> contacts = new ConcurrentHashMap<>();
|
2014-10-22 16:38:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Contact getContact(final Jid jid) {
|
|
|
|
final Jid bareJid = jid.toBareJid();
|
|
|
|
if (contacts.containsKey(bareJid.toString())) {
|
|
|
|
return contacts.get(bareJid.toString());
|
2014-10-22 16:38:44 +00:00
|
|
|
} else {
|
2014-11-05 20:55:47 +00:00
|
|
|
Contact contact = new Contact(bareJid);
|
2014-10-22 16:38:44 +00:00
|
|
|
contact.setAccount(account);
|
2014-11-05 20:55:47 +00:00
|
|
|
contacts.put(bareJid.toString(), contact);
|
2014-10-22 16:38:44 +00:00
|
|
|
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() {
|
2014-11-05 20:55:47 +00:00
|
|
|
return new ArrayList<>(this.contacts.values());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public void initContact(final Contact contact) {
|
2014-10-22 16:38:44 +00:00
|
|
|
contact.setAccount(account);
|
|
|
|
contact.setOption(Contact.Options.IN_ROSTER);
|
2014-11-05 20:55:47 +00:00
|
|
|
contacts.put(contact.getJid().toBareJid().toString(), contact);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setVersion(String version) {
|
|
|
|
this.version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getVersion() {
|
|
|
|
return this.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Account getAccount() {
|
|
|
|
return this.account;
|
|
|
|
}
|
|
|
|
}
|