use of org.jivesoftware.smack.roster.RosterEntry in project xabber-android by redsolution.
the class RosterManager method setName.
public void setName(String account, String bareAddress, final String name) {
final Roster roster = getRoster(account);
if (roster == null) {
return;
}
final RosterEntry entry = roster.getEntry(bareAddress);
if (entry == null) {
return;
}
try {
entry.setName(name.trim());
} catch (SmackException.NotConnectedException e) {
Application.getInstance().onError(R.string.NOT_CONNECTED);
} catch (SmackException.NoResponseException e) {
Application.getInstance().onError(R.string.CONNECTION_FAILED);
} catch (XMPPException.XMPPErrorException e) {
Application.getInstance().onError(R.string.XMPP_EXCEPTION);
}
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class ChatRoomImpl method processPacket.
/**
* Process incoming packets.
*
* @param stanza - the packet to process
*/
public void processPacket(final Stanza stanza) {
final Runnable runnable = () -> {
try {
if (stanza instanceof Presence) {
Presence.Type oldType = presence.getType();
presence = (Presence) stanza;
final Presence presence1 = (Presence) stanza;
ContactList list = SparkManager.getWorkspace().getContactList();
ContactItem contactItem = list.getContactItemByJID(getParticipantJID());
String time = DateFormat.getTimeInstance(DateFormat.SHORT).format(new Date());
if (presence1.getType() == Presence.Type.unavailable && contactItem != null) {
getTranscriptWindow().insertNotificationMessage("*** " + Res.getString("message.went.offline", participantNickname, time), ChatManager.NOTIFICATION_COLOR);
} else if (oldType == Presence.Type.unavailable && presence1.getType() == Presence.Type.available) {
getTranscriptWindow().insertNotificationMessage("*** " + Res.getString("message.came.online", participantNickname, time), ChatManager.NOTIFICATION_COLOR);
}
} else if (stanza instanceof Message) {
lastActivity = System.currentTimeMillis();
// Do something with the incoming packet here.
final Message message = (Message) stanza;
fireReceivingIncomingMessage(message);
if (message.getError() != null) {
if (message.getError().getCondition() == XMPPError.Condition.item_not_found) {
// Check to see if the user is online to recieve this message.
RosterEntry entry = roster.getEntry(participantJID);
if (!presence.isAvailable() && !offlineSent && entry != null) {
getTranscriptWindow().insertNotificationMessage(Res.getString("message.offline.error"), ChatManager.ERROR_COLOR);
offlineSent = true;
}
}
return;
}
// Check to see if the user is online to recieve this message.
RosterEntry entry = roster.getEntry(participantJID);
if (!presence.isAvailable() && !offlineSent && entry != null) {
getTranscriptWindow().insertNotificationMessage(Res.getString("message.offline"), ChatManager.ERROR_COLOR);
offlineSent = true;
}
if (threadID == null) {
threadID = message.getThread();
if (threadID == null) {
threadID = StringUtils.randomString(6);
}
}
final JivePropertiesExtension extension = ((JivePropertiesExtension) message.getExtension(JivePropertiesExtension.NAMESPACE));
final boolean broadcast = extension != null && extension.getProperty("broadcast") != null;
// If this is a group chat message, discard
if (message.getType() == Message.Type.groupchat || broadcast || message.getType() == Message.Type.normal || message.getType() == Message.Type.headline) {
return;
}
// Do not accept Administrative messages.
final String host = SparkManager.getSessionManager().getServerAddress();
if (host.equals(message.getFrom())) {
return;
}
final CarbonExtension carbon = (CarbonExtension) message.getExtension(CarbonExtension.NAMESPACE);
if (carbon != null) {
// Is the a carbon copy?
final Message forwardedStanza = (Message) carbon.getForwarded().getForwardedPacket();
if (forwardedStanza.getBody() != null) {
if (carbon.getDirection() == CarbonExtension.Direction.received) {
// This is a stanza that we received from someone on one of our other clients.
participantJID = forwardedStanza.getFrom();
insertMessage(forwardedStanza);
} else {
// This is a stanza that one of our own clients sent.
participantJID = forwardedStanza.getTo();
displaySendMessage(forwardedStanza);
}
showTyping(false);
}
} else if (message.getBody() != null) {
// If the message is not from the current agent. Append to chat.
participantJID = message.getFrom();
insertMessage(message);
showTyping(false);
}
}
} catch (Exception e) {
Log.error("An exception occurred while processing this incoming stanza: " + stanza, e);
}
};
SwingUtilities.invokeLater(runnable);
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class ContactListAssistantPlugin method addContactItem.
/**
* Copies or moves a new <code>ContactItem</code> into the <code>ContactGroup</code>.
*
* @param contactGroup the ContactGroup.
* @param item the ContactItem to move.
* @param move true if the ContactItem should be moved, otherwise false.
*/
private void addContactItem(final ContactGroup contactGroup, final ContactItem item, final boolean move) {
ContactItem newContact = UIComponentRegistry.createContactItem(item.getAlias(), item.getNickname(), item.getJID());
newContact.setPresence(item.getPresence());
newContact.setIcon(item.getIcon());
newContact.getNicknameLabel().setFont(item.getNicknameLabel().getFont());
boolean groupHadAvailableContacts = false;
// Do not copy/move a contact item only if it is not already in the Group.
if (contactGroup.getContactItemByJID(item.getJID(), true) != null) {
return;
}
if (!PresenceManager.isOnline(item.getJID())) {
contactGroup.addOfflineContactItem(item.getAlias(), item.getNickname(), item.getJID(), null);
} else {
groupHadAvailableContacts = contactGroup.hasAvailableContacts();
contactGroup.addContactItem(newContact);
}
contactGroup.clearSelection();
// Updating group title
contactGroup.fireContactGroupUpdated();
final ContactGroup oldGroup = getContactGroup(item.getGroupName());
final boolean groupAvailableContacts = groupHadAvailableContacts;
SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(item.getJID());
RosterGroup groupFound = null;
for (RosterGroup group : roster.getGroups()) {
if (group.getName().equals(contactGroup.getGroupName())) {
try {
groupFound = group;
if (!groupAvailableContacts) {
SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
}
group.addEntry(entry);
} catch (XMPPException | SmackException e1) {
Log.error(e1);
return false;
}
}
}
// This is a new group
if (groupFound == null) {
groupFound = roster.createGroup(contactGroup.getGroupName());
try {
groupFound.addEntry(entry);
if (!groupAvailableContacts) {
SparkManager.getContactList().toggleGroupVisibility(groupFound.getName(), true);
}
} catch (XMPPException | SmackException e) {
Log.error(e);
}
}
return true;
}
@Override
public void finished() {
if ((Boolean) get()) {
// Now try and remove the group from the old one.
if (move) {
removeContactItem(oldGroup, item);
if (!localPreferences.isEmptyGroupsShown() && !oldGroup.hasAvailableContacts()) {
SparkManager.getContactList().toggleGroupVisibility(oldGroup.getGroupName(), false);
}
}
}
}
};
worker.start();
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class UriManager method handleRemove.
/**
* Handles the ?remove URI
*
* @param uri
* the decoded uri
* @throws Exception
*/
public void handleRemove(URI uri) throws Exception {
// xmpp:romeo@montague.net?remove
String jid = retrieveJID(uri);
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry entry = roster.getEntry(jid);
roster.removeEntry(entry);
}
use of org.jivesoftware.smack.roster.RosterEntry in project Spark by igniterealtime.
the class UriManager method handleRoster.
/**
* Handles the ?roster URI<br>
* with name= and group=
*
* @param uri
* the decoded uri
* @throws Exception
*/
public void handleRoster(URI uri) throws Exception {
// xmpp:romeo@montague.net?roster
// xmpp:romeo@montague.net?roster;name=Romeo%20Montague
// xmpp:romeo@montague.net?roster;group=Friends
// xmpp:romeo@montague.net?roster;name=Romeo%20Montague;group=Friends
String jid = retrieveJID(uri);
String name = "";
String query = uri.getQuery();
if (query.contains("name=")) {
StringBuilder buf = new StringBuilder();
int x = query.indexOf("name=") + 5;
while (x < query.length() && query.charAt(x) != ';') {
buf.append(query.charAt(x));
x++;
}
}
String group = "";
if (query.contains("group=")) {
StringBuilder buf = new StringBuilder();
int x = query.indexOf("group=") + 6;
while (x < query.length() && query.charAt(x) != ';') {
buf.append(query.charAt(x));
x++;
}
}
Roster roster = Roster.getInstanceFor(SparkManager.getConnection());
RosterEntry userEntry = roster.getEntry(jid);
roster.createEntry(jid, name, new String[] { group });
RosterGroup rosterGroup = roster.getGroup(group);
if (rosterGroup == null) {
rosterGroup = roster.createGroup(group);
}
if (userEntry == null) {
roster.createEntry(jid, name, new String[] { group });
userEntry = roster.getEntry(jid);
} else {
userEntry.setName(name);
rosterGroup.addEntry(userEntry);
}
userEntry = roster.getEntry(jid);
}
Aggregations